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 * 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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 char dialog_input_result[MAX_LEN + 1];
26 * Print the termination buttons
28 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
30 int x = width / 2 - 11;
33 print_button(dialog, gettext(" Ok "), y, x, selected == 0);
34 print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
36 wmove(dialog, y, x + 1 + 14 * selected);
41 * Display a dialog box for inputing a string
43 int dialog_inputbox(const char *title, const char *prompt, int height, int width,
46 int i, x, y, box_y, box_x, box_width;
47 int input_x = 0, scroll = 0, key = 0, button = -1;
48 char *instr = dialog_input_result;
57 if (getmaxy(stdscr) <= (height - 2))
58 return -ERRDISPLAYTOOSMALL;
59 if (getmaxx(stdscr) <= (width - 2))
60 return -ERRDISPLAYTOOSMALL;
62 /* center dialog box on screen */
63 x = (COLS - width) / 2;
64 y = (LINES - height) / 2;
66 draw_shadow(stdscr, y, x, height, width);
68 dialog = newwin(height, width, y, x);
71 draw_box(dialog, 0, 0, height, width,
72 dlg.dialog.atr, dlg.border.atr);
73 wattrset(dialog, dlg.border.atr);
74 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
75 for (i = 0; i < width - 2; i++)
76 waddch(dialog, ACS_HLINE);
77 wattrset(dialog, dlg.dialog.atr);
78 waddch(dialog, ACS_RTEE);
80 print_title(dialog, title, width);
82 wattrset(dialog, dlg.dialog.atr);
83 print_autowrap(dialog, prompt, width - 2, 1, 3);
85 /* Draw the input field box */
86 box_width = width - 6;
89 box_x = (width - box_width) / 2;
90 draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
91 dlg.dialog.atr, dlg.border.atr);
93 print_buttons(dialog, height, width, 0);
95 /* Set up the initial value */
96 wmove(dialog, box_y, box_x);
97 wattrset(dialog, dlg.inputbox.atr);
99 input_x = strlen(instr);
101 if (input_x >= box_width) {
102 scroll = input_x - box_width + 1;
103 input_x = box_width - 1;
104 for (i = 0; i < box_width - 1; i++)
105 waddch(dialog, instr[scroll + i]);
107 waddstr(dialog, instr);
110 wmove(dialog, box_y, box_x + input_x);
114 while (key != KEY_ESC) {
115 key = wgetch(dialog);
117 if (button == -1) { /* Input box selected */
129 if (input_x || scroll) {
130 wattrset(dialog, dlg.inputbox.atr);
132 scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
133 wmove(dialog, box_y, box_x);
134 for (i = 0; i < box_width; i++)
136 instr[scroll + input_x + i] ?
137 instr[scroll + input_x + i] : ' ');
138 input_x = strlen(instr) - scroll;
141 instr[scroll + input_x] = '\0';
142 mvwaddch(dialog, box_y, input_x + box_x, ' ');
143 wmove(dialog, box_y, input_x + box_x);
148 if (key < 0x100 && isprint(key)) {
149 if (scroll + input_x < MAX_LEN) {
150 wattrset(dialog, dlg.inputbox.atr);
151 instr[scroll + input_x] = key;
152 instr[scroll + input_x + 1] = '\0';
153 if (input_x == box_width - 1) {
155 wmove(dialog, box_y, box_x);
156 for (i = 0; i < box_width - 1; i++)
157 waddch(dialog, instr [scroll + i]);
159 wmove(dialog, box_y, input_x++ + box_x);
164 flash(); /* Alarm user about overflow */
182 button = 1; /* Indicates "Cancel" button is selected */
183 print_buttons(dialog, height, width, 1);
186 button = -1; /* Indicates input box is selected */
187 print_buttons(dialog, height, width, 0);
188 wmove(dialog, box_y, box_x + input_x);
192 button = 0; /* Indicates "OK" button is selected */
193 print_buttons(dialog, height, width, 0);
202 button = 0; /* Indicates "OK" button is selected */
203 print_buttons(dialog, height, width, 0);
206 button = 1; /* Indicates "Cancel" button is selected */
207 print_buttons(dialog, height, width, 1);
210 button = -1; /* Indicates input box is selected */
211 print_buttons(dialog, height, width, 0);
212 wmove(dialog, box_y, box_x + input_x);
220 return (button == -1 ? 0 : button);
226 key = on_key_esc(dialog);
236 return KEY_ESC; /* ESC pressed */