xref: /freebsd/contrib/dialog/inputbox.c (revision 2a3e3873a1e4cd958f2b0f85d3b10cfa40575d30)
14c8945a0SNathan Whitehorn /*
2*2a3e3873SBaptiste Daroussin  *  $Id: inputbox.c,v 1.76 2012/12/03 11:46:50 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  inputbox.c -- implements the input box
54c8945a0SNathan Whitehorn  *
6*2a3e3873SBaptiste Daroussin  *  Copyright 2000-2011,2012 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 
274c8945a0SNathan Whitehorn #include <dialog.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 
404c8945a0SNathan Whitehorn /*
414c8945a0SNathan Whitehorn  * Display a dialog box for entering a string
424c8945a0SNathan Whitehorn  */
434c8945a0SNathan Whitehorn int
444c8945a0SNathan Whitehorn dialog_inputbox(const char *title, const char *cprompt, int height, int width,
454c8945a0SNathan Whitehorn 		const char *init, const int password)
464c8945a0SNathan Whitehorn {
474c8945a0SNathan Whitehorn     /* *INDENT-OFF* */
484c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding[] = {
49682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
504c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
514c8945a0SNathan Whitehorn 	NAVIGATE_BINDINGS,
524c8945a0SNathan Whitehorn 	END_KEYS_BINDING
534c8945a0SNathan Whitehorn     };
544c8945a0SNathan Whitehorn     static DLG_KEYS_BINDING binding2[] = {
554c8945a0SNathan Whitehorn 	INPUTSTR_BINDINGS,
56682c9e0fSNathan Whitehorn 	HELPKEY_BINDINGS,
574c8945a0SNathan Whitehorn 	ENTERKEY_BINDINGS,
584c8945a0SNathan Whitehorn 	NAVIGATE_BINDINGS,
594c8945a0SNathan Whitehorn 	END_KEYS_BINDING
604c8945a0SNathan Whitehorn     };
614c8945a0SNathan Whitehorn     /* *INDENT-ON* */
624c8945a0SNathan Whitehorn 
634c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
644c8945a0SNathan Whitehorn     int old_height = height;
654c8945a0SNathan Whitehorn     int old_width = width;
664c8945a0SNathan Whitehorn #endif
674c8945a0SNathan Whitehorn     int xorg, yorg;
684c8945a0SNathan Whitehorn     int x, y, box_y, box_x, box_width;
694c8945a0SNathan Whitehorn     int show_buttons;
704c8945a0SNathan Whitehorn     int col_offset = 0;
714c8945a0SNathan Whitehorn     int chr_offset = 0;
724c8945a0SNathan Whitehorn     int key, fkey, code;
734c8945a0SNathan Whitehorn     int result = DLG_EXIT_UNKNOWN;
744c8945a0SNathan Whitehorn     int state;
754c8945a0SNathan Whitehorn     int first;
76*2a3e3873SBaptiste Daroussin     int edited;
774c8945a0SNathan Whitehorn     char *input;
784c8945a0SNathan Whitehorn     WINDOW *dialog;
794c8945a0SNathan Whitehorn     WINDOW *editor;
804c8945a0SNathan Whitehorn     char *prompt = dlg_strclone(cprompt);
814c8945a0SNathan Whitehorn     const char **buttons = dlg_ok_labels();
824c8945a0SNathan Whitehorn 
834c8945a0SNathan Whitehorn     dlg_does_output();
844c8945a0SNathan Whitehorn 
854c8945a0SNathan Whitehorn     dlg_tab_correct_str(prompt);
864c8945a0SNathan Whitehorn 
874c8945a0SNathan Whitehorn     /* Set up the initial value */
884c8945a0SNathan Whitehorn     input = dlg_set_result(init);
89*2a3e3873SBaptiste Daroussin     edited = FALSE;
904c8945a0SNathan Whitehorn 
914c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
924c8945a0SNathan Whitehorn   retry:
934c8945a0SNathan Whitehorn #endif
944c8945a0SNathan Whitehorn     show_buttons = TRUE;
95*2a3e3873SBaptiste Daroussin     state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
964c8945a0SNathan Whitehorn     first = (state == sTEXT);
974c8945a0SNathan Whitehorn     key = fkey = 0;
984c8945a0SNathan Whitehorn 
994c8945a0SNathan Whitehorn     if (init != NULL) {
1004c8945a0SNathan Whitehorn 	dlg_auto_size(title, prompt, &height, &width, 5,
1014c8945a0SNathan Whitehorn 		      MIN(MAX(dlg_count_columns(init) + 7, 26),
1024c8945a0SNathan Whitehorn 			  SCOLS - (dialog_vars.begin_set ?
1034c8945a0SNathan Whitehorn 				   dialog_vars.begin_x : 0)));
1044c8945a0SNathan Whitehorn 	chr_offset = (int) strlen(init);
1054c8945a0SNathan Whitehorn     } else {
1064c8945a0SNathan Whitehorn 	dlg_auto_size(title, prompt, &height, &width, 5, 26);
1074c8945a0SNathan Whitehorn     }
1084c8945a0SNathan Whitehorn     dlg_button_layout(buttons, &width);
1094c8945a0SNathan Whitehorn     dlg_print_size(height, width);
1104c8945a0SNathan Whitehorn     dlg_ctl_size(height, width);
1114c8945a0SNathan Whitehorn 
1124c8945a0SNathan Whitehorn     xorg = dlg_box_x_ordinate(width);
1134c8945a0SNathan Whitehorn     yorg = dlg_box_y_ordinate(height);
1144c8945a0SNathan Whitehorn 
1154c8945a0SNathan Whitehorn     dialog = dlg_new_window(height, width, yorg, xorg);
1164c8945a0SNathan Whitehorn     dlg_register_window(dialog, "inputbox", binding);
1174c8945a0SNathan Whitehorn     dlg_register_buttons(dialog, "inputbox", buttons);
1184c8945a0SNathan Whitehorn 
1194c8945a0SNathan Whitehorn     dlg_mouse_setbase(xorg, yorg);
1204c8945a0SNathan Whitehorn 
121*2a3e3873SBaptiste Daroussin     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
122*2a3e3873SBaptiste Daroussin     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
1234c8945a0SNathan Whitehorn     dlg_draw_title(dialog, title);
1244c8945a0SNathan Whitehorn 
125*2a3e3873SBaptiste Daroussin     (void) wattrset(dialog, dialog_attr);
126682c9e0fSNathan Whitehorn     dlg_draw_helpline(dialog, FALSE);
1274c8945a0SNathan Whitehorn     dlg_print_autowrap(dialog, prompt, height, width);
1284c8945a0SNathan Whitehorn 
1294c8945a0SNathan Whitehorn     /* Draw the input field box */
1304c8945a0SNathan Whitehorn     box_width = width - 6;
1314c8945a0SNathan Whitehorn     getyx(dialog, y, x);
132*2a3e3873SBaptiste Daroussin     (void) x;
1334c8945a0SNathan Whitehorn     box_y = y + 2;
1344c8945a0SNathan Whitehorn     box_x = (width - box_width) / 2;
1354c8945a0SNathan Whitehorn     dlg_mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
1364c8945a0SNathan Whitehorn     dlg_draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
137*2a3e3873SBaptiste Daroussin 		 border_attr, border2_attr);
1384c8945a0SNathan Whitehorn 
1394c8945a0SNathan Whitehorn     /* Make a window for the input-field, to associate bindings */
1404c8945a0SNathan Whitehorn     editor = dlg_sub_window(dialog, 1, box_width, yorg + box_y, xorg + box_x);
141*2a3e3873SBaptiste Daroussin     dlg_register_window(editor, "inputbox2", binding2);
1424c8945a0SNathan Whitehorn 
143*2a3e3873SBaptiste Daroussin     if (*input != '\0') {
144*2a3e3873SBaptiste Daroussin 	dlg_show_string(editor, input, chr_offset, inputbox_attr,
145*2a3e3873SBaptiste Daroussin 			0, 0, box_width, password, first);
146*2a3e3873SBaptiste Daroussin 	wsyncup(editor);
147*2a3e3873SBaptiste Daroussin 	wcursyncup(editor);
148*2a3e3873SBaptiste Daroussin     }
1494c8945a0SNathan Whitehorn     while (result == DLG_EXIT_UNKNOWN) {
1504c8945a0SNathan Whitehorn 	int edit = 0;
1514c8945a0SNathan Whitehorn 
1524c8945a0SNathan Whitehorn 	/*
1534c8945a0SNathan Whitehorn 	 * The last field drawn determines where the cursor is shown:
1544c8945a0SNathan Whitehorn 	 */
1554c8945a0SNathan Whitehorn 	if (show_buttons) {
1564c8945a0SNathan Whitehorn 	    show_buttons = FALSE;
1574c8945a0SNathan Whitehorn 	    col_offset = dlg_edit_offset(input, chr_offset, box_width);
1584c8945a0SNathan Whitehorn 	    (void) wmove(dialog, box_y, box_x + col_offset);
1594c8945a0SNathan Whitehorn 	    dlg_draw_buttons(dialog, height - 2, 0, buttons, state, FALSE, width);
1604c8945a0SNathan Whitehorn 	}
1614c8945a0SNathan Whitehorn 
1624c8945a0SNathan Whitehorn 	if (!first) {
163*2a3e3873SBaptiste Daroussin 	    if (*input != '\0' && !edited) {
164*2a3e3873SBaptiste Daroussin 		dlg_show_string(editor, input, chr_offset, inputbox_attr,
165*2a3e3873SBaptiste Daroussin 				0, 0, box_width, password, first);
166*2a3e3873SBaptiste Daroussin 		wmove(editor, 0, chr_offset);
167*2a3e3873SBaptiste Daroussin 		wsyncup(editor);
168*2a3e3873SBaptiste Daroussin 		wcursyncup(editor);
169*2a3e3873SBaptiste Daroussin 	    }
1704c8945a0SNathan Whitehorn 	    key = dlg_mouse_wgetch((state == sTEXT) ? editor : dialog, &fkey);
1714c8945a0SNathan Whitehorn 	    if (dlg_result_key(key, fkey, &result))
1724c8945a0SNathan Whitehorn 		break;
1734c8945a0SNathan Whitehorn 	}
1744c8945a0SNathan Whitehorn 
1754c8945a0SNathan Whitehorn 	/*
1764c8945a0SNathan Whitehorn 	 * Handle mouse clicks first, since we want to know if this is a button,
1774c8945a0SNathan Whitehorn 	 * or something that dlg_edit_string() should handle.
1784c8945a0SNathan Whitehorn 	 */
1794c8945a0SNathan Whitehorn 	if (fkey
1804c8945a0SNathan Whitehorn 	    && is_DLGK_MOUSE(key)
1814c8945a0SNathan Whitehorn 	    && (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
1824c8945a0SNathan Whitehorn 	    result = code;
1834c8945a0SNathan Whitehorn 	    continue;
1844c8945a0SNathan Whitehorn 	}
1854c8945a0SNathan Whitehorn 
1864c8945a0SNathan Whitehorn 	if (state == sTEXT) {	/* Input box selected */
1874c8945a0SNathan Whitehorn 	    edit = dlg_edit_string(input, &chr_offset, key, fkey, first);
1884c8945a0SNathan Whitehorn 
1894c8945a0SNathan Whitehorn 	    if (edit) {
190*2a3e3873SBaptiste Daroussin 		dlg_show_string(editor, input, chr_offset, inputbox_attr,
191*2a3e3873SBaptiste Daroussin 				0, 0, box_width, password, first);
192*2a3e3873SBaptiste Daroussin 		wsyncup(editor);
193*2a3e3873SBaptiste Daroussin 		wcursyncup(editor);
1944c8945a0SNathan Whitehorn 		first = FALSE;
195*2a3e3873SBaptiste Daroussin 		edited = TRUE;
1964c8945a0SNathan Whitehorn 		continue;
1974c8945a0SNathan Whitehorn 	    } else if (first) {
1984c8945a0SNathan Whitehorn 		first = FALSE;
1994c8945a0SNathan Whitehorn 		continue;
2004c8945a0SNathan Whitehorn 	    }
2014c8945a0SNathan Whitehorn 	}
2024c8945a0SNathan Whitehorn 
2034c8945a0SNathan Whitehorn 	/* handle non-functionkeys */
2044c8945a0SNathan Whitehorn 	if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
2054c8945a0SNathan Whitehorn 	    dlg_del_window(dialog);
2064c8945a0SNathan Whitehorn 	    result = dlg_ok_buttoncode(code);
2074c8945a0SNathan Whitehorn 	    continue;
2084c8945a0SNathan Whitehorn 	}
2094c8945a0SNathan Whitehorn 
2104c8945a0SNathan Whitehorn 	/* handle functionkeys */
2114c8945a0SNathan Whitehorn 	if (fkey) {
2124c8945a0SNathan Whitehorn 	    switch (key) {
2134c8945a0SNathan Whitehorn 	    case DLGK_MOUSE('i'):	/* mouse enter events */
2144c8945a0SNathan Whitehorn 		state = 0;
2154c8945a0SNathan Whitehorn 		/* FALLTHRU */
2164c8945a0SNathan Whitehorn 	    case DLGK_FIELD_PREV:
2174c8945a0SNathan Whitehorn 		show_buttons = TRUE;
2184c8945a0SNathan Whitehorn 		state = dlg_prev_ok_buttonindex(state, sTEXT);
2194c8945a0SNathan Whitehorn 		break;
2204c8945a0SNathan Whitehorn 	    case DLGK_FIELD_NEXT:
2214c8945a0SNathan Whitehorn 		show_buttons = TRUE;
2224c8945a0SNathan Whitehorn 		state = dlg_next_ok_buttonindex(state, sTEXT);
2234c8945a0SNathan Whitehorn 		break;
2244c8945a0SNathan Whitehorn 	    case ' ':		/* FIXME: conflict with inputstr.c */
2254c8945a0SNathan Whitehorn 	    case DLGK_ENTER:
2264c8945a0SNathan Whitehorn 		dlg_del_window(dialog);
227682c9e0fSNathan Whitehorn 		result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
2284c8945a0SNathan Whitehorn 		break;
2294c8945a0SNathan Whitehorn #ifdef KEY_RESIZE
2304c8945a0SNathan Whitehorn 	    case KEY_RESIZE:
2314c8945a0SNathan Whitehorn 		/* reset data */
2324c8945a0SNathan Whitehorn 		height = old_height;
2334c8945a0SNathan Whitehorn 		width = old_width;
2344c8945a0SNathan Whitehorn 		/* repaint */
2354c8945a0SNathan Whitehorn 		dlg_clear();
2364c8945a0SNathan Whitehorn 		dlg_del_window(dialog);
2374c8945a0SNathan Whitehorn 		refresh();
2384c8945a0SNathan Whitehorn 		dlg_mouse_free_regions();
2394c8945a0SNathan Whitehorn 		goto retry;
2404c8945a0SNathan Whitehorn #endif
2414c8945a0SNathan Whitehorn 	    default:
2424c8945a0SNathan Whitehorn 		beep();
2434c8945a0SNathan Whitehorn 		break;
2444c8945a0SNathan Whitehorn 	    }
2454c8945a0SNathan Whitehorn 	} else {
2464c8945a0SNathan Whitehorn 	    beep();
2474c8945a0SNathan Whitehorn 	}
2484c8945a0SNathan Whitehorn     }
2494c8945a0SNathan Whitehorn 
2504c8945a0SNathan Whitehorn     dlg_unregister_window(editor);
2514c8945a0SNathan Whitehorn     dlg_del_window(dialog);
2524c8945a0SNathan Whitehorn     dlg_mouse_free_regions();
2534c8945a0SNathan Whitehorn     free(prompt);
2544c8945a0SNathan Whitehorn     return result;
2554c8945a0SNathan Whitehorn }
256