freebsd-dev/contrib/dialog/pause.c

247 lines
6.3 KiB
C
Raw Normal View History

2011-01-13 02:21:23 +00:00
/*
2012-10-21 18:18:09 +00:00
* $Id: pause.c,v 1.36 2012/07/03 00:01:59 tom Exp $
2011-01-13 02:21:23 +00:00
*
* pause.c -- implements the pause dialog
*
2012-10-21 18:18:09 +00:00
* Copyright 2004-2011,2012 Thomas E. Dickey
2011-01-13 02:21:23 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 2.1
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to
* Free Software Foundation, Inc.
* 51 Franklin St., Fifth Floor
* Boston, MA 02110, USA.
*
* This is adapted from source contributed by
* Yura Kalinichenko
*/
#include <dialog.h>
#include <dlg_keys.h>
#define MY_TIMEOUT 50
#define MIN_HIGH (4)
#define MIN_WIDE (10 + 2 * (2 + MARGIN))
#define BTN_HIGH (1 + 2 * MARGIN)
/*
* This is like gauge, but can be interrupted.
*
* A pause box displays a meter along the bottom of the box. The meter
* indicates how many seconds remain until the end of the pause. The pause
* exits when timeout is reached (status OK) or the user presses:
* OK button (status OK)
* CANCEL button (status CANCEL)
* Esc key (status ESC)
*
*/
int
dialog_pause(const char *title,
const char *cprompt,
int height,
int width,
int seconds)
{
/* *INDENT-OFF* */
static DLG_KEYS_BINDING binding[] = {
HELPKEY_BINDINGS,
2011-01-13 02:21:23 +00:00
ENTERKEY_BINDINGS,
2012-10-21 18:18:09 +00:00
TRAVERSE_BINDINGS,
2011-01-13 02:21:23 +00:00
END_KEYS_BINDING
};
/* *INDENT-ON* */
#ifdef KEY_RESIZE
int old_height = height;
int old_width = width;
#endif
int i, x, y, step;
2012-10-21 18:18:09 +00:00
int button = dlg_default_button();
2011-01-13 02:21:23 +00:00
int seconds_orig;
WINDOW *dialog;
const char **buttons = dlg_ok_labels();
bool have_buttons = (dlg_button_count(buttons) != 0);
2012-10-21 18:18:09 +00:00
bool first;
2011-01-13 02:21:23 +00:00
int key = 0, fkey;
int result = DLG_EXIT_UNKNOWN;
int button_high = (have_buttons ? BTN_HIGH : MARGIN);
2011-04-17 17:00:55 +00:00
int gauge_y;
2011-01-13 02:21:23 +00:00
char *prompt = dlg_strclone(cprompt);
2012-10-21 18:18:09 +00:00
int save_timeout = dialog_vars.timeout_secs;
2011-01-13 02:21:23 +00:00
curs_set(0);
dlg_tab_correct_str(prompt);
2012-10-21 18:18:09 +00:00
dialog_vars.timeout_secs = 0;
2011-01-13 02:21:23 +00:00
seconds_orig = (seconds > 0) ? seconds : 1;
#ifdef KEY_RESIZE
retry:
height = old_height;
width = old_width;
#endif
if (have_buttons) {
dlg_auto_size(title, prompt, &height, &width,
MIN_HIGH,
MIN_WIDE);
dlg_button_layout(buttons, &width);
} else {
dlg_auto_size(title, prompt, &height, &width,
MIN_HIGH + MARGIN - BTN_HIGH,
MIN_WIDE);
}
2011-04-17 17:00:55 +00:00
gauge_y = height - button_high - (1 + 2 * MARGIN);
2011-01-13 02:21:23 +00:00
dlg_print_size(height, width);
dlg_ctl_size(height, width);
/* center dialog box on screen */
x = dlg_box_x_ordinate(width);
y = dlg_box_y_ordinate(height);
dialog = dlg_new_window(height, width, y, x);
dlg_register_window(dialog, "pause", binding);
dlg_register_buttons(dialog, "pause", buttons);
dlg_mouse_setbase(x, y);
nodelay(dialog, TRUE);
2012-10-21 18:18:09 +00:00
first = TRUE;
2011-01-13 02:21:23 +00:00
do {
(void) werase(dialog);
2012-10-21 18:18:09 +00:00
dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
2011-01-13 02:21:23 +00:00
dlg_draw_title(dialog, title);
dlg_draw_helpline(dialog, FALSE);
2011-01-13 02:21:23 +00:00
2012-10-21 18:18:09 +00:00
(void) wattrset(dialog, dialog_attr);
2011-01-13 02:21:23 +00:00
dlg_print_autowrap(dialog, prompt, height, width);
2012-10-21 18:18:09 +00:00
dlg_draw_box2(dialog,
gauge_y, 2 + MARGIN,
2 + MARGIN, width - 2 * (2 + MARGIN),
dialog_attr,
border_attr,
border2_attr);
2011-01-13 02:21:23 +00:00
/*
* Clear the area for the progress bar by filling it with spaces
* in the title-attribute, and write the percentage with that
* attribute.
*/
2011-04-17 17:00:55 +00:00
(void) wmove(dialog, gauge_y + MARGIN, 4);
2012-10-21 18:18:09 +00:00
(void) wattrset(dialog, title_attr);
2011-01-13 02:21:23 +00:00
for (i = 0; i < (width - 2 * (3 + MARGIN)); i++)
(void) waddch(dialog, ' ');
2011-04-17 17:00:55 +00:00
(void) wmove(dialog, gauge_y + MARGIN, (width / 2) - 2);
2011-01-13 02:21:23 +00:00
(void) wprintw(dialog, "%3d", seconds);
/*
* Now draw a bar in reverse, relative to the background.
* The window attribute was useful for painting the background,
* but requires some tweaks to reverse it.
*/
x = (seconds * (width - 2 * (3 + MARGIN))) / seconds_orig;
if ((title_attr & A_REVERSE) != 0) {
wattroff(dialog, A_REVERSE);
} else {
2012-10-21 18:18:09 +00:00
(void) wattrset(dialog, A_REVERSE);
2011-01-13 02:21:23 +00:00
}
2011-04-17 17:00:55 +00:00
(void) wmove(dialog, gauge_y + MARGIN, 4);
2011-01-13 02:21:23 +00:00
for (i = 0; i < x; i++) {
chtype ch = winch(dialog);
if (title_attr & A_REVERSE) {
ch &= ~A_REVERSE;
}
(void) waddch(dialog, ch);
}
mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
if (have_buttons) {
2012-10-21 18:18:09 +00:00
dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
2011-01-13 02:21:23 +00:00
dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
}
2012-10-21 18:18:09 +00:00
if (first) {
(void) wrefresh(dialog);
dlg_trace_win(dialog);
first = FALSE;
}
2011-01-13 02:21:23 +00:00
for (step = 0;
(result == DLG_EXIT_UNKNOWN) && (step < 1000);
step += MY_TIMEOUT) {
napms(MY_TIMEOUT);
key = dlg_mouse_wgetch_nowait(dialog, &fkey);
if (key == ERR) {
; /* ignore errors in nodelay mode */
} else {
if (dlg_result_key(key, fkey, &result))
break;
}
switch (key) {
#ifdef KEY_RESIZE
case KEY_RESIZE:
dlg_clear(); /* fill the background */
dlg_del_window(dialog); /* delete this window */
refresh(); /* get it all onto the terminal */
goto retry;
#endif
case DLGK_FIELD_NEXT:
button = dlg_next_button(buttons, button);
if (button < 0)
button = 0;
dlg_draw_buttons(dialog,
height - 2, 0,
buttons, button,
FALSE, width);
break;
case DLGK_FIELD_PREV:
button = dlg_prev_button(buttons, button);
if (button < 0)
button = 0;
dlg_draw_buttons(dialog,
height - 2, 0,
buttons, button,
FALSE, width);
break;
case DLGK_ENTER:
result = dlg_enter_buttoncode(button);
2011-01-13 02:21:23 +00:00
break;
case ERR:
break;
default:
2012-10-21 18:18:09 +00:00
if (is_DLGK_MOUSE(key)) {
result = dlg_ok_buttoncode(key - M_EVENT);
if (result < 0)
result = DLG_EXIT_OK;
}
2011-01-13 02:21:23 +00:00
break;
}
}
} while ((result == DLG_EXIT_UNKNOWN) && (seconds-- > 0));
curs_set(1);
dlg_mouse_free_regions();
dlg_del_window(dialog);
free(prompt);
2012-10-21 18:18:09 +00:00
dialog_vars.timeout_secs = save_timeout;
2011-01-13 02:21:23 +00:00
return ((result == DLG_EXIT_UNKNOWN) ? DLG_EXIT_OK : result);
}