2 * $Id: progressbox.c,v 1.54 2020/11/22 15:48:27 tom Exp $
4 * progressbox.c -- implements the progress box
6 * Copyright 2006-2019,2020 Thomas E. Dickey
7 * Copyright 2005 Valery Reznic
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License, version 2.1
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to
20 * Free Software Foundation, Inc.
21 * 51 Franklin St., Fifth Floor
22 * Boston, MA 02110, USA.
33 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
36 typedef struct _wrote {
47 int old_high, old_wide;
48 char line[MAX_LEN + 1];
56 free_obj(MY_OBJ * obj)
58 dlg_del_window(obj->obj.win);
62 WROTE *wrote = obj->wrote;
63 obj->wrote = wrote->link;
73 restart_obj(MY_OBJ * obj)
76 obj->high = obj->old_high;
77 obj->wide = obj->old_wide;
79 dlg_del_window(obj->obj.win);
84 start_obj(MY_OBJ * obj, const char *title, const char *cprompt)
88 obj->prompt = dlg_strclone(cprompt);
89 dlg_tab_correct_str(obj->prompt);
90 dlg_auto_size(title, obj->prompt, &obj->high, &obj->wide, MIN_HIGH, MIN_WIDE);
92 dlg_print_size(obj->high, obj->wide);
93 dlg_ctl_size(obj->high, obj->wide);
95 x = dlg_box_x_ordinate(obj->wide);
96 y = dlg_box_y_ordinate(obj->high);
97 thigh = obj->high - (2 * MARGIN);
99 obj->obj.win = dlg_new_window(obj->high, obj->wide, y, x);
101 dlg_draw_box2(obj->obj.win,
103 obj->high, obj->wide,
107 dlg_draw_title(obj->obj.win, title);
108 dlg_draw_helpline(obj->obj.win, FALSE);
110 if (obj->prompt[0] != '\0') {
114 dlg_attrset(obj->obj.win, dialog_attr);
115 dlg_print_autowrap(obj->obj.win, obj->prompt, obj->high, obj->wide);
116 getyx(obj->obj.win, y2, x2);
119 wmove(obj->obj.win, y2, MARGIN);
120 for (i = 0; i < getmaxx(obj->obj.win) - 2 * MARGIN; i++)
121 (void) waddch(obj->obj.win, dlg_boxchar(ACS_HLINE));
126 /* Create window for text region, used for scrolling text */
127 obj->text = dlg_sub_window(obj->obj.win,
129 obj->wide - (2 * MARGIN),
133 (void) wrefresh(obj->obj.win);
135 (void) wmove(obj->obj.win, getmaxy(obj->text), (MARGIN + 1));
136 (void) wnoutrefresh(obj->obj.win);
138 dlg_attr_clear(obj->text, getmaxy(obj->text), getmaxx(obj->text), dialog_attr);
142 * Return current line of text.
145 get_line(MY_OBJ * obj, int *restart)
147 FILE *fp = obj->obj.input;
150 char *result = obj->line;
156 /* SIGWINCH may have interrupted this - try to ignore if resizable */
167 if (feof(fp) || ferror(fp)) {
180 if ((ch == TAB) && (dialog_vars.tab_correct)) {
181 tmpint = dialog_state.tab_len
182 - (col % dialog_state.tab_len);
183 for (j = 0; j < tmpint; j++) {
185 obj->line[col] = ' ';
192 obj->line[col] = (char) ch;
197 obj->line[col] = '\0';
200 if (result != NULL) {
201 WINDOW *win = obj->text;
202 WROTE *wrote = dlg_calloc(WROTE, 1);
205 wrote->text = dlg_strclone(obj->line);
206 wrote->link = obj->wrote;
211 if (wgetch(win) == KEY_RESIZE) {
221 * Print a new line of text.
224 print_line(MY_OBJ * obj, const char *line, int row)
226 int width = obj->wide - (2 * MARGIN);
227 int limit = MIN((int) strlen(line), width - 2);
229 (void) wmove(obj->text, row, 0); /* move cursor to correct line */
230 wprintw(obj->text, " %.*s", limit, line);
231 while (++limit < width) {
232 waddch(obj->text, ' ');
238 wrote_size(MY_OBJ * obj, int want)
241 WROTE *wrote = obj->wrote;
242 while (wrote != NULL && want > 0) {
251 wrote_data(MY_OBJ * obj, int want)
253 const char *result = NULL;
254 WROTE *wrote = obj->wrote;
255 while (wrote != NULL && want > 0) {
256 result = wrote->text;
264 reprint_lines(MY_OBJ * obj, int buttons)
266 int want = getmaxy(obj->text) - (buttons ? 2 : 0);
267 int have = wrote_size(obj, want);
269 for (n = 0; n < have; ++n) {
270 print_line(obj, wrote_data(obj, have - n), n);
272 (void) wrefresh(obj->text);
278 pause_for_ok(MY_OBJ * obj, const char *title, const char *cprompt)
281 static DLG_KEYS_BINDING binding[] = {
291 int result = DLG_EXIT_UNKNOWN;
292 const char **buttons = dlg_ok_label();
293 bool save_nocancel = dialog_vars.nocancel;
299 dialog_vars.nocancel = TRUE;
300 button = dlg_default_button();
306 dlg_register_window(obj->obj.win, "progressbox", binding);
307 dlg_register_buttons(obj->obj.win, "progressbox", buttons);
309 dlg_draw_bottom_box2(obj->obj.win, border_attr, border2_attr, dialog_attr);
311 while (result == DLG_EXIT_UNKNOWN) {
318 dlg_draw_buttons(obj->obj.win,
324 key = dlg_mouse_wgetch(obj->obj.win, &fkey);
325 if (dlg_result_key(key, fkey, &result)) {
326 if (!dlg_button_key(result, &button, &key, &fkey))
330 if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
331 result = dlg_ok_buttoncode(check);
337 case DLGK_FIELD_NEXT:
338 button = dlg_next_button(buttons, button);
341 case DLGK_FIELD_PREV:
342 button = dlg_prev_button(buttons, button);
346 result = dlg_ok_buttoncode(button);
350 dlg_will_resize(obj->obj.win);
352 start_obj(obj, title, cprompt);
353 reprint_lines(obj, TRUE);
358 if (is_DLGK_MOUSE(key)) {
359 result = dlg_ok_buttoncode(key - M_EVENT);
361 result = DLG_EXIT_OK;
368 } else if (key > 0) {
372 dlg_add_last_key(-1);
374 dlg_mouse_free_regions();
375 dlg_unregister_window(obj->obj.win);
377 dialog_vars.nocancel = save_nocancel;
382 dlg_progressbox(const char *title,
395 DLG_TRACE(("# progressbox args:\n"));
396 DLG_TRACE2S("title", title);
397 DLG_TRACE2S("message", cprompt);
398 DLG_TRACE2N("height", height);
399 DLG_TRACE2N("width", width);
400 DLG_TRACE2N("pause", pauseopt);
401 DLG_TRACE2N("fp", fp ? fileno(fp) : -1);
403 obj = dlg_calloc(MY_OBJ, 1);
404 assert_ptr(obj, "dlg_progressbox");
411 obj->old_high = height;
412 obj->old_wide = width;
418 start_obj(obj, title, cprompt);
421 toprow = reprint_lines(obj, FALSE);
425 for (i = toprow; get_line(obj, &again); i++) {
428 dlg_will_resize(obj->obj.win);
433 if (i < getmaxy(obj->text)) {
434 print_line(obj, obj->line, i);
436 scrollok(obj->text, TRUE);
438 scrollok(obj->text, FALSE);
439 print_line(obj, obj->line, getmaxy(obj->text) - 1);
441 (void) wrefresh(obj->text);
446 dlg_trace_win(obj->obj.win);
450 int need = 1 + MARGIN;
451 int base = getmaxy(obj->text) - need;
457 scrollok(obj->text, TRUE);
461 (void) wrefresh(obj->text);
462 result = pause_for_ok(obj, title, cprompt);
464 wrefresh(obj->obj.win);
465 result = DLG_EXIT_OK;
474 * Display text from a stdin in a scrolling window.
477 dialog_progressbox(const char *title, const char *cprompt, int height, int width)
480 result = dlg_progressbox(title,
485 dialog_state.pipe_input);
486 dialog_state.pipe_input = 0;