yesno.c revision 3d4e8889889e5e36302454225999f7e146d3219c
1/*
2 *  $Id: yesno.c,v 1.49 2010/01/15 10:54:54 tom Exp $
3 *
4 *  yesno.c -- implements the yes/no box
5 *
6 *  Copyright 1999-2009,2010	Thomas E. Dickey
7 *
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.
11 *
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.
16 *
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.
22 *
23 *  An earlier version of this program lists as authors
24 *	Savio Lam (lam836@cs.cuhk.hk)
25 */
26
27#include <dialog.h>
28#include <dlg_keys.h>
29
30/*
31 * Display a dialog box with two buttons - Yes and No.
32 */
33int
34dialog_yesno(const char *title, const char *cprompt, int height, int width)
35{
36    /* *INDENT-OFF* */
37    static DLG_KEYS_BINDING binding[] = {
38	ENTERKEY_BINDINGS,
39	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
40	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_DOWN ),
41	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
42	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
43	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_UP ),
44	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
45	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
46	SCROLLKEY_BINDINGS,
47	END_KEYS_BINDING
48    };
49    /* *INDENT-ON* */
50
51    int x, y;
52    int key = 0, fkey;
53    int code;
54    int button = dlg_defaultno_button();
55    WINDOW *dialog = 0;
56    int result = DLG_EXIT_UNKNOWN;
57    char *prompt = dlg_strclone(cprompt);
58    const char **buttons = dlg_yes_labels();
59    int min_width = 25;
60    bool show = TRUE;
61    int page, last = 0, offset = 0;
62
63#ifdef KEY_RESIZE
64    int req_high = height;
65    int req_wide = width;
66  restart:
67#endif
68
69    dlg_tab_correct_str(prompt);
70    dlg_button_layout(buttons, &min_width);
71    dlg_auto_size(title, prompt, &height, &width, 2, min_width);
72    dlg_print_size(height, width);
73    dlg_ctl_size(height, width);
74
75    x = dlg_box_x_ordinate(width);
76    y = dlg_box_y_ordinate(height);
77
78#ifdef KEY_RESIZE
79    if (dialog != 0)
80	dlg_move_window(dialog, height, width, y, x);
81    else
82#endif
83    {
84	dialog = dlg_new_window(height, width, y, x);
85	dlg_register_window(dialog, "yesno", binding);
86	dlg_register_buttons(dialog, "yesno", buttons);
87    }
88
89    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
90    dlg_draw_bottom_box(dialog);
91    dlg_draw_title(dialog, title);
92
93    wattrset(dialog, dialog_attr);
94
95    page = height - (1 + 3 * MARGIN);
96    dlg_draw_buttons(dialog,
97		     height - 2 * MARGIN, 0,
98		     buttons, button, FALSE, width);
99
100    while (result == DLG_EXIT_UNKNOWN) {
101	if (show) {
102	    last = dlg_print_scrolled(dialog, prompt, offset,
103				      page, width, TRUE);
104	    show = FALSE;
105	}
106	key = dlg_mouse_wgetch(dialog, &fkey);
107	if (dlg_result_key(key, fkey, &result))
108	    break;
109	if ((code = dlg_char_to_button(key, buttons)) >= 0) {
110	    result = dlg_ok_buttoncode(code);
111	    break;
112	}
113	/* handle function keys */
114	if (fkey) {
115	    switch (key) {
116	    case DLGK_FIELD_NEXT:
117		button = dlg_next_button(buttons, button);
118		if (button < 0)
119		    button = 0;
120		dlg_draw_buttons(dialog,
121				 height - 2, 0,
122				 buttons, button,
123				 FALSE, width);
124		break;
125	    case DLGK_FIELD_PREV:
126		button = dlg_prev_button(buttons, button);
127		if (button < 0)
128		    button = 0;
129		dlg_draw_buttons(dialog,
130				 height - 2, 0,
131				 buttons, button,
132				 FALSE, width);
133		break;
134	    case DLGK_ENTER:
135		result = dlg_yes_buttoncode(button);
136		break;
137#ifdef KEY_RESIZE
138	    case KEY_RESIZE:
139		dlg_clear();
140		height = req_high;
141		width = req_wide;
142		goto restart;
143#endif
144	    default:
145		if (is_DLGK_MOUSE(key)) {
146		    result = dlg_yes_buttoncode(key - M_EVENT);
147		    if (result < 0)
148			result = DLG_EXIT_OK;
149		} else if (dlg_check_scrolled(key, last, page,
150					      &show, &offset) != 0) {
151		    beep();
152		}
153		break;
154	    }
155	} else {
156	    beep();
157	}
158    }
159
160    dlg_del_window(dialog);
161    dlg_mouse_free_regions();
162    free(prompt);
163    return result;
164}
165