2 * $Id: inputbox.c,v 1.95 2021/05/30 18:41:54 tom Exp $
4 * inputbox.c -- implements the input box
6 * Copyright 2000-2020,2021 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 <dlg_internals.h>
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 )
42 #define MIN_HIGH (HDR_HIGH + 1 + (BTN_HIGH + MARGIN * 2))
46 * Display a dialog box for entering a string
49 dialog_inputbox(const char *title
, const char *cprompt
, int height
, int width
,
50 const char *init
, const int password
)
53 static DLG_KEYS_BINDING binding
[] = {
60 static DLG_KEYS_BINDING binding2
[] = {
65 /* no TOGGLEKEY_BINDINGS, since that includes space... */
71 int old_height
= height
;
72 int old_width
= width
;
75 int x
, y
, box_y
, box_x
, box_width
;
80 int result
= DLG_EXIT_UNKNOWN
;
88 char *prompt
= dlg_strclone(cprompt
);
89 const char **buttons
= dlg_ok_labels();
93 DLG_TRACE(("# inputbox args:\n"));
94 DLG_TRACE2S("title", title
);
95 DLG_TRACE2S("message", cprompt
);
96 DLG_TRACE2N("height", height
);
97 DLG_TRACE2N("width", width
);
98 DLG_TRACE2S("init", init
);
99 DLG_TRACE2N("password", password
);
101 dlg_tab_correct_str(prompt
);
103 /* Set up the initial value */
104 input
= dlg_set_result(init
);
111 state
= dialog_vars
.default_button
>= 0 ? dlg_default_button() : sTEXT
;
112 first
= (state
== sTEXT
);
115 partauto
= ((height
== 0) ^ (width
== 0));
117 dlg_auto_size(title
, prompt
,
120 MIN(MAX(dlg_count_columns(init
) + 7, MIN_WIDE
),
121 SCOLS
- (dialog_vars
.begin_set
?
122 dialog_vars
.begin_x
: 0)));
123 chr_offset
= (int) strlen(init
);
125 dlg_auto_size(title
, prompt
,
130 dlg_button_layout(buttons
, &width
);
131 dlg_print_size(height
, width
);
132 dlg_ctl_size(height
, width
);
134 xorg
= dlg_box_x_ordinate(width
);
135 yorg
= dlg_box_y_ordinate(height
);
137 dialog
= dlg_new_window(height
, width
, yorg
, xorg
);
138 dlg_register_window(dialog
, "inputbox", binding
);
139 dlg_register_buttons(dialog
, "inputbox", buttons
);
141 dlg_mouse_setbase(xorg
, yorg
);
143 dlg_draw_box2(dialog
, 0, 0, height
, width
, dialog_attr
, border_attr
, border2_attr
);
144 dlg_draw_bottom_box2(dialog
, border_attr
, border2_attr
, dialog_attr
);
145 dlg_draw_title(dialog
, title
);
147 dlg_attrset(dialog
, dialog_attr
);
148 dlg_draw_helpline(dialog
, FALSE
);
149 dlg_print_autowrap(dialog
, prompt
, height
, width
);
151 /* Draw the input field box */
152 box_width
= width
- 6;
156 box_x
= (width
- box_width
) / 2;
157 dlg_mouse_mkregion(y
+ 1, box_x
- 1, 3, box_width
+ 2, 'i');
158 dlg_draw_box(dialog
, y
+ 1, box_x
- 1, 3, box_width
+ 2,
159 border_attr
, border2_attr
);
161 /* Make a window for the input-field, to associate bindings */
162 editor
= dlg_sub_window(dialog
, 1, box_width
, yorg
+ box_y
, xorg
+ box_x
);
163 dlg_register_window(editor
, "inputbox2", binding2
);
165 if (*input
!= '\0') {
166 dlg_show_string(editor
, input
, chr_offset
, inputbox_attr
,
167 0, 0, box_width
, (bool) (password
!= 0), first
);
172 while (result
== DLG_EXIT_UNKNOWN
) {
174 * The last field drawn determines where the cursor is shown:
177 show_buttons
= FALSE
;
178 col_offset
= dlg_edit_offset(input
, chr_offset
, box_width
);
179 (void) wmove(dialog
, box_y
, box_x
+ col_offset
);
180 dlg_draw_buttons(dialog
, height
- 2, 0, buttons
, state
, FALSE
, width
);
184 if (*input
!= '\0' && !edited
) {
185 dlg_show_string(editor
, input
, chr_offset
, inputbox_attr
,
186 0, 0, box_width
, (bool) (password
!= 0), first
);
187 wmove(editor
, 0, chr_offset
);
191 key
= dlg_mouse_wgetch((state
== sTEXT
) ? editor
: dialog
, &fkey
);
192 if (dlg_result_key(key
, fkey
, &result
)) {
193 if (!dlg_button_key(result
, &code
, &key
, &fkey
))
199 * Handle mouse clicks first, since we want to know if this is a button,
200 * or something that dlg_edit_string() should handle.
203 && is_DLGK_MOUSE(key
)
204 && (code
= dlg_ok_buttoncode(key
- M_EVENT
)) >= 0) {
209 if (state
== sTEXT
) { /* Input box selected */
210 int edit
= dlg_edit_string(input
, &chr_offset
, key
, fkey
, first
);
213 dlg_show_string(editor
, input
, chr_offset
, inputbox_attr
,
214 0, 0, box_width
, (bool) (password
!= 0), first
);
226 /* handle non-functionkeys */
227 if (!fkey
&& (code
= dlg_char_to_button(key
, buttons
)) >= 0) {
228 dlg_del_window(dialog
);
229 result
= dlg_ok_buttoncode(code
);
233 /* handle functionkeys */
236 case DLGK_MOUSE('i'): /* mouse enter events */
239 case DLGK_FIELD_PREV
:
241 state
= dlg_prev_ok_buttonindex(state
, sTEXT
);
243 case DLGK_FIELD_NEXT
:
245 state
= dlg_next_ok_buttonindex(state
, sTEXT
);
249 dlg_del_window(dialog
);
250 result
= (state
>= 0) ? dlg_enter_buttoncode(state
) : DLG_EXIT_OK
;
254 result
= dlg_ok_buttoncode(state
);
258 dlg_will_resize(dialog
);
263 _dlg_resize_cleanup(dialog
);
270 } else if (key
> 0) {
276 dlg_unregister_window(editor
);
277 dlg_del_window(dialog
);
278 dlg_mouse_free_regions();