Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * inputbox.c -- implements the input box |
| 3 | * |
| 4 | * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk) |
| 5 | * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "dialog.h" |
| 23 | |
Keenan Pepper | 84c2a2e | 2005-07-27 14:14:00 -0400 | [diff] [blame] | 24 | char dialog_input_result[MAX_LEN + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Print the termination buttons |
| 28 | */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 29 | static void print_buttons(WINDOW * dialog, int height, int width, int selected) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 31 | int x = width / 2 - 11; |
| 32 | int y = height - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
EGRY Gabor | 75c0a8a | 2008-01-11 23:42:54 +0100 | [diff] [blame] | 34 | print_button(dialog, gettext(" Ok "), y, x, selected == 0); |
| 35 | print_button(dialog, gettext(" Help "), y, x + 14, selected == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 37 | wmove(dialog, y, x + 1 + 14 * selected); |
| 38 | wrefresh(dialog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Display a dialog box for inputing a string |
| 43 | */ |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame] | 44 | int dialog_inputbox(const char *title, const char *prompt, int height, int width, |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 45 | const char *init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 47 | int i, x, y, box_y, box_x, box_width; |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 48 | int input_x = 0, key = 0, button = -1; |
| 49 | int show_x, len, pos; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 50 | char *instr = dialog_input_result; |
| 51 | WINDOW *dialog; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 53 | if (!init) |
| 54 | instr[0] = '\0'; |
| 55 | else |
| 56 | strcpy(instr, init); |
| 57 | |
| 58 | do_resize: |
Sedat Dilek | 851f665 | 2013-06-15 11:07:35 +0200 | [diff] [blame] | 59 | if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN)) |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 60 | return -ERRDISPLAYTOOSMALL; |
Sedat Dilek | 851f665 | 2013-06-15 11:07:35 +0200 | [diff] [blame] | 61 | if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN)) |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 62 | return -ERRDISPLAYTOOSMALL; |
| 63 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 64 | /* center dialog box on screen */ |
Dirk Gouders | 4f2de3e1 | 2013-05-12 12:30:49 +0200 | [diff] [blame] | 65 | x = (getmaxx(stdscr) - width) / 2; |
| 66 | y = (getmaxy(stdscr) - height) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 68 | draw_shadow(stdscr, y, x, height, width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 70 | dialog = newwin(height, width, y, x); |
| 71 | keypad(dialog, TRUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 73 | draw_box(dialog, 0, 0, height, width, |
| 74 | dlg.dialog.atr, dlg.border.atr); |
| 75 | wattrset(dialog, dlg.border.atr); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 76 | mvwaddch(dialog, height - 3, 0, ACS_LTEE); |
| 77 | for (i = 0; i < width - 2; i++) |
| 78 | waddch(dialog, ACS_HLINE); |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 79 | wattrset(dialog, dlg.dialog.atr); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 80 | waddch(dialog, ACS_RTEE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Sam Ravnborg | fa7009d | 2005-11-19 23:38:06 +0100 | [diff] [blame] | 82 | print_title(dialog, title, width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 84 | wattrset(dialog, dlg.dialog.atr); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 85 | print_autowrap(dialog, prompt, width - 2, 1, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 87 | /* Draw the input field box */ |
| 88 | box_width = width - 6; |
| 89 | getyx(dialog, y, x); |
| 90 | box_y = y + 2; |
| 91 | box_x = (width - box_width) / 2; |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 92 | draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2, |
Roel Kluin | 79d6e53 | 2008-03-20 21:30:32 +0100 | [diff] [blame] | 93 | dlg.dialog.atr, dlg.border.atr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 95 | print_buttons(dialog, height, width, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 97 | /* Set up the initial value */ |
| 98 | wmove(dialog, box_y, box_x); |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 99 | wattrset(dialog, dlg.inputbox.atr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 101 | len = strlen(instr); |
| 102 | pos = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 104 | if (len >= box_width) { |
| 105 | show_x = len - box_width + 1; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 106 | input_x = box_width - 1; |
| 107 | for (i = 0; i < box_width - 1; i++) |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 108 | waddch(dialog, instr[show_x + i]); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame] | 109 | } else { |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 110 | show_x = 0; |
| 111 | input_x = len; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 112 | waddstr(dialog, instr); |
Sam Ravnborg | dec69da | 2005-11-19 21:56:20 +0100 | [diff] [blame] | 113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 115 | wmove(dialog, box_y, box_x + input_x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 117 | wrefresh(dialog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Sam Ravnborg | f3cbcdc | 2006-07-28 23:57:48 +0200 | [diff] [blame] | 119 | while (key != KEY_ESC) { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 120 | key = wgetch(dialog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 122 | if (button == -1) { /* Input box selected */ |
| 123 | switch (key) { |
| 124 | case TAB: |
| 125 | case KEY_UP: |
| 126 | case KEY_DOWN: |
| 127 | break; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 128 | case KEY_BACKSPACE: |
| 129 | case 127: |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 130 | if (pos) { |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 131 | wattrset(dialog, dlg.inputbox.atr); |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 132 | if (input_x == 0) { |
| 133 | show_x--; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 134 | } else |
| 135 | input_x--; |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 136 | |
| 137 | if (pos < len) { |
| 138 | for (i = pos - 1; i < len; i++) { |
| 139 | instr[i] = instr[i+1]; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pos--; |
| 144 | len--; |
| 145 | instr[len] = '\0'; |
| 146 | wmove(dialog, box_y, box_x); |
| 147 | for (i = 0; i < box_width; i++) { |
| 148 | if (!instr[show_x + i]) { |
| 149 | waddch(dialog, ' '); |
| 150 | break; |
| 151 | } |
| 152 | waddch(dialog, instr[show_x + i]); |
| 153 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 154 | wmove(dialog, box_y, input_x + box_x); |
| 155 | wrefresh(dialog); |
| 156 | } |
| 157 | continue; |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 158 | case KEY_LEFT: |
| 159 | if (pos > 0) { |
| 160 | if (input_x > 0) { |
| 161 | wmove(dialog, box_y, --input_x + box_x); |
| 162 | } else if (input_x == 0) { |
| 163 | show_x--; |
| 164 | wmove(dialog, box_y, box_x); |
| 165 | for (i = 0; i < box_width; i++) { |
| 166 | if (!instr[show_x + i]) { |
| 167 | waddch(dialog, ' '); |
| 168 | break; |
| 169 | } |
| 170 | waddch(dialog, instr[show_x + i]); |
| 171 | } |
| 172 | wmove(dialog, box_y, box_x); |
| 173 | } |
| 174 | pos--; |
| 175 | } |
| 176 | continue; |
| 177 | case KEY_RIGHT: |
| 178 | if (pos < len) { |
| 179 | if (input_x < box_width - 1) { |
| 180 | wmove(dialog, box_y, ++input_x + box_x); |
| 181 | } else if (input_x == box_width - 1) { |
| 182 | show_x++; |
| 183 | wmove(dialog, box_y, box_x); |
| 184 | for (i = 0; i < box_width; i++) { |
| 185 | if (!instr[show_x + i]) { |
| 186 | waddch(dialog, ' '); |
| 187 | break; |
| 188 | } |
| 189 | waddch(dialog, instr[show_x + i]); |
| 190 | } |
| 191 | wmove(dialog, box_y, input_x + box_x); |
| 192 | } |
| 193 | pos++; |
| 194 | } |
| 195 | continue; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 196 | default: |
| 197 | if (key < 0x100 && isprint(key)) { |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 198 | if (len < MAX_LEN) { |
Sam Ravnborg | 98e5a15 | 2006-07-24 21:40:46 +0200 | [diff] [blame] | 199 | wattrset(dialog, dlg.inputbox.atr); |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 200 | if (pos < len) { |
| 201 | for (i = len; i > pos; i--) |
| 202 | instr[i] = instr[i-1]; |
| 203 | instr[pos] = key; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 204 | } else { |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 205 | instr[len] = key; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 206 | } |
Wang YanQing | 87727d4 | 2012-12-17 22:38:02 +0800 | [diff] [blame] | 207 | pos++; |
| 208 | len++; |
| 209 | instr[len] = '\0'; |
| 210 | |
| 211 | if (input_x == box_width - 1) { |
| 212 | show_x++; |
| 213 | } else { |
| 214 | input_x++; |
| 215 | } |
| 216 | |
| 217 | wmove(dialog, box_y, box_x); |
| 218 | for (i = 0; i < box_width; i++) { |
| 219 | if (!instr[show_x + i]) { |
| 220 | waddch(dialog, ' '); |
| 221 | break; |
| 222 | } |
| 223 | waddch(dialog, instr[show_x + i]); |
| 224 | } |
| 225 | wmove(dialog, box_y, input_x + box_x); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 226 | wrefresh(dialog); |
| 227 | } else |
| 228 | flash(); /* Alarm user about overflow */ |
| 229 | continue; |
| 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 233 | switch (key) { |
| 234 | case 'O': |
| 235 | case 'o': |
| 236 | delwin(dialog); |
| 237 | return 0; |
| 238 | case 'H': |
| 239 | case 'h': |
| 240 | delwin(dialog); |
| 241 | return 1; |
| 242 | case KEY_UP: |
| 243 | case KEY_LEFT: |
| 244 | switch (button) { |
| 245 | case -1: |
Li Zefan | 4280eae | 2010-04-14 11:44:05 +0800 | [diff] [blame] | 246 | button = 1; /* Indicates "Help" button is selected */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 247 | print_buttons(dialog, height, width, 1); |
| 248 | break; |
| 249 | case 0: |
| 250 | button = -1; /* Indicates input box is selected */ |
| 251 | print_buttons(dialog, height, width, 0); |
| 252 | wmove(dialog, box_y, box_x + input_x); |
| 253 | wrefresh(dialog); |
| 254 | break; |
| 255 | case 1: |
| 256 | button = 0; /* Indicates "OK" button is selected */ |
| 257 | print_buttons(dialog, height, width, 0); |
| 258 | break; |
| 259 | } |
| 260 | break; |
| 261 | case TAB: |
| 262 | case KEY_DOWN: |
| 263 | case KEY_RIGHT: |
| 264 | switch (button) { |
| 265 | case -1: |
| 266 | button = 0; /* Indicates "OK" button is selected */ |
| 267 | print_buttons(dialog, height, width, 0); |
| 268 | break; |
| 269 | case 0: |
Li Zefan | 4280eae | 2010-04-14 11:44:05 +0800 | [diff] [blame] | 270 | button = 1; /* Indicates "Help" button is selected */ |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 271 | print_buttons(dialog, height, width, 1); |
| 272 | break; |
| 273 | case 1: |
| 274 | button = -1; /* Indicates input box is selected */ |
| 275 | print_buttons(dialog, height, width, 0); |
| 276 | wmove(dialog, box_y, box_x + input_x); |
| 277 | wrefresh(dialog); |
| 278 | break; |
| 279 | } |
| 280 | break; |
| 281 | case ' ': |
| 282 | case '\n': |
| 283 | delwin(dialog); |
| 284 | return (button == -1 ? 0 : button); |
| 285 | case 'X': |
| 286 | case 'x': |
Sam Ravnborg | f3cbcdc | 2006-07-28 23:57:48 +0200 | [diff] [blame] | 287 | key = KEY_ESC; |
| 288 | break; |
| 289 | case KEY_ESC: |
| 290 | key = on_key_esc(dialog); |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 291 | break; |
Sam Ravnborg | c8dc68a | 2006-07-29 22:48:57 +0200 | [diff] [blame] | 292 | case KEY_RESIZE: |
| 293 | delwin(dialog); |
| 294 | on_key_resize(); |
| 295 | goto do_resize; |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame] | 299 | delwin(dialog); |
Sam Ravnborg | f3cbcdc | 2006-07-28 23:57:48 +0200 | [diff] [blame] | 300 | return KEY_ESC; /* ESC pressed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |