2 * inputbox.c -- implements the input box
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
7 * SPDX-License-Identifier: GPL-2.0+
12 char dialog_input_result[MAX_LEN + 1];
15 * Print the termination buttons
17 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
19 int x = width / 2 - 11;
22 print_button(dialog, gettext(" Ok "), y, x, selected == 0);
23 print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
25 wmove(dialog, y, x + 1 + 14 * selected);
30 * Display a dialog box for inputing a string
32 int dialog_inputbox(const char *title, const char *prompt, int height, int width,
35 int i, x, y, box_y, box_x, box_width;
36 int input_x = 0, key = 0, button = -1;
38 char *instr = dialog_input_result;
47 if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
48 return -ERRDISPLAYTOOSMALL;
49 if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
50 return -ERRDISPLAYTOOSMALL;
52 /* center dialog box on screen */
53 x = (getmaxx(stdscr) - width) / 2;
54 y = (getmaxy(stdscr) - height) / 2;
56 draw_shadow(stdscr, y, x, height, width);
58 dialog = newwin(height, width, y, x);
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);
70 print_title(dialog, title, width);
72 wattrset(dialog, dlg.dialog.atr);
73 print_autowrap(dialog, prompt, width - 2, 1, 3);
75 /* Draw the input field box */
76 box_width = width - 6;
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);
83 print_buttons(dialog, height, width, 0);
85 /* Set up the initial value */
86 wmove(dialog, box_y, box_x);
87 wattrset(dialog, dlg.inputbox.atr);
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]);
100 waddstr(dialog, instr);
103 wmove(dialog, box_y, box_x + input_x);
107 while (key != KEY_ESC) {
108 key = wgetch(dialog);
110 if (button == -1) { /* Input box selected */
119 wattrset(dialog, dlg.inputbox.atr);
126 for (i = pos - 1; i < len; i++) {
127 instr[i] = instr[i+1];
134 wmove(dialog, box_y, box_x);
135 for (i = 0; i < box_width; i++) {
136 if (!instr[show_x + i]) {
140 waddch(dialog, instr[show_x + i]);
142 wmove(dialog, box_y, input_x + box_x);
149 wmove(dialog, box_y, --input_x + box_x);
150 } else if (input_x == 0) {
152 wmove(dialog, box_y, box_x);
153 for (i = 0; i < box_width; i++) {
154 if (!instr[show_x + i]) {
158 waddch(dialog, instr[show_x + i]);
160 wmove(dialog, box_y, box_x);
167 if (input_x < box_width - 1) {
168 wmove(dialog, box_y, ++input_x + box_x);
169 } else if (input_x == box_width - 1) {
171 wmove(dialog, box_y, box_x);
172 for (i = 0; i < box_width; i++) {
173 if (!instr[show_x + i]) {
177 waddch(dialog, instr[show_x + i]);
179 wmove(dialog, box_y, input_x + box_x);
185 if (key < 0x100 && isprint(key)) {
187 wattrset(dialog, dlg.inputbox.atr);
189 for (i = len; i > pos; i--)
190 instr[i] = instr[i-1];
199 if (input_x == box_width - 1) {
205 wmove(dialog, box_y, box_x);
206 for (i = 0; i < box_width; i++) {
207 if (!instr[show_x + i]) {
211 waddch(dialog, instr[show_x + i]);
213 wmove(dialog, box_y, input_x + box_x);
216 flash(); /* Alarm user about overflow */
234 button = 1; /* Indicates "Help" button is selected */
235 print_buttons(dialog, height, width, 1);
238 button = -1; /* Indicates input box is selected */
239 print_buttons(dialog, height, width, 0);
240 wmove(dialog, box_y, box_x + input_x);
244 button = 0; /* Indicates "OK" button is selected */
245 print_buttons(dialog, height, width, 0);
254 button = 0; /* Indicates "OK" button is selected */
255 print_buttons(dialog, height, width, 0);
258 button = 1; /* Indicates "Help" button is selected */
259 print_buttons(dialog, height, width, 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);
272 return (button == -1 ? 0 : button);
278 key = on_key_esc(dialog);
288 return KEY_ESC; /* ESC pressed */