msgbox.c revision eb1a5f8de9f7ea602c373a710f531abbf81141c4
1/*
2 *  $Id: msgbox.c,v 1.75 2012/12/01 01:48:08 tom Exp $
3 *
4 *  msgbox.c -- implements the message box and info box
5 *
6 *  Copyright 2000-2011,2012	Thomas E. Dickey
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU Lesser General Public License, version 2.1
10 *  as published by the Free Software Foundation.
11 *
12 *  This program is distributed in the hope that it will be useful, but
13 *  WITHOUT ANY WARRANTY; without even the implied warranty of
14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 *  Lesser General Public License for more details.
16 *
17 *  You should have received a copy of the GNU Lesser General Public
18 *  License along with this program; if not, write to
19 *	Free Software Foundation, Inc.
20 *	51 Franklin St., Fifth Floor
21 *	Boston, MA 02110, USA.
22 *
23 *  An earlier version of this program lists as authors:
24 *	Savio Lam (lam836@cs.cuhk.hk)
25 */
26
27#include <dialog.h>
28#include <dlg_keys.h>
29
30/*
31 * Display a message box. Program will pause and display an "OK" button
32 * if the parameter 'pauseopt' is non-zero.
33 */
34int
35dialog_msgbox(const char *title, const char *cprompt, int height, int width,
36	      int pauseopt)
37{
38    /* *INDENT-OFF* */
39    static DLG_KEYS_BINDING binding[] = {
40	HELPKEY_BINDINGS,
41	ENTERKEY_BINDINGS,
42	TRAVERSE_BINDINGS,
43	SCROLLKEY_BINDINGS,
44	END_KEYS_BINDING
45    };
46    /* *INDENT-ON* */
47
48    int x, y, last = 0, page;
49    int button;
50    int key = 0, fkey;
51    int result = DLG_EXIT_UNKNOWN;
52    WINDOW *dialog = 0;
53    char *prompt = dlg_strclone(cprompt);
54    const char **buttons = dlg_ok_label();
55    int offset = 0;
56    int check;
57    bool show = TRUE;
58    int min_width = (pauseopt == 1 ? 12 : 0);
59    int save_nocancel = dialog_vars.nocancel;
60#ifdef KEY_RESIZE
61    int req_high;
62    int req_wide;
63#endif
64
65    dialog_vars.nocancel = TRUE;
66    button = dlg_default_button();
67
68#ifdef KEY_RESIZE
69    req_high = height;
70    req_wide = width;
71  restart:
72#endif
73
74    dlg_button_layout(buttons, &min_width);
75
76    dlg_tab_correct_str(prompt);
77    dlg_auto_size(title, prompt, &height, &width,
78		  (pauseopt == 1 ? 2 : 0),
79		  min_width);
80    dlg_print_size(height, width);
81    dlg_ctl_size(height, width);
82
83    x = dlg_box_x_ordinate(width);
84    y = dlg_box_y_ordinate(height);
85
86#ifdef KEY_RESIZE
87    if (dialog != 0)
88	dlg_move_window(dialog, height, width, y, x);
89    else
90#endif
91    {
92	dialog = dlg_new_window(height, width, y, x);
93	dlg_register_window(dialog, "msgbox", binding);
94	dlg_register_buttons(dialog, "msgbox", buttons);
95    }
96    page = height - (1 + 3 * MARGIN);
97
98    dlg_mouse_setbase(x, y);
99
100    dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
101    dlg_draw_title(dialog, title);
102
103    (void) wattrset(dialog, dialog_attr);
104
105    if (pauseopt) {
106	dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
107	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
108	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
109	dlg_draw_helpline(dialog, FALSE);
110
111	while (result == DLG_EXIT_UNKNOWN) {
112	    if (show) {
113		last = dlg_print_scrolled(dialog, prompt, offset,
114					  page, width, pauseopt);
115		dlg_trace_win(dialog);
116		show = FALSE;
117	    }
118	    key = dlg_mouse_wgetch(dialog, &fkey);
119	    if (dlg_result_key(key, fkey, &result))
120		break;
121
122	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
123		result = dlg_ok_buttoncode(check);
124		break;
125	    }
126
127	    if (fkey) {
128		switch (key) {
129#ifdef KEY_RESIZE
130		case KEY_RESIZE:
131		    dlg_clear();
132		    height = req_high;
133		    width = req_wide;
134		    show = TRUE;
135		    goto restart;
136#endif
137		case DLGK_FIELD_NEXT:
138		    button = dlg_next_button(buttons, button);
139		    if (button < 0)
140			button = 0;
141		    dlg_draw_buttons(dialog,
142				     height - 2, 0,
143				     buttons, button,
144				     FALSE, width);
145		    break;
146		case DLGK_FIELD_PREV:
147		    button = dlg_prev_button(buttons, button);
148		    if (button < 0)
149			button = 0;
150		    dlg_draw_buttons(dialog,
151				     height - 2, 0,
152				     buttons, button,
153				     FALSE, width);
154		    break;
155		case DLGK_ENTER:
156		    result = dlg_ok_buttoncode(button);
157		    break;
158		default:
159		    if (is_DLGK_MOUSE(key)) {
160			result = dlg_ok_buttoncode(key - M_EVENT);
161			if (result < 0)
162			    result = DLG_EXIT_OK;
163		    } else if (dlg_check_scrolled(key,
164						  last,
165						  page,
166						  &show,
167						  &offset) == 0) {
168		    } else {
169			beep();
170		    }
171		    break;
172		}
173	    } else {
174		beep();
175	    }
176	}
177    } else {
178	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
179	dlg_draw_helpline(dialog, FALSE);
180	wrefresh(dialog);
181	dlg_trace_win(dialog);
182	result = DLG_EXIT_OK;
183    }
184
185    dlg_del_window(dialog);
186    dlg_mouse_free_regions();
187    free(prompt);
188
189    dialog_vars.nocancel = save_nocancel;
190
191    return result;
192}
193