xref: /freebsd/contrib/dialog/inputbox.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*
2  *  $Id: inputbox.c,v 1.64 2010/01/19 01:03:39 tom Exp $
3  *
4  * inputbox.c -- implements the input box
5  *
6  * Copyright 2000-2009,2010 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 #define sTEXT -1
31 
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 )
39 
40 /*
41  * Display a dialog box for entering a string
42  */
43 int
44 dialog_inputbox(const char *title, const char *cprompt, int height, int width,
45 		const char *init, const int password)
46 {
47     /* *INDENT-OFF* */
48     static DLG_KEYS_BINDING binding[] = {
49 	ENTERKEY_BINDINGS,
50 	NAVIGATE_BINDINGS,
51 	END_KEYS_BINDING
52     };
53     static DLG_KEYS_BINDING binding2[] = {
54 	INPUTSTR_BINDINGS,
55 	ENTERKEY_BINDINGS,
56 	NAVIGATE_BINDINGS,
57 	END_KEYS_BINDING
58     };
59     /* *INDENT-ON* */
60 
61 #ifdef KEY_RESIZE
62     int old_height = height;
63     int old_width = width;
64 #endif
65     int xorg, yorg;
66     int x, y, box_y, box_x, box_width;
67     int show_buttons;
68     int col_offset = 0;
69     int chr_offset = 0;
70     int key, fkey, code;
71     int result = DLG_EXIT_UNKNOWN;
72     int state;
73     int first;
74     char *input;
75     WINDOW *dialog;
76     WINDOW *editor;
77     char *prompt = dlg_strclone(cprompt);
78     const char **buttons = dlg_ok_labels();
79 
80     dlg_does_output();
81 
82     dlg_tab_correct_str(prompt);
83 
84     /* Set up the initial value */
85     input = dlg_set_result(init);
86 
87 #ifdef KEY_RESIZE
88   retry:
89 #endif
90     show_buttons = TRUE;
91     state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
92     first = (state == sTEXT);
93     key = fkey = 0;
94 
95     if (init != NULL) {
96 	dlg_auto_size(title, prompt, &height, &width, 5,
97 		      MIN(MAX(dlg_count_columns(init) + 7, 26),
98 			  SCOLS - (dialog_vars.begin_set ?
99 				   dialog_vars.begin_x : 0)));
100 	chr_offset = (int) strlen(init);
101     } else {
102 	dlg_auto_size(title, prompt, &height, &width, 5, 26);
103     }
104     dlg_button_layout(buttons, &width);
105     dlg_print_size(height, width);
106     dlg_ctl_size(height, width);
107 
108     xorg = dlg_box_x_ordinate(width);
109     yorg = dlg_box_y_ordinate(height);
110 
111     dialog = dlg_new_window(height, width, yorg, xorg);
112     dlg_register_window(dialog, "inputbox", binding);
113     dlg_register_buttons(dialog, "inputbox", buttons);
114 
115     dlg_mouse_setbase(xorg, yorg);
116 
117     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
118     dlg_draw_bottom_box(dialog);
119     dlg_draw_title(dialog, title);
120 
121     wattrset(dialog, dialog_attr);
122     dlg_print_autowrap(dialog, prompt, height, width);
123 
124     /* Draw the input field box */
125     box_width = width - 6;
126     getyx(dialog, y, x);
127     box_y = y + 2;
128     box_x = (width - box_width) / 2;
129     dlg_mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
130     dlg_draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
131 		 border_attr, dialog_attr);
132 
133     /* Make a window for the input-field, to associate bindings */
134     editor = dlg_sub_window(dialog, 1, box_width, yorg + box_y, xorg + box_x);
135     dlg_register_window(editor, "inputbox", binding2);
136 
137     while (result == DLG_EXIT_UNKNOWN) {
138 	int edit = 0;
139 
140 	/*
141 	 * The last field drawn determines where the cursor is shown:
142 	 */
143 	if (show_buttons) {
144 	    show_buttons = FALSE;
145 	    col_offset = dlg_edit_offset(input, chr_offset, box_width);
146 	    (void) wmove(dialog, box_y, box_x + col_offset);
147 	    dlg_draw_buttons(dialog, height - 2, 0, buttons, state, FALSE, width);
148 	}
149 
150 	if (!first) {
151 	    key = dlg_mouse_wgetch((state == sTEXT) ? editor : dialog, &fkey);
152 	    if (dlg_result_key(key, fkey, &result))
153 		break;
154 	}
155 
156 	/*
157 	 * Handle mouse clicks first, since we want to know if this is a button,
158 	 * or something that dlg_edit_string() should handle.
159 	 */
160 	if (fkey
161 	    && is_DLGK_MOUSE(key)
162 	    && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
163 	    result = code;
164 	    continue;
165 	}
166 
167 	if (state == sTEXT) {	/* Input box selected */
168 	    edit = dlg_edit_string(input, &chr_offset, key, fkey, first);
169 
170 	    if (edit) {
171 		dlg_show_string(dialog, input, chr_offset, inputbox_attr,
172 				box_y, box_x, box_width, password, first);
173 		first = FALSE;
174 		continue;
175 	    } else if (first) {
176 		first = FALSE;
177 		continue;
178 	    }
179 	}
180 
181 	/* handle non-functionkeys */
182 	if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
183 	    dlg_del_window(dialog);
184 	    result = dlg_ok_buttoncode(code);
185 	    continue;
186 	}
187 
188 	/* handle functionkeys */
189 	if (fkey) {
190 	    switch (key) {
191 	    case DLGK_MOUSE('i'):	/* mouse enter events */
192 		state = 0;
193 		/* FALLTHRU */
194 	    case DLGK_FIELD_PREV:
195 		show_buttons = TRUE;
196 		state = dlg_prev_ok_buttonindex(state, sTEXT);
197 		break;
198 	    case DLGK_FIELD_NEXT:
199 		show_buttons = TRUE;
200 		state = dlg_next_ok_buttonindex(state, sTEXT);
201 		break;
202 	    case ' ':		/* FIXME: conflict with inputstr.c */
203 	    case DLGK_ENTER:
204 		dlg_del_window(dialog);
205 		result = (state >= 0) ? dlg_ok_buttoncode(state) : DLG_EXIT_OK;
206 		break;
207 #ifdef KEY_RESIZE
208 	    case KEY_RESIZE:
209 		/* reset data */
210 		height = old_height;
211 		width = old_width;
212 		/* repaint */
213 		dlg_clear();
214 		dlg_del_window(dialog);
215 		refresh();
216 		dlg_mouse_free_regions();
217 		goto retry;
218 #endif
219 	    default:
220 		beep();
221 		break;
222 	    }
223 	} else {
224 	    beep();
225 	}
226     }
227 
228     dlg_unregister_window(editor);
229     dlg_del_window(dialog);
230     dlg_mouse_free_regions();
231     free(prompt);
232     return result;
233 }
234