2021-11-24 08:42:13 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*
|
2022-01-28 08:14:55 +00:00
|
|
|
* Copyright (c) 2021-2022 Alfonso Sabato Siciliano
|
2021-11-24 08:42:13 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
#include <curses.h>
|
2022-09-03 14:36:16 +00:00
|
|
|
#include <stdlib.h>
|
2021-12-21 15:06:19 +00:00
|
|
|
#include <string.h>
|
2022-09-03 14:36:16 +00:00
|
|
|
#include <wchar.h>
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
#include "bsddialog.h"
|
|
|
|
#include "bsddialog_theme.h"
|
2022-01-28 08:14:55 +00:00
|
|
|
#include "lib_util.h"
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
static void
|
|
|
|
updateborders(struct bsddialog_conf *conf, WINDOW *widget, int padmargin,
|
|
|
|
int hpad, int wpad, int ypad, int xpad)
|
|
|
|
{
|
|
|
|
int h, w;
|
|
|
|
chtype arrowch, borderch;
|
|
|
|
|
|
|
|
getmaxyx(widget, h, w);
|
|
|
|
|
|
|
|
if (conf->no_lines)
|
|
|
|
borderch = ' ';
|
|
|
|
else if (conf->ascii_lines)
|
|
|
|
borderch = '|';
|
|
|
|
else
|
|
|
|
borderch = ACS_VLINE;
|
|
|
|
|
|
|
|
if (xpad > 0) {
|
|
|
|
arrowch = conf->ascii_lines ? '<' : ACS_LARROW;
|
|
|
|
arrowch |= A_ATTRIBUTES & t.dialog.arrowcolor;
|
|
|
|
} else {
|
|
|
|
arrowch = borderch;
|
|
|
|
arrowch |= A_ATTRIBUTES & t.dialog.lineraisecolor;
|
|
|
|
}
|
|
|
|
mvwvline(widget, (h / 2) - 2, 0, arrowch, 4);
|
|
|
|
|
|
|
|
if (xpad + w-2-padmargin < wpad) {
|
|
|
|
arrowch = conf->ascii_lines ? '>' : ACS_RARROW;
|
|
|
|
arrowch |= A_ATTRIBUTES & t.dialog.arrowcolor;
|
|
|
|
} else {
|
|
|
|
arrowch = borderch;
|
|
|
|
arrowch |= A_ATTRIBUTES & t.dialog.linelowercolor;
|
|
|
|
}
|
|
|
|
mvwvline(widget, (h / 2) - 2, w - 1, arrowch, 4);
|
|
|
|
|
|
|
|
if (hpad > h - 4) {
|
|
|
|
wattron(widget, t.dialog.arrowcolor);
|
|
|
|
mvwprintw(widget, h-3, w-6, "%3d%%", 100 * (ypad+h-4)/ hpad);
|
|
|
|
wattroff(widget, t.dialog.arrowcolor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 08:42:13 +00:00
|
|
|
static void
|
2022-01-28 08:14:55 +00:00
|
|
|
textbox_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h,
|
2022-09-03 14:36:16 +00:00
|
|
|
int *w, int hpad, int wpad, int padmargin, struct buttons bs)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
2022-01-28 08:14:55 +00:00
|
|
|
if (cols == BSDDIALOG_AUTOSIZE)
|
2022-09-03 14:36:16 +00:00
|
|
|
*w = widget_min_width(conf, 0, wpad + padmargin, &bs);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
if (rows == BSDDIALOG_AUTOSIZE)
|
|
|
|
*h = widget_min_height(conf, 0, hpad, true);
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
static int
|
|
|
|
textbox_checksize(int rows, int cols, int hpad, struct buttons bs)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
|
|
|
int mincols;
|
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
mincols = VBORDERS;
|
|
|
|
mincols += buttons_min_width(bs);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
return (0);
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
/* API */
|
2021-12-06 10:35:28 +00:00
|
|
|
int
|
2022-01-28 08:14:55 +00:00
|
|
|
bsddialog_textbox(struct bsddialog_conf *conf, const char* file, int rows,
|
|
|
|
int cols)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
2022-09-03 14:36:16 +00:00
|
|
|
bool loop, has_multi_col;
|
|
|
|
int i, retval, y, x, h, w;
|
|
|
|
int hpad, wpad, ypad, xpad, ys, ye, xs, xe, padmargin, printrows;
|
|
|
|
unsigned int defaulttablen, linecols;
|
|
|
|
wint_t input;
|
2022-01-28 08:14:55 +00:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
FILE *fp;
|
|
|
|
struct buttons bs;
|
|
|
|
WINDOW *shadow, *widget, *pad;
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2021-12-06 10:35:28 +00:00
|
|
|
if ((fp = fopen(file, "r")) == NULL)
|
2021-11-24 08:42:13 +00:00
|
|
|
RETURN_ERROR("Cannot open file");
|
2021-12-06 10:35:28 +00:00
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
defaulttablen = TABSIZE;
|
|
|
|
set_tabsize((conf->text.tablen == 0) ? TABSIZE : (int)conf->text.tablen);
|
2021-11-24 08:42:13 +00:00
|
|
|
hpad = 1;
|
|
|
|
wpad = 1;
|
|
|
|
pad = newpad(hpad, wpad);
|
2021-12-13 07:54:07 +00:00
|
|
|
wbkgd(pad, t.dialog.color);
|
2022-09-03 14:36:16 +00:00
|
|
|
padmargin = 0;
|
2021-11-24 08:42:13 +00:00
|
|
|
i = 0;
|
2022-01-28 08:14:55 +00:00
|
|
|
while (fgets(buf, BUFSIZ, fp) != NULL) {
|
2022-09-03 14:36:16 +00:00
|
|
|
if (str_props(buf, &linecols, &has_multi_col) != 0)
|
|
|
|
continue;
|
|
|
|
if ((int)linecols > wpad) {
|
|
|
|
wpad = linecols;
|
2021-11-24 08:42:13 +00:00
|
|
|
wresize(pad, hpad, wpad);
|
|
|
|
}
|
|
|
|
if (i > hpad-1) {
|
|
|
|
hpad++;
|
|
|
|
wresize(pad, hpad, wpad);
|
|
|
|
}
|
|
|
|
mvwaddstr(pad, i, 0, buf);
|
|
|
|
i++;
|
2022-09-03 14:36:16 +00:00
|
|
|
if (has_multi_col)
|
|
|
|
padmargin = 2;
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
fclose(fp);
|
2022-09-03 14:36:16 +00:00
|
|
|
set_tabsize(defaulttablen);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
get_buttons(conf, &bs, "EXIT", NULL);
|
2022-01-28 08:14:55 +00:00
|
|
|
bs.curr = 0;
|
2022-09-03 14:36:16 +00:00
|
|
|
bs.nbuttons = 1;
|
2022-01-28 08:14:55 +00:00
|
|
|
|
2021-11-24 08:42:13 +00:00
|
|
|
if (set_widget_size(conf, rows, cols, &h, &w) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
2022-09-03 14:36:16 +00:00
|
|
|
textbox_autosize(conf, rows, cols, &h, &w, hpad, wpad, padmargin, bs);
|
2022-01-28 08:14:55 +00:00
|
|
|
if (textbox_checksize(h, w, hpad, bs) != 0)
|
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
if (set_widget_position(conf, &y, &x, h, w) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
if (new_dialog(conf, &shadow, &widget, y, x, h, w, NULL, NULL, &bs,
|
|
|
|
true) != 0)
|
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
ys = y + 1;
|
2022-09-03 14:36:16 +00:00
|
|
|
xs = (padmargin == 0) ? x + 1 : x + 2;
|
2021-11-24 08:42:13 +00:00
|
|
|
ye = ys + h - 5;
|
2022-09-03 14:36:16 +00:00
|
|
|
xe = xs + w - 3 - padmargin;
|
2021-11-24 08:42:13 +00:00
|
|
|
ypad = xpad = 0;
|
|
|
|
printrows = h-4;
|
|
|
|
loop = true;
|
2022-01-28 08:14:55 +00:00
|
|
|
while (loop) {
|
2022-09-03 14:36:16 +00:00
|
|
|
updateborders(conf, widget, padmargin, hpad, wpad, ypad, xpad);
|
|
|
|
/*
|
|
|
|
* Overflow multicolumn charchter right border:
|
|
|
|
* wnoutrefresh(widget);
|
|
|
|
* pnoutrefresh(pad, ypad, xpad, ys, xs, ye, xe);
|
|
|
|
* doupdate();
|
|
|
|
*/
|
|
|
|
wrefresh(widget);
|
|
|
|
prefresh(pad, ypad, xpad, ys, xs, ye, xe);
|
|
|
|
if (get_wch(&input) == ERR)
|
|
|
|
continue;
|
2021-11-24 08:42:13 +00:00
|
|
|
switch(input) {
|
|
|
|
case KEY_ENTER:
|
|
|
|
case 10: /* Enter */
|
2022-09-03 14:36:16 +00:00
|
|
|
retval = BSDDIALOG_OK;
|
2021-11-24 08:42:13 +00:00
|
|
|
loop = false;
|
|
|
|
break;
|
|
|
|
case 27: /* Esc */
|
2022-01-28 08:14:55 +00:00
|
|
|
if (conf->key.enable_esc) {
|
2022-09-03 14:36:16 +00:00
|
|
|
retval = BSDDIALOG_ESC;
|
2022-01-28 08:14:55 +00:00
|
|
|
loop = false;
|
|
|
|
}
|
2021-11-24 08:42:13 +00:00
|
|
|
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;
|
2022-01-28 08:14:55 +00:00
|
|
|
if (ypad + printrows > hpad)
|
|
|
|
ypad = hpad - printrows;
|
2021-11-24 08:42:13 +00:00
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
xpad = 0;
|
|
|
|
case KEY_LEFT:
|
|
|
|
case 'h':
|
|
|
|
xpad = xpad > 0 ? xpad - 1 : 0;
|
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
|
|
|
case 'l':
|
2022-09-03 14:36:16 +00:00
|
|
|
xpad = (xpad + w-2-padmargin) < wpad ? xpad + 1 : xpad;
|
2021-11-24 08:42:13 +00:00
|
|
|
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):
|
2022-03-13 01:34:57 +00:00
|
|
|
if (conf->key.f1_file == NULL &&
|
|
|
|
conf->key.f1_message == NULL)
|
2021-11-24 08:42:13 +00:00
|
|
|
break;
|
|
|
|
if (f1help(conf) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
|
|
|
/* No break, screen size can change */
|
2021-11-24 08:42:13 +00:00
|
|
|
case KEY_RESIZE:
|
2022-01-28 08:14:55 +00:00
|
|
|
/* Important for decreasing screen */
|
|
|
|
hide_widget(y, x, h, w, conf->shadow);
|
2021-11-24 08:42:13 +00:00
|
|
|
refresh();
|
|
|
|
|
|
|
|
if (set_widget_size(conf, rows, cols, &h, &w) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
|
|
|
textbox_autosize(conf, rows, cols, &h, &w, hpad, wpad,
|
2022-09-03 14:36:16 +00:00
|
|
|
padmargin, bs);
|
2022-01-28 08:14:55 +00:00
|
|
|
if (textbox_checksize(h, w, hpad, bs) != 0)
|
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
if (set_widget_position(conf, &y, &x, h, w) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
ys = y + 1;
|
2022-09-03 14:36:16 +00:00
|
|
|
xs = (padmargin == 0) ? x + 1 : x + 2;
|
2021-11-24 08:42:13 +00:00
|
|
|
ye = ys + h - 5;
|
2022-09-03 14:36:16 +00:00
|
|
|
xe = xs + w - 3 - padmargin;
|
2021-11-24 08:42:13 +00:00
|
|
|
ypad = xpad = 0;
|
|
|
|
printrows = h - 4;
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
if (update_dialog(conf, shadow, widget, y, x, h, w,
|
|
|
|
NULL, NULL, &bs, true) != 0)
|
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
/* Important to fix grey lines expanding screen */
|
|
|
|
refresh();
|
|
|
|
break;
|
2022-01-28 08:14:55 +00:00
|
|
|
default:
|
|
|
|
if (shortcut_buttons(input, &bs)) {
|
2022-09-03 14:36:16 +00:00
|
|
|
retval = bs.value[bs.curr];
|
2022-01-28 08:14:55 +00:00
|
|
|
loop = false;
|
|
|
|
}
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
end_dialog(conf, shadow, widget, pad);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
return (retval);
|
|
|
|
}
|