183cbb37nwhitehorn/*
2f8604a5bapt *  $Id: msgbox.c,v 1.81 2018/06/21 23:29:59 tom Exp $
383cbb37nwhitehorn *
483cbb37nwhitehorn *  msgbox.c -- implements the message box and info box
583cbb37nwhitehorn *
6f8604a5bapt *  Copyright 2000-2012,2018	Thomas E. Dickey
783cbb37nwhitehorn *
883cbb37nwhitehorn *  This program is free software; you can redistribute it and/or modify
983cbb37nwhitehorn *  it under the terms of the GNU Lesser General Public License, version 2.1
1083cbb37nwhitehorn *  as published by the Free Software Foundation.
1183cbb37nwhitehorn *
1283cbb37nwhitehorn *  This program is distributed in the hope that it will be useful, but
1383cbb37nwhitehorn *  WITHOUT ANY WARRANTY; without even the implied warranty of
1483cbb37nwhitehorn *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1583cbb37nwhitehorn *  Lesser General Public License for more details.
1683cbb37nwhitehorn *
1783cbb37nwhitehorn *  You should have received a copy of the GNU Lesser General Public
1883cbb37nwhitehorn *  License along with this program; if not, write to
1983cbb37nwhitehorn *	Free Software Foundation, Inc.
2083cbb37nwhitehorn *	51 Franklin St., Fifth Floor
2183cbb37nwhitehorn *	Boston, MA 02110, USA.
2283cbb37nwhitehorn *
2383cbb37nwhitehorn *  An earlier version of this program lists as authors:
2483cbb37nwhitehorn *	Savio Lam (lam836@cs.cuhk.hk)
2583cbb37nwhitehorn */
2683cbb37nwhitehorn
2783cbb37nwhitehorn#include <dialog.h>
2883cbb37nwhitehorn#include <dlg_keys.h>
2983cbb37nwhitehorn
3083cbb37nwhitehorn/*
3183cbb37nwhitehorn * Display a message box. Program will pause and display an "OK" button
3283cbb37nwhitehorn * if the parameter 'pauseopt' is non-zero.
3383cbb37nwhitehorn */
3483cbb37nwhitehornint
3583cbb37nwhitehorndialog_msgbox(const char *title, const char *cprompt, int height, int width,
3683cbb37nwhitehorn	      int pauseopt)
3783cbb37nwhitehorn{
3883cbb37nwhitehorn    /* *INDENT-OFF* */
3983cbb37nwhitehorn    static DLG_KEYS_BINDING binding[] = {
4094f19e0nwhitehorn	HELPKEY_BINDINGS,
4183cbb37nwhitehorn	ENTERKEY_BINDINGS,
4283cbb37nwhitehorn	SCROLLKEY_BINDINGS,
43f8604a5bapt	TRAVERSE_BINDINGS,
4483cbb37nwhitehorn	END_KEYS_BINDING
4583cbb37nwhitehorn    };
4683cbb37nwhitehorn    /* *INDENT-ON* */
4783cbb37nwhitehorn
4883cbb37nwhitehorn    int x, y, last = 0, page;
49a94245bnwhitehorn    int button;
5083cbb37nwhitehorn    int key = 0, fkey;
5183cbb37nwhitehorn    int result = DLG_EXIT_UNKNOWN;
5283cbb37nwhitehorn    WINDOW *dialog = 0;
53f8604a5bapt    char *prompt;
5483cbb37nwhitehorn    const char **buttons = dlg_ok_label();
5583cbb37nwhitehorn    int offset = 0;
5683cbb37nwhitehorn    int check;
5783cbb37nwhitehorn    bool show = TRUE;
5883cbb37nwhitehorn    int min_width = (pauseopt == 1 ? 12 : 0);
59f8604a5bapt    bool save_nocancel = dialog_vars.nocancel;
60a94245bnwhitehorn#ifdef KEY_RESIZE
61a94245bnwhitehorn    int req_high;
62a94245bnwhitehorn    int req_wide;
63a94245bnwhitehorn#endif
64a94245bnwhitehorn
65f8604a5bapt    DLG_TRACE(("# msgbox args:\n"));
66f8604a5bapt    DLG_TRACE2S("title", title);
67f8604a5bapt    DLG_TRACE2S("message", cprompt);
68f8604a5bapt    DLG_TRACE2N("height", height);
69f8604a5bapt    DLG_TRACE2N("width", width);
70f8604a5bapt    DLG_TRACE2N("pauseopt", pauseopt);
71f8604a5bapt
72a94245bnwhitehorn    dialog_vars.nocancel = TRUE;
73a94245bnwhitehorn    button = dlg_default_button();
7483cbb37nwhitehorn
7583cbb37nwhitehorn#ifdef KEY_RESIZE
76a94245bnwhitehorn    req_high = height;
77a94245bnwhitehorn    req_wide = width;
7883cbb37nwhitehorn  restart:
7983cbb37nwhitehorn#endif
8083cbb37nwhitehorn
8183cbb37nwhitehorn    dlg_button_layout(buttons, &min_width);
8283cbb37nwhitehorn
83f8604a5bapt    prompt = dlg_strclone(cprompt);
8483cbb37nwhitehorn    dlg_tab_correct_str(prompt);
8583cbb37nwhitehorn    dlg_auto_size(title, prompt, &height, &width,
8683cbb37nwhitehorn		  (pauseopt == 1 ? 2 : 0),
8783cbb37nwhitehorn		  min_width);
8883cbb37nwhitehorn    dlg_print_size(height, width);
8983cbb37nwhitehorn    dlg_ctl_size(height, width);
9083cbb37nwhitehorn
9183cbb37nwhitehorn    x = dlg_box_x_ordinate(width);
9283cbb37nwhitehorn    y = dlg_box_y_ordinate(height);
9383cbb37nwhitehorn
9483cbb37nwhitehorn#ifdef KEY_RESIZE
9583cbb37nwhitehorn    if (dialog != 0)
9683cbb37nwhitehorn	dlg_move_window(dialog, height, width, y, x);
9783cbb37nwhitehorn    else
9883cbb37nwhitehorn#endif
9983cbb37nwhitehorn    {
10083cbb37nwhitehorn	dialog = dlg_new_window(height, width, y, x);
10183cbb37nwhitehorn	dlg_register_window(dialog, "msgbox", binding);
10283cbb37nwhitehorn	dlg_register_buttons(dialog, "msgbox", buttons);
10383cbb37nwhitehorn    }
10483cbb37nwhitehorn    page = height - (1 + 3 * MARGIN);
10583cbb37nwhitehorn
10683cbb37nwhitehorn    dlg_mouse_setbase(x, y);
10783cbb37nwhitehorn
108a94245bnwhitehorn    dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
10983cbb37nwhitehorn    dlg_draw_title(dialog, title);
11083cbb37nwhitehorn
111f8604a5bapt    dlg_attrset(dialog, dialog_attr);
11283cbb37nwhitehorn
11383cbb37nwhitehorn    if (pauseopt) {
114a94245bnwhitehorn	dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
11583cbb37nwhitehorn	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
11683cbb37nwhitehorn	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
11794f19e0nwhitehorn	dlg_draw_helpline(dialog, FALSE);
11883cbb37nwhitehorn
11983cbb37nwhitehorn	while (result == DLG_EXIT_UNKNOWN) {
12083cbb37nwhitehorn	    if (show) {
12183cbb37nwhitehorn		last = dlg_print_scrolled(dialog, prompt, offset,
12283cbb37nwhitehorn					  page, width, pauseopt);
123a94245bnwhitehorn		dlg_trace_win(dialog);
12483cbb37nwhitehorn		show = FALSE;
12583cbb37nwhitehorn	    }
12683cbb37nwhitehorn	    key = dlg_mouse_wgetch(dialog, &fkey);
12783cbb37nwhitehorn	    if (dlg_result_key(key, fkey, &result))
12883cbb37nwhitehorn		break;
12983cbb37nwhitehorn
13083cbb37nwhitehorn	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
131a94245bnwhitehorn		result = dlg_ok_buttoncode(check);
13283cbb37nwhitehorn		break;
13383cbb37nwhitehorn	    }
13483cbb37nwhitehorn
13583cbb37nwhitehorn	    if (fkey) {
13683cbb37nwhitehorn		switch (key) {
13783cbb37nwhitehorn#ifdef KEY_RESIZE
13883cbb37nwhitehorn		case KEY_RESIZE:
139f8604a5bapt		    dlg_will_resize(dialog);
14083cbb37nwhitehorn		    dlg_clear();
141f8604a5bapt		    free(prompt);
14283cbb37nwhitehorn		    height = req_high;
14383cbb37nwhitehorn		    width = req_wide;
14483cbb37nwhitehorn		    show = TRUE;
14583cbb37nwhitehorn		    goto restart;
14683cbb37nwhitehorn#endif
14783cbb37nwhitehorn		case DLGK_FIELD_NEXT:
14883cbb37nwhitehorn		    button = dlg_next_button(buttons, button);
14983cbb37nwhitehorn		    if (button < 0)
15083cbb37nwhitehorn			button = 0;
15183cbb37nwhitehorn		    dlg_draw_buttons(dialog,
15283cbb37nwhitehorn				     height - 2, 0,
15383cbb37nwhitehorn				     buttons, button,
15483cbb37nwhitehorn				     FALSE, width);
15583cbb37nwhitehorn		    break;
15683cbb37nwhitehorn		case DLGK_FIELD_PREV:
15783cbb37nwhitehorn		    button = dlg_prev_button(buttons, button);
15883cbb37nwhitehorn		    if (button < 0)
15983cbb37nwhitehorn			button = 0;
16083cbb37nwhitehorn		    dlg_draw_buttons(dialog,
16183cbb37nwhitehorn				     height - 2, 0,
16283cbb37nwhitehorn				     buttons, button,
16383cbb37nwhitehorn				     FALSE, width);
16483cbb37nwhitehorn		    break;
16583cbb37nwhitehorn		case DLGK_ENTER:
166a94245bnwhitehorn		    result = dlg_ok_buttoncode(button);
16783cbb37nwhitehorn		    break;
16883cbb37nwhitehorn		default:
169a94245bnwhitehorn		    if (is_DLGK_MOUSE(key)) {
170a94245bnwhitehorn			result = dlg_ok_buttoncode(key - M_EVENT);
171a94245bnwhitehorn			if (result < 0)
172a94245bnwhitehorn			    result = DLG_EXIT_OK;
173a94245bnwhitehorn		    } else if (dlg_check_scrolled(key,
174a94245bnwhitehorn						  last,
175a94245bnwhitehorn						  page,
176a94245bnwhitehorn						  &show,
177a94245bnwhitehorn						  &offset) == 0) {
178a94245bnwhitehorn		    } else {
179a94245bnwhitehorn			beep();
180a94245bnwhitehorn		    }
18183cbb37nwhitehorn		    break;
18283cbb37nwhitehorn		}
18383cbb37nwhitehorn	    } else {
18483cbb37nwhitehorn		beep();
18583cbb37nwhitehorn	    }
18683cbb37nwhitehorn	}
18783cbb37nwhitehorn    } else {
18883cbb37nwhitehorn	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
18994f19e0nwhitehorn	dlg_draw_helpline(dialog, FALSE);
19083cbb37nwhitehorn	wrefresh(dialog);
191a94245bnwhitehorn	dlg_trace_win(dialog);
19283cbb37nwhitehorn	result = DLG_EXIT_OK;
19383cbb37nwhitehorn    }
19483cbb37nwhitehorn
19583cbb37nwhitehorn    dlg_del_window(dialog);
19683cbb37nwhitehorn    dlg_mouse_free_regions();
19783cbb37nwhitehorn    free(prompt);
198a94245bnwhitehorn
199a94245bnwhitehorn    dialog_vars.nocancel = save_nocancel;
200a94245bnwhitehorn
20183cbb37nwhitehorn    return result;
20283cbb37nwhitehorn}
203