xref: /freebsd/contrib/dialog/msgbox.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*
2  *  $Id: msgbox.c,v 1.64 2010/01/15 10:50:17 tom Exp $
3  *
4  *  msgbox.c -- implements the message box and info box
5  *
6  *  Copyright 2000-2009,2010	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 	ENTERKEY_BINDINGS,
41 	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
42 	SCROLLKEY_BINDINGS,
43 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_DOWN ),
44 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
45 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
46 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_UP ),
47 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
48 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
49 	END_KEYS_BINDING
50     };
51     /* *INDENT-ON* */
52 
53     int x, y, last = 0, page;
54     int button = 0;
55     int key = 0, fkey;
56     int result = DLG_EXIT_UNKNOWN;
57     WINDOW *dialog = 0;
58     char *prompt = dlg_strclone(cprompt);
59     const char **buttons = dlg_ok_label();
60     int offset = 0;
61     int check;
62     bool show = TRUE;
63     int min_width = (pauseopt == 1 ? 12 : 0);
64 
65 #ifdef KEY_RESIZE
66     int req_high = height;
67     int req_wide = width;
68   restart:
69 #endif
70 
71     dlg_button_layout(buttons, &min_width);
72 
73     dlg_tab_correct_str(prompt);
74     dlg_auto_size(title, prompt, &height, &width,
75 		  (pauseopt == 1 ? 2 : 0),
76 		  min_width);
77     dlg_print_size(height, width);
78     dlg_ctl_size(height, width);
79 
80     x = dlg_box_x_ordinate(width);
81     y = dlg_box_y_ordinate(height);
82 
83 #ifdef KEY_RESIZE
84     if (dialog != 0)
85 	dlg_move_window(dialog, height, width, y, x);
86     else
87 #endif
88     {
89 	dialog = dlg_new_window(height, width, y, x);
90 	dlg_register_window(dialog, "msgbox", binding);
91 	dlg_register_buttons(dialog, "msgbox", buttons);
92     }
93     page = height - (1 + 3 * MARGIN);
94 
95     dlg_mouse_setbase(x, y);
96 
97     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
98     dlg_draw_title(dialog, title);
99 
100     wattrset(dialog, dialog_attr);
101 
102     if (pauseopt) {
103 	dlg_draw_bottom_box(dialog);
104 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
105 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
106 
107 	while (result == DLG_EXIT_UNKNOWN) {
108 	    if (show) {
109 		last = dlg_print_scrolled(dialog, prompt, offset,
110 					  page, width, pauseopt);
111 		show = FALSE;
112 	    }
113 	    key = dlg_mouse_wgetch(dialog, &fkey);
114 	    if (dlg_result_key(key, fkey, &result))
115 		break;
116 
117 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
118 		result = check ? DLG_EXIT_HELP : DLG_EXIT_OK;
119 		break;
120 	    }
121 
122 	    if (fkey) {
123 		switch (key) {
124 #ifdef KEY_RESIZE
125 		case KEY_RESIZE:
126 		    dlg_clear();
127 		    height = req_high;
128 		    width = req_wide;
129 		    show = TRUE;
130 		    goto restart;
131 #endif
132 		case DLGK_FIELD_NEXT:
133 		    button = dlg_next_button(buttons, button);
134 		    if (button < 0)
135 			button = 0;
136 		    dlg_draw_buttons(dialog,
137 				     height - 2, 0,
138 				     buttons, button,
139 				     FALSE, width);
140 		    break;
141 		case DLGK_FIELD_PREV:
142 		    button = dlg_prev_button(buttons, button);
143 		    if (button < 0)
144 			button = 0;
145 		    dlg_draw_buttons(dialog,
146 				     height - 2, 0,
147 				     buttons, button,
148 				     FALSE, width);
149 		    break;
150 		case DLGK_ENTER:
151 		    result = button ? DLG_EXIT_HELP : DLG_EXIT_OK;
152 		    break;
153 		case DLGK_MOUSE(0):
154 		    result = DLG_EXIT_OK;
155 		    break;
156 		case DLGK_MOUSE(1):
157 		    result = DLG_EXIT_HELP;
158 		    break;
159 		default:
160 		    if (dlg_check_scrolled(key,
161 					   last,
162 					   page,
163 					   &show,
164 					   &offset) == 0)
165 			break;
166 		    beep();
167 		    break;
168 		}
169 	    } else {
170 		beep();
171 	    }
172 	}
173     } else {
174 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
175 	wrefresh(dialog);
176 	result = DLG_EXIT_OK;
177     }
178 
179     dlg_del_window(dialog);
180     dlg_mouse_free_regions();
181     free(prompt);
182     return result;
183 }
184