xref: /dflybsd-src/contrib/dialog/progressbox.c (revision b2dabe2e739bd72461a68ac543307c2dedfb048c)
15382d832SPeter Avalos /*
2*a8e38dc0SAntonio Huete Jimenez  *  $Id: progressbox.c,v 1.55 2022/04/03 22:38:16 tom Exp $
35382d832SPeter Avalos  *
45382d832SPeter Avalos  *  progressbox.c -- implements the progress box
55382d832SPeter Avalos  *
6*a8e38dc0SAntonio Huete Jimenez  *  Copyright 2006-2020,2022	Thomas E. Dickey
75382d832SPeter Avalos  *  Copyright 2005		Valery Reznic
85382d832SPeter Avalos  *
95382d832SPeter Avalos  *  This program is free software; you can redistribute it and/or modify
105940c9abSDaniel Fojt  *  it under the terms of the GNU Lesser General Public License, version 2.1
115940c9abSDaniel Fojt  *  as published by the Free Software Foundation.
125382d832SPeter Avalos  *
135382d832SPeter Avalos  *  This program is distributed in the hope that it will be useful, but
145382d832SPeter Avalos  *  WITHOUT ANY WARRANTY; without even the implied warranty of
155382d832SPeter Avalos  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
165382d832SPeter Avalos  *  Lesser General Public License for more details.
175382d832SPeter Avalos  *
185382d832SPeter Avalos  *  You should have received a copy of the GNU Lesser General Public
195382d832SPeter Avalos  *  License along with this program; if not, write to
205382d832SPeter Avalos  *	Free Software Foundation, Inc.
215382d832SPeter Avalos  *	51 Franklin St., Fifth Floor
225382d832SPeter Avalos  *	Boston, MA 02110, USA.
235382d832SPeter Avalos  */
245382d832SPeter Avalos 
25*a8e38dc0SAntonio Huete Jimenez #include <dlg_internals.h>
265382d832SPeter Avalos #include <dlg_keys.h>
275382d832SPeter Avalos 
285382d832SPeter Avalos #define MIN_HIGH (4)
295382d832SPeter Avalos #define MIN_WIDE (10 + 2 * (2 + MARGIN))
305382d832SPeter Avalos 
315940c9abSDaniel Fojt #ifdef KEY_RESIZE
325940c9abSDaniel Fojt typedef struct _wrote {
335940c9abSDaniel Fojt     struct _wrote *link;
345940c9abSDaniel Fojt     char *text;
355940c9abSDaniel Fojt } WROTE;
365940c9abSDaniel Fojt #endif
375940c9abSDaniel Fojt 
385382d832SPeter Avalos typedef struct {
395382d832SPeter Avalos     DIALOG_CALLBACK obj;
405382d832SPeter Avalos     WINDOW *text;
415940c9abSDaniel Fojt     char *prompt;
425940c9abSDaniel Fojt     int high, wide;
435940c9abSDaniel Fojt     int old_high, old_wide;
445382d832SPeter Avalos     char line[MAX_LEN + 1];
455382d832SPeter Avalos     int is_eof;
465940c9abSDaniel Fojt #ifdef KEY_RESIZE
475940c9abSDaniel Fojt     WROTE *wrote;
485940c9abSDaniel Fojt #endif
495382d832SPeter Avalos } MY_OBJ;
505382d832SPeter Avalos 
515940c9abSDaniel Fojt static void
free_obj(MY_OBJ * obj)525940c9abSDaniel Fojt free_obj(MY_OBJ * obj)
535940c9abSDaniel Fojt {
545940c9abSDaniel Fojt     dlg_del_window(obj->obj.win);
555940c9abSDaniel Fojt     free(obj->prompt);
565940c9abSDaniel Fojt #ifdef KEY_RESIZE
575940c9abSDaniel Fojt     while (obj->wrote) {
585940c9abSDaniel Fojt 	WROTE *wrote = obj->wrote;
595940c9abSDaniel Fojt 	obj->wrote = wrote->link;
605940c9abSDaniel Fojt 	free(wrote->text);
615940c9abSDaniel Fojt 	free(wrote);
625940c9abSDaniel Fojt     }
635940c9abSDaniel Fojt #endif
645940c9abSDaniel Fojt     free(obj);
655940c9abSDaniel Fojt }
665940c9abSDaniel Fojt 
67*a8e38dc0SAntonio Huete Jimenez #ifdef KEY_RESIZE
685940c9abSDaniel Fojt static void
restart_obj(MY_OBJ * obj)695940c9abSDaniel Fojt restart_obj(MY_OBJ * obj)
705940c9abSDaniel Fojt {
715940c9abSDaniel Fojt     free(obj->prompt);
725940c9abSDaniel Fojt     obj->high = obj->old_high;
735940c9abSDaniel Fojt     obj->wide = obj->old_wide;
745940c9abSDaniel Fojt     dlg_clear();
755940c9abSDaniel Fojt     dlg_del_window(obj->obj.win);
765940c9abSDaniel Fojt }
77*a8e38dc0SAntonio Huete Jimenez #endif
785940c9abSDaniel Fojt 
795940c9abSDaniel Fojt static void
start_obj(MY_OBJ * obj,const char * title,const char * cprompt)805940c9abSDaniel Fojt start_obj(MY_OBJ * obj, const char *title, const char *cprompt)
815940c9abSDaniel Fojt {
825940c9abSDaniel Fojt     int y, x, thigh;
835940c9abSDaniel Fojt 
845940c9abSDaniel Fojt     obj->prompt = dlg_strclone(cprompt);
855940c9abSDaniel Fojt     dlg_tab_correct_str(obj->prompt);
865940c9abSDaniel Fojt     dlg_auto_size(title, obj->prompt, &obj->high, &obj->wide, MIN_HIGH, MIN_WIDE);
875940c9abSDaniel Fojt 
885940c9abSDaniel Fojt     dlg_print_size(obj->high, obj->wide);
895940c9abSDaniel Fojt     dlg_ctl_size(obj->high, obj->wide);
905940c9abSDaniel Fojt 
915940c9abSDaniel Fojt     x = dlg_box_x_ordinate(obj->wide);
925940c9abSDaniel Fojt     y = dlg_box_y_ordinate(obj->high);
935940c9abSDaniel Fojt     thigh = obj->high - (2 * MARGIN);
945940c9abSDaniel Fojt 
955940c9abSDaniel Fojt     obj->obj.win = dlg_new_window(obj->high, obj->wide, y, x);
965940c9abSDaniel Fojt 
975940c9abSDaniel Fojt     dlg_draw_box2(obj->obj.win,
985940c9abSDaniel Fojt 		  0, 0,
995940c9abSDaniel Fojt 		  obj->high, obj->wide,
1005940c9abSDaniel Fojt 		  dialog_attr,
1015940c9abSDaniel Fojt 		  border_attr,
1025940c9abSDaniel Fojt 		  border2_attr);
1035940c9abSDaniel Fojt     dlg_draw_title(obj->obj.win, title);
1045940c9abSDaniel Fojt     dlg_draw_helpline(obj->obj.win, FALSE);
1055940c9abSDaniel Fojt 
1065940c9abSDaniel Fojt     if (obj->prompt[0] != '\0') {
1075940c9abSDaniel Fojt 	int i;
1085940c9abSDaniel Fojt 	int y2, x2;
1095940c9abSDaniel Fojt 
1105940c9abSDaniel Fojt 	dlg_attrset(obj->obj.win, dialog_attr);
1115940c9abSDaniel Fojt 	dlg_print_autowrap(obj->obj.win, obj->prompt, obj->high, obj->wide);
1125940c9abSDaniel Fojt 	getyx(obj->obj.win, y2, x2);
1135940c9abSDaniel Fojt 	(void) x2;
1145940c9abSDaniel Fojt 	++y2;
1155940c9abSDaniel Fojt 	wmove(obj->obj.win, y2, MARGIN);
1165940c9abSDaniel Fojt 	for (i = 0; i < getmaxx(obj->obj.win) - 2 * MARGIN; i++)
1175940c9abSDaniel Fojt 	    (void) waddch(obj->obj.win, dlg_boxchar(ACS_HLINE));
1185940c9abSDaniel Fojt 	y += y2;
1195940c9abSDaniel Fojt 	thigh -= y2;
1205940c9abSDaniel Fojt     }
1215940c9abSDaniel Fojt 
1225940c9abSDaniel Fojt     /* Create window for text region, used for scrolling text */
1235940c9abSDaniel Fojt     obj->text = dlg_sub_window(obj->obj.win,
1245940c9abSDaniel Fojt 			       thigh,
1255940c9abSDaniel Fojt 			       obj->wide - (2 * MARGIN),
1265940c9abSDaniel Fojt 			       y + MARGIN,
1275940c9abSDaniel Fojt 			       x + MARGIN);
1285940c9abSDaniel Fojt 
1295940c9abSDaniel Fojt     (void) wrefresh(obj->obj.win);
1305940c9abSDaniel Fojt 
1315940c9abSDaniel Fojt     (void) wmove(obj->obj.win, getmaxy(obj->text), (MARGIN + 1));
1325940c9abSDaniel Fojt     (void) wnoutrefresh(obj->obj.win);
1335940c9abSDaniel Fojt 
1345940c9abSDaniel Fojt     dlg_attr_clear(obj->text, getmaxy(obj->text), getmaxx(obj->text), dialog_attr);
1355940c9abSDaniel Fojt }
1365940c9abSDaniel Fojt 
1375382d832SPeter Avalos /*
1385382d832SPeter Avalos  * Return current line of text.
1395382d832SPeter Avalos  */
1405382d832SPeter Avalos static char *
get_line(MY_OBJ * obj,int * restart)1415940c9abSDaniel Fojt get_line(MY_OBJ * obj, int *restart)
1425382d832SPeter Avalos {
1435382d832SPeter Avalos     FILE *fp = obj->obj.input;
1445382d832SPeter Avalos     int col = 0;
1455940c9abSDaniel Fojt     int j, tmpint;
1465940c9abSDaniel Fojt     char *result = obj->line;
1475382d832SPeter Avalos 
1485940c9abSDaniel Fojt     *restart = 0;
1495382d832SPeter Avalos     for (;;) {
1505940c9abSDaniel Fojt 	int ch = getc(fp);
1515940c9abSDaniel Fojt #ifdef KEY_RESIZE
1525940c9abSDaniel Fojt 	/* SIGWINCH may have interrupted this - try to ignore if resizable */
1535940c9abSDaniel Fojt 	if (ferror(fp)) {
1545940c9abSDaniel Fojt 	    switch (errno) {
1555940c9abSDaniel Fojt 	    case EINTR:
1565940c9abSDaniel Fojt 		clearerr(fp);
1575940c9abSDaniel Fojt 		continue;
1585940c9abSDaniel Fojt 	    default:
1595382d832SPeter Avalos 		break;
1605382d832SPeter Avalos 	    }
1615382d832SPeter Avalos 	}
1625940c9abSDaniel Fojt #endif
1635940c9abSDaniel Fojt 	if (feof(fp) || ferror(fp)) {
1645940c9abSDaniel Fojt 	    obj->is_eof = 1;
1655940c9abSDaniel Fojt 	    if (!col) {
1665940c9abSDaniel Fojt 		result = NULL;
1675940c9abSDaniel Fojt 	    }
1685940c9abSDaniel Fojt 	    break;
1695940c9abSDaniel Fojt 	}
1705382d832SPeter Avalos 	if (ch == '\n')
1715382d832SPeter Avalos 	    break;
1725382d832SPeter Avalos 	if (ch == '\r')
1735382d832SPeter Avalos 	    break;
1745382d832SPeter Avalos 	if (col >= MAX_LEN)
1755382d832SPeter Avalos 	    continue;
1765382d832SPeter Avalos 	if ((ch == TAB) && (dialog_vars.tab_correct)) {
1775382d832SPeter Avalos 	    tmpint = dialog_state.tab_len
1785382d832SPeter Avalos 		- (col % dialog_state.tab_len);
1795382d832SPeter Avalos 	    for (j = 0; j < tmpint; j++) {
1805382d832SPeter Avalos 		if (col < MAX_LEN) {
1815382d832SPeter Avalos 		    obj->line[col] = ' ';
1825382d832SPeter Avalos 		    ++col;
1835382d832SPeter Avalos 		} else {
1845382d832SPeter Avalos 		    break;
1855382d832SPeter Avalos 		}
1865382d832SPeter Avalos 	    }
1875382d832SPeter Avalos 	} else {
1885382d832SPeter Avalos 	    obj->line[col] = (char) ch;
1895382d832SPeter Avalos 	    ++col;
1905382d832SPeter Avalos 	}
1915382d832SPeter Avalos     }
1925382d832SPeter Avalos 
1935382d832SPeter Avalos     obj->line[col] = '\0';
1945382d832SPeter Avalos 
1955940c9abSDaniel Fojt #ifdef KEY_RESIZE
1965940c9abSDaniel Fojt     if (result != NULL) {
1975940c9abSDaniel Fojt 	WINDOW *win = obj->text;
1985940c9abSDaniel Fojt 	WROTE *wrote = dlg_calloc(WROTE, 1);
1995940c9abSDaniel Fojt 
2005940c9abSDaniel Fojt 	if (wrote != 0) {
2015940c9abSDaniel Fojt 	    wrote->text = dlg_strclone(obj->line);
2025940c9abSDaniel Fojt 	    wrote->link = obj->wrote;
2035940c9abSDaniel Fojt 	    obj->wrote = wrote;
2045940c9abSDaniel Fojt 	}
2055940c9abSDaniel Fojt 
2065940c9abSDaniel Fojt 	nodelay(win, TRUE);
2075940c9abSDaniel Fojt 	if (wgetch(win) == KEY_RESIZE) {
2085940c9abSDaniel Fojt 	    *restart = 1;
2095940c9abSDaniel Fojt 	}
2105940c9abSDaniel Fojt 	nodelay(win, FALSE);
2115940c9abSDaniel Fojt     }
2125940c9abSDaniel Fojt #endif
2135940c9abSDaniel Fojt     return result;
2145382d832SPeter Avalos }
2155382d832SPeter Avalos 
2165382d832SPeter Avalos /*
2175382d832SPeter Avalos  * Print a new line of text.
2185382d832SPeter Avalos  */
2195382d832SPeter Avalos static void
print_line(MY_OBJ * obj,const char * line,int row)2205940c9abSDaniel Fojt print_line(MY_OBJ * obj, const char *line, int row)
2215382d832SPeter Avalos {
2225940c9abSDaniel Fojt     int width = obj->wide - (2 * MARGIN);
2235940c9abSDaniel Fojt     int limit = MIN((int) strlen(line), width - 2);
2245382d832SPeter Avalos 
2255940c9abSDaniel Fojt     (void) wmove(obj->text, row, 0);	/* move cursor to correct line */
2265940c9abSDaniel Fojt     wprintw(obj->text, " %.*s", limit, line);
2275940c9abSDaniel Fojt     while (++limit < width) {
2285940c9abSDaniel Fojt 	waddch(obj->text, ' ');
2295940c9abSDaniel Fojt     }
2305940c9abSDaniel Fojt }
2315382d832SPeter Avalos 
2325940c9abSDaniel Fojt #ifdef KEY_RESIZE
2335940c9abSDaniel Fojt static int
wrote_size(MY_OBJ * obj,int want)2345940c9abSDaniel Fojt wrote_size(MY_OBJ * obj, int want)
2355940c9abSDaniel Fojt {
2365940c9abSDaniel Fojt     int result = 0;
2375940c9abSDaniel Fojt     WROTE *wrote = obj->wrote;
2385940c9abSDaniel Fojt     while (wrote != NULL && want > 0) {
2395940c9abSDaniel Fojt 	wrote = wrote->link;
2405940c9abSDaniel Fojt 	want--;
2415940c9abSDaniel Fojt 	result++;
2425940c9abSDaniel Fojt     }
2435940c9abSDaniel Fojt     return result;
2445940c9abSDaniel Fojt }
2455940c9abSDaniel Fojt 
2465940c9abSDaniel Fojt static const char *
wrote_data(MY_OBJ * obj,int want)2475940c9abSDaniel Fojt wrote_data(MY_OBJ * obj, int want)
2485940c9abSDaniel Fojt {
2495940c9abSDaniel Fojt     const char *result = NULL;
2505940c9abSDaniel Fojt     WROTE *wrote = obj->wrote;
2515940c9abSDaniel Fojt     while (wrote != NULL && want > 0) {
2525940c9abSDaniel Fojt 	result = wrote->text;
2535940c9abSDaniel Fojt 	wrote = wrote->link;
2545940c9abSDaniel Fojt 	want--;
2555940c9abSDaniel Fojt     }
2565940c9abSDaniel Fojt     return result;
2575382d832SPeter Avalos }
2585382d832SPeter Avalos 
2595382d832SPeter Avalos static int
reprint_lines(MY_OBJ * obj,int buttons)2605940c9abSDaniel Fojt reprint_lines(MY_OBJ * obj, int buttons)
2615940c9abSDaniel Fojt {
2625940c9abSDaniel Fojt     int want = getmaxy(obj->text) - (buttons ? 2 : 0);
2635940c9abSDaniel Fojt     int have = wrote_size(obj, want);
2645940c9abSDaniel Fojt     int n;
2655940c9abSDaniel Fojt     for (n = 0; n < have; ++n) {
2665940c9abSDaniel Fojt 	print_line(obj, wrote_data(obj, have - n), n);
2675940c9abSDaniel Fojt     }
2685940c9abSDaniel Fojt     (void) wrefresh(obj->text);
2695940c9abSDaniel Fojt     return have;
2705940c9abSDaniel Fojt }
2715940c9abSDaniel Fojt #endif
2725940c9abSDaniel Fojt 
2735940c9abSDaniel Fojt static int
pause_for_ok(MY_OBJ * obj,const char * title,const char * cprompt)2745940c9abSDaniel Fojt pause_for_ok(MY_OBJ * obj, const char *title, const char *cprompt)
2755382d832SPeter Avalos {
2765382d832SPeter Avalos     /* *INDENT-OFF* */
2775382d832SPeter Avalos     static DLG_KEYS_BINDING binding[] = {
2785382d832SPeter Avalos 	HELPKEY_BINDINGS,
2795382d832SPeter Avalos 	ENTERKEY_BINDINGS,
2805382d832SPeter Avalos 	TRAVERSE_BINDINGS,
2815382d832SPeter Avalos 	END_KEYS_BINDING
2825382d832SPeter Avalos     };
2835382d832SPeter Avalos     /* *INDENT-ON* */
2845382d832SPeter Avalos 
2855382d832SPeter Avalos     int button;
2865940c9abSDaniel Fojt     int key, fkey;
2875382d832SPeter Avalos     int result = DLG_EXIT_UNKNOWN;
2885382d832SPeter Avalos     const char **buttons = dlg_ok_label();
2895940c9abSDaniel Fojt     bool save_nocancel = dialog_vars.nocancel;
2905382d832SPeter Avalos     bool redraw = TRUE;
2915382d832SPeter Avalos 
292*a8e38dc0SAntonio Huete Jimenez     (void) title;
293*a8e38dc0SAntonio Huete Jimenez     (void) cprompt;
294*a8e38dc0SAntonio Huete Jimenez 
2955382d832SPeter Avalos     dialog_vars.nocancel = TRUE;
2965382d832SPeter Avalos     button = dlg_default_button();
2975382d832SPeter Avalos 
2985940c9abSDaniel Fojt #ifdef KEY_RESIZE
2995940c9abSDaniel Fojt   restart:
3005940c9abSDaniel Fojt #endif
3015382d832SPeter Avalos 
3025940c9abSDaniel Fojt     dlg_register_window(obj->obj.win, "progressbox", binding);
3035940c9abSDaniel Fojt     dlg_register_buttons(obj->obj.win, "progressbox", buttons);
3045940c9abSDaniel Fojt 
3055940c9abSDaniel Fojt     dlg_draw_bottom_box2(obj->obj.win, border_attr, border2_attr, dialog_attr);
3065382d832SPeter Avalos 
3075382d832SPeter Avalos     while (result == DLG_EXIT_UNKNOWN) {
3085940c9abSDaniel Fojt 	int check;
3095940c9abSDaniel Fojt 
3105382d832SPeter Avalos 	if (redraw) {
3115382d832SPeter Avalos 	    redraw = FALSE;
3125382d832SPeter Avalos 	    if (button < 0)
3135382d832SPeter Avalos 		button = 0;
3145940c9abSDaniel Fojt 	    dlg_draw_buttons(obj->obj.win,
3155940c9abSDaniel Fojt 			     obj->high - 2, 0,
3165382d832SPeter Avalos 			     buttons, button,
3175940c9abSDaniel Fojt 			     FALSE, obj->wide);
3185382d832SPeter Avalos 	}
3195382d832SPeter Avalos 
3205940c9abSDaniel Fojt 	key = dlg_mouse_wgetch(obj->obj.win, &fkey);
3215940c9abSDaniel Fojt 	if (dlg_result_key(key, fkey, &result)) {
3225940c9abSDaniel Fojt 	    if (!dlg_button_key(result, &button, &key, &fkey))
3235382d832SPeter Avalos 		break;
3245940c9abSDaniel Fojt 	}
3255382d832SPeter Avalos 
3265382d832SPeter Avalos 	if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
3275382d832SPeter Avalos 	    result = dlg_ok_buttoncode(check);
3285382d832SPeter Avalos 	    break;
3295382d832SPeter Avalos 	}
3305382d832SPeter Avalos 
3315382d832SPeter Avalos 	if (fkey) {
3325382d832SPeter Avalos 	    switch (key) {
3335382d832SPeter Avalos 	    case DLGK_FIELD_NEXT:
3345382d832SPeter Avalos 		button = dlg_next_button(buttons, button);
3355382d832SPeter Avalos 		redraw = TRUE;
3365382d832SPeter Avalos 		break;
3375382d832SPeter Avalos 	    case DLGK_FIELD_PREV:
3385382d832SPeter Avalos 		button = dlg_prev_button(buttons, button);
3395382d832SPeter Avalos 		redraw = TRUE;
3405382d832SPeter Avalos 		break;
3415382d832SPeter Avalos 	    case DLGK_ENTER:
3425382d832SPeter Avalos 		result = dlg_ok_buttoncode(button);
3435382d832SPeter Avalos 		break;
3445940c9abSDaniel Fojt #ifdef KEY_RESIZE
3455940c9abSDaniel Fojt 	    case KEY_RESIZE:
3465940c9abSDaniel Fojt 		dlg_will_resize(obj->obj.win);
3475940c9abSDaniel Fojt 		restart_obj(obj);
3485940c9abSDaniel Fojt 		start_obj(obj, title, cprompt);
3495940c9abSDaniel Fojt 		reprint_lines(obj, TRUE);
3505940c9abSDaniel Fojt 		redraw = TRUE;
3515940c9abSDaniel Fojt 		goto restart;
3525940c9abSDaniel Fojt #endif
3535382d832SPeter Avalos 	    default:
3545382d832SPeter Avalos 		if (is_DLGK_MOUSE(key)) {
3555382d832SPeter Avalos 		    result = dlg_ok_buttoncode(key - M_EVENT);
3565382d832SPeter Avalos 		    if (result < 0)
3575382d832SPeter Avalos 			result = DLG_EXIT_OK;
3585382d832SPeter Avalos 		} else {
3595382d832SPeter Avalos 		    beep();
3605382d832SPeter Avalos 		}
3615382d832SPeter Avalos 		break;
3625382d832SPeter Avalos 	    }
3635382d832SPeter Avalos 
3645940c9abSDaniel Fojt 	} else if (key > 0) {
3655382d832SPeter Avalos 	    beep();
3665382d832SPeter Avalos 	}
3675382d832SPeter Avalos     }
3685940c9abSDaniel Fojt     dlg_add_last_key(-1);
3695940c9abSDaniel Fojt 
3705940c9abSDaniel Fojt     dlg_mouse_free_regions();
3715940c9abSDaniel Fojt     dlg_unregister_window(obj->obj.win);
3725382d832SPeter Avalos 
3735382d832SPeter Avalos     dialog_vars.nocancel = save_nocancel;
3745382d832SPeter Avalos     return result;
3755382d832SPeter Avalos }
3765382d832SPeter Avalos 
3775382d832SPeter Avalos int
dlg_progressbox(const char * title,const char * cprompt,int height,int width,int pauseopt,FILE * fp)3785382d832SPeter Avalos dlg_progressbox(const char *title,
3795382d832SPeter Avalos 		const char *cprompt,
3805382d832SPeter Avalos 		int height,
3815382d832SPeter Avalos 		int width,
3825382d832SPeter Avalos 		int pauseopt,
3835382d832SPeter Avalos 		FILE *fp)
3845382d832SPeter Avalos {
3855382d832SPeter Avalos     int i;
3865382d832SPeter Avalos     MY_OBJ *obj;
3875940c9abSDaniel Fojt     int again = 0;
3885940c9abSDaniel Fojt     int toprow = 0;
3895382d832SPeter Avalos     int result;
3905382d832SPeter Avalos 
3915940c9abSDaniel Fojt     DLG_TRACE(("# progressbox args:\n"));
3925940c9abSDaniel Fojt     DLG_TRACE2S("title", title);
3935940c9abSDaniel Fojt     DLG_TRACE2S("message", cprompt);
3945940c9abSDaniel Fojt     DLG_TRACE2N("height", height);
3955940c9abSDaniel Fojt     DLG_TRACE2N("width", width);
3965940c9abSDaniel Fojt     DLG_TRACE2N("pause", pauseopt);
3975940c9abSDaniel Fojt     DLG_TRACE2N("fp", fp ? fileno(fp) : -1);
3985382d832SPeter Avalos 
3995382d832SPeter Avalos     obj = dlg_calloc(MY_OBJ, 1);
4005382d832SPeter Avalos     assert_ptr(obj, "dlg_progressbox");
4015382d832SPeter Avalos     obj->obj.input = fp;
4025382d832SPeter Avalos 
4035940c9abSDaniel Fojt     obj->high = height;
4045940c9abSDaniel Fojt     obj->wide = width;
4055940c9abSDaniel Fojt 
4065940c9abSDaniel Fojt #ifdef KEY_RESIZE
4075940c9abSDaniel Fojt     obj->old_high = height;
4085940c9abSDaniel Fojt     obj->old_wide = width;
4095940c9abSDaniel Fojt 
4105940c9abSDaniel Fojt     curs_set(0);
4115940c9abSDaniel Fojt   restart:
4125940c9abSDaniel Fojt #endif
4135940c9abSDaniel Fojt 
4145940c9abSDaniel Fojt     start_obj(obj, title, cprompt);
4155940c9abSDaniel Fojt #ifdef KEY_RESIZE
4165940c9abSDaniel Fojt     if (again) {
4175940c9abSDaniel Fojt 	toprow = reprint_lines(obj, FALSE);
4185382d832SPeter Avalos     }
4195940c9abSDaniel Fojt #endif
4205940c9abSDaniel Fojt 
4215940c9abSDaniel Fojt     for (i = toprow; get_line(obj, &again); i++) {
4225940c9abSDaniel Fojt #ifdef KEY_RESIZE
4235940c9abSDaniel Fojt 	if (again) {
4245940c9abSDaniel Fojt 	    dlg_will_resize(obj->obj.win);
4255940c9abSDaniel Fojt 	    restart_obj(obj);
4265940c9abSDaniel Fojt 	    goto restart;
4275940c9abSDaniel Fojt 	}
4285940c9abSDaniel Fojt #endif
4295940c9abSDaniel Fojt 	if (i < getmaxy(obj->text)) {
4305940c9abSDaniel Fojt 	    print_line(obj, obj->line, i);
4315940c9abSDaniel Fojt 	} else {
4325940c9abSDaniel Fojt 	    scrollok(obj->text, TRUE);
4335940c9abSDaniel Fojt 	    scroll(obj->text);
4345940c9abSDaniel Fojt 	    scrollok(obj->text, FALSE);
4355940c9abSDaniel Fojt 	    print_line(obj, obj->line, getmaxy(obj->text) - 1);
4365940c9abSDaniel Fojt 	}
4375940c9abSDaniel Fojt 	(void) wrefresh(obj->text);
4385382d832SPeter Avalos 	if (obj->is_eof)
4395382d832SPeter Avalos 	    break;
4405382d832SPeter Avalos     }
4415382d832SPeter Avalos 
4425940c9abSDaniel Fojt     dlg_trace_win(obj->obj.win);
4435940c9abSDaniel Fojt     curs_set(1);
4445940c9abSDaniel Fojt 
4455382d832SPeter Avalos     if (pauseopt) {
4461ef6786aSJohn Marino 	int need = 1 + MARGIN;
4475940c9abSDaniel Fojt 	int base = getmaxy(obj->text) - need;
4481ef6786aSJohn Marino 	if (i >= base) {
4491ef6786aSJohn Marino 	    i -= base;
4501ef6786aSJohn Marino 	    if (i > need)
4511ef6786aSJohn Marino 		i = need;
4521ef6786aSJohn Marino 	    if (i > 0) {
4535940c9abSDaniel Fojt 		scrollok(obj->text, TRUE);
4541ef6786aSJohn Marino 	    }
4555940c9abSDaniel Fojt 	    wscrl(obj->text, i);
4561ef6786aSJohn Marino 	}
4575940c9abSDaniel Fojt 	(void) wrefresh(obj->text);
4585940c9abSDaniel Fojt 	result = pause_for_ok(obj, title, cprompt);
4595382d832SPeter Avalos     } else {
4605940c9abSDaniel Fojt 	wrefresh(obj->obj.win);
4615382d832SPeter Avalos 	result = DLG_EXIT_OK;
4625382d832SPeter Avalos     }
4635382d832SPeter Avalos 
4645940c9abSDaniel Fojt     free_obj(obj);
4655382d832SPeter Avalos 
4665382d832SPeter Avalos     return result;
4675382d832SPeter Avalos }
4685382d832SPeter Avalos 
4695382d832SPeter Avalos /*
4705382d832SPeter Avalos  * Display text from a stdin in a scrolling window.
4715382d832SPeter Avalos  */
4725382d832SPeter Avalos int
dialog_progressbox(const char * title,const char * cprompt,int height,int width)4735382d832SPeter Avalos dialog_progressbox(const char *title, const char *cprompt, int height, int width)
4745382d832SPeter Avalos {
4755382d832SPeter Avalos     int result;
4765382d832SPeter Avalos     result = dlg_progressbox(title,
4775382d832SPeter Avalos 			     cprompt,
4785382d832SPeter Avalos 			     height,
4795382d832SPeter Avalos 			     width,
4805382d832SPeter Avalos 			     FALSE,
4815382d832SPeter Avalos 			     dialog_state.pipe_input);
4825382d832SPeter Avalos     dialog_state.pipe_input = 0;
4835382d832SPeter Avalos     return result;
4845382d832SPeter Avalos }
485