xref: /freebsd/contrib/dialog/msgbox.c (revision 682c9e0fed0115eb6f283e755901c0aac90e86e8)
14c8945a0SNathan Whitehorn /*
2*682c9e0fSNathan Whitehorn  *  $Id: msgbox.c,v 1.66 2011/06/27 08:36:28 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  msgbox.c -- implements the message box and info box
54c8945a0SNathan Whitehorn  *
6*682c9e0fSNathan Whitehorn  *  Copyright 2000-2010,2011	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[] = {
40*682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
414c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
424c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
434c8945a0SNathan Whitehorn 	SCROLLKEY_BINDINGS,
444c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_DOWN ),
454c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
464c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
474c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_UP ),
484c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
494c8945a0SNathan Whitehorn 	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
504c8945a0SNathan Whitehorn 	END_KEYS_BINDING
514c8945a0SNathan Whitehorn     };
524c8945a0SNathan Whitehorn     /* *INDENT-ON* */
534c8945a0SNathan Whitehorn 
544c8945a0SNathan Whitehorn     int x, y, last = 0, page;
554c8945a0SNathan Whitehorn     int button = 0;
564c8945a0SNathan Whitehorn     int key = 0, fkey;
574c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
584c8945a0SNathan Whitehorn     WINDOW *dialog = 0;
594c8945a0SNathan Whitehorn     char *prompt = dlg_strclone(cprompt);
604c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_label();
614c8945a0SNathan Whitehorn     int offset = 0;
624c8945a0SNathan Whitehorn     int check;
634c8945a0SNathan Whitehorn     bool show = TRUE;
644c8945a0SNathan Whitehorn     int min_width = (pauseopt == 1 ? 12 : 0);
654c8945a0SNathan Whitehorn 
664c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
674c8945a0SNathan Whitehorn     int req_high = height;
684c8945a0SNathan Whitehorn     int req_wide = width;
694c8945a0SNathan Whitehorn   restart:
704c8945a0SNathan Whitehorn #endif
714c8945a0SNathan Whitehorn 
724c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &min_width);
734c8945a0SNathan Whitehorn 
744c8945a0SNathan Whitehorn     dlg_tab_correct_str(prompt);
754c8945a0SNathan Whitehorn     dlg_auto_size(title, prompt, &height, &width,
764c8945a0SNathan Whitehorn 		  (pauseopt == 1 ? 2 : 0),
774c8945a0SNathan Whitehorn 		  min_width);
784c8945a0SNathan Whitehorn     dlg_print_size(height, width);
794c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
804c8945a0SNathan Whitehorn 
814c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
824c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
834c8945a0SNathan Whitehorn 
844c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
854c8945a0SNathan Whitehorn     if (dialog != 0)
864c8945a0SNathan Whitehorn 	dlg_move_window(dialog, height, width, y, x);
874c8945a0SNathan Whitehorn     else
884c8945a0SNathan Whitehorn #endif
894c8945a0SNathan Whitehorn     {
904c8945a0SNathan Whitehorn 	dialog = dlg_new_window(height, width, y, x);
914c8945a0SNathan Whitehorn 	dlg_register_window(dialog, "msgbox", binding);
924c8945a0SNathan Whitehorn 	dlg_register_buttons(dialog, "msgbox", buttons);
934c8945a0SNathan Whitehorn     }
944c8945a0SNathan Whitehorn     page = height - (1 + 3 * MARGIN);
954c8945a0SNathan Whitehorn 
964c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
974c8945a0SNathan Whitehorn 
984c8945a0SNathan Whitehorn     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
994c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
1004c8945a0SNathan Whitehorn 
1014c8945a0SNathan Whitehorn     wattrset(dialog, dialog_attr);
1024c8945a0SNathan Whitehorn 
1034c8945a0SNathan Whitehorn     if (pauseopt) {
1044c8945a0SNathan Whitehorn 	dlg_draw_bottom_box(dialog);
1054c8945a0SNathan Whitehorn 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
1064c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
107*682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1084c8945a0SNathan Whitehorn 
1094c8945a0SNathan Whitehorn 	while (result == DLG_EXIT_UNKNOWN) {
1104c8945a0SNathan Whitehorn 	    if (show) {
1114c8945a0SNathan Whitehorn 		last = dlg_print_scrolled(dialog, prompt, offset,
1124c8945a0SNathan Whitehorn 					  page, width, pauseopt);
1134c8945a0SNathan Whitehorn 		show = FALSE;
1144c8945a0SNathan Whitehorn 	    }
1154c8945a0SNathan Whitehorn 	    key = dlg_mouse_wgetch(dialog, &fkey);
1164c8945a0SNathan Whitehorn 	    if (dlg_result_key(key, fkey, &result))
1174c8945a0SNathan Whitehorn 		break;
1184c8945a0SNathan Whitehorn 
1194c8945a0SNathan Whitehorn 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
1204c8945a0SNathan Whitehorn 		result = check ? DLG_EXIT_HELP : DLG_EXIT_OK;
1214c8945a0SNathan Whitehorn 		break;
1224c8945a0SNathan Whitehorn 	    }
1234c8945a0SNathan Whitehorn 
1244c8945a0SNathan Whitehorn 	    if (fkey) {
1254c8945a0SNathan Whitehorn 		switch (key) {
1264c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1274c8945a0SNathan Whitehorn 		case KEY_RESIZE:
1284c8945a0SNathan Whitehorn 		    dlg_clear();
1294c8945a0SNathan Whitehorn 		    height = req_high;
1304c8945a0SNathan Whitehorn 		    width = req_wide;
1314c8945a0SNathan Whitehorn 		    show = TRUE;
1324c8945a0SNathan Whitehorn 		    goto restart;
1334c8945a0SNathan Whitehorn #endif
1344c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
1354c8945a0SNathan Whitehorn 		    button = dlg_next_button(buttons, button);
1364c8945a0SNathan Whitehorn 		    if (button < 0)
1374c8945a0SNathan Whitehorn 			button = 0;
1384c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1394c8945a0SNathan Whitehorn 				     height - 2, 0,
1404c8945a0SNathan Whitehorn 				     buttons, button,
1414c8945a0SNathan Whitehorn 				     FALSE, width);
1424c8945a0SNathan Whitehorn 		    break;
1434c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
1444c8945a0SNathan Whitehorn 		    button = dlg_prev_button(buttons, button);
1454c8945a0SNathan Whitehorn 		    if (button < 0)
1464c8945a0SNathan Whitehorn 			button = 0;
1474c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1484c8945a0SNathan Whitehorn 				     height - 2, 0,
1494c8945a0SNathan Whitehorn 				     buttons, button,
1504c8945a0SNathan Whitehorn 				     FALSE, width);
1514c8945a0SNathan Whitehorn 		    break;
1524c8945a0SNathan Whitehorn 		case DLGK_ENTER:
1534c8945a0SNathan Whitehorn 		    result = button ? DLG_EXIT_HELP : DLG_EXIT_OK;
1544c8945a0SNathan Whitehorn 		    break;
1554c8945a0SNathan Whitehorn 		case DLGK_MOUSE(0):
1564c8945a0SNathan Whitehorn 		    result = DLG_EXIT_OK;
1574c8945a0SNathan Whitehorn 		    break;
1584c8945a0SNathan Whitehorn 		case DLGK_MOUSE(1):
1594c8945a0SNathan Whitehorn 		    result = DLG_EXIT_HELP;
1604c8945a0SNathan Whitehorn 		    break;
1614c8945a0SNathan Whitehorn 		default:
1624c8945a0SNathan Whitehorn 		    if (dlg_check_scrolled(key,
1634c8945a0SNathan Whitehorn 					   last,
1644c8945a0SNathan Whitehorn 					   page,
1654c8945a0SNathan Whitehorn 					   &show,
1664c8945a0SNathan Whitehorn 					   &offset) == 0)
1674c8945a0SNathan Whitehorn 			break;
1684c8945a0SNathan Whitehorn 		    beep();
1694c8945a0SNathan Whitehorn 		    break;
1704c8945a0SNathan Whitehorn 		}
1714c8945a0SNathan Whitehorn 	    } else {
1724c8945a0SNathan Whitehorn 		beep();
1734c8945a0SNathan Whitehorn 	    }
1744c8945a0SNathan Whitehorn 	}
1754c8945a0SNathan Whitehorn     } else {
1764c8945a0SNathan Whitehorn 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
177*682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1784c8945a0SNathan Whitehorn 	wrefresh(dialog);
1794c8945a0SNathan Whitehorn 	result = DLG_EXIT_OK;
1804c8945a0SNathan Whitehorn     }
1814c8945a0SNathan Whitehorn 
1824c8945a0SNathan Whitehorn     dlg_del_window(dialog);
1834c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
1844c8945a0SNathan Whitehorn     free(prompt);
1854c8945a0SNathan Whitehorn     return result;
1864c8945a0SNathan Whitehorn }
187