1 // SPDX-License-Identifier: GPL-2.0+
3 * inputbox.c -- implements the input box
5 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
11 char dialog_input_result[MAX_LEN + 1];
14 * Print the termination buttons
16 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
18 int x = width / 2 - 11;
21 print_button(dialog, gettext(" Ok "), y, x, selected == 0);
22 print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
24 wmove(dialog, y, x + 1 + 14 * selected);
29 * Display a dialog box for inputing a string
31 int dialog_inputbox(const char *title, const char *prompt, int height, int width,
34 int i, x, y, box_y, box_x, box_width;
35 int input_x = 0, key = 0, button = -1;
37 char *instr = dialog_input_result;
46 if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
47 return -ERRDISPLAYTOOSMALL;
48 if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
49 return -ERRDISPLAYTOOSMALL;
51 /* center dialog box on screen */
52 x = (getmaxx(stdscr) - width) / 2;
53 y = (getmaxy(stdscr) - height) / 2;
55 draw_shadow(stdscr, y, x, height, width);
57 dialog = newwin(height, width, y, x);
60 draw_box(dialog, 0, 0, height, width,
61 dlg.dialog.atr, dlg.border.atr);
62 wattrset(dialog, dlg.border.atr);
63 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
64 for (i = 0; i < width - 2; i++)
65 waddch(dialog, ACS_HLINE);
66 wattrset(dialog, dlg.dialog.atr);
67 waddch(dialog, ACS_RTEE);
69 print_title(dialog, title, width);
71 wattrset(dialog, dlg.dialog.atr);
72 print_autowrap(dialog, prompt, width - 2, 1, 3);
74 /* Draw the input field box */
75 box_width = width - 6;
78 box_x = (width - box_width) / 2;
79 draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
80 dlg.dialog.atr, dlg.border.atr);
82 print_buttons(dialog, height, width, 0);
84 /* Set up the initial value */
85 wmove(dialog, box_y, box_x);
86 wattrset(dialog, dlg.inputbox.atr);
91 if (len >= box_width) {
92 show_x = len - box_width + 1;
93 input_x = box_width - 1;
94 for (i = 0; i < box_width - 1; i++)
95 waddch(dialog, instr[show_x + i]);
99 waddstr(dialog, instr);
102 wmove(dialog, box_y, box_x + input_x);
106 while (key != KEY_ESC) {
107 key = wgetch(dialog);
109 if (button == -1) { /* Input box selected */
118 wattrset(dialog, dlg.inputbox.atr);
125 for (i = pos - 1; i < len; i++) {
126 instr[i] = instr[i+1];
133 wmove(dialog, box_y, box_x);
134 for (i = 0; i < box_width; i++) {
135 if (!instr[show_x + i]) {
139 waddch(dialog, instr[show_x + i]);
141 wmove(dialog, box_y, input_x + box_x);
148 wmove(dialog, box_y, --input_x + box_x);
149 } else if (input_x == 0) {
151 wmove(dialog, box_y, box_x);
152 for (i = 0; i < box_width; i++) {
153 if (!instr[show_x + i]) {
157 waddch(dialog, instr[show_x + i]);
159 wmove(dialog, box_y, box_x);
166 if (input_x < box_width - 1) {
167 wmove(dialog, box_y, ++input_x + box_x);
168 } else if (input_x == box_width - 1) {
170 wmove(dialog, box_y, box_x);
171 for (i = 0; i < box_width; i++) {
172 if (!instr[show_x + i]) {
176 waddch(dialog, instr[show_x + i]);
178 wmove(dialog, box_y, input_x + box_x);
184 if (key < 0x100 && isprint(key)) {
186 wattrset(dialog, dlg.inputbox.atr);
188 for (i = len; i > pos; i--)
189 instr[i] = instr[i-1];
198 if (input_x == box_width - 1) {
204 wmove(dialog, box_y, box_x);
205 for (i = 0; i < box_width; i++) {
206 if (!instr[show_x + i]) {
210 waddch(dialog, instr[show_x + i]);
212 wmove(dialog, box_y, input_x + box_x);
215 flash(); /* Alarm user about overflow */
233 button = 1; /* Indicates "Help" button is selected */
234 print_buttons(dialog, height, width, 1);
237 button = -1; /* Indicates input box is selected */
238 print_buttons(dialog, height, width, 0);
239 wmove(dialog, box_y, box_x + input_x);
243 button = 0; /* Indicates "OK" button is selected */
244 print_buttons(dialog, height, width, 0);
253 button = 0; /* Indicates "OK" button is selected */
254 print_buttons(dialog, height, width, 0);
257 button = 1; /* Indicates "Help" button is selected */
258 print_buttons(dialog, height, width, 1);
261 button = -1; /* Indicates input box is selected */
262 print_buttons(dialog, height, width, 0);
263 wmove(dialog, box_y, box_x + input_x);
271 return (button == -1 ? 0 : button);
277 key = on_key_esc(dialog);
287 return KEY_ESC; /* ESC pressed */