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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 char dialog_input_result[MAX_LEN + 1];
27 * Print the termination buttons
29 static void print_buttons(WINDOW * dialog, int height, int width, int selected)
31 int x = width / 2 - 11;
34 print_button(dialog, gettext(" Ok "), y, x, selected == 0);
35 print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
37 wmove(dialog, y, x + 1 + 14 * selected);
42 * Display a dialog box for inputing a string
44 int dialog_inputbox(const char *title, const char *prompt, int height, int width,
47 int i, x, y, box_y, box_x, box_width;
48 int input_x = 0, key = 0, button = -1;
50 char *instr = dialog_input_result;
59 if (getmaxy(stdscr) <= (height - 2))
60 return -ERRDISPLAYTOOSMALL;
61 if (getmaxx(stdscr) <= (width - 2))
62 return -ERRDISPLAYTOOSMALL;
64 /* center dialog box on screen */
65 x = (COLS - width) / 2;
66 y = (LINES - height) / 2;
68 draw_shadow(stdscr, y, x, height, width);
70 dialog = newwin(height, width, y, x);
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);
82 print_title(dialog, title, width);
84 wattrset(dialog, dlg.dialog.atr);
85 print_autowrap(dialog, prompt, width - 2, 1, 3);
87 /* Draw the input field box */
88 box_width = width - 6;
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);
95 print_buttons(dialog, height, width, 0);
97 /* Set up the initial value */
98 wmove(dialog, box_y, box_x);
99 wattrset(dialog, dlg.inputbox.atr);
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]);
112 waddstr(dialog, instr);
115 wmove(dialog, box_y, box_x + input_x);
119 while (key != KEY_ESC) {
120 key = wgetch(dialog);
122 if (button == -1) { /* Input box selected */
131 wattrset(dialog, dlg.inputbox.atr);
138 for (i = pos - 1; i < len; i++) {
139 instr[i] = instr[i+1];
146 wmove(dialog, box_y, box_x);
147 for (i = 0; i < box_width; i++) {
148 if (!instr[show_x + i]) {
152 waddch(dialog, instr[show_x + i]);
154 wmove(dialog, box_y, input_x + box_x);
161 wmove(dialog, box_y, --input_x + box_x);
162 } else if (input_x == 0) {
164 wmove(dialog, box_y, box_x);
165 for (i = 0; i < box_width; i++) {
166 if (!instr[show_x + i]) {
170 waddch(dialog, instr[show_x + i]);
172 wmove(dialog, box_y, box_x);
179 if (input_x < box_width - 1) {
180 wmove(dialog, box_y, ++input_x + box_x);
181 } else if (input_x == box_width - 1) {
183 wmove(dialog, box_y, box_x);
184 for (i = 0; i < box_width; i++) {
185 if (!instr[show_x + i]) {
189 waddch(dialog, instr[show_x + i]);
191 wmove(dialog, box_y, input_x + box_x);
197 if (key < 0x100 && isprint(key)) {
199 wattrset(dialog, dlg.inputbox.atr);
201 for (i = len; i > pos; i--)
202 instr[i] = instr[i-1];
211 if (input_x == box_width - 1) {
217 wmove(dialog, box_y, box_x);
218 for (i = 0; i < box_width; i++) {
219 if (!instr[show_x + i]) {
223 waddch(dialog, instr[show_x + i]);
225 wmove(dialog, box_y, input_x + box_x);
228 flash(); /* Alarm user about overflow */
246 button = 1; /* Indicates "Help" button is selected */
247 print_buttons(dialog, height, width, 1);
250 button = -1; /* Indicates input box is selected */
251 print_buttons(dialog, height, width, 0);
252 wmove(dialog, box_y, box_x + input_x);
256 button = 0; /* Indicates "OK" button is selected */
257 print_buttons(dialog, height, width, 0);
266 button = 0; /* Indicates "OK" button is selected */
267 print_buttons(dialog, height, width, 0);
270 button = 1; /* Indicates "Help" button is selected */
271 print_buttons(dialog, height, width, 1);
274 button = -1; /* Indicates input box is selected */
275 print_buttons(dialog, height, width, 0);
276 wmove(dialog, box_y, box_x + input_x);
284 return (button == -1 ? 0 : button);
290 key = on_key_esc(dialog);
300 return KEY_ESC; /* ESC pressed */