BSD.include.dist: Use spaces to indent (see etc/mtree/README).
[dragonfly.git] / contrib / dialog / inputbox.c
blob1d7ad7d56cc449f07a85e1580f44a13ff95c1813
1 /*
2 * $Id: inputbox.c,v 1.76 2012/12/03 11:46:50 tom Exp $
4 * inputbox.c -- implements the input box
6 * Copyright 2000-2011,2012 Thomas E. Dickey
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.
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.
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.
23 * An earlier version of this program lists as authors:
24 * Savio Lam (lam836@cs.cuhk.hk)
27 #include <dialog.h>
28 #include <dlg_keys.h>
30 #define sTEXT -1
32 #define NAVIGATE_BINDINGS \
33 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_DOWN ), \
34 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
35 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
36 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
37 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ), \
38 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_UP )
41 * Display a dialog box for entering a string
43 int
44 dialog_inputbox(const char *title, const char *cprompt, int height, int width,
45 const char *init, const int password)
47 /* *INDENT-OFF* */
48 static DLG_KEYS_BINDING binding[] = {
49 HELPKEY_BINDINGS,
50 ENTERKEY_BINDINGS,
51 NAVIGATE_BINDINGS,
52 END_KEYS_BINDING
54 static DLG_KEYS_BINDING binding2[] = {
55 INPUTSTR_BINDINGS,
56 HELPKEY_BINDINGS,
57 ENTERKEY_BINDINGS,
58 NAVIGATE_BINDINGS,
59 END_KEYS_BINDING
61 /* *INDENT-ON* */
63 #ifdef KEY_RESIZE
64 int old_height = height;
65 int old_width = width;
66 #endif
67 int xorg, yorg;
68 int x, y, box_y, box_x, box_width;
69 int show_buttons;
70 int col_offset = 0;
71 int chr_offset = 0;
72 int key, fkey, code;
73 int result = DLG_EXIT_UNKNOWN;
74 int state;
75 int first;
76 int edited;
77 char *input;
78 WINDOW *dialog;
79 WINDOW *editor;
80 char *prompt = dlg_strclone(cprompt);
81 const char **buttons = dlg_ok_labels();
83 dlg_does_output();
85 dlg_tab_correct_str(prompt);
87 /* Set up the initial value */
88 input = dlg_set_result(init);
89 edited = FALSE;
91 #ifdef KEY_RESIZE
92 retry:
93 #endif
94 show_buttons = TRUE;
95 state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
96 first = (state == sTEXT);
97 key = fkey = 0;
99 if (init != NULL) {
100 dlg_auto_size(title, prompt, &height, &width, 5,
101 MIN(MAX(dlg_count_columns(init) + 7, 26),
102 SCOLS - (dialog_vars.begin_set ?
103 dialog_vars.begin_x : 0)));
104 chr_offset = (int) strlen(init);
105 } else {
106 dlg_auto_size(title, prompt, &height, &width, 5, 26);
108 dlg_button_layout(buttons, &width);
109 dlg_print_size(height, width);
110 dlg_ctl_size(height, width);
112 xorg = dlg_box_x_ordinate(width);
113 yorg = dlg_box_y_ordinate(height);
115 dialog = dlg_new_window(height, width, yorg, xorg);
116 dlg_register_window(dialog, "inputbox", binding);
117 dlg_register_buttons(dialog, "inputbox", buttons);
119 dlg_mouse_setbase(xorg, yorg);
121 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
122 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
123 dlg_draw_title(dialog, title);
125 (void) wattrset(dialog, dialog_attr);
126 dlg_draw_helpline(dialog, FALSE);
127 dlg_print_autowrap(dialog, prompt, height, width);
129 /* Draw the input field box */
130 box_width = width - 6;
131 getyx(dialog, y, x);
132 (void) x;
133 box_y = y + 2;
134 box_x = (width - box_width) / 2;
135 dlg_mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
136 dlg_draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
137 border_attr, border2_attr);
139 /* Make a window for the input-field, to associate bindings */
140 editor = dlg_sub_window(dialog, 1, box_width, yorg + box_y, xorg + box_x);
141 dlg_register_window(editor, "inputbox2", binding2);
143 if (*input != '\0') {
144 dlg_show_string(editor, input, chr_offset, inputbox_attr,
145 0, 0, box_width, password, first);
146 wsyncup(editor);
147 wcursyncup(editor);
149 while (result == DLG_EXIT_UNKNOWN) {
150 int edit = 0;
153 * The last field drawn determines where the cursor is shown:
155 if (show_buttons) {
156 show_buttons = FALSE;
157 col_offset = dlg_edit_offset(input, chr_offset, box_width);
158 (void) wmove(dialog, box_y, box_x + col_offset);
159 dlg_draw_buttons(dialog, height - 2, 0, buttons, state, FALSE, width);
162 if (!first) {
163 if (*input != '\0' && !edited) {
164 dlg_show_string(editor, input, chr_offset, inputbox_attr,
165 0, 0, box_width, password, first);
166 wmove(editor, 0, chr_offset);
167 wsyncup(editor);
168 wcursyncup(editor);
170 key = dlg_mouse_wgetch((state == sTEXT) ? editor : dialog, &fkey);
171 if (dlg_result_key(key, fkey, &result))
172 break;
176 * Handle mouse clicks first, since we want to know if this is a button,
177 * or something that dlg_edit_string() should handle.
179 if (fkey
180 && is_DLGK_MOUSE(key)
181 && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
182 result = code;
183 continue;
186 if (state == sTEXT) { /* Input box selected */
187 edit = dlg_edit_string(input, &chr_offset, key, fkey, first);
189 if (edit) {
190 dlg_show_string(editor, input, chr_offset, inputbox_attr,
191 0, 0, box_width, password, first);
192 wsyncup(editor);
193 wcursyncup(editor);
194 first = FALSE;
195 edited = TRUE;
196 continue;
197 } else if (first) {
198 first = FALSE;
199 continue;
203 /* handle non-functionkeys */
204 if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
205 dlg_del_window(dialog);
206 result = dlg_ok_buttoncode(code);
207 continue;
210 /* handle functionkeys */
211 if (fkey) {
212 switch (key) {
213 case DLGK_MOUSE('i'): /* mouse enter events */
214 state = 0;
215 /* FALLTHRU */
216 case DLGK_FIELD_PREV:
217 show_buttons = TRUE;
218 state = dlg_prev_ok_buttonindex(state, sTEXT);
219 break;
220 case DLGK_FIELD_NEXT:
221 show_buttons = TRUE;
222 state = dlg_next_ok_buttonindex(state, sTEXT);
223 break;
224 case ' ': /* FIXME: conflict with inputstr.c */
225 case DLGK_ENTER:
226 dlg_del_window(dialog);
227 result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
228 break;
229 #ifdef KEY_RESIZE
230 case KEY_RESIZE:
231 /* reset data */
232 height = old_height;
233 width = old_width;
234 /* repaint */
235 dlg_clear();
236 dlg_del_window(dialog);
237 refresh();
238 dlg_mouse_free_regions();
239 goto retry;
240 #endif
241 default:
242 beep();
243 break;
245 } else {
246 beep();
250 dlg_unregister_window(editor);
251 dlg_del_window(dialog);
252 dlg_mouse_free_regions();
253 free(prompt);
254 return result;