2 * $Id: pause.c,v 1.29 2011/06/29 09:48:53 tom Exp $
4 * pause.c -- implements the pause dialog
6 * Copyright 2004-2010,2011 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * This is adapted from source contributed by
33 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
34 #define BTN_HIGH (1 + 2 * MARGIN)
37 * This is like gauge, but can be interrupted.
39 * A pause box displays a meter along the bottom of the box. The meter
40 * indicates how many seconds remain until the end of the pause. The pause
41 * exits when timeout is reached (status OK) or the user presses:
42 * OK button (status OK)
43 * CANCEL button (status CANCEL)
44 * Esc key (status ESC)
48 dialog_pause(const char *title,
55 static DLG_KEYS_BINDING binding[] = {
58 DLG_KEYS_DATA( DLGK_ENTER, ' ' ),
59 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_DOWN ),
60 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
61 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
62 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_UP ),
63 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
64 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
70 int old_height = height;
71 int old_width = width;
78 const char **buttons = dlg_ok_labels();
79 bool have_buttons = (dlg_button_count(buttons) != 0);
81 int result = DLG_EXIT_UNKNOWN;
82 int button_high = (have_buttons ? BTN_HIGH : MARGIN);
84 char *prompt = dlg_strclone(cprompt);
88 dlg_tab_correct_str(prompt);
90 seconds_orig = (seconds > 0) ? seconds : 1;
99 dlg_auto_size(title, prompt, &height, &width,
102 dlg_button_layout(buttons, &width);
104 dlg_auto_size(title, prompt, &height, &width,
105 MIN_HIGH + MARGIN - BTN_HIGH,
108 gauge_y = height - button_high - (1 + 2 * MARGIN);
109 dlg_print_size(height, width);
110 dlg_ctl_size(height, width);
112 /* center dialog box on screen */
113 x = dlg_box_x_ordinate(width);
114 y = dlg_box_y_ordinate(height);
116 dialog = dlg_new_window(height, width, y, x);
117 dlg_register_window(dialog, "pause", binding);
118 dlg_register_buttons(dialog, "pause", buttons);
120 dlg_mouse_setbase(x, y);
121 nodelay(dialog, TRUE);
124 (void) werase(dialog);
125 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
127 dlg_draw_title(dialog, title);
128 dlg_draw_helpline(dialog, FALSE);
130 wattrset(dialog, dialog_attr);
131 dlg_print_autowrap(dialog, prompt, height, width);
135 2 + MARGIN, width - 2 * (2 + MARGIN),
140 * Clear the area for the progress bar by filling it with spaces
141 * in the title-attribute, and write the percentage with that
144 (void) wmove(dialog, gauge_y + MARGIN, 4);
145 wattrset(dialog, title_attr);
147 for (i = 0; i < (width - 2 * (3 + MARGIN)); i++)
148 (void) waddch(dialog, ' ');
150 (void) wmove(dialog, gauge_y + MARGIN, (width / 2) - 2);
151 (void) wprintw(dialog, "%3d", seconds);
154 * Now draw a bar in reverse, relative to the background.
155 * The window attribute was useful for painting the background,
156 * but requires some tweaks to reverse it.
158 x = (seconds * (width - 2 * (3 + MARGIN))) / seconds_orig;
159 if ((title_attr & A_REVERSE) != 0) {
160 wattroff(dialog, A_REVERSE);
162 wattrset(dialog, A_REVERSE);
164 (void) wmove(dialog, gauge_y + MARGIN, 4);
165 for (i = 0; i < x; i++) {
166 chtype ch = winch(dialog);
167 if (title_attr & A_REVERSE) {
170 (void) waddch(dialog, ch);
173 mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
175 dlg_draw_bottom_box(dialog);
176 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
178 (void) wrefresh(dialog);
181 (result == DLG_EXIT_UNKNOWN) && (step < 1000);
182 step += MY_TIMEOUT) {
185 key = dlg_mouse_wgetch_nowait(dialog, &fkey);
187 ; /* ignore errors in nodelay mode */
189 if (dlg_result_key(key, fkey, &result))
196 dlg_clear(); /* fill the background */
197 dlg_del_window(dialog); /* delete this window */
198 refresh(); /* get it all onto the terminal */
201 case DLGK_FIELD_NEXT:
202 button = dlg_next_button(buttons, button);
205 dlg_draw_buttons(dialog,
210 case DLGK_FIELD_PREV:
211 button = dlg_prev_button(buttons, button);
214 dlg_draw_buttons(dialog,
220 result = dlg_enter_buttoncode(button);
223 result = DLG_EXIT_OK;
226 result = DLG_EXIT_CANCEL;
234 } while ((result == DLG_EXIT_UNKNOWN) && (seconds-- > 0));
236 nodelay(dialog, FALSE);
238 dlg_mouse_free_regions();
239 dlg_del_window(dialog);
241 return ((result == DLG_EXIT_UNKNOWN) ? DLG_EXIT_OK : result);