14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin * $Id: inputbox.c,v 1.93 2021/01/17 16:36:37 tom Exp $
34c8945a0SNathan Whitehorn *
44c8945a0SNathan Whitehorn * inputbox.c -- implements the input box
54c8945a0SNathan Whitehorn *
6*a96ef450SBaptiste Daroussin * Copyright 2000-2020,2021 Thomas E. Dickey
74c8945a0SNathan Whitehorn *
84c8945a0SNathan Whitehorn * This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn * it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn * as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn *
124c8945a0SNathan Whitehorn * This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn * WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
154c8945a0SNathan Whitehorn * Lesser General Public License for more details.
164c8945a0SNathan Whitehorn *
174c8945a0SNathan Whitehorn * You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn * License along with this program; if not, write to
194c8945a0SNathan Whitehorn * Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn * 51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn * Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn *
234c8945a0SNathan Whitehorn * An earlier version of this program lists as authors:
244c8945a0SNathan Whitehorn * Savio Lam (lam836@cs.cuhk.hk)
254c8945a0SNathan Whitehorn */
264c8945a0SNathan Whitehorn
27*a96ef450SBaptiste Daroussin #include <dlg_internals.h>
284c8945a0SNathan Whitehorn #include <dlg_keys.h>
294c8945a0SNathan Whitehorn
304c8945a0SNathan Whitehorn #define sTEXT -1
314c8945a0SNathan Whitehorn
324c8945a0SNathan Whitehorn #define NAVIGATE_BINDINGS \
334c8945a0SNathan Whitehorn DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_DOWN ), \
344c8945a0SNathan Whitehorn DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
354c8945a0SNathan Whitehorn DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
364c8945a0SNathan Whitehorn DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
374c8945a0SNathan Whitehorn DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ), \
384c8945a0SNathan Whitehorn DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_UP )
394c8945a0SNathan Whitehorn
40*a96ef450SBaptiste Daroussin #define BTN_HIGH 1
41*a96ef450SBaptiste Daroussin #define HDR_HIGH 1
42*a96ef450SBaptiste Daroussin #define MIN_HIGH (HDR_HIGH + (MARGIN * 2 + 1) + (BTN_HIGH + MARGIN * 2))
43*a96ef450SBaptiste Daroussin #define MIN_WIDE 26
44*a96ef450SBaptiste Daroussin
454c8945a0SNathan Whitehorn /*
464c8945a0SNathan Whitehorn * Display a dialog box for entering a string
474c8945a0SNathan Whitehorn */
484c8945a0SNathan Whitehorn int
dialog_inputbox(const char * title,const char * cprompt,int height,int width,const char * init,const int password)494c8945a0SNathan Whitehorn dialog_inputbox(const char *title, const char *cprompt, int height, int width,
504c8945a0SNathan Whitehorn const char *init, const int password)
514c8945a0SNathan Whitehorn {
524c8945a0SNathan Whitehorn /* *INDENT-OFF* */
534c8945a0SNathan Whitehorn static DLG_KEYS_BINDING binding[] = {
54682c9e0fSNathan Whitehorn HELPKEY_BINDINGS,
554c8945a0SNathan Whitehorn ENTERKEY_BINDINGS,
564c8945a0SNathan Whitehorn NAVIGATE_BINDINGS,
57f4f33ea0SBaptiste Daroussin TOGGLEKEY_BINDINGS,
584c8945a0SNathan Whitehorn END_KEYS_BINDING
594c8945a0SNathan Whitehorn };
604c8945a0SNathan Whitehorn static DLG_KEYS_BINDING binding2[] = {
614c8945a0SNathan Whitehorn INPUTSTR_BINDINGS,
62682c9e0fSNathan Whitehorn HELPKEY_BINDINGS,
634c8945a0SNathan Whitehorn ENTERKEY_BINDINGS,
644c8945a0SNathan Whitehorn NAVIGATE_BINDINGS,
65f4f33ea0SBaptiste Daroussin /* no TOGGLEKEY_BINDINGS, since that includes space... */
664c8945a0SNathan Whitehorn END_KEYS_BINDING
674c8945a0SNathan Whitehorn };
684c8945a0SNathan Whitehorn /* *INDENT-ON* */
694c8945a0SNathan Whitehorn
704c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
714c8945a0SNathan Whitehorn int old_height = height;
724c8945a0SNathan Whitehorn int old_width = width;
734c8945a0SNathan Whitehorn #endif
744c8945a0SNathan Whitehorn int xorg, yorg;
754c8945a0SNathan Whitehorn int x, y, box_y, box_x, box_width;
764c8945a0SNathan Whitehorn int show_buttons;
774c8945a0SNathan Whitehorn int col_offset = 0;
784c8945a0SNathan Whitehorn int chr_offset = 0;
794c8945a0SNathan Whitehorn int key, fkey, code;
804c8945a0SNathan Whitehorn int result = DLG_EXIT_UNKNOWN;
814c8945a0SNathan Whitehorn int state;
82f4f33ea0SBaptiste Daroussin bool first;
83f4f33ea0SBaptiste Daroussin bool edited;
844c8945a0SNathan Whitehorn char *input;
854c8945a0SNathan Whitehorn WINDOW *dialog;
864c8945a0SNathan Whitehorn WINDOW *editor;
874c8945a0SNathan Whitehorn char *prompt = dlg_strclone(cprompt);
884c8945a0SNathan Whitehorn const char **buttons = dlg_ok_labels();
894c8945a0SNathan Whitehorn
904c8945a0SNathan Whitehorn dlg_does_output();
914c8945a0SNathan Whitehorn
92f4f33ea0SBaptiste Daroussin DLG_TRACE(("# inputbox args:\n"));
93f4f33ea0SBaptiste Daroussin DLG_TRACE2S("title", title);
94f4f33ea0SBaptiste Daroussin DLG_TRACE2S("message", cprompt);
95f4f33ea0SBaptiste Daroussin DLG_TRACE2N("height", height);
96f4f33ea0SBaptiste Daroussin DLG_TRACE2N("width", width);
97f4f33ea0SBaptiste Daroussin DLG_TRACE2S("init", init);
98f4f33ea0SBaptiste Daroussin DLG_TRACE2N("password", password);
99f4f33ea0SBaptiste Daroussin
1004c8945a0SNathan Whitehorn dlg_tab_correct_str(prompt);
1014c8945a0SNathan Whitehorn
1024c8945a0SNathan Whitehorn /* Set up the initial value */
1034c8945a0SNathan Whitehorn input = dlg_set_result(init);
1042a3e3873SBaptiste Daroussin edited = FALSE;
1054c8945a0SNathan Whitehorn
1064c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
1074c8945a0SNathan Whitehorn retry:
1084c8945a0SNathan Whitehorn #endif
1094c8945a0SNathan Whitehorn show_buttons = TRUE;
1102a3e3873SBaptiste Daroussin state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
1114c8945a0SNathan Whitehorn first = (state == sTEXT);
1124c8945a0SNathan Whitehorn key = fkey = 0;
1134c8945a0SNathan Whitehorn
1144c8945a0SNathan Whitehorn if (init != NULL) {
115*a96ef450SBaptiste Daroussin dlg_auto_size(title, prompt, &height, &width, MIN_HIGH,
116*a96ef450SBaptiste Daroussin MIN(MAX(dlg_count_columns(init) + 7, MIN_WIDE),
1174c8945a0SNathan Whitehorn SCOLS - (dialog_vars.begin_set ?
1184c8945a0SNathan Whitehorn dialog_vars.begin_x : 0)));
1194c8945a0SNathan Whitehorn chr_offset = (int) strlen(init);
1204c8945a0SNathan Whitehorn } else {
121*a96ef450SBaptiste Daroussin dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
1224c8945a0SNathan Whitehorn }
1234c8945a0SNathan Whitehorn dlg_button_layout(buttons, &width);
1244c8945a0SNathan Whitehorn dlg_print_size(height, width);
1254c8945a0SNathan Whitehorn dlg_ctl_size(height, width);
1264c8945a0SNathan Whitehorn
1274c8945a0SNathan Whitehorn xorg = dlg_box_x_ordinate(width);
1284c8945a0SNathan Whitehorn yorg = dlg_box_y_ordinate(height);
1294c8945a0SNathan Whitehorn
1304c8945a0SNathan Whitehorn dialog = dlg_new_window(height, width, yorg, xorg);
1314c8945a0SNathan Whitehorn dlg_register_window(dialog, "inputbox", binding);
1324c8945a0SNathan Whitehorn dlg_register_buttons(dialog, "inputbox", buttons);
1334c8945a0SNathan Whitehorn
1344c8945a0SNathan Whitehorn dlg_mouse_setbase(xorg, yorg);
1354c8945a0SNathan Whitehorn
1362a3e3873SBaptiste Daroussin dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
1372a3e3873SBaptiste Daroussin dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1384c8945a0SNathan Whitehorn dlg_draw_title(dialog, title);
1394c8945a0SNathan Whitehorn
140f4f33ea0SBaptiste Daroussin dlg_attrset(dialog, dialog_attr);
141682c9e0fSNathan Whitehorn dlg_draw_helpline(dialog, FALSE);
1424c8945a0SNathan Whitehorn dlg_print_autowrap(dialog, prompt, height, width);
1434c8945a0SNathan Whitehorn
1444c8945a0SNathan Whitehorn /* Draw the input field box */
1454c8945a0SNathan Whitehorn box_width = width - 6;
1464c8945a0SNathan Whitehorn getyx(dialog, y, x);
1472a3e3873SBaptiste Daroussin (void) x;
1484c8945a0SNathan Whitehorn box_y = y + 2;
1494c8945a0SNathan Whitehorn box_x = (width - box_width) / 2;
1504c8945a0SNathan Whitehorn dlg_mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
1514c8945a0SNathan Whitehorn dlg_draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
1522a3e3873SBaptiste Daroussin border_attr, border2_attr);
1534c8945a0SNathan Whitehorn
1544c8945a0SNathan Whitehorn /* Make a window for the input-field, to associate bindings */
1554c8945a0SNathan Whitehorn editor = dlg_sub_window(dialog, 1, box_width, yorg + box_y, xorg + box_x);
1562a3e3873SBaptiste Daroussin dlg_register_window(editor, "inputbox2", binding2);
1574c8945a0SNathan Whitehorn
1582a3e3873SBaptiste Daroussin if (*input != '\0') {
1592a3e3873SBaptiste Daroussin dlg_show_string(editor, input, chr_offset, inputbox_attr,
160f4f33ea0SBaptiste Daroussin 0, 0, box_width, (bool) (password != 0), first);
1612a3e3873SBaptiste Daroussin wsyncup(editor);
1622a3e3873SBaptiste Daroussin wcursyncup(editor);
1632a3e3873SBaptiste Daroussin }
1644c8945a0SNathan Whitehorn
165*a96ef450SBaptiste Daroussin while (result == DLG_EXIT_UNKNOWN) {
1664c8945a0SNathan Whitehorn /*
1674c8945a0SNathan Whitehorn * The last field drawn determines where the cursor is shown:
1684c8945a0SNathan Whitehorn */
1694c8945a0SNathan Whitehorn if (show_buttons) {
1704c8945a0SNathan Whitehorn show_buttons = FALSE;
1714c8945a0SNathan Whitehorn col_offset = dlg_edit_offset(input, chr_offset, box_width);
1724c8945a0SNathan Whitehorn (void) wmove(dialog, box_y, box_x + col_offset);
1734c8945a0SNathan Whitehorn dlg_draw_buttons(dialog, height - 2, 0, buttons, state, FALSE, width);
1744c8945a0SNathan Whitehorn }
1754c8945a0SNathan Whitehorn
1764c8945a0SNathan Whitehorn if (!first) {
1772a3e3873SBaptiste Daroussin if (*input != '\0' && !edited) {
1782a3e3873SBaptiste Daroussin dlg_show_string(editor, input, chr_offset, inputbox_attr,
179f4f33ea0SBaptiste Daroussin 0, 0, box_width, (bool) (password != 0), first);
1802a3e3873SBaptiste Daroussin wmove(editor, 0, chr_offset);
1812a3e3873SBaptiste Daroussin wsyncup(editor);
1822a3e3873SBaptiste Daroussin wcursyncup(editor);
1832a3e3873SBaptiste Daroussin }
1844c8945a0SNathan Whitehorn key = dlg_mouse_wgetch((state == sTEXT) ? editor : dialog, &fkey);
185*a96ef450SBaptiste Daroussin if (dlg_result_key(key, fkey, &result)) {
186*a96ef450SBaptiste Daroussin if (!dlg_button_key(result, &code, &key, &fkey))
1874c8945a0SNathan Whitehorn break;
1884c8945a0SNathan Whitehorn }
189*a96ef450SBaptiste Daroussin }
1904c8945a0SNathan Whitehorn
1914c8945a0SNathan Whitehorn /*
1924c8945a0SNathan Whitehorn * Handle mouse clicks first, since we want to know if this is a button,
1934c8945a0SNathan Whitehorn * or something that dlg_edit_string() should handle.
1944c8945a0SNathan Whitehorn */
1954c8945a0SNathan Whitehorn if (fkey
1964c8945a0SNathan Whitehorn && is_DLGK_MOUSE(key)
1974c8945a0SNathan Whitehorn && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
1984c8945a0SNathan Whitehorn result = code;
1994c8945a0SNathan Whitehorn continue;
2004c8945a0SNathan Whitehorn }
2014c8945a0SNathan Whitehorn
2024c8945a0SNathan Whitehorn if (state == sTEXT) { /* Input box selected */
203*a96ef450SBaptiste Daroussin int edit = dlg_edit_string(input, &chr_offset, key, fkey, first);
2044c8945a0SNathan Whitehorn
2054c8945a0SNathan Whitehorn if (edit) {
2062a3e3873SBaptiste Daroussin dlg_show_string(editor, input, chr_offset, inputbox_attr,
207f4f33ea0SBaptiste Daroussin 0, 0, box_width, (bool) (password != 0), first);
2082a3e3873SBaptiste Daroussin wsyncup(editor);
2092a3e3873SBaptiste Daroussin wcursyncup(editor);
2104c8945a0SNathan Whitehorn first = FALSE;
2112a3e3873SBaptiste Daroussin edited = TRUE;
2124c8945a0SNathan Whitehorn continue;
2134c8945a0SNathan Whitehorn } else if (first) {
2144c8945a0SNathan Whitehorn first = FALSE;
2154c8945a0SNathan Whitehorn continue;
2164c8945a0SNathan Whitehorn }
2174c8945a0SNathan Whitehorn }
2184c8945a0SNathan Whitehorn
2194c8945a0SNathan Whitehorn /* handle non-functionkeys */
2204c8945a0SNathan Whitehorn if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
2214c8945a0SNathan Whitehorn dlg_del_window(dialog);
2224c8945a0SNathan Whitehorn result = dlg_ok_buttoncode(code);
2234c8945a0SNathan Whitehorn continue;
2244c8945a0SNathan Whitehorn }
2254c8945a0SNathan Whitehorn
2264c8945a0SNathan Whitehorn /* handle functionkeys */
2274c8945a0SNathan Whitehorn if (fkey) {
2284c8945a0SNathan Whitehorn switch (key) {
2294c8945a0SNathan Whitehorn case DLGK_MOUSE('i'): /* mouse enter events */
2304c8945a0SNathan Whitehorn state = 0;
2314c8945a0SNathan Whitehorn /* FALLTHRU */
2324c8945a0SNathan Whitehorn case DLGK_FIELD_PREV:
2334c8945a0SNathan Whitehorn show_buttons = TRUE;
2344c8945a0SNathan Whitehorn state = dlg_prev_ok_buttonindex(state, sTEXT);
2354c8945a0SNathan Whitehorn break;
2364c8945a0SNathan Whitehorn case DLGK_FIELD_NEXT:
2374c8945a0SNathan Whitehorn show_buttons = TRUE;
2384c8945a0SNathan Whitehorn state = dlg_next_ok_buttonindex(state, sTEXT);
2394c8945a0SNathan Whitehorn break;
240f4f33ea0SBaptiste Daroussin case DLGK_TOGGLE:
2414c8945a0SNathan Whitehorn case DLGK_ENTER:
2424c8945a0SNathan Whitehorn dlg_del_window(dialog);
243682c9e0fSNathan Whitehorn result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
2444c8945a0SNathan Whitehorn break;
245*a96ef450SBaptiste Daroussin case DLGK_LEAVE:
246*a96ef450SBaptiste Daroussin if (state >= 0)
247*a96ef450SBaptiste Daroussin result = dlg_ok_buttoncode(state);
248*a96ef450SBaptiste Daroussin break;
2494c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
2504c8945a0SNathan Whitehorn case KEY_RESIZE:
251f4f33ea0SBaptiste Daroussin dlg_will_resize(dialog);
2524c8945a0SNathan Whitehorn /* reset data */
2534c8945a0SNathan Whitehorn height = old_height;
2544c8945a0SNathan Whitehorn width = old_width;
2554c8945a0SNathan Whitehorn /* repaint */
256*a96ef450SBaptiste Daroussin _dlg_resize_cleanup(dialog);
2574c8945a0SNathan Whitehorn goto retry;
2584c8945a0SNathan Whitehorn #endif
2594c8945a0SNathan Whitehorn default:
2604c8945a0SNathan Whitehorn beep();
2614c8945a0SNathan Whitehorn break;
2624c8945a0SNathan Whitehorn }
263*a96ef450SBaptiste Daroussin } else if (key > 0) {
2644c8945a0SNathan Whitehorn beep();
2654c8945a0SNathan Whitehorn }
2664c8945a0SNathan Whitehorn }
267*a96ef450SBaptiste Daroussin AddLastKey();
2684c8945a0SNathan Whitehorn
2694c8945a0SNathan Whitehorn dlg_unregister_window(editor);
2704c8945a0SNathan Whitehorn dlg_del_window(dialog);
2714c8945a0SNathan Whitehorn dlg_mouse_free_regions();
2724c8945a0SNathan Whitehorn free(prompt);
2734c8945a0SNathan Whitehorn return result;
2744c8945a0SNathan Whitehorn }
275