2021-12-21 16:13:00 +01:00

249 lines
6.3 KiB
C

/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2021 Alfonso Sabato Siciliano
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#ifdef PORTNCURSES
#include <ncurses/ncurses.h>
#else
#include <ncurses.h>
#endif
#include <string.h>
#include "bsddialog.h"
#include "lib_util.h"
#include "bsddialog_theme.h"
/* "Text": textbox */
#define BUTTON_TEXTBOX "EXIT"
extern struct bsddialog_theme t;
static void
textbox_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h, int *w,
int hpad, int wpad)
{
if (cols == BSDDIALOG_AUTOSIZE) {
*w = VBORDERS;
/* buttons size */
*w += strlen(BUTTON_TEXTBOX) + 2 /* text delims*/;
/* text size */
*w = MAX(*w, wpad + VBORDERS);
/* conf.auto_minwidth */
*w = MAX(*w, (int)conf->auto_minwidth);
/* avoid terminal overflow */
*w = MIN(*w, widget_max_width(conf)-1); /* again -1, fix util.c */
}
if (rows == BSDDIALOG_AUTOSIZE) {
*h = hpad + 4; /* HBORDERS + button border */
/* conf.auto_minheight */
*h = MAX(*h, (int)conf->auto_minheight);
/* avoid terminal overflow */
*h = MIN(*h, widget_max_height(conf));
}
}
static int textbox_checksize(int rows, int cols, int hpad)
{
int mincols;
mincols = VBORDERS + strlen(BUTTON_TEXTBOX) + 2 /* text delims */;
if (cols < mincols)
RETURN_ERROR("Few cols for the textbox");
if (rows < 4 /* HBORDERS + button*/ + (hpad > 0 ? 1 : 0))
RETURN_ERROR("Few rows for the textbox");
return 0;
}
int
bsddialog_textbox(struct bsddialog_conf *conf, char* file, int rows, int cols)
{
WINDOW *widget, *pad, *shadow;
int i, input, y, x, h, w, hpad, wpad, ypad, xpad, ys, ye, xs, xe, printrows;
char buf[BUFSIZ], *exitbutt;
FILE *fp;
bool loop;
int output;
if ((fp = fopen(file, "r")) == NULL)
RETURN_ERROR("Cannot open file");
hpad = 1;
wpad = 1;
pad = newpad(hpad, wpad);
wbkgd(pad, t.dialog.color);
i = 0;
while(fgets(buf, BUFSIZ, fp) != NULL) {
if ((int) strlen(buf) > wpad) {
wpad = strlen(buf);
wresize(pad, hpad, wpad);
}
if (i > hpad-1) {
hpad++;
wresize(pad, hpad, wpad);
}
mvwaddstr(pad, i, 0, buf);
i++;
}
fclose(fp);
if (set_widget_size(conf, rows, cols, &h, &w) != 0)
return BSDDIALOG_ERROR;
textbox_autosize(conf, rows, cols, &h, &w, hpad, wpad);
if (textbox_checksize(h, w, hpad) != 0)
return BSDDIALOG_ERROR;
if (set_widget_position(conf, &y, &x, h, w) != 0)
return BSDDIALOG_ERROR;
if (new_widget_withtextpad(conf, &shadow, &widget, y, x, h, w, RAISED,
NULL, NULL, NULL, true) != 0)
return BSDDIALOG_ERROR;
exitbutt = conf->button.exit_label == NULL ? BUTTON_TEXTBOX : conf->button.exit_label;
draw_button(widget, h-2, (w-2)/2 - strlen(exitbutt)/2, strlen(exitbutt)+2,
exitbutt, true, false);
wrefresh(widget);
ys = y + 1;
xs = x + 1;
ye = ys + h - 5;
xe = xs + w - 3;
ypad = xpad = 0;
printrows = h-4;
loop = true;
while(loop) {
prefresh(pad, ypad, xpad, ys, xs, ye, xe);
input = getch();
switch(input) {
case KEY_ENTER:
case 10: /* Enter */
output = BSDDIALOG_OK;
loop = false;
break;
case 27: /* Esc */
output = BSDDIALOG_ESC;
loop = false;
break;
case KEY_HOME:
ypad = 0;
break;
case KEY_END:
ypad = hpad - printrows;
ypad = ypad < 0 ? 0 : ypad;
break;
case KEY_PPAGE:
ypad -= printrows;
ypad = ypad < 0 ? 0 : ypad;
break;
case KEY_NPAGE:
ypad += printrows;
ypad = ypad + printrows > hpad ? hpad - printrows : ypad;
break;
case '0':
xpad = 0;
case KEY_LEFT:
case 'h':
xpad = xpad > 0 ? xpad - 1 : 0;
break;
case KEY_RIGHT:
case 'l':
xpad = (xpad + w-2) < wpad-1 ? xpad + 1 : xpad;
break;
case KEY_UP:
case 'k':
ypad = ypad > 0 ? ypad - 1 : 0;
break;
case KEY_DOWN:
case'j':
ypad = ypad + printrows <= hpad -1 ? ypad + 1 : ypad;
break;
case KEY_F(1):
if (conf->f1_file == NULL && conf->f1_message == NULL)
break;
if (f1help(conf) != 0)
return BSDDIALOG_ERROR;
/* No break! the terminal size can change */
case KEY_RESIZE:
hide_widget(y, x, h, w,conf->shadow);
/*
* Unnecessary, but, when the columns decrease the
* following "refresh" seem not work
*/
refresh();
if (set_widget_size(conf, rows, cols, &h, &w) != 0)
return BSDDIALOG_ERROR;
textbox_autosize(conf, rows, cols, &h, &w, hpad, wpad);
if (textbox_checksize(h, w, hpad) != 0)
return BSDDIALOG_ERROR;
if (set_widget_position(conf, &y, &x, h, w) != 0)
return BSDDIALOG_ERROR;
wclear(shadow);
mvwin(shadow, y + t.shadow.h, x + t.shadow.w);
wresize(shadow, h, w);
wclear(widget);
mvwin(widget, y, x);
wresize(widget, h, w);
ys = y + 1;
xs = x + 1;
ye = ys + h - 5;
xe = xs + w - 3;
ypad = xpad = 0;
printrows = h - 4;
if(update_widget_withtextpad(conf, shadow, widget, h, w,
RAISED, NULL, NULL, NULL, true) != 0)
return BSDDIALOG_ERROR;
draw_button(widget, h-2, (w-2)/2 - strlen(exitbutt)/2,
strlen(exitbutt)+2, exitbutt, true, false);
wrefresh(widget); /* for button */
/* Important to fix grey lines expanding screen */
refresh();
break;
}
}
end_widget_withtextpad(conf, widget, h, w, pad, shadow);
return output;
}