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>
|
2021-12-21 15:06:19 +00:00
|
|
|
#include <string.h>
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
#include "bsddialog.h"
|
2022-09-03 14:36:16 +00:00
|
|
|
#include "bsddialog_theme.h"
|
2022-01-28 08:14:55 +00:00
|
|
|
#include "lib_util.h"
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
static int
|
2021-12-21 15:06:19 +00:00
|
|
|
message_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h,
|
2022-09-03 14:36:16 +00:00
|
|
|
int *w, const char *text, bool *hastext, struct buttons bs)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
2022-01-28 08:14:55 +00:00
|
|
|
int htext, wtext;
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
if (cols == BSDDIALOG_AUTOSIZE || rows == BSDDIALOG_AUTOSIZE ||
|
|
|
|
hastext != NULL) {
|
|
|
|
if (text_size(conf, rows, cols, text, &bs, 0, 1, &htext,
|
|
|
|
&wtext) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
2022-09-03 14:36:16 +00:00
|
|
|
if (hastext != NULL)
|
|
|
|
*hastext = htext > 0 ? true : false;
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
if (cols == BSDDIALOG_AUTOSIZE)
|
|
|
|
*w = widget_min_width(conf, wtext, 0, &bs);
|
|
|
|
|
|
|
|
if (rows == BSDDIALOG_AUTOSIZE)
|
|
|
|
*h = widget_min_height(conf, htext, 0, true);
|
|
|
|
|
|
|
|
return (0);
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
static int
|
|
|
|
message_checksize(int rows, int cols, bool hastext, struct buttons bs)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
|
|
|
int mincols;
|
|
|
|
|
|
|
|
mincols = VBORDERS;
|
2022-09-03 14:36:16 +00:00
|
|
|
mincols += buttons_min_width(bs);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
if (cols < mincols)
|
2022-01-28 08:14:55 +00:00
|
|
|
RETURN_ERROR("Few cols, Msgbox and Yesno need at least width "
|
2021-11-24 08:42:13 +00:00
|
|
|
"for borders, buttons and spaces between buttons");
|
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
if (rows < HBORDERS + 2 /* buttons */)
|
|
|
|
RETURN_ERROR("Msgbox and Yesno need at least 4 rows");
|
|
|
|
if (hastext && rows < HBORDERS + 2 /*buttons*/ + 1 /* text row */)
|
|
|
|
RETURN_ERROR("Msgbox and Yesno with text need at least 5 rows");
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
return (0);
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-09-03 14:36:16 +00:00
|
|
|
textupdate(WINDOW *widget, WINDOW *textpad, int htextpad, int ytextpad,
|
|
|
|
bool hastext)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
2022-01-28 08:14:55 +00:00
|
|
|
int y, x, h, w;
|
|
|
|
|
|
|
|
getbegyx(widget, y, x);
|
|
|
|
getmaxyx(widget, h, w);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
if (hastext && htextpad > h - 4) {
|
|
|
|
wattron(widget, t.dialog.arrowcolor);
|
2021-11-24 08:42:13 +00:00
|
|
|
mvwprintw(widget, h-3, w-6, "%3d%%",
|
2022-01-28 08:14:55 +00:00
|
|
|
100 * (ytextpad+h-4)/ htextpad);
|
2022-09-03 14:36:16 +00:00
|
|
|
wattroff(widget, t.dialog.arrowcolor);
|
2021-11-24 08:42:13 +00:00
|
|
|
wnoutrefresh(widget);
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
pnoutrefresh(textpad, ytextpad, 0, y+1, x+2, y+h-4, x+w-2);
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-01-28 08:14:55 +00:00
|
|
|
do_message(struct bsddialog_conf *conf, const char *text, int rows, int cols,
|
2021-12-21 15:06:19 +00:00
|
|
|
struct buttons bs)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
2022-09-03 14:36:16 +00:00
|
|
|
bool hastext, loop;
|
2022-09-25 13:04:33 +00:00
|
|
|
int y, x, h, w, retval, ytextpad, htextpad, printrows, unused;
|
2022-01-28 08:14:55 +00:00
|
|
|
WINDOW *widget, *textpad, *shadow;
|
2022-09-03 14:36:16 +00:00
|
|
|
wint_t input;
|
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
|
|
|
if (message_autosize(conf, rows, cols, &h, &w, text, &hastext, bs) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
2022-09-03 14:36:16 +00:00
|
|
|
if (message_checksize(h, w, hastext, bs) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
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, &textpad, text, &bs,
|
|
|
|
true) != 0)
|
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-25 13:04:33 +00:00
|
|
|
printrows = h - 4;
|
2022-01-28 08:14:55 +00:00
|
|
|
ytextpad = 0;
|
|
|
|
getmaxyx(textpad, htextpad, unused);
|
|
|
|
unused++; /* fix unused error */
|
2021-11-24 08:42:13 +00:00
|
|
|
loop = true;
|
2022-01-28 08:14:55 +00:00
|
|
|
while (loop) {
|
2022-09-25 13:04:33 +00:00
|
|
|
textupdate(widget, textpad, htextpad, ytextpad, hastext);
|
2021-11-24 08:42:13 +00:00
|
|
|
doupdate();
|
2022-09-03 14:36:16 +00:00
|
|
|
if (get_wch(&input) == ERR)
|
|
|
|
continue;
|
2021-11-24 08:42:13 +00:00
|
|
|
switch (input) {
|
2022-01-28 08:14:55 +00:00
|
|
|
case KEY_ENTER:
|
2021-11-24 08:42:13 +00:00
|
|
|
case 10: /* Enter */
|
2022-09-03 14:36:16 +00:00
|
|
|
retval = bs.value[bs.curr];
|
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 '\t': /* TAB */
|
|
|
|
bs.curr = (bs.curr + 1) % bs.nbuttons;
|
2022-01-28 08:14:55 +00:00
|
|
|
draw_buttons(widget, bs, true);
|
|
|
|
wnoutrefresh(widget);
|
|
|
|
break;
|
|
|
|
case KEY_LEFT:
|
|
|
|
if (bs.curr > 0) {
|
|
|
|
bs.curr--;
|
|
|
|
draw_buttons(widget, bs, true);
|
|
|
|
wnoutrefresh(widget);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
|
|
|
if (bs.curr < (int)bs.nbuttons - 1) {
|
|
|
|
bs.curr++;
|
|
|
|
draw_buttons(widget, bs, true);
|
|
|
|
wnoutrefresh(widget);
|
|
|
|
}
|
2021-11-24 08:42:13 +00:00
|
|
|
break;
|
2022-09-25 13:04:33 +00:00
|
|
|
case KEY_UP:
|
|
|
|
if (ytextpad > 0)
|
|
|
|
ytextpad--;
|
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
|
|
if (ytextpad + printrows < htextpad)
|
|
|
|
ytextpad++;
|
|
|
|
break;
|
|
|
|
case KEY_HOME:
|
|
|
|
ytextpad = 0;
|
|
|
|
break;
|
|
|
|
case KEY_END:
|
|
|
|
ytextpad = htextpad - printrows;
|
|
|
|
ytextpad = ytextpad < 0 ? 0 : ytextpad;
|
|
|
|
break;
|
|
|
|
case KEY_PPAGE:
|
|
|
|
ytextpad -= printrows;
|
|
|
|
ytextpad = ytextpad < 0 ? 0 : ytextpad;
|
|
|
|
break;
|
|
|
|
case KEY_NPAGE:
|
|
|
|
ytextpad += printrows;
|
|
|
|
if (ytextpad + printrows > htextpad)
|
|
|
|
ytextpad = htextpad - printrows;
|
|
|
|
break;
|
2021-11-24 08:42:13 +00:00
|
|
|
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);
|
|
|
|
if (message_autosize(conf, rows, cols, &h, &w, text,
|
2022-09-03 14:36:16 +00:00
|
|
|
NULL, bs) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
return (BSDDIALOG_ERROR);
|
2022-09-03 14:36:16 +00:00
|
|
|
if (message_checksize(h, w, hastext, bs) != 0)
|
2022-01-28 08:14:55 +00:00
|
|
|
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 (update_dialog(conf, shadow, widget, y, x, h, w,
|
|
|
|
textpad, text, &bs, true) != 0)
|
|
|
|
return (BSDDIALOG_ERROR);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-25 13:04:33 +00:00
|
|
|
printrows = h - 4;
|
2022-01-28 08:14:55 +00:00
|
|
|
getmaxyx(textpad, htextpad, unused);
|
2022-09-03 14:36:16 +00:00
|
|
|
ytextpad = 0;
|
|
|
|
textupdate(widget, textpad, htextpad, ytextpad, hastext);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
|
|
|
/* Important to fix grey lines expanding screen */
|
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
default:
|
2022-01-28 08:14:55 +00:00
|
|
|
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, textpad);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-09-03 14:36:16 +00:00
|
|
|
return (retval);
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* API */
|
|
|
|
int
|
2022-01-28 08:14:55 +00:00
|
|
|
bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows,
|
|
|
|
int cols)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
|
|
|
struct buttons bs;
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
get_buttons(conf, &bs, BUTTON_OK_LABEL, NULL);
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
return (do_message(conf, text, rows, cols, bs));
|
2021-11-24 08:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2022-01-28 08:14:55 +00:00
|
|
|
bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows,
|
|
|
|
int cols)
|
2021-11-24 08:42:13 +00:00
|
|
|
{
|
|
|
|
struct buttons bs;
|
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
get_buttons(conf, &bs, "Yes", "No");
|
2021-11-24 08:42:13 +00:00
|
|
|
|
2022-01-28 08:14:55 +00:00
|
|
|
return (do_message(conf, text, rows, cols, bs));
|
|
|
|
}
|