xref: /dflybsd-src/contrib/dialog/inputbox.c (revision b2dabe2e739bd72461a68ac543307c2dedfb048c)
15382d832SPeter Avalos /*
2*a8e38dc0SAntonio Huete Jimenez  *  $Id: inputbox.c,v 1.95 2021/05/30 18:41:54 tom Exp $
35382d832SPeter Avalos  *
45382d832SPeter Avalos  *  inputbox.c -- implements the input box
55382d832SPeter Avalos  *
6*a8e38dc0SAntonio Huete Jimenez  *  Copyright 2000-2020,2021 Thomas E. Dickey
75382d832SPeter Avalos  *
85382d832SPeter Avalos  *  This program is free software; you can redistribute it and/or modify
95382d832SPeter Avalos  *  it under the terms of the GNU Lesser General Public License, version 2.1
105382d832SPeter Avalos  *  as published by the Free Software Foundation.
115382d832SPeter Avalos  *
125382d832SPeter Avalos  *  This program is distributed in the hope that it will be useful, but
135382d832SPeter Avalos  *  WITHOUT ANY WARRANTY; without even the implied warranty of
145382d832SPeter Avalos  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
155382d832SPeter Avalos  *  Lesser General Public License for more details.
165382d832SPeter Avalos  *
175382d832SPeter Avalos  *  You should have received a copy of the GNU Lesser General Public
185382d832SPeter Avalos  *  License along with this program; if not, write to
195382d832SPeter Avalos  *	Free Software Foundation, Inc.
205382d832SPeter Avalos  *	51 Franklin St., Fifth Floor
215382d832SPeter Avalos  *	Boston, MA 02110, USA.
225382d832SPeter Avalos  *
235382d832SPeter Avalos  *  An earlier version of this program lists as authors:
245382d832SPeter Avalos  *	Savio Lam (lam836@cs.cuhk.hk)
255382d832SPeter Avalos  */
265382d832SPeter Avalos 
275940c9abSDaniel Fojt #include <dlg_internals.h>
285382d832SPeter Avalos #include <dlg_keys.h>
295382d832SPeter Avalos 
305382d832SPeter Avalos #define sTEXT -1
315382d832SPeter Avalos 
325382d832SPeter Avalos #define NAVIGATE_BINDINGS \
335382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_DOWN ), \
345382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	KEY_RIGHT ), \
355382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_NEXT,	TAB ), \
365382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_BTAB ), \
375382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_LEFT ), \
385382d832SPeter Avalos 	DLG_KEYS_DATA( DLGK_FIELD_PREV,	KEY_UP )
395382d832SPeter Avalos 
40*a8e38dc0SAntonio Huete Jimenez #define BTN_HIGH 1
41*a8e38dc0SAntonio Huete Jimenez #define HDR_HIGH 1
42*a8e38dc0SAntonio Huete Jimenez #define MIN_HIGH (HDR_HIGH + 1 + (BTN_HIGH + MARGIN * 2))
43*a8e38dc0SAntonio Huete Jimenez #define MIN_WIDE 26
44*a8e38dc0SAntonio Huete Jimenez 
455382d832SPeter Avalos /*
465382d832SPeter Avalos  * Display a dialog box for entering a string
475382d832SPeter Avalos  */
485382d832SPeter Avalos int
dialog_inputbox(const char * title,const char * cprompt,int height,int width,const char * init,const int password)495382d832SPeter Avalos dialog_inputbox(const char *title, const char *cprompt, int height, int width,
505382d832SPeter Avalos 		const char *init, const int password)
515382d832SPeter Avalos {
525382d832SPeter Avalos     /* *INDENT-OFF* */
535382d832SPeter Avalos     static DLG_KEYS_BINDING binding[] = {
545382d832SPeter Avalos 	HELPKEY_BINDINGS,
555382d832SPeter Avalos 	ENTERKEY_BINDINGS,
565382d832SPeter Avalos 	NAVIGATE_BINDINGS,
575940c9abSDaniel Fojt 	TOGGLEKEY_BINDINGS,
585382d832SPeter Avalos 	END_KEYS_BINDING
595382d832SPeter Avalos     };
605382d832SPeter Avalos     static DLG_KEYS_BINDING binding2[] = {
615382d832SPeter Avalos 	INPUTSTR_BINDINGS,
625382d832SPeter Avalos 	HELPKEY_BINDINGS,
635382d832SPeter Avalos 	ENTERKEY_BINDINGS,
645382d832SPeter Avalos 	NAVIGATE_BINDINGS,
655940c9abSDaniel Fojt 	/* no TOGGLEKEY_BINDINGS, since that includes space... */
665382d832SPeter Avalos 	END_KEYS_BINDING
675382d832SPeter Avalos     };
685382d832SPeter Avalos     /* *INDENT-ON* */
695382d832SPeter Avalos 
705382d832SPeter Avalos #ifdef KEY_RESIZE
715382d832SPeter Avalos     int old_height = height;
725382d832SPeter Avalos     int old_width = width;
735382d832SPeter Avalos #endif
745382d832SPeter Avalos     int xorg, yorg;
755382d832SPeter Avalos     int x, y, box_y, box_x, box_width;
765382d832SPeter Avalos     int show_buttons;
775382d832SPeter Avalos     int col_offset = 0;
785382d832SPeter Avalos     int chr_offset = 0;
795382d832SPeter Avalos     int key, fkey, code;
805382d832SPeter Avalos     int result = DLG_EXIT_UNKNOWN;
815382d832SPeter Avalos     int state;
82*a8e38dc0SAntonio Huete Jimenez     int partauto;
835940c9abSDaniel Fojt     bool first;
845940c9abSDaniel Fojt     bool edited;
855382d832SPeter Avalos     char *input;
865382d832SPeter Avalos     WINDOW *dialog;
875382d832SPeter Avalos     WINDOW *editor;
885382d832SPeter Avalos     char *prompt = dlg_strclone(cprompt);
895382d832SPeter Avalos     const char **buttons = dlg_ok_labels();
905382d832SPeter Avalos 
915382d832SPeter Avalos     dlg_does_output();
925382d832SPeter Avalos 
935940c9abSDaniel Fojt     DLG_TRACE(("# inputbox args:\n"));
945940c9abSDaniel Fojt     DLG_TRACE2S("title", title);
955940c9abSDaniel Fojt     DLG_TRACE2S("message", cprompt);
965940c9abSDaniel Fojt     DLG_TRACE2N("height", height);
975940c9abSDaniel Fojt     DLG_TRACE2N("width", width);
985940c9abSDaniel Fojt     DLG_TRACE2S("init", init);
995940c9abSDaniel Fojt     DLG_TRACE2N("password", password);
1005940c9abSDaniel Fojt 
1015382d832SPeter Avalos     dlg_tab_correct_str(prompt);
1025382d832SPeter Avalos 
1035382d832SPeter Avalos     /* Set up the initial value */
1045382d832SPeter Avalos     input = dlg_set_result(init);
1055382d832SPeter Avalos     edited = FALSE;
1065382d832SPeter Avalos 
1075382d832SPeter Avalos #ifdef KEY_RESIZE
1085382d832SPeter Avalos   retry:
1095382d832SPeter Avalos #endif
1105382d832SPeter Avalos     show_buttons = TRUE;
1115382d832SPeter Avalos     state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
1125382d832SPeter Avalos     first = (state == sTEXT);
1135382d832SPeter Avalos     key = fkey = 0;
1145382d832SPeter Avalos 
115*a8e38dc0SAntonio Huete Jimenez     partauto = ((height == 0) ^ (width == 0));
1165382d832SPeter Avalos     if (init != NULL) {
117*a8e38dc0SAntonio Huete Jimenez 	dlg_auto_size(title, prompt,
118*a8e38dc0SAntonio Huete Jimenez 		      &height, &width,
119*a8e38dc0SAntonio Huete Jimenez 		      MIN_HIGH + partauto,
120*a8e38dc0SAntonio Huete Jimenez 		      MIN(MAX(dlg_count_columns(init) + 7, MIN_WIDE),
1215382d832SPeter Avalos 			  SCOLS - (dialog_vars.begin_set ?
1225382d832SPeter Avalos 				   dialog_vars.begin_x : 0)));
1235382d832SPeter Avalos 	chr_offset = (int) strlen(init);
1245382d832SPeter Avalos     } else {
125*a8e38dc0SAntonio Huete Jimenez 	dlg_auto_size(title, prompt,
126*a8e38dc0SAntonio Huete Jimenez 		      &height, &width,
127*a8e38dc0SAntonio Huete Jimenez 		      MIN_HIGH + partauto,
128*a8e38dc0SAntonio Huete Jimenez 		      MIN_WIDE);
1295382d832SPeter Avalos     }
1305382d832SPeter Avalos     dlg_button_layout(buttons, &width);
1315382d832SPeter Avalos     dlg_print_size(height, width);
1325382d832SPeter Avalos     dlg_ctl_size(height, width);
1335382d832SPeter Avalos 
1345382d832SPeter Avalos     xorg = dlg_box_x_ordinate(width);
1355382d832SPeter Avalos     yorg = dlg_box_y_ordinate(height);
1365382d832SPeter Avalos 
1375382d832SPeter Avalos     dialog = dlg_new_window(height, width, yorg, xorg);
1385382d832SPeter Avalos     dlg_register_window(dialog, "inputbox", binding);
1395382d832SPeter Avalos     dlg_register_buttons(dialog, "inputbox", buttons);
1405382d832SPeter Avalos 
1415382d832SPeter Avalos     dlg_mouse_setbase(xorg, yorg);
1425382d832SPeter Avalos 
1435382d832SPeter Avalos     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
1445382d832SPeter Avalos     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1455382d832SPeter Avalos     dlg_draw_title(dialog, title);
1465382d832SPeter Avalos 
1475940c9abSDaniel Fojt     dlg_attrset(dialog, dialog_attr);
1485382d832SPeter Avalos     dlg_draw_helpline(dialog, FALSE);
1495382d832SPeter Avalos     dlg_print_autowrap(dialog, prompt, height, width);
1505382d832SPeter Avalos 
1515382d832SPeter Avalos     /* Draw the input field box */
1525382d832SPeter Avalos     box_width = width - 6;
1535382d832SPeter Avalos     getyx(dialog, y, x);
1545382d832SPeter Avalos     (void) x;
1555382d832SPeter Avalos     box_y = y + 2;
1565382d832SPeter Avalos     box_x = (width - box_width) / 2;
1575382d832SPeter Avalos     dlg_mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
1585382d832SPeter Avalos     dlg_draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
1595382d832SPeter Avalos 		 border_attr, border2_attr);
1605382d832SPeter Avalos 
1615382d832SPeter Avalos     /* Make a window for the input-field, to associate bindings */
1625382d832SPeter Avalos     editor = dlg_sub_window(dialog, 1, box_width, yorg + box_y, xorg + box_x);
1635382d832SPeter Avalos     dlg_register_window(editor, "inputbox2", binding2);
1645382d832SPeter Avalos 
1655382d832SPeter Avalos     if (*input != '\0') {
1665382d832SPeter Avalos 	dlg_show_string(editor, input, chr_offset, inputbox_attr,
1675940c9abSDaniel Fojt 			0, 0, box_width, (bool) (password != 0), first);
1685382d832SPeter Avalos 	wsyncup(editor);
1695382d832SPeter Avalos 	wcursyncup(editor);
1705382d832SPeter Avalos     }
1715382d832SPeter Avalos 
1725940c9abSDaniel Fojt     while (result == DLG_EXIT_UNKNOWN) {
1735382d832SPeter Avalos 	/*
1745382d832SPeter Avalos 	 * The last field drawn determines where the cursor is shown:
1755382d832SPeter Avalos 	 */
1765382d832SPeter Avalos 	if (show_buttons) {
1775382d832SPeter Avalos 	    show_buttons = FALSE;
1785382d832SPeter Avalos 	    col_offset = dlg_edit_offset(input, chr_offset, box_width);
1795382d832SPeter Avalos 	    (void) wmove(dialog, box_y, box_x + col_offset);
1805382d832SPeter Avalos 	    dlg_draw_buttons(dialog, height - 2, 0, buttons, state, FALSE, width);
1815382d832SPeter Avalos 	}
1825382d832SPeter Avalos 
1835382d832SPeter Avalos 	if (!first) {
1845382d832SPeter Avalos 	    if (*input != '\0' && !edited) {
1855382d832SPeter Avalos 		dlg_show_string(editor, input, chr_offset, inputbox_attr,
1865940c9abSDaniel Fojt 				0, 0, box_width, (bool) (password != 0), first);
1875382d832SPeter Avalos 		wmove(editor, 0, chr_offset);
1885382d832SPeter Avalos 		wsyncup(editor);
1895382d832SPeter Avalos 		wcursyncup(editor);
1905382d832SPeter Avalos 	    }
1915382d832SPeter Avalos 	    key = dlg_mouse_wgetch((state == sTEXT) ? editor : dialog, &fkey);
1925940c9abSDaniel Fojt 	    if (dlg_result_key(key, fkey, &result)) {
1935940c9abSDaniel Fojt 		if (!dlg_button_key(result, &code, &key, &fkey))
1945382d832SPeter Avalos 		    break;
1955382d832SPeter Avalos 	    }
1965940c9abSDaniel Fojt 	}
1975382d832SPeter Avalos 
1985382d832SPeter Avalos 	/*
1995382d832SPeter Avalos 	 * Handle mouse clicks first, since we want to know if this is a button,
2005382d832SPeter Avalos 	 * or something that dlg_edit_string() should handle.
2015382d832SPeter Avalos 	 */
2025382d832SPeter Avalos 	if (fkey
2035382d832SPeter Avalos 	    && is_DLGK_MOUSE(key)
2045382d832SPeter Avalos 	    && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
2055382d832SPeter Avalos 	    result = code;
2065382d832SPeter Avalos 	    continue;
2075382d832SPeter Avalos 	}
2085382d832SPeter Avalos 
2095382d832SPeter Avalos 	if (state == sTEXT) {	/* Input box selected */
2105940c9abSDaniel Fojt 	    int edit = dlg_edit_string(input, &chr_offset, key, fkey, first);
2115382d832SPeter Avalos 
2125382d832SPeter Avalos 	    if (edit) {
2135382d832SPeter Avalos 		dlg_show_string(editor, input, chr_offset, inputbox_attr,
2145940c9abSDaniel Fojt 				0, 0, box_width, (bool) (password != 0), first);
2155382d832SPeter Avalos 		wsyncup(editor);
2165382d832SPeter Avalos 		wcursyncup(editor);
2175382d832SPeter Avalos 		first = FALSE;
2185382d832SPeter Avalos 		edited = TRUE;
2195382d832SPeter Avalos 		continue;
2205382d832SPeter Avalos 	    } else if (first) {
2215382d832SPeter Avalos 		first = FALSE;
2225382d832SPeter Avalos 		continue;
2235382d832SPeter Avalos 	    }
2245382d832SPeter Avalos 	}
2255382d832SPeter Avalos 
2265382d832SPeter Avalos 	/* handle non-functionkeys */
2275382d832SPeter Avalos 	if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
2285382d832SPeter Avalos 	    dlg_del_window(dialog);
2295382d832SPeter Avalos 	    result = dlg_ok_buttoncode(code);
2305382d832SPeter Avalos 	    continue;
2315382d832SPeter Avalos 	}
2325382d832SPeter Avalos 
2335382d832SPeter Avalos 	/* handle functionkeys */
2345382d832SPeter Avalos 	if (fkey) {
2355382d832SPeter Avalos 	    switch (key) {
2365382d832SPeter Avalos 	    case DLGK_MOUSE('i'):	/* mouse enter events */
2375382d832SPeter Avalos 		state = 0;
2385382d832SPeter Avalos 		/* FALLTHRU */
2395382d832SPeter Avalos 	    case DLGK_FIELD_PREV:
2405382d832SPeter Avalos 		show_buttons = TRUE;
2415382d832SPeter Avalos 		state = dlg_prev_ok_buttonindex(state, sTEXT);
2425382d832SPeter Avalos 		break;
2435382d832SPeter Avalos 	    case DLGK_FIELD_NEXT:
2445382d832SPeter Avalos 		show_buttons = TRUE;
2455382d832SPeter Avalos 		state = dlg_next_ok_buttonindex(state, sTEXT);
2465382d832SPeter Avalos 		break;
2475940c9abSDaniel Fojt 	    case DLGK_TOGGLE:
2485382d832SPeter Avalos 	    case DLGK_ENTER:
2495382d832SPeter Avalos 		dlg_del_window(dialog);
2505382d832SPeter Avalos 		result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
2515382d832SPeter Avalos 		break;
252*a8e38dc0SAntonio Huete Jimenez 	    case DLGK_LEAVE:
253*a8e38dc0SAntonio Huete Jimenez 		if (state >= 0)
254*a8e38dc0SAntonio Huete Jimenez 		    result = dlg_ok_buttoncode(state);
255*a8e38dc0SAntonio Huete Jimenez 		break;
2565382d832SPeter Avalos #ifdef KEY_RESIZE
2575382d832SPeter Avalos 	    case KEY_RESIZE:
2585940c9abSDaniel Fojt 		dlg_will_resize(dialog);
2595382d832SPeter Avalos 		/* reset data */
2605382d832SPeter Avalos 		height = old_height;
2615382d832SPeter Avalos 		width = old_width;
2625382d832SPeter Avalos 		/* repaint */
2635940c9abSDaniel Fojt 		_dlg_resize_cleanup(dialog);
2645382d832SPeter Avalos 		goto retry;
2655382d832SPeter Avalos #endif
2665382d832SPeter Avalos 	    default:
2675382d832SPeter Avalos 		beep();
2685382d832SPeter Avalos 		break;
2695382d832SPeter Avalos 	    }
2705940c9abSDaniel Fojt 	} else if (key > 0) {
2715382d832SPeter Avalos 	    beep();
2725382d832SPeter Avalos 	}
2735382d832SPeter Avalos     }
2745940c9abSDaniel Fojt     AddLastKey();
2755382d832SPeter Avalos 
2765382d832SPeter Avalos     dlg_unregister_window(editor);
2775382d832SPeter Avalos     dlg_del_window(dialog);
2785382d832SPeter Avalos     dlg_mouse_free_regions();
2795382d832SPeter Avalos     free(prompt);
2805382d832SPeter Avalos     return result;
2815382d832SPeter Avalos }
282