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 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 34 | print_button(dialog, " Ok ", y, x, selected == 0); |
| 35 | print_button(dialog, " 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 | */ |
| 44 | int |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 45 | dialog_inputbox(const char *title, const char *prompt, int height, int width, |
| 46 | const char *init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 48 | int i, x, y, box_y, box_x, box_width; |
| 49 | int input_x = 0, scroll = 0, key = 0, button = -1; |
| 50 | char *instr = dialog_input_result; |
| 51 | WINDOW *dialog; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 53 | /* center dialog box on screen */ |
| 54 | x = (COLS - width) / 2; |
| 55 | y = (LINES - height) / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 57 | draw_shadow(stdscr, y, x, height, width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 59 | dialog = newwin(height, width, y, x); |
| 60 | keypad(dialog, TRUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 62 | draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); |
| 63 | wattrset(dialog, border_attr); |
| 64 | mvwaddch(dialog, height - 3, 0, ACS_LTEE); |
| 65 | for (i = 0; i < width - 2; i++) |
| 66 | waddch(dialog, ACS_HLINE); |
| 67 | wattrset(dialog, dialog_attr); |
| 68 | waddch(dialog, ACS_RTEE); |
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 | if (title != NULL && strlen(title) >= width - 2) { |
| 71 | /* truncate long title -- mec */ |
| 72 | char *title2 = malloc(width - 2 + 1); |
| 73 | memcpy(title2, title, width - 2); |
| 74 | title2[width - 2] = '\0'; |
| 75 | title = title2; |
| 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 78 | if (title != NULL) { |
| 79 | wattrset(dialog, title_attr); |
| 80 | mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' '); |
| 81 | waddstr(dialog, (char *)title); |
| 82 | waddch(dialog, ' '); |
| 83 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 85 | wattrset(dialog, dialog_attr); |
| 86 | print_autowrap(dialog, prompt, width - 2, 1, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 88 | /* Draw the input field box */ |
| 89 | box_width = width - 6; |
| 90 | getyx(dialog, y, x); |
| 91 | box_y = y + 2; |
| 92 | box_x = (width - box_width) / 2; |
| 93 | draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2, |
| 94 | border_attr, dialog_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 96 | print_buttons(dialog, height, width, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 98 | /* Set up the initial value */ |
| 99 | wmove(dialog, box_y, box_x); |
| 100 | wattrset(dialog, inputbox_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 102 | if (!init) |
| 103 | instr[0] = '\0'; |
| 104 | else |
| 105 | strcpy(instr, init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 107 | input_x = strlen(instr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 109 | if (input_x >= box_width) { |
| 110 | scroll = input_x - box_width + 1; |
| 111 | input_x = box_width - 1; |
| 112 | for (i = 0; i < box_width - 1; i++) |
| 113 | waddch(dialog, instr[scroll + i]); |
| 114 | } else |
| 115 | waddstr(dialog, instr); |
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 | wmove(dialog, box_y, box_x + input_x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 119 | wrefresh(dialog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 121 | while (key != ESC) { |
| 122 | key = wgetch(dialog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 124 | if (button == -1) { /* Input box selected */ |
| 125 | switch (key) { |
| 126 | case TAB: |
| 127 | case KEY_UP: |
| 128 | case KEY_DOWN: |
| 129 | break; |
| 130 | case KEY_LEFT: |
| 131 | continue; |
| 132 | case KEY_RIGHT: |
| 133 | continue; |
| 134 | case KEY_BACKSPACE: |
| 135 | case 127: |
| 136 | if (input_x || scroll) { |
| 137 | wattrset(dialog, inputbox_attr); |
| 138 | if (!input_x) { |
| 139 | scroll = |
| 140 | scroll < |
| 141 | box_width - 1 ? 0 : scroll - |
| 142 | (box_width - 1); |
| 143 | wmove(dialog, box_y, box_x); |
| 144 | for (i = 0; i < box_width; i++) |
| 145 | waddch(dialog, |
| 146 | instr[scroll + |
| 147 | input_x + |
| 148 | i] ? |
| 149 | instr[scroll + |
| 150 | input_x + |
| 151 | i] : ' '); |
| 152 | input_x = |
| 153 | strlen(instr) - scroll; |
| 154 | } else |
| 155 | input_x--; |
| 156 | instr[scroll + input_x] = '\0'; |
| 157 | mvwaddch(dialog, box_y, input_x + box_x, |
| 158 | ' '); |
| 159 | wmove(dialog, box_y, input_x + box_x); |
| 160 | wrefresh(dialog); |
| 161 | } |
| 162 | continue; |
| 163 | default: |
| 164 | if (key < 0x100 && isprint(key)) { |
| 165 | if (scroll + input_x < MAX_LEN) { |
| 166 | wattrset(dialog, inputbox_attr); |
| 167 | instr[scroll + input_x] = key; |
| 168 | instr[scroll + input_x + 1] = |
| 169 | '\0'; |
| 170 | if (input_x == box_width - 1) { |
| 171 | scroll++; |
| 172 | wmove(dialog, box_y, |
| 173 | box_x); |
| 174 | for (i = 0; |
| 175 | i < box_width - 1; |
| 176 | i++) |
| 177 | waddch(dialog, |
| 178 | instr |
| 179 | [scroll + |
| 180 | i]); |
| 181 | } else { |
| 182 | wmove(dialog, box_y, |
| 183 | input_x++ + |
| 184 | box_x); |
| 185 | waddch(dialog, key); |
| 186 | } |
| 187 | wrefresh(dialog); |
| 188 | } else |
| 189 | flash(); /* Alarm user about overflow */ |
| 190 | continue; |
| 191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 194 | switch (key) { |
| 195 | case 'O': |
| 196 | case 'o': |
| 197 | delwin(dialog); |
| 198 | return 0; |
| 199 | case 'H': |
| 200 | case 'h': |
| 201 | delwin(dialog); |
| 202 | return 1; |
| 203 | case KEY_UP: |
| 204 | case KEY_LEFT: |
| 205 | switch (button) { |
| 206 | case -1: |
| 207 | button = 1; /* Indicates "Cancel" button is selected */ |
| 208 | print_buttons(dialog, height, width, 1); |
| 209 | break; |
| 210 | case 0: |
| 211 | button = -1; /* Indicates input box is selected */ |
| 212 | print_buttons(dialog, height, width, 0); |
| 213 | wmove(dialog, box_y, box_x + input_x); |
| 214 | wrefresh(dialog); |
| 215 | break; |
| 216 | case 1: |
| 217 | button = 0; /* Indicates "OK" button is selected */ |
| 218 | print_buttons(dialog, height, width, 0); |
| 219 | break; |
| 220 | } |
| 221 | break; |
| 222 | case TAB: |
| 223 | case KEY_DOWN: |
| 224 | case KEY_RIGHT: |
| 225 | switch (button) { |
| 226 | case -1: |
| 227 | button = 0; /* Indicates "OK" button is selected */ |
| 228 | print_buttons(dialog, height, width, 0); |
| 229 | break; |
| 230 | case 0: |
| 231 | button = 1; /* Indicates "Cancel" button is selected */ |
| 232 | print_buttons(dialog, height, width, 1); |
| 233 | break; |
| 234 | case 1: |
| 235 | button = -1; /* Indicates input box is selected */ |
| 236 | print_buttons(dialog, height, width, 0); |
| 237 | wmove(dialog, box_y, box_x + input_x); |
| 238 | wrefresh(dialog); |
| 239 | break; |
| 240 | } |
| 241 | break; |
| 242 | case ' ': |
| 243 | case '\n': |
| 244 | delwin(dialog); |
| 245 | return (button == -1 ? 0 : button); |
| 246 | case 'X': |
| 247 | case 'x': |
| 248 | key = ESC; |
| 249 | case ESC: |
| 250 | break; |
| 251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Sam Ravnborg | b1c5f1c | 2005-11-19 19:13:34 +0100 | [diff] [blame^] | 254 | delwin(dialog); |
| 255 | return -1; /* ESC pressed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |