1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/*
* $Id: inputbox.c,v 1.76 2012/12/03 11:46:50 tom Exp $
*
* inputbox.c -- implements the input box
*
* Copyright 2000-2011,2012 Thomas E. Dickey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License, version 2.1
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to
* Free Software Foundation, Inc.
* 51 Franklin St., Fifth Floor
* Boston, MA 02110, USA.
*
* An earlier version of this program lists as authors:
* Savio Lam (lam836@cs.cuhk.hk)
*/
#include <dialog.h>
#include <dlg_keys.h>
#define sTEXT -1
#define NAVIGATE_BINDINGS \
DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_DOWN ), \
DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), \
DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ), \
DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_UP )
/*
* Display a dialog box for entering a string
*/
int
dialog_inputbox(const char *title, const char *cprompt, int height, int width,
const char *init, const int password)
{
/* *INDENT-OFF* */
static DLG_KEYS_BINDING binding[] = {
HELPKEY_BINDINGS,
ENTERKEY_BINDINGS,
NAVIGATE_BINDINGS,
END_KEYS_BINDING
};
static DLG_KEYS_BINDING binding2[] = {
INPUTSTR_BINDINGS,
HELPKEY_BINDINGS,
ENTERKEY_BINDINGS,
NAVIGATE_BINDINGS,
END_KEYS_BINDING
};
/* *INDENT-ON* */
#ifdef KEY_RESIZE
int old_height = height;
int old_width = width;
#endif
int xorg, yorg;
int x, y, box_y, box_x, box_width;
int show_buttons;
int col_offset = 0;
int chr_offset = 0;
int key, fkey, code;
int result = DLG_EXIT_UNKNOWN;
int state;
int first;
int edited;
char *input;
WINDOW *dialog;
WINDOW *editor;
char *prompt = dlg_strclone(cprompt);
const char **buttons = dlg_ok_labels();
dlg_does_output();
dlg_tab_correct_str(prompt);
/* Set up the initial value */
input = dlg_set_result(init);
edited = FALSE;
#ifdef KEY_RESIZE
retry:
#endif
show_buttons = TRUE;
state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
first = (state == sTEXT);
key = fkey = 0;
if (init != NULL) {
dlg_auto_size(title, prompt, &height, &width, 5,
MIN(MAX(dlg_count_columns(init) + 7, 26),
SCOLS - (dialog_vars.begin_set ?
dialog_vars.begin_x : 0)));
chr_offset = (int) strlen(init);
} else {
dlg_auto_size(title, prompt, &height, &width, 5, 26);
}
dlg_button_layout(buttons, &width);
dlg_print_size(height, width);
dlg_ctl_size(height, width);
xorg = dlg_box_x_ordinate(width);
yorg = dlg_box_y_ordinate(height);
dialog = dlg_new_window(height, width, yorg, xorg);
dlg_register_window(dialog, "inputbox", binding);
dlg_register_buttons(dialog, "inputbox", buttons);
dlg_mouse_setbase(xorg, yorg);
dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
dlg_draw_title(dialog, title);
(void) wattrset(dialog, dialog_attr);
dlg_draw_helpline(dialog, FALSE);
dlg_print_autowrap(dialog, prompt, height, width);
/* Draw the input field box */
box_width = width - 6;
getyx(dialog, y, x);
(void) x;
box_y = y + 2;
box_x = (width - box_width) / 2;
dlg_mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
dlg_draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
border_attr, border2_attr);
/* Make a window for the input-field, to associate bindings */
editor = dlg_sub_window(dialog, 1, box_width, yorg + box_y, xorg + box_x);
dlg_register_window(editor, "inputbox2", binding2);
if (*input != '\0') {
dlg_show_string(editor, input, chr_offset, inputbox_attr,
0, 0, box_width, password, first);
wsyncup(editor);
wcursyncup(editor);
}
while (result == DLG_EXIT_UNKNOWN) {
int edit = 0;
/*
* The last field drawn determines where the cursor is shown:
*/
if (show_buttons) {
show_buttons = FALSE;
col_offset = dlg_edit_offset(input, chr_offset, box_width);
(void) wmove(dialog, box_y, box_x + col_offset);
dlg_draw_buttons(dialog, height - 2, 0, buttons, state, FALSE, width);
}
if (!first) {
if (*input != '\0' && !edited) {
dlg_show_string(editor, input, chr_offset, inputbox_attr,
0, 0, box_width, password, first);
wmove(editor, 0, chr_offset);
wsyncup(editor);
wcursyncup(editor);
}
key = dlg_mouse_wgetch((state == sTEXT) ? editor : dialog, &fkey);
if (dlg_result_key(key, fkey, &result))
break;
}
/*
* Handle mouse clicks first, since we want to know if this is a button,
* or something that dlg_edit_string() should handle.
*/
if (fkey
&& is_DLGK_MOUSE(key)
&& (code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
result = code;
continue;
}
if (state == sTEXT) { /* Input box selected */
edit = dlg_edit_string(input, &chr_offset, key, fkey, first);
if (edit) {
dlg_show_string(editor, input, chr_offset, inputbox_attr,
0, 0, box_width, password, first);
wsyncup(editor);
wcursyncup(editor);
first = FALSE;
edited = TRUE;
continue;
} else if (first) {
first = FALSE;
continue;
}
}
/* handle non-functionkeys */
if (!fkey && (code = dlg_char_to_button(key, buttons)) >= 0) {
dlg_del_window(dialog);
result = dlg_ok_buttoncode(code);
continue;
}
/* handle functionkeys */
if (fkey) {
switch (key) {
case DLGK_MOUSE('i'): /* mouse enter events */
state = 0;
/* FALLTHRU */
case DLGK_FIELD_PREV:
show_buttons = TRUE;
state = dlg_prev_ok_buttonindex(state, sTEXT);
break;
case DLGK_FIELD_NEXT:
show_buttons = TRUE;
state = dlg_next_ok_buttonindex(state, sTEXT);
break;
case ' ': /* FIXME: conflict with inputstr.c */
case DLGK_ENTER:
dlg_del_window(dialog);
result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
break;
#ifdef KEY_RESIZE
case KEY_RESIZE:
/* reset data */
height = old_height;
width = old_width;
/* repaint */
dlg_clear();
dlg_del_window(dialog);
refresh();
dlg_mouse_free_regions();
goto retry;
#endif
default:
beep();
break;
}
} else {
beep();
}
}
dlg_unregister_window(editor);
dlg_del_window(dialog);
dlg_mouse_free_regions();
free(prompt);
return result;
}
|