1 /* vi: set sw=4 ts=4: */
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)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 char dialog_input_result[MAX_LEN + 1];
28 * Print the termination buttons
31 print_buttons(WINDOW *dialog, int height, int width, int selected)
33 int x = width / 2 - 11;
36 print_button (dialog, " Ok ", y, x, selected==0);
37 print_button (dialog, " Help ", y, x + 14, selected==1);
39 wmove(dialog, y, x+1+14*selected);
44 * Display a dialog box for inputing a string
47 dialog_inputbox (const char *title, const char *prompt, int height, int width,
50 int i, x, y, box_y, box_x, box_width;
51 int input_x = 0, scroll = 0, key = 0, button = -1;
52 char *instr = dialog_input_result;
55 /* center dialog box on screen */
56 x = (COLS - width) / 2;
57 y = (LINES - height) / 2;
60 draw_shadow (stdscr, y, x, height, width);
62 dialog = newwin (height, width, y, x);
63 keypad (dialog, TRUE);
65 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
66 wattrset (dialog, border_attr);
67 mvwaddch (dialog, height-3, 0, ACS_LTEE);
68 for (i = 0; i < width - 2; i++)
69 waddch (dialog, ACS_HLINE);
70 wattrset (dialog, dialog_attr);
71 waddch (dialog, ACS_RTEE);
73 if (title != NULL && strlen(title) >= width-2 ) {
74 /* truncate long title -- mec */
75 char * title2 = malloc(width-2+1);
76 memcpy( title2, title, width-2 );
77 title2[width-2] = '\0';
82 wattrset (dialog, title_attr);
83 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
84 waddstr (dialog, (char *)title);
88 wattrset (dialog, dialog_attr);
89 print_autowrap (dialog, prompt, width - 2, 1, 3);
91 /* Draw the input field box */
92 box_width = width - 6;
95 box_x = (width - box_width) / 2;
96 draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
97 border_attr, dialog_attr);
99 print_buttons(dialog, height, width, 0);
101 /* Set up the initial value */
102 wmove (dialog, box_y, box_x);
103 wattrset (dialog, inputbox_attr);
108 strcpy (instr, init);
110 input_x = strlen (instr);
112 if (input_x >= box_width) {
113 scroll = input_x - box_width + 1;
114 input_x = box_width - 1;
115 for (i = 0; i < box_width - 1; i++)
116 waddch (dialog, instr[scroll + i]);
118 waddstr (dialog, instr);
120 wmove (dialog, box_y, box_x + input_x);
125 key = wgetch (dialog);
127 if (button == -1) { /* Input box selected */
139 if (input_x || scroll) {
140 wattrset (dialog, inputbox_attr);
142 scroll = scroll < box_width - 1 ?
143 0 : scroll - (box_width - 1);
144 wmove (dialog, box_y, box_x);
145 for (i = 0; i < box_width; i++)
146 waddch (dialog, instr[scroll + input_x + i] ?
147 instr[scroll + input_x + i] : ' ');
148 input_x = strlen (instr) - scroll;
151 instr[scroll + input_x] = '\0';
152 mvwaddch (dialog, box_y, input_x + box_x, ' ');
153 wmove (dialog, box_y, input_x + box_x);
158 if (key < 0x100 && isprint (key)) {
159 if (scroll + input_x < MAX_LEN) {
160 wattrset (dialog, inputbox_attr);
161 instr[scroll + input_x] = key;
162 instr[scroll + input_x + 1] = '\0';
163 if (input_x == box_width - 1) {
165 wmove (dialog, box_y, box_x);
166 for (i = 0; i < box_width - 1; i++)
167 waddch (dialog, instr[scroll + i]);
169 wmove (dialog, box_y, input_x++ + box_x);
170 waddch (dialog, key);
174 flash (); /* Alarm user about overflow */
192 button = 1; /* Indicates "Cancel" button is selected */
193 print_buttons(dialog, height, width, 1);
196 button = -1; /* Indicates input box is selected */
197 print_buttons(dialog, height, width, 0);
198 wmove (dialog, box_y, box_x + input_x);
202 button = 0; /* Indicates "OK" button is selected */
203 print_buttons(dialog, height, width, 0);
212 button = 0; /* Indicates "OK" button is selected */
213 print_buttons(dialog, height, width, 0);
216 button = 1; /* Indicates "Cancel" button is selected */
217 print_buttons(dialog, height, width, 1);
220 button = -1; /* Indicates input box is selected */
221 print_buttons(dialog, height, width, 0);
222 wmove (dialog, box_y, box_x + input_x);
230 return (button == -1 ? 0 : button);
240 return -1; /* ESC pressed */