blob: 616c60138183c8c9b74c3f2562f7e4a1e5b6d5d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Pepper84c2a2e2005-07-27 14:14:00 -040024char dialog_input_result[MAX_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Print the termination buttons
28 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010029static void print_buttons(WINDOW * dialog, int height, int width, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010031 int x = width / 2 - 11;
32 int y = height - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
EGRY Gabor75c0a8a2008-01-11 23:42:54 +010034 print_button(dialog, gettext(" Ok "), y, x, selected == 0);
35 print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010037 wmove(dialog, y, x + 1 + 14 * selected);
38 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41/*
42 * Display a dialog box for inputing a string
43 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +010044int dialog_inputbox(const char *title, const char *prompt, int height, int width,
45 const char *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010047 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 Torvalds1da177e2005-04-16 15:20:36 -070051
Sam Ravnborgc8dc68a2006-07-29 22:48:57 +020052 if (!init)
53 instr[0] = '\0';
54 else
55 strcpy(instr, init);
56
57do_resize:
58 if (getmaxy(stdscr) <= (height - 2))
59 return -ERRDISPLAYTOOSMALL;
60 if (getmaxx(stdscr) <= (width - 2))
61 return -ERRDISPLAYTOOSMALL;
62
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010063 /* center dialog box on screen */
64 x = (COLS - width) / 2;
65 y = (LINES - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010067 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010069 dialog = newwin(height, width, y, x);
70 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Sam Ravnborg98e5a152006-07-24 21:40:46 +020072 draw_box(dialog, 0, 0, height, width,
73 dlg.dialog.atr, dlg.border.atr);
74 wattrset(dialog, dlg.border.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010075 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
76 for (i = 0; i < width - 2; i++)
77 waddch(dialog, ACS_HLINE);
Sam Ravnborg98e5a152006-07-24 21:40:46 +020078 wattrset(dialog, dlg.dialog.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010079 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Sam Ravnborgfa7009d2005-11-19 23:38:06 +010081 print_title(dialog, title, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Sam Ravnborg98e5a152006-07-24 21:40:46 +020083 wattrset(dialog, dlg.dialog.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010084 print_autowrap(dialog, prompt, width - 2, 1, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010086 /* Draw the input field box */
87 box_width = width - 6;
88 getyx(dialog, y, x);
89 box_y = y + 2;
90 box_x = (width - box_width) / 2;
Sam Ravnborg98e5a152006-07-24 21:40:46 +020091 draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
Roel Kluin79d6e532008-03-20 21:30:32 +010092 dlg.dialog.atr, dlg.border.atr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010094 print_buttons(dialog, height, width, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010096 /* Set up the initial value */
97 wmove(dialog, box_y, box_x);
Sam Ravnborg98e5a152006-07-24 21:40:46 +020098 wattrset(dialog, dlg.inputbox.atr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100100 input_x = strlen(instr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100102 if (input_x >= box_width) {
103 scroll = input_x - box_width + 1;
104 input_x = box_width - 1;
105 for (i = 0; i < box_width - 1; i++)
106 waddch(dialog, instr[scroll + i]);
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100107 } else {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100108 waddstr(dialog, instr);
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100111 wmove(dialog, box_y, box_x + input_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100113 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +0200115 while (key != KEY_ESC) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100116 key = wgetch(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100118 if (button == -1) { /* Input box selected */
119 switch (key) {
120 case TAB:
121 case KEY_UP:
122 case KEY_DOWN:
123 break;
124 case KEY_LEFT:
125 continue;
126 case KEY_RIGHT:
127 continue;
128 case KEY_BACKSPACE:
129 case 127:
130 if (input_x || scroll) {
Sam Ravnborg98e5a152006-07-24 21:40:46 +0200131 wattrset(dialog, dlg.inputbox.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100132 if (!input_x) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100133 scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100134 wmove(dialog, box_y, box_x);
135 for (i = 0; i < box_width; i++)
136 waddch(dialog,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100137 instr[scroll + input_x + i] ?
138 instr[scroll + input_x + i] : ' ');
139 input_x = strlen(instr) - scroll;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100140 } else
141 input_x--;
142 instr[scroll + input_x] = '\0';
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100143 mvwaddch(dialog, box_y, input_x + box_x, ' ');
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100144 wmove(dialog, box_y, input_x + box_x);
145 wrefresh(dialog);
146 }
147 continue;
148 default:
149 if (key < 0x100 && isprint(key)) {
150 if (scroll + input_x < MAX_LEN) {
Sam Ravnborg98e5a152006-07-24 21:40:46 +0200151 wattrset(dialog, dlg.inputbox.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100152 instr[scroll + input_x] = key;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100153 instr[scroll + input_x + 1] = '\0';
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100154 if (input_x == box_width - 1) {
155 scroll++;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100156 wmove(dialog, box_y, box_x);
157 for (i = 0; i < box_width - 1; i++)
158 waddch(dialog, instr [scroll + i]);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100159 } else {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100160 wmove(dialog, box_y, input_x++ + box_x);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100161 waddch(dialog, key);
162 }
163 wrefresh(dialog);
164 } else
165 flash(); /* Alarm user about overflow */
166 continue;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100170 switch (key) {
171 case 'O':
172 case 'o':
173 delwin(dialog);
174 return 0;
175 case 'H':
176 case 'h':
177 delwin(dialog);
178 return 1;
179 case KEY_UP:
180 case KEY_LEFT:
181 switch (button) {
182 case -1:
183 button = 1; /* Indicates "Cancel" button is selected */
184 print_buttons(dialog, height, width, 1);
185 break;
186 case 0:
187 button = -1; /* Indicates input box is selected */
188 print_buttons(dialog, height, width, 0);
189 wmove(dialog, box_y, box_x + input_x);
190 wrefresh(dialog);
191 break;
192 case 1:
193 button = 0; /* Indicates "OK" button is selected */
194 print_buttons(dialog, height, width, 0);
195 break;
196 }
197 break;
198 case TAB:
199 case KEY_DOWN:
200 case KEY_RIGHT:
201 switch (button) {
202 case -1:
203 button = 0; /* Indicates "OK" button is selected */
204 print_buttons(dialog, height, width, 0);
205 break;
206 case 0:
207 button = 1; /* Indicates "Cancel" button is selected */
208 print_buttons(dialog, height, width, 1);
209 break;
210 case 1:
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 }
217 break;
218 case ' ':
219 case '\n':
220 delwin(dialog);
221 return (button == -1 ? 0 : button);
222 case 'X':
223 case 'x':
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +0200224 key = KEY_ESC;
225 break;
226 case KEY_ESC:
227 key = on_key_esc(dialog);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100228 break;
Sam Ravnborgc8dc68a2006-07-29 22:48:57 +0200229 case KEY_RESIZE:
230 delwin(dialog);
231 on_key_resize();
232 goto do_resize;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100236 delwin(dialog);
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +0200237 return KEY_ESC; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}