blob: 1dcfb288ee63630e7e73be6fe28f1fd1a3bc5857 [file] [log] [blame]
Masahiro Yamada0c874102018-12-18 21:13:35 +09001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * inputbox.c -- implements the input box
4 *
5 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9#include "dialog.h"
10
Keenan Pepper84c2a2e2005-07-27 14:14:00 -040011char dialog_input_result[MAX_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13/*
14 * Print the termination buttons
15 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010016static void print_buttons(WINDOW * dialog, int height, int width, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010018 int x = width / 2 - 11;
19 int y = height - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Sam Ravnborg694c49a2018-05-22 21:36:12 +020021 print_button(dialog, " Ok ", y, x, selected == 0);
22 print_button(dialog, " Help ", y, x + 14, selected == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010024 wmove(dialog, y, x + 1 + 14 * selected);
25 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}
27
28/*
29 * Display a dialog box for inputing a string
30 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +010031int dialog_inputbox(const char *title, const char *prompt, int height, int width,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +090032 const char *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010034 int i, x, y, box_y, box_x, box_width;
Wang YanQing87727d42012-12-17 22:38:02 +080035 int input_x = 0, key = 0, button = -1;
36 int show_x, len, pos;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010037 char *instr = dialog_input_result;
38 WINDOW *dialog;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Sam Ravnborgc8dc68a2006-07-29 22:48:57 +020040 if (!init)
41 instr[0] = '\0';
42 else
43 strcpy(instr, init);
44
45do_resize:
Sedat Dilek851f6652013-06-15 11:07:35 +020046 if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
Sam Ravnborgc8dc68a2006-07-29 22:48:57 +020047 return -ERRDISPLAYTOOSMALL;
Sedat Dilek851f6652013-06-15 11:07:35 +020048 if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
Sam Ravnborgc8dc68a2006-07-29 22:48:57 +020049 return -ERRDISPLAYTOOSMALL;
50
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010051 /* center dialog box on screen */
Dirk Gouders4f2de3e12013-05-12 12:30:49 +020052 x = (getmaxx(stdscr) - width) / 2;
53 y = (getmaxy(stdscr) - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010055 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010057 dialog = newwin(height, width, y, x);
58 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Sam Ravnborg98e5a152006-07-24 21:40:46 +020060 draw_box(dialog, 0, 0, height, width,
61 dlg.dialog.atr, dlg.border.atr);
62 wattrset(dialog, dlg.border.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010063 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
64 for (i = 0; i < width - 2; i++)
65 waddch(dialog, ACS_HLINE);
Sam Ravnborg98e5a152006-07-24 21:40:46 +020066 wattrset(dialog, dlg.dialog.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010067 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Sam Ravnborgfa7009d2005-11-19 23:38:06 +010069 print_title(dialog, title, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Sam Ravnborg98e5a152006-07-24 21:40:46 +020071 wattrset(dialog, dlg.dialog.atr);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010072 print_autowrap(dialog, prompt, width - 2, 1, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010074 /* Draw the input field box */
75 box_width = width - 6;
76 getyx(dialog, y, x);
77 box_y = y + 2;
78 box_x = (width - box_width) / 2;
Sam Ravnborg98e5a152006-07-24 21:40:46 +020079 draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
Roel Kluin79d6e532008-03-20 21:30:32 +010080 dlg.dialog.atr, dlg.border.atr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010082 print_buttons(dialog, height, width, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010084 /* Set up the initial value */
85 wmove(dialog, box_y, box_x);
Sam Ravnborg98e5a152006-07-24 21:40:46 +020086 wattrset(dialog, dlg.inputbox.atr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Wang YanQing87727d42012-12-17 22:38:02 +080088 len = strlen(instr);
89 pos = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Wang YanQing87727d42012-12-17 22:38:02 +080091 if (len >= box_width) {
92 show_x = len - box_width + 1;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010093 input_x = box_width - 1;
94 for (i = 0; i < box_width - 1; i++)
Wang YanQing87727d42012-12-17 22:38:02 +080095 waddch(dialog, instr[show_x + i]);
Sam Ravnborgdec69da2005-11-19 21:56:20 +010096 } else {
Wang YanQing87727d42012-12-17 22:38:02 +080097 show_x = 0;
98 input_x = len;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010099 waddstr(dialog, instr);
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100102 wmove(dialog, box_y, box_x + input_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100104 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +0200106 while (key != KEY_ESC) {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100107 key = wgetch(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100109 if (button == -1) { /* Input box selected */
110 switch (key) {
111 case TAB:
112 case KEY_UP:
113 case KEY_DOWN:
114 break;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100115 case KEY_BACKSPACE:
Changbin Du9c38f1f2019-03-25 15:16:47 +0000116 case 8: /* ^H */
117 case 127: /* ^? */
Wang YanQing87727d42012-12-17 22:38:02 +0800118 if (pos) {
Sam Ravnborg98e5a152006-07-24 21:40:46 +0200119 wattrset(dialog, dlg.inputbox.atr);
Wang YanQing87727d42012-12-17 22:38:02 +0800120 if (input_x == 0) {
121 show_x--;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100122 } else
123 input_x--;
Wang YanQing87727d42012-12-17 22:38:02 +0800124
125 if (pos < len) {
126 for (i = pos - 1; i < len; i++) {
127 instr[i] = instr[i+1];
128 }
129 }
130
131 pos--;
132 len--;
133 instr[len] = '\0';
134 wmove(dialog, box_y, box_x);
135 for (i = 0; i < box_width; i++) {
136 if (!instr[show_x + i]) {
137 waddch(dialog, ' ');
138 break;
139 }
140 waddch(dialog, instr[show_x + i]);
141 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100142 wmove(dialog, box_y, input_x + box_x);
143 wrefresh(dialog);
144 }
145 continue;
Wang YanQing87727d42012-12-17 22:38:02 +0800146 case KEY_LEFT:
147 if (pos > 0) {
148 if (input_x > 0) {
149 wmove(dialog, box_y, --input_x + box_x);
150 } else if (input_x == 0) {
151 show_x--;
152 wmove(dialog, box_y, box_x);
153 for (i = 0; i < box_width; i++) {
154 if (!instr[show_x + i]) {
155 waddch(dialog, ' ');
156 break;
157 }
158 waddch(dialog, instr[show_x + i]);
159 }
160 wmove(dialog, box_y, box_x);
161 }
162 pos--;
163 }
164 continue;
165 case KEY_RIGHT:
166 if (pos < len) {
167 if (input_x < box_width - 1) {
168 wmove(dialog, box_y, ++input_x + box_x);
169 } else if (input_x == box_width - 1) {
170 show_x++;
171 wmove(dialog, box_y, box_x);
172 for (i = 0; i < box_width; i++) {
173 if (!instr[show_x + i]) {
174 waddch(dialog, ' ');
175 break;
176 }
177 waddch(dialog, instr[show_x + i]);
178 }
179 wmove(dialog, box_y, input_x + box_x);
180 }
181 pos++;
182 }
183 continue;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100184 default:
185 if (key < 0x100 && isprint(key)) {
Wang YanQing87727d42012-12-17 22:38:02 +0800186 if (len < MAX_LEN) {
Sam Ravnborg98e5a152006-07-24 21:40:46 +0200187 wattrset(dialog, dlg.inputbox.atr);
Wang YanQing87727d42012-12-17 22:38:02 +0800188 if (pos < len) {
189 for (i = len; i > pos; i--)
190 instr[i] = instr[i-1];
191 instr[pos] = key;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100192 } else {
Wang YanQing87727d42012-12-17 22:38:02 +0800193 instr[len] = key;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100194 }
Wang YanQing87727d42012-12-17 22:38:02 +0800195 pos++;
196 len++;
197 instr[len] = '\0';
198
199 if (input_x == box_width - 1) {
200 show_x++;
201 } else {
202 input_x++;
203 }
204
205 wmove(dialog, box_y, box_x);
206 for (i = 0; i < box_width; i++) {
207 if (!instr[show_x + i]) {
208 waddch(dialog, ' ');
209 break;
210 }
211 waddch(dialog, instr[show_x + i]);
212 }
213 wmove(dialog, box_y, input_x + box_x);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100214 wrefresh(dialog);
215 } else
216 flash(); /* Alarm user about overflow */
217 continue;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100221 switch (key) {
222 case 'O':
223 case 'o':
224 delwin(dialog);
225 return 0;
226 case 'H':
227 case 'h':
228 delwin(dialog);
229 return 1;
230 case KEY_UP:
231 case KEY_LEFT:
232 switch (button) {
233 case -1:
Li Zefan4280eae2010-04-14 11:44:05 +0800234 button = 1; /* Indicates "Help" button is selected */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100235 print_buttons(dialog, height, width, 1);
236 break;
237 case 0:
238 button = -1; /* Indicates input box is selected */
239 print_buttons(dialog, height, width, 0);
240 wmove(dialog, box_y, box_x + input_x);
241 wrefresh(dialog);
242 break;
243 case 1:
244 button = 0; /* Indicates "OK" button is selected */
245 print_buttons(dialog, height, width, 0);
246 break;
247 }
248 break;
249 case TAB:
250 case KEY_DOWN:
251 case KEY_RIGHT:
252 switch (button) {
253 case -1:
254 button = 0; /* Indicates "OK" button is selected */
255 print_buttons(dialog, height, width, 0);
256 break;
257 case 0:
Li Zefan4280eae2010-04-14 11:44:05 +0800258 button = 1; /* Indicates "Help" button is selected */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100259 print_buttons(dialog, height, width, 1);
260 break;
261 case 1:
262 button = -1; /* Indicates input box is selected */
263 print_buttons(dialog, height, width, 0);
264 wmove(dialog, box_y, box_x + input_x);
265 wrefresh(dialog);
266 break;
267 }
268 break;
269 case ' ':
270 case '\n':
271 delwin(dialog);
272 return (button == -1 ? 0 : button);
273 case 'X':
274 case 'x':
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +0200275 key = KEY_ESC;
276 break;
277 case KEY_ESC:
278 key = on_key_esc(dialog);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100279 break;
Sam Ravnborgc8dc68a2006-07-29 22:48:57 +0200280 case KEY_RESIZE:
281 delwin(dialog);
282 on_key_resize();
283 goto do_resize;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100287 delwin(dialog);
Sam Ravnborgf3cbcdc2006-07-28 23:57:48 +0200288 return KEY_ESC; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}