msgbox.c revision 3d4e8889889e5e36302454225999f7e146d3219c
17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate *  $Id: msgbox.c,v 1.64 2010/01/15 10:50:17 tom Exp $
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate *  msgbox.c -- implements the message box and info box
5a574db8raf *
6a574db8raf *  Copyright 2000-2009,2010	Thomas E. Dickey
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate *  This program is free software; you can redistribute it and/or modify
97c478bdstevel@tonic-gate *  it under the terms of the GNU Lesser General Public License, version 2.1
107c478bdstevel@tonic-gate *  as published by the Free Software Foundation.
117c478bdstevel@tonic-gate *
127c478bdstevel@tonic-gate *  This program is distributed in the hope that it will be useful, but
137c478bdstevel@tonic-gate *  WITHOUT ANY WARRANTY; without even the implied warranty of
147c478bdstevel@tonic-gate *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
157c478bdstevel@tonic-gate *  Lesser General Public License for more details.
167c478bdstevel@tonic-gate *
177c478bdstevel@tonic-gate *  You should have received a copy of the GNU Lesser General Public
187c478bdstevel@tonic-gate *  License along with this program; if not, write to
197c478bdstevel@tonic-gate *	Free Software Foundation, Inc.
207c478bdstevel@tonic-gate *	51 Franklin St., Fifth Floor
21a574db8raf *	Boston, MA 02110, USA.
227c478bdstevel@tonic-gate *
23a574db8raf *  An earlier version of this program lists as authors:
247c478bdstevel@tonic-gate *	Savio Lam (lam836@cs.cuhk.hk)
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#include <dialog.h>
287c478bdstevel@tonic-gate#include <dlg_keys.h>
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * Display a message box. Program will pause and display an "OK" button
327257d1braf * if the parameter 'pauseopt' is non-zero.
337c478bdstevel@tonic-gate */
347c478bdstevel@tonic-gateint
357c478bdstevel@tonic-gatedialog_msgbox(const char *title, const char *cprompt, int height, int width,
367c478bdstevel@tonic-gate	      int pauseopt)
377c478bdstevel@tonic-gate{
387c478bdstevel@tonic-gate    /* *INDENT-OFF* */
397c478bdstevel@tonic-gate    static DLG_KEYS_BINDING binding[] = {
407c478bdstevel@tonic-gate	ENTERKEY_BINDINGS,
417c478bdstevel@tonic-gate	DLG_KEYS_DATA( DLGK_ENTER,	' ' ),
427c478bdstevel@tonic-gate	SCROLLKEY_BINDINGS,
437257d1braf	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_DOWN ),
447257d1braf	DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
457c478bdstevel@tonic-gate	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
46a574db8raf	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_UP ),
47a574db8raf	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
487c478bdstevel@tonic-gate	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
497c478bdstevel@tonic-gate	END_KEYS_BINDING
507c478bdstevel@tonic-gate    };
517c478bdstevel@tonic-gate    /* *INDENT-ON* */
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate    int x, y, last = 0, page;
547c478bdstevel@tonic-gate    int button = 0;
557c478bdstevel@tonic-gate    int key = 0, fkey;
567c478bdstevel@tonic-gate    int result = DLG_EXIT_UNKNOWN;
577c478bdstevel@tonic-gate    WINDOW *dialog = 0;
587c478bdstevel@tonic-gate    char *prompt = dlg_strclone(cprompt);
597c478bdstevel@tonic-gate    const char **buttons = dlg_ok_label();
60a574db8raf    int offset = 0;
617c478bdstevel@tonic-gate    int check;
627c478bdstevel@tonic-gate    bool show = TRUE;
637c478bdstevel@tonic-gate    int min_width = (pauseopt == 1 ? 12 : 0);
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate#ifdef KEY_RESIZE
667c478bdstevel@tonic-gate    int req_high = height;
677c478bdstevel@tonic-gate    int req_wide = width;
687c478bdstevel@tonic-gate  restart:
697c478bdstevel@tonic-gate#endif
708cd4554raf
717c478bdstevel@tonic-gate    dlg_button_layout(buttons, &min_width);
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate    dlg_tab_correct_str(prompt);
747c478bdstevel@tonic-gate    dlg_auto_size(title, prompt, &height, &width,
757c478bdstevel@tonic-gate		  (pauseopt == 1 ? 2 : 0),
767c478bdstevel@tonic-gate		  min_width);
777c478bdstevel@tonic-gate    dlg_print_size(height, width);
787c478bdstevel@tonic-gate    dlg_ctl_size(height, width);
797c478bdstevel@tonic-gate
807257d1braf    x = dlg_box_x_ordinate(width);
817c478bdstevel@tonic-gate    y = dlg_box_y_ordinate(height);
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gate#ifdef KEY_RESIZE
847c478bdstevel@tonic-gate    if (dialog != 0)
857c478bdstevel@tonic-gate	dlg_move_window(dialog, height, width, y, x);
867c478bdstevel@tonic-gate    else
877c478bdstevel@tonic-gate#endif
887c478bdstevel@tonic-gate    {
897257d1braf	dialog = dlg_new_window(height, width, y, x);
907c478bdstevel@tonic-gate	dlg_register_window(dialog, "msgbox", binding);
917c478bdstevel@tonic-gate	dlg_register_buttons(dialog, "msgbox", buttons);
927c478bdstevel@tonic-gate    }
937c478bdstevel@tonic-gate    page = height - (1 + 3 * MARGIN);
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate    dlg_mouse_setbase(x, y);
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate    dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
987c478bdstevel@tonic-gate    dlg_draw_title(dialog, title);
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gate    wattrset(dialog, dialog_attr);
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate    if (pauseopt) {
1037c478bdstevel@tonic-gate	dlg_draw_bottom_box(dialog);
1047c478bdstevel@tonic-gate	mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
1057c478bdstevel@tonic-gate	dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	while (result == DLG_EXIT_UNKNOWN) {
1087c478bdstevel@tonic-gate	    if (show) {
1097c478bdstevel@tonic-gate		last = dlg_print_scrolled(dialog, prompt, offset,
1107c478bdstevel@tonic-gate					  page, width, pauseopt);
1117c478bdstevel@tonic-gate		show = FALSE;
1127c478bdstevel@tonic-gate	    }
1137c478bdstevel@tonic-gate	    key = dlg_mouse_wgetch(dialog, &fkey);
1147c478bdstevel@tonic-gate	    if (dlg_result_key(key, fkey, &result))
1157c478bdstevel@tonic-gate		break;
1167c478bdstevel@tonic-gate
1177c478bdstevel@tonic-gate	    if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
1187c478bdstevel@tonic-gate		result = check ? DLG_EXIT_HELP : DLG_EXIT_OK;
119		break;
120	    }
121
122	    if (fkey) {
123		switch (key) {
124#ifdef KEY_RESIZE
125		case KEY_RESIZE:
126		    dlg_clear();
127		    height = req_high;
128		    width = req_wide;
129		    show = TRUE;
130		    goto restart;
131#endif
132		case DLGK_FIELD_NEXT:
133		    button = dlg_next_button(buttons, button);
134		    if (button < 0)
135			button = 0;
136		    dlg_draw_buttons(dialog,
137				     height - 2, 0,
138				     buttons, button,
139				     FALSE, width);
140		    break;
141		case DLGK_FIELD_PREV:
142		    button = dlg_prev_button(buttons, button);
143		    if (button < 0)
144			button = 0;
145		    dlg_draw_buttons(dialog,
146				     height - 2, 0,
147				     buttons, button,
148				     FALSE, width);
149		    break;
150		case DLGK_ENTER:
151		    result = button ? DLG_EXIT_HELP : DLG_EXIT_OK;
152		    break;
153		case DLGK_MOUSE(0):
154		    result = DLG_EXIT_OK;
155		    break;
156		case DLGK_MOUSE(1):
157		    result = DLG_EXIT_HELP;
158		    break;
159		default:
160		    if (dlg_check_scrolled(key,
161					   last,
162					   page,
163					   &show,
164					   &offset) == 0)
165			break;
166		    beep();
167		    break;
168		}
169	    } else {
170		beep();
171	    }
172	}
173    } else {
174	dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
175	wrefresh(dialog);
176	result = DLG_EXIT_OK;
177    }
178
179    dlg_del_window(dialog);
180    dlg_mouse_free_regions();
181    free(prompt);
182    return result;
183}
184