2 * $Id: progressbox.c,v 1.47 2018/06/21 09:14:47 tom Exp $
4 * progressbox.c -- implements the progress box
6 * Copyright 2006-2014,2018 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 as
11 * published by the Free Software Foundation; either version 2.1 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, write to
21 * Free Software Foundation, Inc.
22 * 51 Franklin St., Fifth Floor
23 * Boston, MA 02110, USA.
34 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
37 typedef struct _wrote {
48 int old_high, old_wide;
49 char line[MAX_LEN + 1];
57 free_obj(MY_OBJ * obj)
59 dlg_del_window(obj->obj.win);
63 WROTE *wrote = obj->wrote;
64 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);
83 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') {
113 dlg_attrset(obj->obj.win, dialog_attr);
114 dlg_print_autowrap(obj->obj.win, obj->prompt, obj->high, obj->wide);
115 getyx(obj->obj.win, y2, x2);
118 wmove(obj->obj.win, y2, MARGIN);
119 for (i = 0; i < getmaxx(obj->obj.win) - 2 * MARGIN; i++)
120 (void) waddch(obj->obj.win, dlg_boxchar(ACS_HLINE));
125 /* Create window for text region, used for scrolling text */
126 obj->text = dlg_sub_window(obj->obj.win,
128 obj->wide - (2 * MARGIN),
132 (void) wrefresh(obj->obj.win);
134 (void) wmove(obj->obj.win, getmaxy(obj->text), (MARGIN + 1));
135 (void) wnoutrefresh(obj->obj.win);
137 dlg_attr_clear(obj->text, getmaxy(obj->text), getmaxx(obj->text), dialog_attr);
141 * Return current line of text.
144 get_line(MY_OBJ * obj, int *restart)
146 FILE *fp = obj->obj.input;
149 char *result = obj->line;
155 /* SIGWINCH may have interrupted this - try to ignore if resizable */
166 if (feof(fp) || ferror(fp)) {
179 if ((ch == TAB) && (dialog_vars.tab_correct)) {
180 tmpint = dialog_state.tab_len
181 - (col % dialog_state.tab_len);
182 for (j = 0; j < tmpint; j++) {
184 obj->line[col] = ' ';
191 obj->line[col] = (char) ch;
196 obj->line[col] = '\0';
199 if (result != NULL) {
200 WINDOW *win = obj->text;
201 WROTE *wrote = dlg_calloc(WROTE, 1);
204 wrote->text = dlg_strclone(obj->line);
205 wrote->link = obj->wrote;
210 if ((ch = wgetch(win)) == KEY_RESIZE) {
220 * Print a new line of text.
223 print_line(MY_OBJ * obj, const char *line, int row)
225 int width = obj->wide - (2 * MARGIN);
226 int limit = MIN((int) strlen(line), width - 2);
228 (void) wmove(obj->text, row, 0); /* move cursor to correct line */
229 wprintw(obj->text, " %.*s", limit, line);
230 wclrtoeol(obj->text);
235 wrote_size(MY_OBJ * obj, int want)
238 WROTE *wrote = obj->wrote;
239 while (wrote != NULL && want > 0) {
248 wrote_data(MY_OBJ * obj, int want)
250 const char *result = NULL;
251 WROTE *wrote = obj->wrote;
252 while (wrote != NULL && want > 0) {
253 result = wrote->text;
261 reprint_lines(MY_OBJ * obj, int buttons)
263 int want = getmaxy(obj->text) - (buttons ? 2 : 0);
264 int have = wrote_size(obj, want);
266 for (n = 0; n < have; ++n) {
267 print_line(obj, wrote_data(obj, have - n), n);
269 (void) wrefresh(obj->text);
275 pause_for_ok(MY_OBJ * obj, const char *title, const char *cprompt)
278 static DLG_KEYS_BINDING binding[] = {
288 int result = DLG_EXIT_UNKNOWN;
289 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) {
311 dlg_draw_buttons(obj->obj.win,
317 key = dlg_mouse_wgetch(obj->obj.win, &fkey);
318 if (dlg_result_key(key, fkey, &result))
321 if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
322 result = dlg_ok_buttoncode(check);
328 case DLGK_FIELD_NEXT:
329 button = dlg_next_button(buttons, button);
332 case DLGK_FIELD_PREV:
333 button = dlg_prev_button(buttons, button);
337 result = dlg_ok_buttoncode(button);
341 dlg_will_resize(obj->obj.win);
343 start_obj(obj, title, cprompt);
344 reprint_lines(obj, TRUE);
349 if (is_DLGK_MOUSE(key)) {
350 result = dlg_ok_buttoncode(key - M_EVENT);
352 result = DLG_EXIT_OK;
363 dlg_mouse_free_regions();
364 dlg_unregister_window(obj->obj.win);
366 dialog_vars.nocancel = save_nocancel;
371 dlg_progressbox(const char *title,
384 DLG_TRACE(("# progressbox args:\n"));
385 DLG_TRACE2S("title", title);
386 DLG_TRACE2S("message", cprompt);
387 DLG_TRACE2N("height", height);
388 DLG_TRACE2N("width", width);
389 DLG_TRACE2N("pause", pauseopt);
390 DLG_TRACE2N("fp", fp ? fileno(fp) : -1);
392 obj = dlg_calloc(MY_OBJ, 1);
393 assert_ptr(obj, "dlg_progressbox");
400 obj->old_high = height;
401 obj->old_wide = width;
407 start_obj(obj, title, cprompt);
410 toprow = reprint_lines(obj, FALSE);
414 for (i = toprow; get_line(obj, &again); i++) {
417 dlg_will_resize(obj->obj.win);
422 if (i < getmaxy(obj->text)) {
423 print_line(obj, obj->line, i);
425 scrollok(obj->text, TRUE);
427 scrollok(obj->text, FALSE);
428 print_line(obj, obj->line, getmaxy(obj->text) - 1);
430 (void) wrefresh(obj->text);
435 dlg_trace_win(obj->obj.win);
439 int need = 1 + MARGIN;
440 int base = getmaxy(obj->text) - need;
446 scrollok(obj->text, TRUE);
450 (void) wrefresh(obj->text);
451 result = pause_for_ok(obj, title, cprompt);
453 wrefresh(obj->obj.win);
454 result = DLG_EXIT_OK;
463 * Display text from a stdin in a scrolling window.
466 dialog_progressbox(const char *title, const char *cprompt, int height, int width)
469 result = dlg_progressbox(title,
474 dialog_state.pipe_input);
475 dialog_state.pipe_input = 0;