xref: /freebsd/contrib/dialog/msgbox.c (revision 4c8945a06b01a5c8122cdeb402af36bb46a06acc)
1*4c8945a0SNathan Whitehorn /*
2*4c8945a0SNathan Whitehorn  *  $Id: msgbox.c,v 1.64 2010/01/15 10:50:17 tom Exp $
3*4c8945a0SNathan Whitehorn  *
4*4c8945a0SNathan Whitehorn  *  msgbox.c -- implements the message box and info box
5*4c8945a0SNathan Whitehorn  *
6*4c8945a0SNathan Whitehorn  *  Copyright 2000-2009,2010	Thomas E. Dickey
7*4c8945a0SNathan Whitehorn  *
8*4c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
9*4c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
10*4c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
11*4c8945a0SNathan Whitehorn  *
12*4c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
13*4c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14*4c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*4c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
16*4c8945a0SNathan Whitehorn  *
17*4c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
18*4c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
19*4c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
20*4c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
21*4c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
22*4c8945a0SNathan Whitehorn  *
23*4c8945a0SNathan Whitehorn  *  An earlier version of this program lists as authors:
24*4c8945a0SNathan Whitehorn  *	Savio Lam (lam836@cs.cuhk.hk)
25*4c8945a0SNathan Whitehorn  */
26*4c8945a0SNathan Whitehorn 
27*4c8945a0SNathan Whitehorn #include <dialog.h>
28*4c8945a0SNathan Whitehorn #include <dlg_keys.h>
29*4c8945a0SNathan Whitehorn 
30*4c8945a0SNathan Whitehorn /*
31*4c8945a0SNathan Whitehorn  * Display a message box. Program will pause and display an "OK" button
32*4c8945a0SNathan Whitehorn  * if the parameter 'pauseopt' is non-zero.
33*4c8945a0SNathan Whitehorn  */
34*4c8945a0SNathan Whitehorn int
35*4c8945a0SNathan Whitehorn dialog_msgbox(const char *title, const char *cprompt, int height, int width,
36*4c8945a0SNathan Whitehorn 	      int pauseopt)
37*4c8945a0SNathan Whitehorn {
38*4c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
39*4c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
40*4c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
41*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
42*4c8945a0SNathan Whitehorn 	SCROLLKEY_BINDINGS,
43*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_DOWN ),
44*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
45*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
46*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_UP ),
47*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
48*4c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
49*4c8945a0SNathan Whitehorn 	END_KEYS_BINDING
50*4c8945a0SNathan Whitehorn     };
51*4c8945a0SNathan Whitehorn     /* *INDENT-ON* */
52*4c8945a0SNathan Whitehorn 
53*4c8945a0SNathan Whitehorn     int x, y, last = 0, page;
54*4c8945a0SNathan Whitehorn     int button = 0;
55*4c8945a0SNathan Whitehorn     int key = 0, fkey;
56*4c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
57*4c8945a0SNathan Whitehorn     WINDOW *dialog = 0;
58*4c8945a0SNathan Whitehorn     char *prompt = dlg_strclone(cprompt);
59*4c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_label();
60*4c8945a0SNathan Whitehorn     int offset = 0;
61*4c8945a0SNathan Whitehorn     int check;
62*4c8945a0SNathan Whitehorn     bool show = TRUE;
63*4c8945a0SNathan Whitehorn     int min_width = (pauseopt == 1 ? 12 : 0);
64*4c8945a0SNathan Whitehorn 
65*4c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
66*4c8945a0SNathan Whitehorn     int req_high = height;
67*4c8945a0SNathan Whitehorn     int req_wide = width;
68*4c8945a0SNathan Whitehorn   restart:
69*4c8945a0SNathan Whitehorn #endif
70*4c8945a0SNathan Whitehorn 
71*4c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &min_width);
72*4c8945a0SNathan Whitehorn 
73*4c8945a0SNathan Whitehorn     dlg_tab_correct_str(prompt);
74*4c8945a0SNathan Whitehorn     dlg_auto_size(title, prompt, &height, &width,
75*4c8945a0SNathan Whitehorn 		  (pauseopt == 1 ? 2 : 0),
76*4c8945a0SNathan Whitehorn 		  min_width);
77*4c8945a0SNathan Whitehorn     dlg_print_size(height, width);
78*4c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
79*4c8945a0SNathan Whitehorn 
80*4c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
81*4c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
82*4c8945a0SNathan Whitehorn 
83*4c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
84*4c8945a0SNathan Whitehorn     if (dialog != 0)
85*4c8945a0SNathan Whitehorn 	dlg_move_window(dialog, height, width, y, x);
86*4c8945a0SNathan Whitehorn     else
87*4c8945a0SNathan Whitehorn #endif
88*4c8945a0SNathan Whitehorn     {
89*4c8945a0SNathan Whitehorn 	dialog = dlg_new_window(height, width, y, x);
90*4c8945a0SNathan Whitehorn 	dlg_register_window(dialog, "msgbox", binding);
91*4c8945a0SNathan Whitehorn 	dlg_register_buttons(dialog, "msgbox", buttons);
92*4c8945a0SNathan Whitehorn     }
93*4c8945a0SNathan Whitehorn     page = height - (1 + 3 * MARGIN);
94*4c8945a0SNathan Whitehorn 
95*4c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
96*4c8945a0SNathan Whitehorn 
97*4c8945a0SNathan Whitehorn     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
98*4c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
99*4c8945a0SNathan Whitehorn 
100*4c8945a0SNathan Whitehorn     wattrset(dialog, dialog_attr);
101*4c8945a0SNathan Whitehorn 
102*4c8945a0SNathan Whitehorn     if (pauseopt) {
103*4c8945a0SNathan Whitehorn 	dlg_draw_bottom_box(dialog);
104*4c8945a0SNathan Whitehorn 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
105*4c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
106*4c8945a0SNathan Whitehorn 
107*4c8945a0SNathan Whitehorn 	while (result == DLG_EXIT_UNKNOWN) {
108*4c8945a0SNathan Whitehorn 	    if (show) {
109*4c8945a0SNathan Whitehorn 		last = dlg_print_scrolled(dialog, prompt, offset,
110*4c8945a0SNathan Whitehorn 					  page, width, pauseopt);
111*4c8945a0SNathan Whitehorn 		show = FALSE;
112*4c8945a0SNathan Whitehorn 	    }
113*4c8945a0SNathan Whitehorn 	    key = dlg_mouse_wgetch(dialog, &fkey);
114*4c8945a0SNathan Whitehorn 	    if (dlg_result_key(key, fkey, &result))
115*4c8945a0SNathan Whitehorn 		break;
116*4c8945a0SNathan Whitehorn 
117*4c8945a0SNathan Whitehorn 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
118*4c8945a0SNathan Whitehorn 		result = check ? DLG_EXIT_HELP : DLG_EXIT_OK;
119*4c8945a0SNathan Whitehorn 		break;
120*4c8945a0SNathan Whitehorn 	    }
121*4c8945a0SNathan Whitehorn 
122*4c8945a0SNathan Whitehorn 	    if (fkey) {
123*4c8945a0SNathan Whitehorn 		switch (key) {
124*4c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
125*4c8945a0SNathan Whitehorn 		case KEY_RESIZE:
126*4c8945a0SNathan Whitehorn 		    dlg_clear();
127*4c8945a0SNathan Whitehorn 		    height = req_high;
128*4c8945a0SNathan Whitehorn 		    width = req_wide;
129*4c8945a0SNathan Whitehorn 		    show = TRUE;
130*4c8945a0SNathan Whitehorn 		    goto restart;
131*4c8945a0SNathan Whitehorn #endif
132*4c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
133*4c8945a0SNathan Whitehorn 		    button = dlg_next_button(buttons, button);
134*4c8945a0SNathan Whitehorn 		    if (button < 0)
135*4c8945a0SNathan Whitehorn 			button = 0;
136*4c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
137*4c8945a0SNathan Whitehorn 				     height - 2, 0,
138*4c8945a0SNathan Whitehorn 				     buttons, button,
139*4c8945a0SNathan Whitehorn 				     FALSE, width);
140*4c8945a0SNathan Whitehorn 		    break;
141*4c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
142*4c8945a0SNathan Whitehorn 		    button = dlg_prev_button(buttons, button);
143*4c8945a0SNathan Whitehorn 		    if (button < 0)
144*4c8945a0SNathan Whitehorn 			button = 0;
145*4c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
146*4c8945a0SNathan Whitehorn 				     height - 2, 0,
147*4c8945a0SNathan Whitehorn 				     buttons, button,
148*4c8945a0SNathan Whitehorn 				     FALSE, width);
149*4c8945a0SNathan Whitehorn 		    break;
150*4c8945a0SNathan Whitehorn 		case DLGK_ENTER:
151*4c8945a0SNathan Whitehorn 		    result = button ? DLG_EXIT_HELP : DLG_EXIT_OK;
152*4c8945a0SNathan Whitehorn 		    break;
153*4c8945a0SNathan Whitehorn 		case DLGK_MOUSE(0):
154*4c8945a0SNathan Whitehorn 		    result = DLG_EXIT_OK;
155*4c8945a0SNathan Whitehorn 		    break;
156*4c8945a0SNathan Whitehorn 		case DLGK_MOUSE(1):
157*4c8945a0SNathan Whitehorn 		    result = DLG_EXIT_HELP;
158*4c8945a0SNathan Whitehorn 		    break;
159*4c8945a0SNathan Whitehorn 		default:
160*4c8945a0SNathan Whitehorn 		    if (dlg_check_scrolled(key,
161*4c8945a0SNathan Whitehorn 					   last,
162*4c8945a0SNathan Whitehorn 					   page,
163*4c8945a0SNathan Whitehorn 					   &show,
164*4c8945a0SNathan Whitehorn 					   &offset) == 0)
165*4c8945a0SNathan Whitehorn 			break;
166*4c8945a0SNathan Whitehorn 		    beep();
167*4c8945a0SNathan Whitehorn 		    break;
168*4c8945a0SNathan Whitehorn 		}
169*4c8945a0SNathan Whitehorn 	    } else {
170*4c8945a0SNathan Whitehorn 		beep();
171*4c8945a0SNathan Whitehorn 	    }
172*4c8945a0SNathan Whitehorn 	}
173*4c8945a0SNathan Whitehorn     } else {
174*4c8945a0SNathan Whitehorn 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
175*4c8945a0SNathan Whitehorn 	wrefresh(dialog);
176*4c8945a0SNathan Whitehorn 	result = DLG_EXIT_OK;
177*4c8945a0SNathan Whitehorn     }
178*4c8945a0SNathan Whitehorn 
179*4c8945a0SNathan Whitehorn     dlg_del_window(dialog);
180*4c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
181*4c8945a0SNathan Whitehorn     free(prompt);
182*4c8945a0SNathan Whitehorn     return result;
183*4c8945a0SNathan Whitehorn }
184