]>
Commit | Line | Data |
---|---|---|
0a9064fb MY |
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 | * | |
5b8031cc | 7 | * SPDX-License-Identifier: GPL-2.0+ |
0a9064fb MY |
8 | */ |
9 | ||
10 | #include "dialog.h" | |
11 | ||
12 | char dialog_input_result[MAX_LEN + 1]; | |
13 | ||
14 | /* | |
15 | * Print the termination buttons | |
16 | */ | |
17 | static void print_buttons(WINDOW * dialog, int height, int width, int selected) | |
18 | { | |
19 | int x = width / 2 - 11; | |
20 | int y = height - 2; | |
21 | ||
22 | print_button(dialog, gettext(" Ok "), y, x, selected == 0); | |
23 | print_button(dialog, gettext(" Help "), y, x + 14, selected == 1); | |
24 | ||
25 | wmove(dialog, y, x + 1 + 14 * selected); | |
26 | wrefresh(dialog); | |
27 | } | |
28 | ||
29 | /* | |
30 | * Display a dialog box for inputing a string | |
31 | */ | |
32 | int dialog_inputbox(const char *title, const char *prompt, int height, int width, | |
33 | const char *init) | |
34 | { | |
35 | int i, x, y, box_y, box_x, box_width; | |
36 | int input_x = 0, key = 0, button = -1; | |
37 | int show_x, len, pos; | |
38 | char *instr = dialog_input_result; | |
39 | WINDOW *dialog; | |
40 | ||
41 | if (!init) | |
42 | instr[0] = '\0'; | |
43 | else | |
44 | strcpy(instr, init); | |
45 | ||
46 | do_resize: | |
47 | if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN)) | |
48 | return -ERRDISPLAYTOOSMALL; | |
49 | if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN)) | |
50 | return -ERRDISPLAYTOOSMALL; | |
51 | ||
52 | /* center dialog box on screen */ | |
53 | x = (getmaxx(stdscr) - width) / 2; | |
54 | y = (getmaxy(stdscr) - height) / 2; | |
55 | ||
56 | draw_shadow(stdscr, y, x, height, width); | |
57 | ||
58 | dialog = newwin(height, width, y, x); | |
59 | keypad(dialog, TRUE); | |
60 | ||
61 | draw_box(dialog, 0, 0, height, width, | |
62 | dlg.dialog.atr, dlg.border.atr); | |
63 | wattrset(dialog, dlg.border.atr); | |
64 | mvwaddch(dialog, height - 3, 0, ACS_LTEE); | |
65 | for (i = 0; i < width - 2; i++) | |
66 | waddch(dialog, ACS_HLINE); | |
67 | wattrset(dialog, dlg.dialog.atr); | |
68 | waddch(dialog, ACS_RTEE); | |
69 | ||
70 | print_title(dialog, title, width); | |
71 | ||
72 | wattrset(dialog, dlg.dialog.atr); | |
73 | print_autowrap(dialog, prompt, width - 2, 1, 3); | |
74 | ||
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; | |
80 | draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2, | |
81 | dlg.dialog.atr, dlg.border.atr); | |
82 | ||
83 | print_buttons(dialog, height, width, 0); | |
84 | ||
85 | /* Set up the initial value */ | |
86 | wmove(dialog, box_y, box_x); | |
87 | wattrset(dialog, dlg.inputbox.atr); | |
88 | ||
89 | len = strlen(instr); | |
90 | pos = len; | |
91 | ||
92 | if (len >= box_width) { | |
93 | show_x = len - box_width + 1; | |
94 | input_x = box_width - 1; | |
95 | for (i = 0; i < box_width - 1; i++) | |
96 | waddch(dialog, instr[show_x + i]); | |
97 | } else { | |
98 | show_x = 0; | |
99 | input_x = len; | |
100 | waddstr(dialog, instr); | |
101 | } | |
102 | ||
103 | wmove(dialog, box_y, box_x + input_x); | |
104 | ||
105 | wrefresh(dialog); | |
106 | ||
107 | while (key != KEY_ESC) { | |
108 | key = wgetch(dialog); | |
109 | ||
110 | if (button == -1) { /* Input box selected */ | |
111 | switch (key) { | |
112 | case TAB: | |
113 | case KEY_UP: | |
114 | case KEY_DOWN: | |
115 | break; | |
116 | case KEY_BACKSPACE: | |
117 | case 127: | |
118 | if (pos) { | |
119 | wattrset(dialog, dlg.inputbox.atr); | |
120 | if (input_x == 0) { | |
121 | show_x--; | |
122 | } else | |
123 | input_x--; | |
124 | ||
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 | } | |
142 | wmove(dialog, box_y, input_x + box_x); | |
143 | wrefresh(dialog); | |
144 | } | |
145 | continue; | |
146 | 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; | |
184 | default: | |
185 | if (key < 0x100 && isprint(key)) { | |
186 | if (len < MAX_LEN) { | |
187 | wattrset(dialog, dlg.inputbox.atr); | |
188 | if (pos < len) { | |
189 | for (i = len; i > pos; i--) | |
190 | instr[i] = instr[i-1]; | |
191 | instr[pos] = key; | |
192 | } else { | |
193 | instr[len] = key; | |
194 | } | |
195 | 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); | |
214 | wrefresh(dialog); | |
215 | } else | |
216 | flash(); /* Alarm user about overflow */ | |
217 | continue; | |
218 | } | |
219 | } | |
220 | } | |
221 | 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: | |
234 | button = 1; /* Indicates "Help" button is selected */ | |
235 | 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: | |
258 | button = 1; /* Indicates "Help" button is selected */ | |
259 | 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': | |
275 | key = KEY_ESC; | |
276 | break; | |
277 | case KEY_ESC: | |
278 | key = on_key_esc(dialog); | |
279 | break; | |
280 | case KEY_RESIZE: | |
281 | delwin(dialog); | |
282 | on_key_resize(); | |
283 | goto do_resize; | |
284 | } | |
285 | } | |
286 | ||
287 | delwin(dialog); | |
288 | return KEY_ESC; /* ESC pressed */ | |
289 | } |