2 * $Id: progressbox.c,v 1.53 2020/03/27 21:10:23 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;
72 restart_obj(MY_OBJ * obj)
75 obj->high = obj->old_high;
76 obj->wide = obj->old_wide;
78 dlg_del_window(obj->obj.win);
82 start_obj(MY_OBJ * obj, const char *title, const char *cprompt)
86 obj->prompt = dlg_strclone(cprompt);
87 dlg_tab_correct_str(obj->prompt);
88 dlg_auto_size(title, obj->prompt, &obj->high, &obj->wide, MIN_HIGH, MIN_WIDE);
90 dlg_print_size(obj->high, obj->wide);
91 dlg_ctl_size(obj->high, obj->wide);
93 x = dlg_box_x_ordinate(obj->wide);
94 y = dlg_box_y_ordinate(obj->high);
95 thigh = obj->high - (2 * MARGIN);
97 obj->obj.win = dlg_new_window(obj->high, obj->wide, y, x);
99 dlg_draw_box2(obj->obj.win,
101 obj->high, obj->wide,
105 dlg_draw_title(obj->obj.win, title);
106 dlg_draw_helpline(obj->obj.win, FALSE);
108 if (obj->prompt[0] != '\0') {
112 dlg_attrset(obj->obj.win, dialog_attr);
113 dlg_print_autowrap(obj->obj.win, obj->prompt, obj->high, obj->wide);
114 getyx(obj->obj.win, y2, x2);
117 wmove(obj->obj.win, y2, MARGIN);
118 for (i = 0; i < getmaxx(obj->obj.win) - 2 * MARGIN; i++)
119 (void) waddch(obj->obj.win, dlg_boxchar(ACS_HLINE));
124 /* Create window for text region, used for scrolling text */
125 obj->text = dlg_sub_window(obj->obj.win,
127 obj->wide - (2 * MARGIN),
131 (void) wrefresh(obj->obj.win);
133 (void) wmove(obj->obj.win, getmaxy(obj->text), (MARGIN + 1));
134 (void) wnoutrefresh(obj->obj.win);
136 dlg_attr_clear(obj->text, getmaxy(obj->text), getmaxx(obj->text), dialog_attr);
140 * Return current line of text.
143 get_line(MY_OBJ * obj, int *restart)
145 FILE *fp = obj->obj.input;
148 char *result = obj->line;
154 /* SIGWINCH may have interrupted this - try to ignore if resizable */
165 if (feof(fp) || ferror(fp)) {
178 if ((ch == TAB) && (dialog_vars.tab_correct)) {
179 tmpint = dialog_state.tab_len
180 - (col % dialog_state.tab_len);
181 for (j = 0; j < tmpint; j++) {
183 obj->line[col] = ' ';
190 obj->line[col] = (char) ch;
195 obj->line[col] = '\0';
198 if (result != NULL) {
199 WINDOW *win = obj->text;
200 WROTE *wrote = dlg_calloc(WROTE, 1);
203 wrote->text = dlg_strclone(obj->line);
204 wrote->link = obj->wrote;
209 if (wgetch(win) == KEY_RESIZE) {
219 * Print a new line of text.
222 print_line(MY_OBJ * obj, const char *line, int row)
224 int width = obj->wide - (2 * MARGIN);
225 int limit = MIN((int) strlen(line), width - 2);
227 (void) wmove(obj->text, row, 0); /* move cursor to correct line */
228 wprintw(obj->text, " %.*s", limit, line);
229 while (++limit < width) {
230 waddch(obj->text, ' ');
236 wrote_size(MY_OBJ * obj, int want)
239 WROTE *wrote = obj->wrote;
240 while (wrote != NULL && want > 0) {
249 wrote_data(MY_OBJ * obj, int want)
251 const char *result = NULL;
252 WROTE *wrote = obj->wrote;
253 while (wrote != NULL && want > 0) {
254 result = wrote->text;
262 reprint_lines(MY_OBJ * obj, int buttons)
264 int want = getmaxy(obj->text) - (buttons ? 2 : 0);
265 int have = wrote_size(obj, want);
267 for (n = 0; n < have; ++n) {
268 print_line(obj, wrote_data(obj, have - n), n);
270 (void) wrefresh(obj->text);
276 pause_for_ok(MY_OBJ * obj, const char *title, const char *cprompt)
279 static DLG_KEYS_BINDING binding[] = {
289 int result = DLG_EXIT_UNKNOWN;
290 const char **buttons = dlg_ok_label();
291 bool save_nocancel = dialog_vars.nocancel;
294 dialog_vars.nocancel = TRUE;
295 button = dlg_default_button();
301 dlg_register_window(obj->obj.win, "progressbox", binding);
302 dlg_register_buttons(obj->obj.win, "progressbox", buttons);
304 dlg_draw_bottom_box2(obj->obj.win, border_attr, border2_attr, dialog_attr);
306 while (result == DLG_EXIT_UNKNOWN) {
313 dlg_draw_buttons(obj->obj.win,
319 key = dlg_mouse_wgetch(obj->obj.win, &fkey);
320 if (dlg_result_key(key, fkey, &result)) {
321 if (!dlg_button_key(result, &button, &key, &fkey))
325 if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
326 result = dlg_ok_buttoncode(check);
332 case DLGK_FIELD_NEXT:
333 button = dlg_next_button(buttons, button);
336 case DLGK_FIELD_PREV:
337 button = dlg_prev_button(buttons, button);
341 result = dlg_ok_buttoncode(button);
345 dlg_will_resize(obj->obj.win);
347 start_obj(obj, title, cprompt);
348 reprint_lines(obj, TRUE);
353 if (is_DLGK_MOUSE(key)) {
354 result = dlg_ok_buttoncode(key - M_EVENT);
356 result = DLG_EXIT_OK;
363 } else if (key > 0) {
367 dlg_add_last_key(-1);
369 dlg_mouse_free_regions();
370 dlg_unregister_window(obj->obj.win);
372 dialog_vars.nocancel = save_nocancel;
377 dlg_progressbox(const char *title,
390 DLG_TRACE(("# progressbox args:\n"));
391 DLG_TRACE2S("title", title);
392 DLG_TRACE2S("message", cprompt);
393 DLG_TRACE2N("height", height);
394 DLG_TRACE2N("width", width);
395 DLG_TRACE2N("pause", pauseopt);
396 DLG_TRACE2N("fp", fp ? fileno(fp) : -1);
398 obj = dlg_calloc(MY_OBJ, 1);
399 assert_ptr(obj, "dlg_progressbox");
406 obj->old_high = height;
407 obj->old_wide = width;
413 start_obj(obj, title, cprompt);
416 toprow = reprint_lines(obj, FALSE);
420 for (i = toprow; get_line(obj, &again); i++) {
423 dlg_will_resize(obj->obj.win);
428 if (i < getmaxy(obj->text)) {
429 print_line(obj, obj->line, i);
431 scrollok(obj->text, TRUE);
433 scrollok(obj->text, FALSE);
434 print_line(obj, obj->line, getmaxy(obj->text) - 1);
436 (void) wrefresh(obj->text);
441 dlg_trace_win(obj->obj.win);
445 int need = 1 + MARGIN;
446 int base = getmaxy(obj->text) - need;
452 scrollok(obj->text, TRUE);
456 (void) wrefresh(obj->text);
457 result = pause_for_ok(obj, title, cprompt);
459 wrefresh(obj->obj.win);
460 result = DLG_EXIT_OK;
469 * Display text from a stdin in a scrolling window.
472 dialog_progressbox(const char *title, const char *cprompt, int height, int width)
475 result = dlg_progressbox(title,
480 dialog_state.pipe_input);
481 dialog_state.pipe_input = 0;