xref: /freebsd/contrib/dialog/yesno.c (revision c697fb7f)
1 /*
2  *  $Id: yesno.c,v 1.62 2018/06/19 22:57:01 tom Exp $
3  *
4  *  yesno.c -- implements the yes/no box
5  *
6  *  Copyright 1999-2012,2018	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  */
33 int
34 dialog_yesno(const char *title, const char *cprompt, int height, int width)
35 {
36     /* *INDENT-OFF* */
37     static DLG_KEYS_BINDING binding[] = {
38 	HELPKEY_BINDINGS,
39 	ENTERKEY_BINDINGS,
40 	SCROLLKEY_BINDINGS,
41 	TRAVERSE_BINDINGS,
42 	END_KEYS_BINDING
43     };
44     /* *INDENT-ON* */
45 
46     int x, y;
47     int key = 0, fkey;
48     int code;
49     int button = dlg_default_button();
50     WINDOW *dialog = 0;
51     int result = DLG_EXIT_UNKNOWN;
52     char *prompt;
53     const char **buttons = dlg_yes_labels();
54     int min_width = 25;
55     bool show = TRUE;
56     int page, last = 0, offset = 0;
57 
58 #ifdef KEY_RESIZE
59     int req_high = height;
60     int req_wide = width;
61 #endif
62 
63     DLG_TRACE(("# yesno args:\n"));
64     DLG_TRACE2S("title", title);
65     DLG_TRACE2S("message", cprompt);
66     DLG_TRACE2N("height", height);
67     DLG_TRACE2N("width", width);
68 
69 #ifdef KEY_RESIZE
70   restart:
71 #endif
72     prompt = dlg_strclone(cprompt);
73     dlg_tab_correct_str(prompt);
74     dlg_button_layout(buttons, &min_width);
75     dlg_auto_size(title, prompt, &height, &width, 2, min_width);
76     dlg_print_size(height, width);
77     dlg_ctl_size(height, width);
78 
79     x = dlg_box_x_ordinate(width);
80     y = dlg_box_y_ordinate(height);
81 
82 #ifdef KEY_RESIZE
83     if (dialog != 0)
84 	dlg_move_window(dialog, height, width, y, x);
85     else
86 #endif
87     {
88 	dialog = dlg_new_window(height, width, y, x);
89 	dlg_register_window(dialog, "yesno", binding);
90 	dlg_register_buttons(dialog, "yesno", buttons);
91     }
92 
93     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
94     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
95     dlg_draw_title(dialog, title);
96     dlg_draw_helpline(dialog, FALSE);
97 
98     dlg_attrset(dialog, dialog_attr);
99 
100     page = height - (1 + 3 * MARGIN);
101     dlg_draw_buttons(dialog,
102 		     height - 2 * MARGIN, 0,
103 		     buttons, button, FALSE, width);
104 
105     while (result == DLG_EXIT_UNKNOWN) {
106 	if (show) {
107 	    last = dlg_print_scrolled(dialog, prompt, offset,
108 				      page, width, TRUE);
109 	    dlg_trace_win(dialog);
110 	    show = FALSE;
111 	}
112 	key = dlg_mouse_wgetch(dialog, &fkey);
113 	if (dlg_result_key(key, fkey, &result))
114 	    break;
115 	if ((code = dlg_char_to_button(key, buttons)) >= 0) {
116 	    result = dlg_ok_buttoncode(code);
117 	    break;
118 	}
119 	/* handle function keys */
120 	if (fkey) {
121 	    switch (key) {
122 	    case DLGK_FIELD_NEXT:
123 		button = dlg_next_button(buttons, button);
124 		if (button < 0)
125 		    button = 0;
126 		dlg_draw_buttons(dialog,
127 				 height - 2, 0,
128 				 buttons, button,
129 				 FALSE, width);
130 		break;
131 	    case DLGK_FIELD_PREV:
132 		button = dlg_prev_button(buttons, button);
133 		if (button < 0)
134 		    button = 0;
135 		dlg_draw_buttons(dialog,
136 				 height - 2, 0,
137 				 buttons, button,
138 				 FALSE, width);
139 		break;
140 	    case DLGK_ENTER:
141 		result = dlg_yes_buttoncode(button);
142 		break;
143 #ifdef KEY_RESIZE
144 	    case KEY_RESIZE:
145 		dlg_will_resize(dialog);
146 		dlg_clear();
147 		free(prompt);
148 		height = req_high;
149 		width = req_wide;
150 		show = TRUE;
151 		goto restart;
152 #endif
153 	    default:
154 		if (is_DLGK_MOUSE(key)) {
155 		    result = dlg_yes_buttoncode(key - M_EVENT);
156 		    if (result < 0)
157 			result = DLG_EXIT_OK;
158 		} else if (dlg_check_scrolled(key, last, page,
159 					      &show, &offset) != 0) {
160 		    beep();
161 		}
162 		break;
163 	    }
164 	} else {
165 	    beep();
166 	}
167     }
168 
169     dlg_del_window(dialog);
170     dlg_mouse_free_regions();
171     free(prompt);
172     return result;
173 }
174