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