xref: /dragonfly/contrib/dialog/msgbox.c (revision a8e38dc0)
15382d832SPeter Avalos /*
2*a8e38dc0SAntonio Huete Jimenez  *  $Id: msgbox.c,v 1.90 2022/04/03 22:38:16 tom Exp $
35382d832SPeter Avalos  *
45382d832SPeter Avalos  *  msgbox.c -- implements the message box and info box
55382d832SPeter Avalos  *
6*a8e38dc0SAntonio Huete Jimenez  *  Copyright 2000-2020,2022	Thomas E. Dickey
75382d832SPeter Avalos  *
85382d832SPeter Avalos  *  This program is free software; you can redistribute it and/or modify
95382d832SPeter Avalos  *  it under the terms of the GNU Lesser General Public License, version 2.1
105382d832SPeter Avalos  *  as published by the Free Software Foundation.
115382d832SPeter Avalos  *
125382d832SPeter Avalos  *  This program is distributed in the hope that it will be useful, but
135382d832SPeter Avalos  *  WITHOUT ANY WARRANTY; without even the implied warranty of
145382d832SPeter Avalos  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
155382d832SPeter Avalos  *  Lesser General Public License for more details.
165382d832SPeter Avalos  *
175382d832SPeter Avalos  *  You should have received a copy of the GNU Lesser General Public
185382d832SPeter Avalos  *  License along with this program; if not, write to
195382d832SPeter Avalos  *	Free Software Foundation, Inc.
205382d832SPeter Avalos  *	51 Franklin St., Fifth Floor
215382d832SPeter Avalos  *	Boston, MA 02110, USA.
225382d832SPeter Avalos  *
235382d832SPeter Avalos  *  An earlier version of this program lists as authors:
245382d832SPeter Avalos  *	Savio Lam (lam836@cs.cuhk.hk)
255382d832SPeter Avalos  */
265382d832SPeter Avalos 
27*a8e38dc0SAntonio Huete Jimenez #include <dlg_internals.h>
285382d832SPeter Avalos #include <dlg_keys.h>
295382d832SPeter Avalos 
305382d832SPeter Avalos /*
315382d832SPeter Avalos  * Display a message box. Program will pause and display an "OK" button
325382d832SPeter Avalos  * if the parameter 'pauseopt' is non-zero.
335382d832SPeter Avalos  */
345382d832SPeter Avalos int
dialog_msgbox(const char * title,const char * cprompt,int height,int width,int pauseopt)355382d832SPeter Avalos dialog_msgbox(const char *title, const char *cprompt, int height, int width,
365382d832SPeter Avalos 	      int pauseopt)
375382d832SPeter Avalos {
385382d832SPeter Avalos     /* *INDENT-OFF* */
395382d832SPeter Avalos     static DLG_KEYS_BINDING binding[] = {
405382d832SPeter Avalos 	HELPKEY_BINDINGS,
415382d832SPeter Avalos 	ENTERKEY_BINDINGS,
425382d832SPeter Avalos 	SCROLLKEY_BINDINGS,
435940c9abSDaniel Fojt 	TRAVERSE_BINDINGS,
445382d832SPeter Avalos 	END_KEYS_BINDING
455382d832SPeter Avalos     };
465382d832SPeter Avalos     /* *INDENT-ON* */
475382d832SPeter Avalos 
485940c9abSDaniel Fojt     int x, y, page;
495382d832SPeter Avalos     int button;
505940c9abSDaniel Fojt     int key, fkey;
515382d832SPeter Avalos     int result = DLG_EXIT_UNKNOWN;
525382d832SPeter Avalos     WINDOW *dialog = 0;
535940c9abSDaniel Fojt     char *prompt;
545382d832SPeter Avalos     const char **buttons = dlg_ok_label();
555382d832SPeter Avalos     int offset = 0;
565382d832SPeter Avalos     bool show = TRUE;
575382d832SPeter Avalos     int min_width = (pauseopt == 1 ? 12 : 0);
585940c9abSDaniel Fojt     bool save_nocancel = dialog_vars.nocancel;
595382d832SPeter Avalos #ifdef KEY_RESIZE
605382d832SPeter Avalos     int req_high;
615382d832SPeter Avalos     int req_wide;
625382d832SPeter Avalos #endif
635382d832SPeter Avalos 
645940c9abSDaniel Fojt     DLG_TRACE(("# msgbox args:\n"));
655940c9abSDaniel Fojt     DLG_TRACE2S("title", title);
665940c9abSDaniel Fojt     DLG_TRACE2S("message", cprompt);
675940c9abSDaniel Fojt     DLG_TRACE2N("height", height);
685940c9abSDaniel Fojt     DLG_TRACE2N("width", width);
695940c9abSDaniel Fojt     DLG_TRACE2N("pauseopt", pauseopt);
705940c9abSDaniel Fojt 
715382d832SPeter Avalos     dialog_vars.nocancel = TRUE;
725382d832SPeter Avalos     button = dlg_default_button();
735382d832SPeter Avalos 
745382d832SPeter Avalos #ifdef KEY_RESIZE
755382d832SPeter Avalos     req_high = height;
765382d832SPeter Avalos     req_wide = width;
775382d832SPeter Avalos   restart:
785382d832SPeter Avalos #endif
795382d832SPeter Avalos 
805382d832SPeter Avalos     dlg_button_layout(buttons, &min_width);
815382d832SPeter Avalos 
825940c9abSDaniel Fojt     prompt = dlg_strclone(cprompt);
835382d832SPeter Avalos     dlg_tab_correct_str(prompt);
845382d832SPeter Avalos     dlg_auto_size(title, prompt, &height, &width,
855382d832SPeter Avalos 		  (pauseopt == 1 ? 2 : 0),
865382d832SPeter Avalos 		  min_width);
875382d832SPeter Avalos     dlg_print_size(height, width);
885382d832SPeter Avalos     dlg_ctl_size(height, width);
895382d832SPeter Avalos 
905382d832SPeter Avalos     x = dlg_box_x_ordinate(width);
915382d832SPeter Avalos     y = dlg_box_y_ordinate(height);
925382d832SPeter Avalos 
935382d832SPeter Avalos     dialog = dlg_new_window(height, width, y, x);
945382d832SPeter Avalos     dlg_register_window(dialog, "msgbox", binding);
955382d832SPeter Avalos     dlg_register_buttons(dialog, "msgbox", buttons);
965940c9abSDaniel Fojt 
975382d832SPeter Avalos     page = height - (1 + 3 * MARGIN);
985382d832SPeter Avalos 
995382d832SPeter Avalos     dlg_mouse_setbase(x, y);
1005382d832SPeter Avalos 
1015382d832SPeter Avalos     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
1025382d832SPeter Avalos     dlg_draw_title(dialog, title);
1035382d832SPeter Avalos 
1045940c9abSDaniel Fojt     dlg_attrset(dialog, dialog_attr);
1055382d832SPeter Avalos 
1065382d832SPeter Avalos     if (pauseopt) {
1075940c9abSDaniel Fojt 	int last = 0;
1085940c9abSDaniel Fojt 
1095382d832SPeter Avalos 	dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1105382d832SPeter Avalos 	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
1115382d832SPeter Avalos 	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
1125382d832SPeter Avalos 	dlg_draw_helpline(dialog, FALSE);
1135382d832SPeter Avalos 
1145382d832SPeter Avalos 	while (result == DLG_EXIT_UNKNOWN) {
1155940c9abSDaniel Fojt 	    int check;
1165940c9abSDaniel Fojt 
1175382d832SPeter Avalos 	    if (show) {
1185382d832SPeter Avalos 		last = dlg_print_scrolled(dialog, prompt, offset,
1195382d832SPeter Avalos 					  page, width, pauseopt);
1205382d832SPeter Avalos 		dlg_trace_win(dialog);
1215382d832SPeter Avalos 		show = FALSE;
1225382d832SPeter Avalos 	    }
1235382d832SPeter Avalos 	    key = dlg_mouse_wgetch(dialog, &fkey);
1245940c9abSDaniel Fojt 	    if (dlg_result_key(key, fkey, &result)) {
1255940c9abSDaniel Fojt 		if (!dlg_button_key(result, &button, &key, &fkey))
1265382d832SPeter Avalos 		    break;
1275940c9abSDaniel Fojt 	    }
1285382d832SPeter Avalos 	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
1295382d832SPeter Avalos 		result = dlg_ok_buttoncode(check);
1305382d832SPeter Avalos 		break;
1315382d832SPeter Avalos 	    }
1325382d832SPeter Avalos 
1335382d832SPeter Avalos 	    if (fkey) {
1345382d832SPeter Avalos 		switch (key) {
1355382d832SPeter Avalos #ifdef KEY_RESIZE
1365382d832SPeter Avalos 		case KEY_RESIZE:
1375940c9abSDaniel Fojt 		    dlg_will_resize(dialog);
1385940c9abSDaniel Fojt 		    free(prompt);
1395382d832SPeter Avalos 		    height = req_high;
1405382d832SPeter Avalos 		    width = req_wide;
1415382d832SPeter Avalos 		    show = TRUE;
1425940c9abSDaniel Fojt 		    _dlg_resize_cleanup(dialog);
1435382d832SPeter Avalos 		    goto restart;
1445382d832SPeter Avalos #endif
1455382d832SPeter Avalos 		case DLGK_FIELD_NEXT:
1465382d832SPeter Avalos 		    button = dlg_next_button(buttons, button);
1475382d832SPeter Avalos 		    if (button < 0)
1485382d832SPeter Avalos 			button = 0;
1495382d832SPeter Avalos 		    dlg_draw_buttons(dialog,
1505382d832SPeter Avalos 				     height - 2, 0,
1515382d832SPeter Avalos 				     buttons, button,
1525382d832SPeter Avalos 				     FALSE, width);
1535382d832SPeter Avalos 		    break;
1545382d832SPeter Avalos 		case DLGK_FIELD_PREV:
1555382d832SPeter Avalos 		    button = dlg_prev_button(buttons, button);
1565382d832SPeter Avalos 		    if (button < 0)
1575382d832SPeter Avalos 			button = 0;
1585382d832SPeter Avalos 		    dlg_draw_buttons(dialog,
1595382d832SPeter Avalos 				     height - 2, 0,
1605382d832SPeter Avalos 				     buttons, button,
1615382d832SPeter Avalos 				     FALSE, width);
1625382d832SPeter Avalos 		    break;
1635382d832SPeter Avalos 		case DLGK_ENTER:
164*a8e38dc0SAntonio Huete Jimenez 		    result = dlg_enter_buttoncode(button);
165*a8e38dc0SAntonio Huete Jimenez 		    break;
166*a8e38dc0SAntonio Huete Jimenez 		case DLGK_LEAVE:
1675382d832SPeter Avalos 		    result = dlg_ok_buttoncode(button);
1685382d832SPeter Avalos 		    break;
1695382d832SPeter Avalos 		default:
1705382d832SPeter Avalos 		    if (is_DLGK_MOUSE(key)) {
1715382d832SPeter Avalos 			result = dlg_ok_buttoncode(key - M_EVENT);
1725382d832SPeter Avalos 			if (result < 0)
1735382d832SPeter Avalos 			    result = DLG_EXIT_OK;
1745382d832SPeter Avalos 		    } else if (dlg_check_scrolled(key,
1755382d832SPeter Avalos 						  last,
1765382d832SPeter Avalos 						  page,
1775382d832SPeter Avalos 						  &show,
1785382d832SPeter Avalos 						  &offset) == 0) {
1795382d832SPeter Avalos 		    } else {
1805382d832SPeter Avalos 			beep();
1815382d832SPeter Avalos 		    }
1825382d832SPeter Avalos 		    break;
1835382d832SPeter Avalos 		}
1845940c9abSDaniel Fojt 	    } else if (key > 0) {
1855382d832SPeter Avalos 		beep();
1865382d832SPeter Avalos 	    }
1875382d832SPeter Avalos 	}
1885382d832SPeter Avalos     } else {
1895382d832SPeter Avalos 	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
1905382d832SPeter Avalos 	dlg_draw_helpline(dialog, FALSE);
1915382d832SPeter Avalos 	wrefresh(dialog);
1925382d832SPeter Avalos 	dlg_trace_win(dialog);
1935382d832SPeter Avalos 	result = DLG_EXIT_OK;
1945382d832SPeter Avalos     }
1955940c9abSDaniel Fojt     dlg_add_last_key(-1);
1965382d832SPeter Avalos 
1975382d832SPeter Avalos     dlg_del_window(dialog);
1985382d832SPeter Avalos     dlg_mouse_free_regions();
1995382d832SPeter Avalos     free(prompt);
2005382d832SPeter Avalos 
2015382d832SPeter Avalos     dialog_vars.nocancel = save_nocancel;
2025382d832SPeter Avalos 
2035382d832SPeter Avalos     return result;
2045382d832SPeter Avalos }
205