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