freebsd-dev/contrib/dialog/progressbox.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

489 lines
10 KiB
C
Raw Normal View History

2011-01-13 02:21:23 +00:00
/*
* $Id: progressbox.c,v 1.54 2020/11/22 15:48:27 tom Exp $
2011-01-13 02:21:23 +00:00
*
* progressbox.c -- implements the progress box
*
* Copyright 2006-2019,2020 Thomas E. Dickey
2018-10-20 20:32:57 +00:00
* Copyright 2005 Valery Reznic
2011-01-13 02:21:23 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 2.1
* as published by the Free Software Foundation.
2011-01-13 02:21:23 +00:00
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to
* Free Software Foundation, Inc.
* 51 Franklin St., Fifth Floor
* Boston, MA 02110, USA.
*/
#include <dialog.h>
#include <dlg_keys.h>
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
#include <errno.h>
#endif
2011-01-13 02:21:23 +00:00
#define MIN_HIGH (4)
#define MIN_WIDE (10 + 2 * (2 + MARGIN))
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
typedef struct _wrote {
struct _wrote *link;
char *text;
} WROTE;
#endif
2011-01-13 02:21:23 +00:00
typedef struct {
DIALOG_CALLBACK obj;
WINDOW *text;
2018-10-20 20:32:57 +00:00
char *prompt;
int high, wide;
int old_high, old_wide;
2011-01-13 02:21:23 +00:00
char line[MAX_LEN + 1];
int is_eof;
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
WROTE *wrote;
#endif
2011-01-13 02:21:23 +00:00
} MY_OBJ;
2018-10-20 20:32:57 +00:00
static void
free_obj(MY_OBJ * obj)
{
dlg_del_window(obj->obj.win);
free(obj->prompt);
#ifdef KEY_RESIZE
while (obj->wrote) {
WROTE *wrote = obj->wrote;
obj->wrote = wrote->link;
free(wrote->text);
free(wrote);
}
#endif
free(obj);
}
#ifdef KEY_RESIZE
2018-10-20 20:32:57 +00:00
static void
restart_obj(MY_OBJ * obj)
{
free(obj->prompt);
obj->high = obj->old_high;
obj->wide = obj->old_wide;
dlg_clear();
dlg_del_window(obj->obj.win);
}
#endif
2018-10-20 20:32:57 +00:00
static void
start_obj(MY_OBJ * obj, const char *title, const char *cprompt)
{
int y, x, thigh;
obj->prompt = dlg_strclone(cprompt);
dlg_tab_correct_str(obj->prompt);
dlg_auto_size(title, obj->prompt, &obj->high, &obj->wide, MIN_HIGH, MIN_WIDE);
dlg_print_size(obj->high, obj->wide);
dlg_ctl_size(obj->high, obj->wide);
x = dlg_box_x_ordinate(obj->wide);
y = dlg_box_y_ordinate(obj->high);
thigh = obj->high - (2 * MARGIN);
obj->obj.win = dlg_new_window(obj->high, obj->wide, y, x);
dlg_draw_box2(obj->obj.win,
0, 0,
obj->high, obj->wide,
dialog_attr,
border_attr,
border2_attr);
dlg_draw_title(obj->obj.win, title);
dlg_draw_helpline(obj->obj.win, FALSE);
if (obj->prompt[0] != '\0') {
int i;
2018-10-20 20:32:57 +00:00
int y2, x2;
dlg_attrset(obj->obj.win, dialog_attr);
dlg_print_autowrap(obj->obj.win, obj->prompt, obj->high, obj->wide);
getyx(obj->obj.win, y2, x2);
(void) x2;
++y2;
wmove(obj->obj.win, y2, MARGIN);
for (i = 0; i < getmaxx(obj->obj.win) - 2 * MARGIN; i++)
(void) waddch(obj->obj.win, dlg_boxchar(ACS_HLINE));
y += y2;
thigh -= y2;
}
/* Create window for text region, used for scrolling text */
obj->text = dlg_sub_window(obj->obj.win,
thigh,
obj->wide - (2 * MARGIN),
y + MARGIN,
x + MARGIN);
(void) wrefresh(obj->obj.win);
(void) wmove(obj->obj.win, getmaxy(obj->text), (MARGIN + 1));
(void) wnoutrefresh(obj->obj.win);
dlg_attr_clear(obj->text, getmaxy(obj->text), getmaxx(obj->text), dialog_attr);
}
2011-01-13 02:21:23 +00:00
/*
* Return current line of text.
*/
static char *
2018-10-20 20:32:57 +00:00
get_line(MY_OBJ * obj, int *restart)
2011-01-13 02:21:23 +00:00
{
FILE *fp = obj->obj.input;
int col = 0;
int j, tmpint;
2018-10-20 20:32:57 +00:00
char *result = obj->line;
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
*restart = 0;
2013-06-17 10:00:48 +00:00
for (;;) {
int ch = getc(fp);
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
/* SIGWINCH may have interrupted this - try to ignore if resizable */
if (ferror(fp)) {
switch (errno) {
case EINTR:
clearerr(fp);
continue;
default:
2011-01-13 02:21:23 +00:00
break;
}
}
2018-10-20 20:32:57 +00:00
#endif
if (feof(fp) || ferror(fp)) {
obj->is_eof = 1;
if (!col) {
result = NULL;
}
break;
}
2011-01-13 02:21:23 +00:00
if (ch == '\n')
break;
if (ch == '\r')
break;
2013-06-17 10:00:48 +00:00
if (col >= MAX_LEN)
continue;
2011-01-13 02:21:23 +00:00
if ((ch == TAB) && (dialog_vars.tab_correct)) {
tmpint = dialog_state.tab_len
- (col % dialog_state.tab_len);
for (j = 0; j < tmpint; j++) {
2013-06-17 10:00:48 +00:00
if (col < MAX_LEN) {
2011-01-13 02:21:23 +00:00
obj->line[col] = ' ';
2013-06-17 10:00:48 +00:00
++col;
} else {
break;
}
2011-01-13 02:21:23 +00:00
}
} else {
obj->line[col] = (char) ch;
++col;
}
}
obj->line[col] = '\0';
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
if (result != NULL) {
WINDOW *win = obj->text;
WROTE *wrote = dlg_calloc(WROTE, 1);
if (wrote != 0) {
wrote->text = dlg_strclone(obj->line);
wrote->link = obj->wrote;
obj->wrote = wrote;
}
nodelay(win, TRUE);
if (wgetch(win) == KEY_RESIZE) {
2018-10-20 20:32:57 +00:00
*restart = 1;
}
nodelay(win, FALSE);
}
#endif
return result;
2011-01-13 02:21:23 +00:00
}
/*
* Print a new line of text.
*/
static void
2018-10-20 20:32:57 +00:00
print_line(MY_OBJ * obj, const char *line, int row)
2011-01-13 02:21:23 +00:00
{
2018-10-20 20:32:57 +00:00
int width = obj->wide - (2 * MARGIN);
int limit = MIN((int) strlen(line), width - 2);
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
(void) wmove(obj->text, row, 0); /* move cursor to correct line */
wprintw(obj->text, " %.*s", limit, line);
while (++limit < width) {
waddch(obj->text, ' ');
}
2011-01-13 02:21:23 +00:00
}
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
2011-04-17 17:00:55 +00:00
static int
2018-10-20 20:32:57 +00:00
wrote_size(MY_OBJ * obj, int want)
{
int result = 0;
WROTE *wrote = obj->wrote;
while (wrote != NULL && want > 0) {
wrote = wrote->link;
want--;
result++;
}
return result;
}
static const char *
wrote_data(MY_OBJ * obj, int want)
{
const char *result = NULL;
WROTE *wrote = obj->wrote;
while (wrote != NULL && want > 0) {
result = wrote->text;
wrote = wrote->link;
want--;
}
return result;
}
static int
reprint_lines(MY_OBJ * obj, int buttons)
{
int want = getmaxy(obj->text) - (buttons ? 2 : 0);
int have = wrote_size(obj, want);
int n;
for (n = 0; n < have; ++n) {
print_line(obj, wrote_data(obj, have - n), n);
}
(void) wrefresh(obj->text);
return have;
}
#endif
static int
pause_for_ok(MY_OBJ * obj, const char *title, const char *cprompt)
2011-04-17 17:00:55 +00:00
{
/* *INDENT-OFF* */
static DLG_KEYS_BINDING binding[] = {
HELPKEY_BINDINGS,
2011-04-17 17:00:55 +00:00
ENTERKEY_BINDINGS,
2012-10-21 18:18:09 +00:00
TRAVERSE_BINDINGS,
2011-04-17 17:00:55 +00:00
END_KEYS_BINDING
};
/* *INDENT-ON* */
2012-10-21 18:18:09 +00:00
int button;
int key, fkey;
2011-04-17 17:00:55 +00:00
int result = DLG_EXIT_UNKNOWN;
const char **buttons = dlg_ok_label();
2018-10-20 20:32:57 +00:00
bool save_nocancel = dialog_vars.nocancel;
2012-10-21 18:18:09 +00:00
bool redraw = TRUE;
(void) title;
(void) cprompt;
2012-10-21 18:18:09 +00:00
dialog_vars.nocancel = TRUE;
button = dlg_default_button();
2011-04-17 17:00:55 +00:00
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
restart:
#endif
dlg_register_window(obj->obj.win, "progressbox", binding);
dlg_register_buttons(obj->obj.win, "progressbox", buttons);
2011-04-17 17:00:55 +00:00
2018-10-20 20:32:57 +00:00
dlg_draw_bottom_box2(obj->obj.win, border_attr, border2_attr, dialog_attr);
2011-04-17 17:00:55 +00:00
while (result == DLG_EXIT_UNKNOWN) {
int check;
2012-10-21 18:18:09 +00:00
if (redraw) {
redraw = FALSE;
if (button < 0)
button = 0;
2018-10-20 20:32:57 +00:00
dlg_draw_buttons(obj->obj.win,
obj->high - 2, 0,
2012-10-21 18:18:09 +00:00
buttons, button,
2018-10-20 20:32:57 +00:00
FALSE, obj->wide);
2012-10-21 18:18:09 +00:00
}
2018-10-20 20:32:57 +00:00
key = dlg_mouse_wgetch(obj->obj.win, &fkey);
if (dlg_result_key(key, fkey, &result)) {
if (!dlg_button_key(result, &button, &key, &fkey))
break;
}
2011-04-17 17:00:55 +00:00
if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
2012-10-21 18:18:09 +00:00
result = dlg_ok_buttoncode(check);
2011-04-17 17:00:55 +00:00
break;
}
if (fkey) {
switch (key) {
2012-10-21 18:18:09 +00:00
case DLGK_FIELD_NEXT:
button = dlg_next_button(buttons, button);
redraw = TRUE;
2011-04-17 17:00:55 +00:00
break;
2012-10-21 18:18:09 +00:00
case DLGK_FIELD_PREV:
button = dlg_prev_button(buttons, button);
redraw = TRUE;
2011-04-17 17:00:55 +00:00
break;
2012-10-21 18:18:09 +00:00
case DLGK_ENTER:
result = dlg_ok_buttoncode(button);
2011-04-17 17:00:55 +00:00
break;
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
case KEY_RESIZE:
dlg_will_resize(obj->obj.win);
restart_obj(obj);
start_obj(obj, title, cprompt);
reprint_lines(obj, TRUE);
redraw = TRUE;
goto restart;
#endif
2011-04-17 17:00:55 +00:00
default:
2012-10-21 18:18:09 +00:00
if (is_DLGK_MOUSE(key)) {
result = dlg_ok_buttoncode(key - M_EVENT);
if (result < 0)
result = DLG_EXIT_OK;
} else {
beep();
}
2011-04-17 17:00:55 +00:00
break;
}
2012-10-21 18:18:09 +00:00
} else if (key > 0) {
2011-04-17 17:00:55 +00:00
beep();
}
}
dlg_add_last_key(-1);
2018-10-20 20:32:57 +00:00
dlg_mouse_free_regions();
dlg_unregister_window(obj->obj.win);
2012-10-21 18:18:09 +00:00
dialog_vars.nocancel = save_nocancel;
2011-04-17 17:00:55 +00:00
return result;
}
2011-01-13 02:21:23 +00:00
int
2011-04-17 17:00:55 +00:00
dlg_progressbox(const char *title,
const char *cprompt,
int height,
int width,
int pauseopt,
FILE *fp)
2011-01-13 02:21:23 +00:00
{
int i;
MY_OBJ *obj;
2018-10-20 20:32:57 +00:00
int again = 0;
int toprow = 0;
2011-04-17 17:00:55 +00:00
int result;
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
DLG_TRACE(("# progressbox args:\n"));
DLG_TRACE2S("title", title);
DLG_TRACE2S("message", cprompt);
DLG_TRACE2N("height", height);
DLG_TRACE2N("width", width);
DLG_TRACE2N("pause", pauseopt);
DLG_TRACE2N("fp", fp ? fileno(fp) : -1);
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
obj = dlg_calloc(MY_OBJ, 1);
assert_ptr(obj, "dlg_progressbox");
obj->obj.input = fp;
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
obj->high = height;
obj->wide = width;
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
#ifdef KEY_RESIZE
obj->old_high = height;
obj->old_wide = width;
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
curs_set(0);
restart:
#endif
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
start_obj(obj, title, cprompt);
#ifdef KEY_RESIZE
if (again) {
toprow = reprint_lines(obj, FALSE);
2011-01-13 02:21:23 +00:00
}
2018-10-20 20:32:57 +00:00
#endif
2011-01-13 02:21:23 +00:00
2018-10-20 20:32:57 +00:00
for (i = toprow; get_line(obj, &again); i++) {
#ifdef KEY_RESIZE
if (again) {
dlg_will_resize(obj->obj.win);
restart_obj(obj);
goto restart;
}
#endif
if (i < getmaxy(obj->text)) {
print_line(obj, obj->line, i);
2011-01-13 02:21:23 +00:00
} else {
2018-10-20 20:32:57 +00:00
scrollok(obj->text, TRUE);
scroll(obj->text);
scrollok(obj->text, FALSE);
print_line(obj, obj->line, getmaxy(obj->text) - 1);
2011-01-13 02:21:23 +00:00
}
2018-10-20 20:32:57 +00:00
(void) wrefresh(obj->text);
2011-01-13 02:21:23 +00:00
if (obj->is_eof)
break;
}
2011-04-17 17:00:55 +00:00
2018-10-20 20:32:57 +00:00
dlg_trace_win(obj->obj.win);
curs_set(1);
2011-04-17 17:00:55 +00:00
if (pauseopt) {
2018-10-20 20:32:57 +00:00
int need = 1 + MARGIN;
int base = getmaxy(obj->text) - need;
if (i >= base) {
i -= base;
if (i > need)
i = need;
if (i > 0) {
scrollok(obj->text, TRUE);
}
wscrl(obj->text, i);
}
(void) wrefresh(obj->text);
result = pause_for_ok(obj, title, cprompt);
2011-04-17 17:00:55 +00:00
} else {
2018-10-20 20:32:57 +00:00
wrefresh(obj->obj.win);
2011-04-17 17:00:55 +00:00
result = DLG_EXIT_OK;
}
2018-10-20 20:32:57 +00:00
free_obj(obj);
2011-01-13 02:21:23 +00:00
2012-10-21 18:18:09 +00:00
return result;
2011-01-13 02:21:23 +00:00
}
2011-04-17 17:00:55 +00:00
/*
* Display text from a stdin in a scrolling window.
*/
int
dialog_progressbox(const char *title, const char *cprompt, int height, int width)
{
int result;
result = dlg_progressbox(title,
cprompt,
height,
width,
FALSE,
dialog_state.pipe_input);
dialog_state.pipe_input = 0;
return result;
}