xref: /freebsd/contrib/dialog/msgbox.c (revision 2a3e3873a1e4cd958f2b0f85d3b10cfa40575d30)
14c8945a0SNathan Whitehorn /*
2*2a3e3873SBaptiste Daroussin  *  $Id: msgbox.c,v 1.75 2012/12/01 01:48:08 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  msgbox.c -- implements the message box and info box
54c8945a0SNathan Whitehorn  *
6*2a3e3873SBaptiste Daroussin  *  Copyright 2000-2011,2012	Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  *
234c8945a0SNathan Whitehorn  *  An earlier version of this program lists as authors:
244c8945a0SNathan Whitehorn  *	Savio Lam (lam836@cs.cuhk.hk)
254c8945a0SNathan Whitehorn  */
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <dialog.h>
284c8945a0SNathan Whitehorn #include <dlg_keys.h>
294c8945a0SNathan Whitehorn 
304c8945a0SNathan Whitehorn /*
314c8945a0SNathan Whitehorn  * Display a message box. Program will pause and display an "OK" button
324c8945a0SNathan Whitehorn  * if the parameter 'pauseopt' is non-zero.
334c8945a0SNathan Whitehorn  */
344c8945a0SNathan Whitehorn int
354c8945a0SNathan Whitehorn dialog_msgbox(const char *title, const char *cprompt, int height, int width,
364c8945a0SNathan Whitehorn 	      int pauseopt)
374c8945a0SNathan Whitehorn {
384c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
394c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
40682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
414c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
42*2a3e3873SBaptiste Daroussin 	TRAVERSE_BINDINGS,
434c8945a0SNathan Whitehorn 	SCROLLKEY_BINDINGS,
444c8945a0SNathan Whitehorn 	END_KEYS_BINDING
454c8945a0SNathan Whitehorn     };
464c8945a0SNathan Whitehorn     /* *INDENT-ON* */
474c8945a0SNathan Whitehorn 
484c8945a0SNathan Whitehorn     int x, y, last = 0, page;
49*2a3e3873SBaptiste Daroussin     int button;
504c8945a0SNathan Whitehorn     int key = 0, fkey;
514c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
524c8945a0SNathan Whitehorn     WINDOW *dialog = 0;
534c8945a0SNathan Whitehorn     char *prompt = dlg_strclone(cprompt);
544c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_label();
554c8945a0SNathan Whitehorn     int offset = 0;
564c8945a0SNathan Whitehorn     int check;
574c8945a0SNathan Whitehorn     bool show = TRUE;
584c8945a0SNathan Whitehorn     int min_width = (pauseopt == 1 ? 12 : 0);
59*2a3e3873SBaptiste Daroussin     int save_nocancel = dialog_vars.nocancel;
60*2a3e3873SBaptiste Daroussin #ifdef KEY_RESIZE
61*2a3e3873SBaptiste Daroussin     int req_high;
62*2a3e3873SBaptiste Daroussin     int req_wide;
63*2a3e3873SBaptiste Daroussin #endif
64*2a3e3873SBaptiste Daroussin 
65*2a3e3873SBaptiste Daroussin     dialog_vars.nocancel = TRUE;
66*2a3e3873SBaptiste Daroussin     button = dlg_default_button();
674c8945a0SNathan Whitehorn 
684c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
69*2a3e3873SBaptiste Daroussin     req_high = height;
70*2a3e3873SBaptiste Daroussin     req_wide = width;
714c8945a0SNathan Whitehorn   restart:
724c8945a0SNathan Whitehorn #endif
734c8945a0SNathan Whitehorn 
744c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &min_width);
754c8945a0SNathan Whitehorn 
764c8945a0SNathan Whitehorn     dlg_tab_correct_str(prompt);
774c8945a0SNathan Whitehorn     dlg_auto_size(title, prompt, &height, &width,
784c8945a0SNathan Whitehorn 		  (pauseopt == 1 ? 2 : 0),
794c8945a0SNathan Whitehorn 		  min_width);
804c8945a0SNathan Whitehorn     dlg_print_size(height, width);
814c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
824c8945a0SNathan Whitehorn 
834c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
844c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
854c8945a0SNathan Whitehorn 
864c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
874c8945a0SNathan Whitehorn     if (dialog != 0)
884c8945a0SNathan Whitehorn 	dlg_move_window(dialog, height, width, y, x);
894c8945a0SNathan Whitehorn     else
904c8945a0SNathan Whitehorn #endif
914c8945a0SNathan Whitehorn     {
924c8945a0SNathan Whitehorn 	dialog = dlg_new_window(height, width, y, x);
934c8945a0SNathan Whitehorn 	dlg_register_window(dialog, "msgbox", binding);
944c8945a0SNathan Whitehorn 	dlg_register_buttons(dialog, "msgbox", buttons);
954c8945a0SNathan Whitehorn     }
964c8945a0SNathan Whitehorn     page = height - (1 + 3 * MARGIN);
974c8945a0SNathan Whitehorn 
984c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
994c8945a0SNathan Whitehorn 
100*2a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
1014c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
1024c8945a0SNathan Whitehorn 
103*2a3e3873SBaptiste Daroussin     (void) wattrset(dialog, dialog_attr);
1044c8945a0SNathan Whitehorn 
1054c8945a0SNathan Whitehorn     if (pauseopt) {
106*2a3e3873SBaptiste Daroussin 	dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1074c8945a0SNathan Whitehorn 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
1084c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
109682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1104c8945a0SNathan Whitehorn 
1114c8945a0SNathan Whitehorn 	while (result == DLG_EXIT_UNKNOWN) {
1124c8945a0SNathan Whitehorn 	    if (show) {
1134c8945a0SNathan Whitehorn 		last = dlg_print_scrolled(dialog, prompt, offset,
1144c8945a0SNathan Whitehorn 					  page, width, pauseopt);
115*2a3e3873SBaptiste Daroussin 		dlg_trace_win(dialog);
1164c8945a0SNathan Whitehorn 		show = FALSE;
1174c8945a0SNathan Whitehorn 	    }
1184c8945a0SNathan Whitehorn 	    key = dlg_mouse_wgetch(dialog, &fkey);
1194c8945a0SNathan Whitehorn 	    if (dlg_result_key(key, fkey, &result))
1204c8945a0SNathan Whitehorn 		break;
1214c8945a0SNathan Whitehorn 
1224c8945a0SNathan Whitehorn 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
123*2a3e3873SBaptiste Daroussin 		result = dlg_ok_buttoncode(check);
1244c8945a0SNathan Whitehorn 		break;
1254c8945a0SNathan Whitehorn 	    }
1264c8945a0SNathan Whitehorn 
1274c8945a0SNathan Whitehorn 	    if (fkey) {
1284c8945a0SNathan Whitehorn 		switch (key) {
1294c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1304c8945a0SNathan Whitehorn 		case KEY_RESIZE:
1314c8945a0SNathan Whitehorn 		    dlg_clear();
1324c8945a0SNathan Whitehorn 		    height = req_high;
1334c8945a0SNathan Whitehorn 		    width = req_wide;
1344c8945a0SNathan Whitehorn 		    show = TRUE;
1354c8945a0SNathan Whitehorn 		    goto restart;
1364c8945a0SNathan Whitehorn #endif
1374c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
1384c8945a0SNathan Whitehorn 		    button = dlg_next_button(buttons, button);
1394c8945a0SNathan Whitehorn 		    if (button < 0)
1404c8945a0SNathan Whitehorn 			button = 0;
1414c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1424c8945a0SNathan Whitehorn 				     height - 2, 0,
1434c8945a0SNathan Whitehorn 				     buttons, button,
1444c8945a0SNathan Whitehorn 				     FALSE, width);
1454c8945a0SNathan Whitehorn 		    break;
1464c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
1474c8945a0SNathan Whitehorn 		    button = dlg_prev_button(buttons, button);
1484c8945a0SNathan Whitehorn 		    if (button < 0)
1494c8945a0SNathan Whitehorn 			button = 0;
1504c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1514c8945a0SNathan Whitehorn 				     height - 2, 0,
1524c8945a0SNathan Whitehorn 				     buttons, button,
1534c8945a0SNathan Whitehorn 				     FALSE, width);
1544c8945a0SNathan Whitehorn 		    break;
1554c8945a0SNathan Whitehorn 		case DLGK_ENTER:
156*2a3e3873SBaptiste Daroussin 		    result = dlg_ok_buttoncode(button);
1574c8945a0SNathan Whitehorn 		    break;
1584c8945a0SNathan Whitehorn 		default:
159*2a3e3873SBaptiste Daroussin 		    if (is_DLGK_MOUSE(key)) {
160*2a3e3873SBaptiste Daroussin 			result = dlg_ok_buttoncode(key - M_EVENT);
161*2a3e3873SBaptiste Daroussin 			if (result < 0)
162*2a3e3873SBaptiste Daroussin 			    result = DLG_EXIT_OK;
163*2a3e3873SBaptiste Daroussin 		    } else if (dlg_check_scrolled(key,
1644c8945a0SNathan Whitehorn 						  last,
1654c8945a0SNathan Whitehorn 						  page,
1664c8945a0SNathan Whitehorn 						  &show,
167*2a3e3873SBaptiste Daroussin 						  &offset) == 0) {
168*2a3e3873SBaptiste Daroussin 		    } else {
1694c8945a0SNathan Whitehorn 			beep();
170*2a3e3873SBaptiste Daroussin 		    }
1714c8945a0SNathan Whitehorn 		    break;
1724c8945a0SNathan Whitehorn 		}
1734c8945a0SNathan Whitehorn 	    } else {
1744c8945a0SNathan Whitehorn 		beep();
1754c8945a0SNathan Whitehorn 	    }
1764c8945a0SNathan Whitehorn 	}
1774c8945a0SNathan Whitehorn     } else {
1784c8945a0SNathan Whitehorn 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
179682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1804c8945a0SNathan Whitehorn 	wrefresh(dialog);
181*2a3e3873SBaptiste Daroussin 	dlg_trace_win(dialog);
1824c8945a0SNathan Whitehorn 	result = DLG_EXIT_OK;
1834c8945a0SNathan Whitehorn     }
1844c8945a0SNathan Whitehorn 
1854c8945a0SNathan Whitehorn     dlg_del_window(dialog);
1864c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
1874c8945a0SNathan Whitehorn     free(prompt);
188*2a3e3873SBaptiste Daroussin 
189*2a3e3873SBaptiste Daroussin     dialog_vars.nocancel = save_nocancel;
190*2a3e3873SBaptiste Daroussin 
1914c8945a0SNathan Whitehorn     return result;
1924c8945a0SNathan Whitehorn }
193