xref: /freebsd/contrib/dialog/msgbox.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin  *  $Id: msgbox.c,v 1.89 2020/11/23 00:32:02 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  msgbox.c -- implements the message box and info box
54c8945a0SNathan Whitehorn  *
6*a96ef450SBaptiste Daroussin  *  Copyright 2000-2019,2020	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
dialog_msgbox(const char * title,const char * cprompt,int height,int width,int pauseopt)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,
43f4f33ea0SBaptiste Daroussin 	TRAVERSE_BINDINGS,
444c8945a0SNathan Whitehorn 	END_KEYS_BINDING
454c8945a0SNathan Whitehorn     };
464c8945a0SNathan Whitehorn     /* *INDENT-ON* */
474c8945a0SNathan Whitehorn 
48*a96ef450SBaptiste Daroussin     int x, y, page;
492a3e3873SBaptiste Daroussin     int button;
50*a96ef450SBaptiste Daroussin     int key, fkey;
514c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
524c8945a0SNathan Whitehorn     WINDOW *dialog = 0;
53f4f33ea0SBaptiste Daroussin     char *prompt;
544c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_label();
554c8945a0SNathan Whitehorn     int offset = 0;
564c8945a0SNathan Whitehorn     bool show = TRUE;
574c8945a0SNathan Whitehorn     int min_width = (pauseopt == 1 ? 12 : 0);
58f4f33ea0SBaptiste Daroussin     bool save_nocancel = dialog_vars.nocancel;
592a3e3873SBaptiste Daroussin #ifdef KEY_RESIZE
602a3e3873SBaptiste Daroussin     int req_high;
612a3e3873SBaptiste Daroussin     int req_wide;
622a3e3873SBaptiste Daroussin #endif
632a3e3873SBaptiste Daroussin 
64f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# msgbox args:\n"));
65f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("title", title);
66f4f33ea0SBaptiste Daroussin     DLG_TRACE2S("message", cprompt);
67f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("height", height);
68f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("width", width);
69f4f33ea0SBaptiste Daroussin     DLG_TRACE2N("pauseopt", pauseopt);
70f4f33ea0SBaptiste Daroussin 
712a3e3873SBaptiste Daroussin     dialog_vars.nocancel = TRUE;
722a3e3873SBaptiste Daroussin     button = dlg_default_button();
734c8945a0SNathan Whitehorn 
744c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
752a3e3873SBaptiste Daroussin     req_high = height;
762a3e3873SBaptiste Daroussin     req_wide = width;
774c8945a0SNathan Whitehorn   restart:
784c8945a0SNathan Whitehorn #endif
794c8945a0SNathan Whitehorn 
804c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &min_width);
814c8945a0SNathan Whitehorn 
82f4f33ea0SBaptiste Daroussin     prompt = dlg_strclone(cprompt);
834c8945a0SNathan Whitehorn     dlg_tab_correct_str(prompt);
844c8945a0SNathan Whitehorn     dlg_auto_size(title, prompt, &height, &width,
854c8945a0SNathan Whitehorn 		  (pauseopt == 1 ? 2 : 0),
864c8945a0SNathan Whitehorn 		  min_width);
874c8945a0SNathan Whitehorn     dlg_print_size(height, width);
884c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
894c8945a0SNathan Whitehorn 
904c8945a0SNathan Whitehorn     x = dlg_box_x_ordinate(width);
914c8945a0SNathan Whitehorn     y = dlg_box_y_ordinate(height);
924c8945a0SNathan Whitehorn 
934c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width, y, x);
944c8945a0SNathan Whitehorn     dlg_register_window(dialog, "msgbox", binding);
954c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "msgbox", buttons);
96*a96ef450SBaptiste Daroussin 
974c8945a0SNathan Whitehorn     page = height - (1 + 3 * MARGIN);
984c8945a0SNathan Whitehorn 
994c8945a0SNathan Whitehorn     dlg_mouse_setbase(x, y);
1004c8945a0SNathan Whitehorn 
1012a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
1024c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
1034c8945a0SNathan Whitehorn 
104f4f33ea0SBaptiste Daroussin     dlg_attrset(dialog, dialog_attr);
1054c8945a0SNathan Whitehorn 
1064c8945a0SNathan Whitehorn     if (pauseopt) {
107*a96ef450SBaptiste Daroussin 	int last = 0;
108*a96ef450SBaptiste Daroussin 
1092a3e3873SBaptiste Daroussin 	dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1104c8945a0SNathan Whitehorn 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
1114c8945a0SNathan Whitehorn 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
112682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1134c8945a0SNathan Whitehorn 
1144c8945a0SNathan Whitehorn 	while (result == DLG_EXIT_UNKNOWN) {
115*a96ef450SBaptiste Daroussin 	    int check;
116*a96ef450SBaptiste Daroussin 
1174c8945a0SNathan Whitehorn 	    if (show) {
1184c8945a0SNathan Whitehorn 		last = dlg_print_scrolled(dialog, prompt, offset,
1194c8945a0SNathan Whitehorn 					  page, width, pauseopt);
1202a3e3873SBaptiste Daroussin 		dlg_trace_win(dialog);
1214c8945a0SNathan Whitehorn 		show = FALSE;
1224c8945a0SNathan Whitehorn 	    }
1234c8945a0SNathan Whitehorn 	    key = dlg_mouse_wgetch(dialog, &fkey);
124*a96ef450SBaptiste Daroussin 	    if (dlg_result_key(key, fkey, &result)) {
125*a96ef450SBaptiste Daroussin 		if (!dlg_button_key(result, &button, &key, &fkey))
1264c8945a0SNathan Whitehorn 		    break;
127*a96ef450SBaptiste Daroussin 	    }
1284c8945a0SNathan Whitehorn 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
1292a3e3873SBaptiste Daroussin 		result = dlg_ok_buttoncode(check);
1304c8945a0SNathan Whitehorn 		break;
1314c8945a0SNathan Whitehorn 	    }
1324c8945a0SNathan Whitehorn 
1334c8945a0SNathan Whitehorn 	    if (fkey) {
1344c8945a0SNathan Whitehorn 		switch (key) {
1354c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1364c8945a0SNathan Whitehorn 		case KEY_RESIZE:
137f4f33ea0SBaptiste Daroussin 		    dlg_will_resize(dialog);
138f4f33ea0SBaptiste Daroussin 		    free(prompt);
1394c8945a0SNathan Whitehorn 		    height = req_high;
1404c8945a0SNathan Whitehorn 		    width = req_wide;
1414c8945a0SNathan Whitehorn 		    show = TRUE;
142*a96ef450SBaptiste Daroussin 		    _dlg_resize_cleanup(dialog);
1434c8945a0SNathan Whitehorn 		    goto restart;
1444c8945a0SNathan Whitehorn #endif
1454c8945a0SNathan Whitehorn 		case DLGK_FIELD_NEXT:
1464c8945a0SNathan Whitehorn 		    button = dlg_next_button(buttons, button);
1474c8945a0SNathan Whitehorn 		    if (button < 0)
1484c8945a0SNathan Whitehorn 			button = 0;
1494c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1504c8945a0SNathan Whitehorn 				     height - 2, 0,
1514c8945a0SNathan Whitehorn 				     buttons, button,
1524c8945a0SNathan Whitehorn 				     FALSE, width);
1534c8945a0SNathan Whitehorn 		    break;
1544c8945a0SNathan Whitehorn 		case DLGK_FIELD_PREV:
1554c8945a0SNathan Whitehorn 		    button = dlg_prev_button(buttons, button);
1564c8945a0SNathan Whitehorn 		    if (button < 0)
1574c8945a0SNathan Whitehorn 			button = 0;
1584c8945a0SNathan Whitehorn 		    dlg_draw_buttons(dialog,
1594c8945a0SNathan Whitehorn 				     height - 2, 0,
1604c8945a0SNathan Whitehorn 				     buttons, button,
1614c8945a0SNathan Whitehorn 				     FALSE, width);
1624c8945a0SNathan Whitehorn 		    break;
1634c8945a0SNathan Whitehorn 		case DLGK_ENTER:
164*a96ef450SBaptiste Daroussin 		    result = dlg_enter_buttoncode(button);
165*a96ef450SBaptiste Daroussin 		    break;
166*a96ef450SBaptiste Daroussin 		case DLGK_LEAVE:
1672a3e3873SBaptiste Daroussin 		    result = dlg_ok_buttoncode(button);
1684c8945a0SNathan Whitehorn 		    break;
1694c8945a0SNathan Whitehorn 		default:
1702a3e3873SBaptiste Daroussin 		    if (is_DLGK_MOUSE(key)) {
1712a3e3873SBaptiste Daroussin 			result = dlg_ok_buttoncode(key - M_EVENT);
1722a3e3873SBaptiste Daroussin 			if (result < 0)
1732a3e3873SBaptiste Daroussin 			    result = DLG_EXIT_OK;
1742a3e3873SBaptiste Daroussin 		    } else if (dlg_check_scrolled(key,
1754c8945a0SNathan Whitehorn 						  last,
1764c8945a0SNathan Whitehorn 						  page,
1774c8945a0SNathan Whitehorn 						  &show,
1782a3e3873SBaptiste Daroussin 						  &offset) == 0) {
1792a3e3873SBaptiste Daroussin 		    } else {
1804c8945a0SNathan Whitehorn 			beep();
1812a3e3873SBaptiste Daroussin 		    }
1824c8945a0SNathan Whitehorn 		    break;
1834c8945a0SNathan Whitehorn 		}
184*a96ef450SBaptiste Daroussin 	    } else if (key > 0) {
1854c8945a0SNathan Whitehorn 		beep();
1864c8945a0SNathan Whitehorn 	    }
1874c8945a0SNathan Whitehorn 	}
1884c8945a0SNathan Whitehorn     } else {
1894c8945a0SNathan Whitehorn 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
190682c9e0fSNathan Whitehorn 	dlg_draw_helpline(dialog, FALSE);
1914c8945a0SNathan Whitehorn 	wrefresh(dialog);
1922a3e3873SBaptiste Daroussin 	dlg_trace_win(dialog);
1934c8945a0SNathan Whitehorn 	result = DLG_EXIT_OK;
1944c8945a0SNathan Whitehorn     }
195*a96ef450SBaptiste Daroussin     dlg_add_last_key(-1);
1964c8945a0SNathan Whitehorn 
1974c8945a0SNathan Whitehorn     dlg_del_window(dialog);
1984c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
1994c8945a0SNathan Whitehorn     free(prompt);
2002a3e3873SBaptiste Daroussin 
2012a3e3873SBaptiste Daroussin     dialog_vars.nocancel = save_nocancel;
2022a3e3873SBaptiste Daroussin 
2034c8945a0SNathan Whitehorn     return result;
2044c8945a0SNathan Whitehorn }
205