]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - scripts/kconfig/lxdialog/inputbox.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / scripts / kconfig / lxdialog / inputbox.c
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
24 char dialog_input_result[MAX_LEN + 1];
25
26 /*
27 * Print the termination buttons
28 */
29 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
30 {
31 int x = width / 2 - 11;
32 int y = height - 2;
33
34 print_button(dialog, gettext(" Ok "), y, x, selected == 0);
35 print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
36
37 wmove(dialog, y, x + 1 + 14 * selected);
38 wrefresh(dialog);
39 }
40
41 /*
42 * Display a dialog box for inputing a string
43 */
44 int dialog_inputbox(const char *title, const char *prompt, int height, int width,
45 const char *init)
46 {
47 int i, x, y, box_y, box_x, box_width;
48 int input_x = 0, key = 0, button = -1;
49 int show_x, len, pos;
50 char *instr = dialog_input_result;
51 WINDOW *dialog;
52
53 if (!init)
54 instr[0] = '\0';
55 else
56 strcpy(instr, init);
57
58 do_resize:
59 if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
60 return -ERRDISPLAYTOOSMALL;
61 if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
62 return -ERRDISPLAYTOOSMALL;
63
64 /* center dialog box on screen */
65 x = (getmaxx(stdscr) - width) / 2;
66 y = (getmaxy(stdscr) - height) / 2;
67
68 draw_shadow(stdscr, y, x, height, width);
69
70 dialog = newwin(height, width, y, x);
71 keypad(dialog, TRUE);
72
73 draw_box(dialog, 0, 0, height, width,
74 dlg.dialog.atr, dlg.border.atr);
75 wattrset(dialog, dlg.border.atr);
76 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
77 for (i = 0; i < width - 2; i++)
78 waddch(dialog, ACS_HLINE);
79 wattrset(dialog, dlg.dialog.atr);
80 waddch(dialog, ACS_RTEE);
81
82 print_title(dialog, title, width);
83
84 wattrset(dialog, dlg.dialog.atr);
85 print_autowrap(dialog, prompt, width - 2, 1, 3);
86
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;
92 draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
93 dlg.dialog.atr, dlg.border.atr);
94
95 print_buttons(dialog, height, width, 0);
96
97 /* Set up the initial value */
98 wmove(dialog, box_y, box_x);
99 wattrset(dialog, dlg.inputbox.atr);
100
101 len = strlen(instr);
102 pos = len;
103
104 if (len >= box_width) {
105 show_x = len - box_width + 1;
106 input_x = box_width - 1;
107 for (i = 0; i < box_width - 1; i++)
108 waddch(dialog, instr[show_x + i]);
109 } else {
110 show_x = 0;
111 input_x = len;
112 waddstr(dialog, instr);
113 }
114
115 wmove(dialog, box_y, box_x + input_x);
116
117 wrefresh(dialog);
118
119 while (key != KEY_ESC) {
120 key = wgetch(dialog);
121
122 if (button == -1) { /* Input box selected */
123 switch (key) {
124 case TAB:
125 case KEY_UP:
126 case KEY_DOWN:
127 break;
128 case KEY_BACKSPACE:
129 case 8: /* ^H */
130 case 127: /* ^? */
131 if (pos) {
132 wattrset(dialog, dlg.inputbox.atr);
133 if (input_x == 0) {
134 show_x--;
135 } else
136 input_x--;
137
138 if (pos < len) {
139 for (i = pos - 1; i < len; i++) {
140 instr[i] = instr[i+1];
141 }
142 }
143
144 pos--;
145 len--;
146 instr[len] = '\0';
147 wmove(dialog, box_y, box_x);
148 for (i = 0; i < box_width; i++) {
149 if (!instr[show_x + i]) {
150 waddch(dialog, ' ');
151 break;
152 }
153 waddch(dialog, instr[show_x + i]);
154 }
155 wmove(dialog, box_y, input_x + box_x);
156 wrefresh(dialog);
157 }
158 continue;
159 case KEY_LEFT:
160 if (pos > 0) {
161 if (input_x > 0) {
162 wmove(dialog, box_y, --input_x + box_x);
163 } else if (input_x == 0) {
164 show_x--;
165 wmove(dialog, box_y, box_x);
166 for (i = 0; i < box_width; i++) {
167 if (!instr[show_x + i]) {
168 waddch(dialog, ' ');
169 break;
170 }
171 waddch(dialog, instr[show_x + i]);
172 }
173 wmove(dialog, box_y, box_x);
174 }
175 pos--;
176 }
177 continue;
178 case KEY_RIGHT:
179 if (pos < len) {
180 if (input_x < box_width - 1) {
181 wmove(dialog, box_y, ++input_x + box_x);
182 } else if (input_x == box_width - 1) {
183 show_x++;
184 wmove(dialog, box_y, box_x);
185 for (i = 0; i < box_width; i++) {
186 if (!instr[show_x + i]) {
187 waddch(dialog, ' ');
188 break;
189 }
190 waddch(dialog, instr[show_x + i]);
191 }
192 wmove(dialog, box_y, input_x + box_x);
193 }
194 pos++;
195 }
196 continue;
197 default:
198 if (key < 0x100 && isprint(key)) {
199 if (len < MAX_LEN) {
200 wattrset(dialog, dlg.inputbox.atr);
201 if (pos < len) {
202 for (i = len; i > pos; i--)
203 instr[i] = instr[i-1];
204 instr[pos] = key;
205 } else {
206 instr[len] = key;
207 }
208 pos++;
209 len++;
210 instr[len] = '\0';
211
212 if (input_x == box_width - 1) {
213 show_x++;
214 } else {
215 input_x++;
216 }
217
218 wmove(dialog, box_y, box_x);
219 for (i = 0; i < box_width; i++) {
220 if (!instr[show_x + i]) {
221 waddch(dialog, ' ');
222 break;
223 }
224 waddch(dialog, instr[show_x + i]);
225 }
226 wmove(dialog, box_y, input_x + box_x);
227 wrefresh(dialog);
228 } else
229 flash(); /* Alarm user about overflow */
230 continue;
231 }
232 }
233 }
234 switch (key) {
235 case 'O':
236 case 'o':
237 delwin(dialog);
238 return 0;
239 case 'H':
240 case 'h':
241 delwin(dialog);
242 return 1;
243 case KEY_UP:
244 case KEY_LEFT:
245 switch (button) {
246 case -1:
247 button = 1; /* Indicates "Help" button is selected */
248 print_buttons(dialog, height, width, 1);
249 break;
250 case 0:
251 button = -1; /* Indicates input box is selected */
252 print_buttons(dialog, height, width, 0);
253 wmove(dialog, box_y, box_x + input_x);
254 wrefresh(dialog);
255 break;
256 case 1:
257 button = 0; /* Indicates "OK" button is selected */
258 print_buttons(dialog, height, width, 0);
259 break;
260 }
261 break;
262 case TAB:
263 case KEY_DOWN:
264 case KEY_RIGHT:
265 switch (button) {
266 case -1:
267 button = 0; /* Indicates "OK" button is selected */
268 print_buttons(dialog, height, width, 0);
269 break;
270 case 0:
271 button = 1; /* Indicates "Help" button is selected */
272 print_buttons(dialog, height, width, 1);
273 break;
274 case 1:
275 button = -1; /* Indicates input box is selected */
276 print_buttons(dialog, height, width, 0);
277 wmove(dialog, box_y, box_x + input_x);
278 wrefresh(dialog);
279 break;
280 }
281 break;
282 case ' ':
283 case '\n':
284 delwin(dialog);
285 return (button == -1 ? 0 : button);
286 case 'X':
287 case 'x':
288 key = KEY_ESC;
289 break;
290 case KEY_ESC:
291 key = on_key_esc(dialog);
292 break;
293 case KEY_RESIZE:
294 delwin(dialog);
295 on_key_resize();
296 goto do_resize;
297 }
298 }
299
300 delwin(dialog);
301 return KEY_ESC; /* ESC pressed */
302 }