xref: /freebsd/contrib/dialog/pause.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin  *  $Id: pause.c,v 1.48 2020/11/23 00:38:19 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  pause.c -- implements the pause dialog
54c8945a0SNathan Whitehorn  *
6*a96ef450SBaptiste Daroussin  *  Copyright 2004-2019,2020	Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  *
234c8945a0SNathan Whitehorn  *  This is adapted from source contributed by
244c8945a0SNathan Whitehorn  *	Yura Kalinichenko
254c8945a0SNathan Whitehorn  */
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <dialog.h>
284c8945a0SNathan Whitehorn #include <dlg_keys.h>
294c8945a0SNathan Whitehorn 
304c8945a0SNathan Whitehorn #define MY_TIMEOUT 50
314c8945a0SNathan Whitehorn 
324c8945a0SNathan Whitehorn #define MIN_HIGH (4)
334c8945a0SNathan Whitehorn #define MIN_WIDE (10 + 2 * (2 + MARGIN))
344c8945a0SNathan Whitehorn #define BTN_HIGH (1 + 2 * MARGIN)
354c8945a0SNathan Whitehorn 
364c8945a0SNathan Whitehorn /*
374c8945a0SNathan Whitehorn  * This is like gauge, but can be interrupted.
384c8945a0SNathan Whitehorn  *
394c8945a0SNathan Whitehorn  * A pause box displays a meter along the bottom of the box.  The meter
404c8945a0SNathan Whitehorn  * indicates how many seconds remain until the end of the pause.  The pause
414c8945a0SNathan Whitehorn  * exits when timeout is reached (status OK) or the user presses:
424c8945a0SNathan Whitehorn  *   OK button (status OK)
434c8945a0SNathan Whitehorn  *   CANCEL button (status CANCEL)
444c8945a0SNathan Whitehorn  *   Esc key (status ESC)
454c8945a0SNathan Whitehorn  *
464c8945a0SNathan Whitehorn  */
474c8945a0SNathan Whitehorn int
dialog_pause(const char * title,const char * cprompt,int height,int width,int seconds)484c8945a0SNathan Whitehorn dialog_pause(const char *title,
494c8945a0SNathan Whitehorn 	     const char *cprompt,
504c8945a0SNathan Whitehorn 	     int height,
514c8945a0SNathan Whitehorn 	     int width,
524c8945a0SNathan Whitehorn 	     int seconds)
534c8945a0SNathan Whitehorn {
544c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
554c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
56682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
574c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
582a3e3873SBaptiste Daroussin 	TRAVERSE_BINDINGS,
594c8945a0SNathan Whitehorn 	END_KEYS_BINDING
604c8945a0SNathan Whitehorn     };
614c8945a0SNathan Whitehorn     /* *INDENT-ON* */
624c8945a0SNathan Whitehorn 
634c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
644c8945a0SNathan Whitehorn     int old_height = height;
654c8945a0SNathan Whitehorn     int old_width = width;
664c8945a0SNathan Whitehorn #endif
674c8945a0SNathan Whitehorn 
684c8945a0SNathan Whitehorn     int i, x, y, step;
692a3e3873SBaptiste Daroussin     int button = dlg_default_button();
704c8945a0SNathan Whitehorn     int seconds_orig;
714c8945a0SNathan Whitehorn     WINDOW *dialog;
724c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_labels();
734c8945a0SNathan Whitehorn     bool have_buttons = (dlg_button_count(buttons) != 0);
742a3e3873SBaptiste Daroussin     bool first;
75*a96ef450SBaptiste Daroussin     int key, fkey;
764c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
774c8945a0SNathan Whitehorn     int button_high = (have_buttons ? BTN_HIGH : MARGIN);
787a1c0d96SNathan Whitehorn     int gauge_y;
79f4f33ea0SBaptiste Daroussin     char *prompt;
802a3e3873SBaptiste Daroussin     int save_timeout = dialog_vars.timeout_secs;
814c8945a0SNathan Whitehorn 
82f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# pause args:\n"));
83f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
84f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("message", cprompt);
85f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
86f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
87f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("seconds", seconds);
884c8945a0SNathan Whitehorn 
89f4f33ea0SBaptiste Daroussin     curs_set(0);
904c8945a0SNathan Whitehorn 
914c8945a0SNathan Whitehorn     seconds_orig = (seconds > 0) ? seconds : 1;
92*a96ef450SBaptiste Daroussin     dialog_vars.pause_secs = seconds_orig;
93*a96ef450SBaptiste Daroussin     dialog_vars.timeout_secs = 0;
944c8945a0SNathan Whitehorn 
954c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
964c8945a0SNathan Whitehorn   retry:
974c8945a0SNathan Whitehorn #endif
984c8945a0SNathan Whitehorn 
99f4f33ea0SBaptiste Daroussin     prompt = dlg_strclone(cprompt);
100f4f33ea0SBaptiste Daroussin     dlg_tab_correct_str(prompt);
101f4f33ea0SBaptiste Daroussin 
1024c8945a0SNathan Whitehorn     if (have_buttons) {
1034c8945a0SNathan Whitehorn 	dlg_auto_size(title, prompt, &height, &width,
1044c8945a0SNathan Whitehorn 		      MIN_HIGH,
1054c8945a0SNathan Whitehorn 		      MIN_WIDE);
1064c8945a0SNathan Whitehorn 	dlg_button_layout(buttons, &width);
1074c8945a0SNathan Whitehorn     } else {
1084c8945a0SNathan Whitehorn 	dlg_auto_size(title, prompt, &height, &width,
1094c8945a0SNathan Whitehorn 		      MIN_HIGH + MARGIN - BTN_HIGH,
1104c8945a0SNathan Whitehorn 		      MIN_WIDE);
1114c8945a0SNathan Whitehorn     }
1127a1c0d96SNathan Whitehorn     gauge_y = height - button_high - (1 + 2 * MARGIN);
1134c8945a0SNathan Whitehorn     dlg_print_size(height, width);
1144c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
1154c8945a0SNathan Whitehorn 
1164c8945a0SNathan Whitehorn     /* center dialog box on screen */
1174c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
1184c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
1194c8945a0SNathan Whitehorn 
1204c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width, y, x);
1214c8945a0SNathan Whitehorn     dlg_register_window(dialog, "pause", binding);
1224c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "pause", buttons);
1234c8945a0SNathan Whitehorn 
1244c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
1254c8945a0SNathan Whitehorn     nodelay(dialog, TRUE);
1264c8945a0SNathan Whitehorn 
1272a3e3873SBaptiste Daroussin     first = TRUE;
1284c8945a0SNathan Whitehorn     do {
1294c8945a0SNathan Whitehorn 	(void) werase(dialog);
1302a3e3873SBaptiste Daroussin 	dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
1314c8945a0SNathan Whitehorn 
1324c8945a0SNathan Whitehorn 	dlg_draw_title(dialog, title);
133682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1344c8945a0SNathan Whitehorn 
135f4f33ea0SBaptiste Daroussin 	dlg_attrset(dialog, dialog_attr);
1364c8945a0SNathan Whitehorn 	dlg_print_autowrap(dialog, prompt, height, width);
1374c8945a0SNathan Whitehorn 
1382a3e3873SBaptiste Daroussin 	dlg_draw_box2(dialog,
1397a1c0d96SNathan Whitehorn 		      gauge_y, 2 + MARGIN,
1404c8945a0SNathan Whitehorn 		      2 + MARGIN, width - 2 * (2 + MARGIN),
1414c8945a0SNathan Whitehorn 		      dialog_attr,
1422a3e3873SBaptiste Daroussin 		      border_attr,
1432a3e3873SBaptiste Daroussin 		      border2_attr);
1444c8945a0SNathan Whitehorn 
1454c8945a0SNathan Whitehorn 	/*
1464c8945a0SNathan Whitehorn 	 * Clear the area for the progress bar by filling it with spaces
1474c8945a0SNathan Whitehorn 	 * in the title-attribute, and write the percentage with that
1484c8945a0SNathan Whitehorn 	 * attribute.
1494c8945a0SNathan Whitehorn 	 */
1507a1c0d96SNathan Whitehorn 	(void) wmove(dialog, gauge_y + MARGIN, 4);
151f4f33ea0SBaptiste Daroussin 	dlg_attrset(dialog, title_attr);
1524c8945a0SNathan Whitehorn 
1534c8945a0SNathan Whitehorn 	for (i = 0; i < (width - 2 * (3 + MARGIN)); i++)
1544c8945a0SNathan Whitehorn 	    (void) waddch(dialog, ' ');
1554c8945a0SNathan Whitehorn 
1567a1c0d96SNathan Whitehorn 	(void) wmove(dialog, gauge_y + MARGIN, (width / 2) - 2);
1574c8945a0SNathan Whitehorn 	(void) wprintw(dialog, "%3d", seconds);
1584c8945a0SNathan Whitehorn 
1594c8945a0SNathan Whitehorn 	/*
1604c8945a0SNathan Whitehorn 	 * Now draw a bar in reverse, relative to the background.
1614c8945a0SNathan Whitehorn 	 * The window attribute was useful for painting the background,
1624c8945a0SNathan Whitehorn 	 * but requires some tweaks to reverse it.
1634c8945a0SNathan Whitehorn 	 */
1644c8945a0SNathan Whitehorn 	x = (seconds * (width - 2 * (3 + MARGIN))) / seconds_orig;
1654c8945a0SNathan Whitehorn 	if ((title_attr & A_REVERSE) != 0) {
166f4f33ea0SBaptiste Daroussin 	    dlg_attroff(dialog, A_REVERSE);
1674c8945a0SNathan Whitehorn 	} else {
168f4f33ea0SBaptiste Daroussin 	    dlg_attrset(dialog, A_REVERSE);
1694c8945a0SNathan Whitehorn 	}
1707a1c0d96SNathan Whitehorn 	(void) wmove(dialog, gauge_y + MARGIN, 4);
1714c8945a0SNathan Whitehorn 	for (i = 0; i < x; i++) {
1724c8945a0SNathan Whitehorn 	    chtype ch = winch(dialog);
1734c8945a0SNathan Whitehorn 	    if (title_attr & A_REVERSE) {
1744c8945a0SNathan Whitehorn 		ch &= ~A_REVERSE;
1754c8945a0SNathan Whitehorn 	    }
1764c8945a0SNathan Whitehorn 	    (void) waddch(dialog, ch);
1774c8945a0SNathan Whitehorn 	}
1784c8945a0SNathan Whitehorn 
1794c8945a0SNathan Whitehorn 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
1804c8945a0SNathan Whitehorn 	if (have_buttons) {
1812a3e3873SBaptiste Daroussin 	    dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1824c8945a0SNathan Whitehorn 	    dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
1834c8945a0SNathan Whitehorn 	}
1842a3e3873SBaptiste Daroussin 	if (first) {
1854c8945a0SNathan Whitehorn 	    (void) wrefresh(dialog);
1862a3e3873SBaptiste Daroussin 	    dlg_trace_win(dialog);
1872a3e3873SBaptiste Daroussin 	    first = FALSE;
1882a3e3873SBaptiste Daroussin 	}
1894c8945a0SNathan Whitehorn 
1904c8945a0SNathan Whitehorn 	for (step = 0;
1914c8945a0SNathan Whitehorn 	     (result == DLG_EXIT_UNKNOWN) && (step < 1000);
1924c8945a0SNathan Whitehorn 	     step += MY_TIMEOUT) {
1934c8945a0SNathan Whitehorn 
1944c8945a0SNathan Whitehorn 	    napms(MY_TIMEOUT);
1954c8945a0SNathan Whitehorn 	    key = dlg_mouse_wgetch_nowait(dialog, &fkey);
1964c8945a0SNathan Whitehorn 	    if (key == ERR) {
1974c8945a0SNathan Whitehorn 		;		/* ignore errors in nodelay mode */
198*a96ef450SBaptiste Daroussin 	    } else if (dlg_result_key(key, fkey, &result)) {
199*a96ef450SBaptiste Daroussin 		if (!dlg_button_key(result, &button, &key, &fkey))
2004c8945a0SNathan Whitehorn 		    break;
2014c8945a0SNathan Whitehorn 	    }
2024c8945a0SNathan Whitehorn 
2034c8945a0SNathan Whitehorn 	    switch (key) {
2044c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
2054c8945a0SNathan Whitehorn 	    case KEY_RESIZE:
206f4f33ea0SBaptiste Daroussin 		dlg_will_resize(dialog);
207f4f33ea0SBaptiste Daroussin 		height = old_height;
208f4f33ea0SBaptiste Daroussin 		width = old_width;
209f4f33ea0SBaptiste Daroussin 		free(prompt);
210*a96ef450SBaptiste Daroussin 		_dlg_resize_cleanup(dialog);
2114c8945a0SNathan Whitehorn 		goto retry;
2124c8945a0SNathan Whitehorn #endif
2134c8945a0SNathan Whitehorn 	    case DLGK_FIELD_NEXT:
2144c8945a0SNathan Whitehorn 		button = dlg_next_button(buttons, button);
2154c8945a0SNathan Whitehorn 		if (button < 0)
2164c8945a0SNathan Whitehorn 		    button = 0;
2174c8945a0SNathan Whitehorn 		dlg_draw_buttons(dialog,
2184c8945a0SNathan Whitehorn 				 height - 2, 0,
2194c8945a0SNathan Whitehorn 				 buttons, button,
2204c8945a0SNathan Whitehorn 				 FALSE, width);
2214c8945a0SNathan Whitehorn 		break;
2224c8945a0SNathan Whitehorn 	    case DLGK_FIELD_PREV:
2234c8945a0SNathan Whitehorn 		button = dlg_prev_button(buttons, button);
2244c8945a0SNathan Whitehorn 		if (button < 0)
2254c8945a0SNathan Whitehorn 		    button = 0;
2264c8945a0SNathan Whitehorn 		dlg_draw_buttons(dialog,
2274c8945a0SNathan Whitehorn 				 height - 2, 0,
2284c8945a0SNathan Whitehorn 				 buttons, button,
2294c8945a0SNathan Whitehorn 				 FALSE, width);
2304c8945a0SNathan Whitehorn 		break;
2314c8945a0SNathan Whitehorn 	    case DLGK_ENTER:
232682c9e0fSNathan Whitehorn 		result = dlg_enter_buttoncode(button);
2334c8945a0SNathan Whitehorn 		break;
234*a96ef450SBaptiste Daroussin 	    case DLGK_LEAVE:
235*a96ef450SBaptiste Daroussin 		result = dlg_ok_buttoncode(button);
236*a96ef450SBaptiste Daroussin 		break;
2374c8945a0SNathan Whitehorn 	    case ERR:
2384c8945a0SNathan Whitehorn 		break;
2394c8945a0SNathan Whitehorn 	    default:
2402a3e3873SBaptiste Daroussin 		if (is_DLGK_MOUSE(key)) {
2412a3e3873SBaptiste Daroussin 		    result = dlg_ok_buttoncode(key - M_EVENT);
2422a3e3873SBaptiste Daroussin 		    if (result < 0)
2432a3e3873SBaptiste Daroussin 			result = DLG_EXIT_OK;
2442a3e3873SBaptiste Daroussin 		}
2454c8945a0SNathan Whitehorn 		break;
2464c8945a0SNathan Whitehorn 	    }
2474c8945a0SNathan Whitehorn 	}
2484c8945a0SNathan Whitehorn     } while ((result == DLG_EXIT_UNKNOWN) && (seconds-- > 0));
249*a96ef450SBaptiste Daroussin     dlg_add_last_key(-1);
2504c8945a0SNathan Whitehorn 
2514c8945a0SNathan Whitehorn     curs_set(1);
2524c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
2534c8945a0SNathan Whitehorn     dlg_del_window(dialog);
2544c8945a0SNathan Whitehorn     free(prompt);
2552a3e3873SBaptiste Daroussin 
2562a3e3873SBaptiste Daroussin     dialog_vars.timeout_secs = save_timeout;
2572a3e3873SBaptiste Daroussin 
2584c8945a0SNathan Whitehorn     return ((result == DLG_EXIT_UNKNOWN) ? DLG_EXIT_OK : result);
2594c8945a0SNathan Whitehorn }
260