]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/dialog/msgbox.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / dialog / msgbox.c
1 /*
2  *  $Id: msgbox.c,v 1.66 2011/06/27 08:36:28 tom Exp $
3  *
4  *  msgbox.c -- implements the message box and info box
5  *
6  *  Copyright 2000-2010,2011    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  */
34 int
35 dialog_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         DLG_KEYS_DATA( DLGK_ENTER,      ' ' ),
43         SCROLLKEY_BINDINGS,
44         DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_DOWN ),
45         DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
46         DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
47         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_UP ),
48         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
49         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
50         END_KEYS_BINDING
51     };
52     /* *INDENT-ON* */
53
54     int x, y, last = 0, page;
55     int button = 0;
56     int key = 0, fkey;
57     int result = DLG_EXIT_UNKNOWN;
58     WINDOW *dialog = 0;
59     char *prompt = dlg_strclone(cprompt);
60     const char **buttons = dlg_ok_label();
61     int offset = 0;
62     int check;
63     bool show = TRUE;
64     int min_width = (pauseopt == 1 ? 12 : 0);
65
66 #ifdef KEY_RESIZE
67     int req_high = height;
68     int req_wide = width;
69   restart:
70 #endif
71
72     dlg_button_layout(buttons, &min_width);
73
74     dlg_tab_correct_str(prompt);
75     dlg_auto_size(title, prompt, &height, &width,
76                   (pauseopt == 1 ? 2 : 0),
77                   min_width);
78     dlg_print_size(height, width);
79     dlg_ctl_size(height, width);
80
81     x = dlg_box_x_ordinate(width);
82     y = dlg_box_y_ordinate(height);
83
84 #ifdef KEY_RESIZE
85     if (dialog != 0)
86         dlg_move_window(dialog, height, width, y, x);
87     else
88 #endif
89     {
90         dialog = dlg_new_window(height, width, y, x);
91         dlg_register_window(dialog, "msgbox", binding);
92         dlg_register_buttons(dialog, "msgbox", buttons);
93     }
94     page = height - (1 + 3 * MARGIN);
95
96     dlg_mouse_setbase(x, y);
97
98     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
99     dlg_draw_title(dialog, title);
100
101     wattrset(dialog, dialog_attr);
102
103     if (pauseopt) {
104         dlg_draw_bottom_box(dialog);
105         mouse_mkbutton(height - 2, width / 2 - 4, 6, '\n');
106         dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
107         dlg_draw_helpline(dialog, FALSE);
108
109         while (result == DLG_EXIT_UNKNOWN) {
110             if (show) {
111                 last = dlg_print_scrolled(dialog, prompt, offset,
112                                           page, width, pauseopt);
113                 show = FALSE;
114             }
115             key = dlg_mouse_wgetch(dialog, &fkey);
116             if (dlg_result_key(key, fkey, &result))
117                 break;
118
119             if (!fkey && (check = dlg_char_to_button(key, buttons)) >= 0) {
120                 result = check ? DLG_EXIT_HELP : DLG_EXIT_OK;
121                 break;
122             }
123
124             if (fkey) {
125                 switch (key) {
126 #ifdef KEY_RESIZE
127                 case KEY_RESIZE:
128                     dlg_clear();
129                     height = req_high;
130                     width = req_wide;
131                     show = TRUE;
132                     goto restart;
133 #endif
134                 case DLGK_FIELD_NEXT:
135                     button = dlg_next_button(buttons, button);
136                     if (button < 0)
137                         button = 0;
138                     dlg_draw_buttons(dialog,
139                                      height - 2, 0,
140                                      buttons, button,
141                                      FALSE, width);
142                     break;
143                 case DLGK_FIELD_PREV:
144                     button = dlg_prev_button(buttons, button);
145                     if (button < 0)
146                         button = 0;
147                     dlg_draw_buttons(dialog,
148                                      height - 2, 0,
149                                      buttons, button,
150                                      FALSE, width);
151                     break;
152                 case DLGK_ENTER:
153                     result = button ? DLG_EXIT_HELP : DLG_EXIT_OK;
154                     break;
155                 case DLGK_MOUSE(0):
156                     result = DLG_EXIT_OK;
157                     break;
158                 case DLGK_MOUSE(1):
159                     result = DLG_EXIT_HELP;
160                     break;
161                 default:
162                     if (dlg_check_scrolled(key,
163                                            last,
164                                            page,
165                                            &show,
166                                            &offset) == 0)
167                         break;
168                     beep();
169                     break;
170                 }
171             } else {
172                 beep();
173             }
174         }
175     } else {
176         dlg_print_scrolled(dialog, prompt, offset, page, width, pauseopt);
177         dlg_draw_helpline(dialog, FALSE);
178         wrefresh(dialog);
179         result = DLG_EXIT_OK;
180     }
181
182     dlg_del_window(dialog);
183     dlg_mouse_free_regions();
184     free(prompt);
185     return result;
186 }