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