xref: /freebsd/contrib/dialog/msgbox.c (revision f4f33ea0c752ff0f9bfad34991d5bbb54e71133d)
14c8945a0SNathan Whitehorn /*
2*f4f33ea0SBaptiste Daroussin  *  $Id: msgbox.c,v 1.81 2018/06/21 23:29:59 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  msgbox.c -- implements the message box and info box
54c8945a0SNathan Whitehorn  *
6*f4f33ea0SBaptiste Daroussin  *  Copyright 2000-2012,2018	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,
424c8945a0SNathan Whitehorn 	SCROLLKEY_BINDINGS,
43*f4f33ea0SBaptiste Daroussin 	TRAVERSE_BINDINGS,
444c8945a0SNathan Whitehorn 	END_KEYS_BINDING
454c8945a0SNathan Whitehorn     };
464c8945a0SNathan Whitehorn     /* *INDENT-ON* */
474c8945a0SNathan Whitehorn 
484c8945a0SNathan Whitehorn     int x, y, last = 0, page;
492a3e3873SBaptiste Daroussin     int button;
504c8945a0SNathan Whitehorn     int key = 0, fkey;
514c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
524c8945a0SNathan Whitehorn     WINDOW *dialog = 0;
53*f4f33ea0SBaptiste Daroussin     char *prompt;
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*f4f33ea0SBaptiste Daroussin     bool save_nocancel = dialog_vars.nocancel;
602a3e3873SBaptiste Daroussin #ifdef KEY_RESIZE
612a3e3873SBaptiste Daroussin     int req_high;
622a3e3873SBaptiste Daroussin     int req_wide;
632a3e3873SBaptiste Daroussin #endif
642a3e3873SBaptiste Daroussin 
65*f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# msgbox args:\n"));
66*f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
67*f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("message", cprompt);
68*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
69*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
70*f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("pauseopt", pauseopt);
71*f4f33ea0SBaptiste Daroussin 
722a3e3873SBaptiste Daroussin     dialog_vars.nocancel = TRUE;
732a3e3873SBaptiste Daroussin     button = dlg_default_button();
744c8945a0SNathan Whitehorn 
754c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
762a3e3873SBaptiste Daroussin     req_high = height;
772a3e3873SBaptiste Daroussin     req_wide = width;
784c8945a0SNathan Whitehorn   restart:
794c8945a0SNathan Whitehorn #endif
804c8945a0SNathan Whitehorn 
814c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &min_width);
824c8945a0SNathan Whitehorn 
83*f4f33ea0SBaptiste Daroussin     prompt = dlg_strclone(cprompt);
844c8945a0SNathan Whitehorn     dlg_tab_correct_str(prompt);
854c8945a0SNathan Whitehorn     dlg_auto_size(title, prompt, &height, &width,
864c8945a0SNathan Whitehorn 		  (pauseopt == 1 ? 2 : 0),
874c8945a0SNathan Whitehorn 		  min_width);
884c8945a0SNathan Whitehorn     dlg_print_size(height, width);
894c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
904c8945a0SNathan Whitehorn 
914c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
924c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
934c8945a0SNathan Whitehorn 
944c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
954c8945a0SNathan Whitehorn     if (dialog != 0)
964c8945a0SNathan Whitehorn 	dlg_move_window(dialog, height, width, y, x);
974c8945a0SNathan Whitehorn     else
984c8945a0SNathan Whitehorn #endif
994c8945a0SNathan Whitehorn     {
1004c8945a0SNathan Whitehorn 	dialog = dlg_new_window(height, width, y, x);
1014c8945a0SNathan Whitehorn 	dlg_register_window(dialog, "msgbox", binding);
1024c8945a0SNathan Whitehorn 	dlg_register_buttons(dialog, "msgbox", buttons);
1034c8945a0SNathan Whitehorn     }
1044c8945a0SNathan Whitehorn     page = height - (1 + 3 * MARGIN);
1054c8945a0SNathan Whitehorn 
1064c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
1074c8945a0SNathan Whitehorn 
1082a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
1094c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
1104c8945a0SNathan Whitehorn 
111*f4f33ea0SBaptiste Daroussin     dlg_attrset(dialog, dialog_attr);
1124c8945a0SNathan Whitehorn 
1134c8945a0SNathan Whitehorn     if (pauseopt) {
1142a3e3873SBaptiste Daroussin 	dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1154c8945a0SNathan Whitehorn 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
1164c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
117682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1184c8945a0SNathan Whitehorn 
1194c8945a0SNathan Whitehorn 	while (result == DLG_EXIT_UNKNOWN) {
1204c8945a0SNathan Whitehorn 	    if (show) {
1214c8945a0SNathan Whitehorn 		last = dlg_print_scrolled(dialog, prompt, offset,
1224c8945a0SNathan Whitehorn 					  page, width, pauseopt);
1232a3e3873SBaptiste Daroussin 		dlg_trace_win(dialog);
1244c8945a0SNathan Whitehorn 		show = FALSE;
1254c8945a0SNathan Whitehorn 	    }
1264c8945a0SNathan Whitehorn 	    key = dlg_mouse_wgetch(dialog, &fkey);
1274c8945a0SNathan Whitehorn 	    if (dlg_result_key(key, fkey, &result))
1284c8945a0SNathan Whitehorn 		break;
1294c8945a0SNathan Whitehorn 
1304c8945a0SNathan Whitehorn 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
1312a3e3873SBaptiste Daroussin 		result = dlg_ok_buttoncode(check);
1324c8945a0SNathan Whitehorn 		break;
1334c8945a0SNathan Whitehorn 	    }
1344c8945a0SNathan Whitehorn 
1354c8945a0SNathan Whitehorn 	    if (fkey) {
1364c8945a0SNathan Whitehorn 		switch (key) {
1374c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1384c8945a0SNathan Whitehorn 		case KEY_RESIZE:
139*f4f33ea0SBaptiste Daroussin 		    dlg_will_resize(dialog);
1404c8945a0SNathan Whitehorn 		    dlg_clear();
141*f4f33ea0SBaptiste Daroussin 		    free(prompt);
1424c8945a0SNathan Whitehorn 		    height = req_high;
1434c8945a0SNathan Whitehorn 		    width = req_wide;
1444c8945a0SNathan Whitehorn 		    show = TRUE;
1454c8945a0SNathan Whitehorn 		    goto restart;
1464c8945a0SNathan Whitehorn #endif
1474c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
1484c8945a0SNathan Whitehorn 		    button = dlg_next_button(buttons, button);
1494c8945a0SNathan Whitehorn 		    if (button < 0)
1504c8945a0SNathan Whitehorn 			button = 0;
1514c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1524c8945a0SNathan Whitehorn 				     height - 2, 0,
1534c8945a0SNathan Whitehorn 				     buttons, button,
1544c8945a0SNathan Whitehorn 				     FALSE, width);
1554c8945a0SNathan Whitehorn 		    break;
1564c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
1574c8945a0SNathan Whitehorn 		    button = dlg_prev_button(buttons, button);
1584c8945a0SNathan Whitehorn 		    if (button < 0)
1594c8945a0SNathan Whitehorn 			button = 0;
1604c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1614c8945a0SNathan Whitehorn 				     height - 2, 0,
1624c8945a0SNathan Whitehorn 				     buttons, button,
1634c8945a0SNathan Whitehorn 				     FALSE, width);
1644c8945a0SNathan Whitehorn 		    break;
1654c8945a0SNathan Whitehorn 		case DLGK_ENTER:
1662a3e3873SBaptiste Daroussin 		    result = dlg_ok_buttoncode(button);
1674c8945a0SNathan Whitehorn 		    break;
1684c8945a0SNathan Whitehorn 		default:
1692a3e3873SBaptiste Daroussin 		    if (is_DLGK_MOUSE(key)) {
1702a3e3873SBaptiste Daroussin 			result = dlg_ok_buttoncode(key - M_EVENT);
1712a3e3873SBaptiste Daroussin 			if (result < 0)
1722a3e3873SBaptiste Daroussin 			    result = DLG_EXIT_OK;
1732a3e3873SBaptiste Daroussin 		    } else if (dlg_check_scrolled(key,
1744c8945a0SNathan Whitehorn 						  last,
1754c8945a0SNathan Whitehorn 						  page,
1764c8945a0SNathan Whitehorn 						  &show,
1772a3e3873SBaptiste Daroussin 						  &offset) == 0) {
1782a3e3873SBaptiste Daroussin 		    } else {
1794c8945a0SNathan Whitehorn 			beep();
1802a3e3873SBaptiste Daroussin 		    }
1814c8945a0SNathan Whitehorn 		    break;
1824c8945a0SNathan Whitehorn 		}
1834c8945a0SNathan Whitehorn 	    } else {
1844c8945a0SNathan Whitehorn 		beep();
1854c8945a0SNathan Whitehorn 	    }
1864c8945a0SNathan Whitehorn 	}
1874c8945a0SNathan Whitehorn     } else {
1884c8945a0SNathan Whitehorn 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
189682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1904c8945a0SNathan Whitehorn 	wrefresh(dialog);
1912a3e3873SBaptiste Daroussin 	dlg_trace_win(dialog);
1924c8945a0SNathan Whitehorn 	result = DLG_EXIT_OK;
1934c8945a0SNathan Whitehorn     }
1944c8945a0SNathan Whitehorn 
1954c8945a0SNathan Whitehorn     dlg_del_window(dialog);
1964c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
1974c8945a0SNathan Whitehorn     free(prompt);
1982a3e3873SBaptiste Daroussin 
1992a3e3873SBaptiste Daroussin     dialog_vars.nocancel = save_nocancel;
2002a3e3873SBaptiste Daroussin 
2014c8945a0SNathan Whitehorn     return result;
2024c8945a0SNathan Whitehorn }
203