xref: /freebsd/contrib/dialog/msgbox.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*
2  *  $Id: msgbox.c,v 1.89 2020/11/23 00:32:02 tom Exp $
3  *
4  *  msgbox.c -- implements the message box and info box
5  *
6  *  Copyright 2000-2019,2020	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 message box. Program will pause and display an "OK" button
32  * if the parameter 'pauseopt' is non-zero.
33  */
34 int
35 dialog_msgbox(const char *title, const char *cprompt, int height, int width,
36 	      int pauseopt)
37 {
38     /* *INDENT-OFF* */
39     static DLG_KEYS_BINDING binding[] = {
40 	HELPKEY_BINDINGS,
41 	ENTERKEY_BINDINGS,
42 	SCROLLKEY_BINDINGS,
43 	TRAVERSE_BINDINGS,
44 	END_KEYS_BINDING
45     };
46     /* *INDENT-ON* */
47 
48     int x, y, page;
49     int button;
50     int key, fkey;
51     int result = DLG_EXIT_UNKNOWN;
52     WINDOW *dialog = 0;
53     char *prompt;
54     const char **buttons = dlg_ok_label();
55     int offset = 0;
56     bool show = TRUE;
57     int min_width = (pauseopt == 1 ? 12 : 0);
58     bool save_nocancel = dialog_vars.nocancel;
59 #ifdef KEY_RESIZE
60     int req_high;
61     int req_wide;
62 #endif
63 
64     DLG_TRACE(("# msgbox args:\n"));
65     DLG_TRACE2S("title", title);
66     DLG_TRACE2S("message", cprompt);
67     DLG_TRACE2N("height", height);
68     DLG_TRACE2N("width", width);
69     DLG_TRACE2N("pauseopt", pauseopt);
70 
71     dialog_vars.nocancel = TRUE;
72     button = dlg_default_button();
73 
74 #ifdef KEY_RESIZE
75     req_high = height;
76     req_wide = width;
77   restart:
78 #endif
79 
80     dlg_button_layout(buttons, &min_width);
81 
82     prompt = dlg_strclone(cprompt);
83     dlg_tab_correct_str(prompt);
84     dlg_auto_size(title, prompt, &height, &width,
85 		  (pauseopt == 1 ? 2 : 0),
86 		  min_width);
87     dlg_print_size(height, width);
88     dlg_ctl_size(height, width);
89 
90     x = dlg_box_x_ordinate(width);
91     y = dlg_box_y_ordinate(height);
92 
93     dialog = dlg_new_window(height, width, y, x);
94     dlg_register_window(dialog, "msgbox", binding);
95     dlg_register_buttons(dialog, "msgbox", buttons);
96 
97     page = height - (1 + 3 * MARGIN);
98 
99     dlg_mouse_setbase(x, y);
100 
101     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
102     dlg_draw_title(dialog, title);
103 
104     dlg_attrset(dialog, dialog_attr);
105 
106     if (pauseopt) {
107 	int last = 0;
108 
109 	dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
110 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
111 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
112 	dlg_draw_helpline(dialog, FALSE);
113 
114 	while (result == DLG_EXIT_UNKNOWN) {
115 	    int check;
116 
117 	    if (show) {
118 		last = dlg_print_scrolled(dialog, prompt, offset,
119 					  page, width, pauseopt);
120 		dlg_trace_win(dialog);
121 		show = FALSE;
122 	    }
123 	    key = dlg_mouse_wgetch(dialog, &fkey);
124 	    if (dlg_result_key(key, fkey, &result)) {
125 		if (!dlg_button_key(result, &button, &key, &fkey))
126 		    break;
127 	    }
128 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
129 		result = dlg_ok_buttoncode(check);
130 		break;
131 	    }
132 
133 	    if (fkey) {
134 		switch (key) {
135 #ifdef KEY_RESIZE
136 		case KEY_RESIZE:
137 		    dlg_will_resize(dialog);
138 		    free(prompt);
139 		    height = req_high;
140 		    width = req_wide;
141 		    show = TRUE;
142 		    _dlg_resize_cleanup(dialog);
143 		    goto restart;
144 #endif
145 		case DLGK_FIELD_NEXT:
146 		    button = dlg_next_button(buttons, button);
147 		    if (button < 0)
148 			button = 0;
149 		    dlg_draw_buttons(dialog,
150 				     height - 2, 0,
151 				     buttons, button,
152 				     FALSE, width);
153 		    break;
154 		case DLGK_FIELD_PREV:
155 		    button = dlg_prev_button(buttons, button);
156 		    if (button < 0)
157 			button = 0;
158 		    dlg_draw_buttons(dialog,
159 				     height - 2, 0,
160 				     buttons, button,
161 				     FALSE, width);
162 		    break;
163 		case DLGK_ENTER:
164 		    result = dlg_enter_buttoncode(button);
165 		    break;
166 		case DLGK_LEAVE:
167 		    result = dlg_ok_buttoncode(button);
168 		    break;
169 		default:
170 		    if (is_DLGK_MOUSE(key)) {
171 			result = dlg_ok_buttoncode(key - M_EVENT);
172 			if (result < 0)
173 			    result = DLG_EXIT_OK;
174 		    } else if (dlg_check_scrolled(key,
175 						  last,
176 						  page,
177 						  &show,
178 						  &offset) == 0) {
179 		    } else {
180 			beep();
181 		    }
182 		    break;
183 		}
184 	    } else if (key > 0) {
185 		beep();
186 	    }
187 	}
188     } else {
189 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
190 	dlg_draw_helpline(dialog, FALSE);
191 	wrefresh(dialog);
192 	dlg_trace_win(dialog);
193 	result = DLG_EXIT_OK;
194     }
195     dlg_add_last_key(-1);
196 
197     dlg_del_window(dialog);
198     dlg_mouse_free_regions();
199     free(prompt);
200 
201     dialog_vars.nocancel = save_nocancel;
202 
203     return result;
204 }
205