Merge branch 'vendor/DHCPCD'
[dragonfly.git] / contrib / dialog / yesno.c
bloba2fb1ef0ae1f7ecde86768e66152898304ce653f
1 /*
2 * $Id: yesno.c,v 1.70 2020/03/27 20:27:41 tom Exp $
4 * yesno.c -- implements the yes/no box
6 * Copyright 1999-2019,2020 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 * An earlier version of this program lists as authors
24 * Savio Lam (lam836@cs.cuhk.hk)
27 #include <dialog.h>
28 #include <dlg_keys.h>
31 * Display a dialog box with two buttons - Yes and No.
33 int
34 dialog_yesno(const char *title, const char *cprompt, int height, int width)
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
44 /* *INDENT-ON* */
46 int x, y;
47 int key, fkey;
48 int button = dlg_default_button();
49 WINDOW *dialog = 0;
50 int result = DLG_EXIT_UNKNOWN;
51 char *prompt;
52 const char **buttons = dlg_yes_labels();
53 int min_width = 25;
54 bool show = TRUE;
55 int page, last = 0, offset = 0;
57 #ifdef KEY_RESIZE
58 int req_high = height;
59 int req_wide = width;
60 #endif
62 DLG_TRACE(("# yesno args:\n"));
63 DLG_TRACE2S("title", title);
64 DLG_TRACE2S("message", cprompt);
65 DLG_TRACE2N("height", height);
66 DLG_TRACE2N("width", width);
68 #ifdef KEY_RESIZE
69 restart:
70 #endif
71 prompt = dlg_strclone(cprompt);
72 dlg_tab_correct_str(prompt);
73 dlg_button_layout(buttons, &min_width);
74 dlg_auto_size(title, prompt, &height, &width, 2, min_width);
75 dlg_print_size(height, width);
76 dlg_ctl_size(height, width);
78 x = dlg_box_x_ordinate(width);
79 y = dlg_box_y_ordinate(height);
81 dialog = dlg_new_window(height, width, y, x);
82 dlg_register_window(dialog, "yesno", binding);
83 dlg_register_buttons(dialog, "yesno", buttons);
85 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
86 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
87 dlg_draw_title(dialog, title);
88 dlg_draw_helpline(dialog, FALSE);
90 dlg_attrset(dialog, dialog_attr);
92 page = height - (1 + 3 * MARGIN);
93 dlg_draw_buttons(dialog,
94 height - 2 * MARGIN, 0,
95 buttons, button, FALSE, width);
97 while (result == DLG_EXIT_UNKNOWN) {
98 int code;
100 if (show) {
101 last = dlg_print_scrolled(dialog, prompt, offset,
102 page, width, TRUE);
103 dlg_trace_win(dialog);
104 show = FALSE;
106 key = dlg_mouse_wgetch(dialog, &fkey);
107 if (dlg_result_key(key, fkey, &result)) {
108 if (!dlg_button_key(result, &button, &key, &fkey))
109 break;
111 if ((code = dlg_char_to_button(key, buttons)) >= 0) {
112 result = dlg_ok_buttoncode(code);
113 break;
115 /* handle function keys */
116 if (fkey) {
117 switch (key) {
118 case DLGK_FIELD_NEXT:
119 button = dlg_next_button(buttons, button);
120 if (button < 0)
121 button = 0;
122 dlg_draw_buttons(dialog,
123 height - 2, 0,
124 buttons, button,
125 FALSE, width);
126 break;
127 case DLGK_FIELD_PREV:
128 button = dlg_prev_button(buttons, button);
129 if (button < 0)
130 button = 0;
131 dlg_draw_buttons(dialog,
132 height - 2, 0,
133 buttons, button,
134 FALSE, width);
135 break;
136 case DLGK_ENTER:
137 result = dlg_yes_buttoncode(button);
138 break;
139 #ifdef KEY_RESIZE
140 case KEY_RESIZE:
141 dlg_will_resize(dialog);
142 height = req_high;
143 width = req_wide;
144 show = TRUE;
145 free(prompt);
146 _dlg_resize_cleanup(dialog);
147 goto restart;
148 #endif
149 default:
150 if (is_DLGK_MOUSE(key)) {
151 result = dlg_yes_buttoncode(key - M_EVENT);
152 if (result < 0)
153 result = DLG_EXIT_OK;
154 } else if (dlg_check_scrolled(key, last, page,
155 &show, &offset) != 0) {
156 beep();
158 break;
160 } else if (key > 0) {
161 beep();
164 dlg_add_last_key(-1);
166 dlg_del_window(dialog);
167 dlg_mouse_free_regions();
168 free(prompt);
169 return result;