blob: 9e969156726930cd1d352c1016907bab69de2785 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inputbox.c -- implements the input box
3 *
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6 *
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.
11 *
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.
16 *
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.
20 */
21
22#include "dialog.h"
23
Keenan Pepper84c2a2e2005-07-27 14:14:00 -040024char dialog_input_result[MAX_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Print the termination buttons
28 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010029static void print_buttons(WINDOW * dialog, int height, int width, int selected)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010031 int x = width / 2 - 11;
32 int y = height - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010034 print_button(dialog, " Ok ", y, x, selected == 0);
35 print_button(dialog, " Help ", y, x + 14, selected == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010037 wmove(dialog, y, x + 1 + 14 * selected);
38 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41/*
42 * Display a dialog box for inputing a string
43 */
44int
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010045dialog_inputbox(const char *title, const char *prompt, int height, int width,
46 const char *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010048 int i, x, y, box_y, box_x, box_width;
49 int input_x = 0, scroll = 0, key = 0, button = -1;
50 char *instr = dialog_input_result;
51 WINDOW *dialog;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010053 /* center dialog box on screen */
54 x = (COLS - width) / 2;
55 y = (LINES - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010057 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010059 dialog = newwin(height, width, y, x);
60 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010062 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
63 wattrset(dialog, border_attr);
64 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
65 for (i = 0; i < width - 2; i++)
66 waddch(dialog, ACS_HLINE);
67 wattrset(dialog, dialog_attr);
68 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010070 if (title != NULL && strlen(title) >= width - 2) {
71 /* truncate long title -- mec */
72 char *title2 = malloc(width - 2 + 1);
73 memcpy(title2, title, width - 2);
74 title2[width - 2] = '\0';
75 title = title2;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010078 if (title != NULL) {
79 wattrset(dialog, title_attr);
80 mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' ');
81 waddstr(dialog, (char *)title);
82 waddch(dialog, ' ');
83 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010085 wattrset(dialog, dialog_attr);
86 print_autowrap(dialog, prompt, width - 2, 1, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010088 /* Draw the input field box */
89 box_width = width - 6;
90 getyx(dialog, y, x);
91 box_y = y + 2;
92 box_x = (width - box_width) / 2;
93 draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
94 border_attr, dialog_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010096 print_buttons(dialog, height, width, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010098 /* Set up the initial value */
99 wmove(dialog, box_y, box_x);
100 wattrset(dialog, inputbox_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100102 if (!init)
103 instr[0] = '\0';
104 else
105 strcpy(instr, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100107 input_x = strlen(instr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100109 if (input_x >= box_width) {
110 scroll = input_x - box_width + 1;
111 input_x = box_width - 1;
112 for (i = 0; i < box_width - 1; i++)
113 waddch(dialog, instr[scroll + i]);
114 } else
115 waddstr(dialog, instr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100117 wmove(dialog, box_y, box_x + input_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100119 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100121 while (key != ESC) {
122 key = wgetch(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100124 if (button == -1) { /* Input box selected */
125 switch (key) {
126 case TAB:
127 case KEY_UP:
128 case KEY_DOWN:
129 break;
130 case KEY_LEFT:
131 continue;
132 case KEY_RIGHT:
133 continue;
134 case KEY_BACKSPACE:
135 case 127:
136 if (input_x || scroll) {
137 wattrset(dialog, inputbox_attr);
138 if (!input_x) {
139 scroll =
140 scroll <
141 box_width - 1 ? 0 : scroll -
142 (box_width - 1);
143 wmove(dialog, box_y, box_x);
144 for (i = 0; i < box_width; i++)
145 waddch(dialog,
146 instr[scroll +
147 input_x +
148 i] ?
149 instr[scroll +
150 input_x +
151 i] : ' ');
152 input_x =
153 strlen(instr) - scroll;
154 } else
155 input_x--;
156 instr[scroll + input_x] = '\0';
157 mvwaddch(dialog, box_y, input_x + box_x,
158 ' ');
159 wmove(dialog, box_y, input_x + box_x);
160 wrefresh(dialog);
161 }
162 continue;
163 default:
164 if (key < 0x100 && isprint(key)) {
165 if (scroll + input_x < MAX_LEN) {
166 wattrset(dialog, inputbox_attr);
167 instr[scroll + input_x] = key;
168 instr[scroll + input_x + 1] =
169 '\0';
170 if (input_x == box_width - 1) {
171 scroll++;
172 wmove(dialog, box_y,
173 box_x);
174 for (i = 0;
175 i < box_width - 1;
176 i++)
177 waddch(dialog,
178 instr
179 [scroll +
180 i]);
181 } else {
182 wmove(dialog, box_y,
183 input_x++ +
184 box_x);
185 waddch(dialog, key);
186 }
187 wrefresh(dialog);
188 } else
189 flash(); /* Alarm user about overflow */
190 continue;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100194 switch (key) {
195 case 'O':
196 case 'o':
197 delwin(dialog);
198 return 0;
199 case 'H':
200 case 'h':
201 delwin(dialog);
202 return 1;
203 case KEY_UP:
204 case KEY_LEFT:
205 switch (button) {
206 case -1:
207 button = 1; /* Indicates "Cancel" button is selected */
208 print_buttons(dialog, height, width, 1);
209 break;
210 case 0:
211 button = -1; /* Indicates input box is selected */
212 print_buttons(dialog, height, width, 0);
213 wmove(dialog, box_y, box_x + input_x);
214 wrefresh(dialog);
215 break;
216 case 1:
217 button = 0; /* Indicates "OK" button is selected */
218 print_buttons(dialog, height, width, 0);
219 break;
220 }
221 break;
222 case TAB:
223 case KEY_DOWN:
224 case KEY_RIGHT:
225 switch (button) {
226 case -1:
227 button = 0; /* Indicates "OK" button is selected */
228 print_buttons(dialog, height, width, 0);
229 break;
230 case 0:
231 button = 1; /* Indicates "Cancel" button is selected */
232 print_buttons(dialog, height, width, 1);
233 break;
234 case 1:
235 button = -1; /* Indicates input box is selected */
236 print_buttons(dialog, height, width, 0);
237 wmove(dialog, box_y, box_x + input_x);
238 wrefresh(dialog);
239 break;
240 }
241 break;
242 case ' ':
243 case '\n':
244 delwin(dialog);
245 return (button == -1 ? 0 : button);
246 case 'X':
247 case 'x':
248 key = ESC;
249 case ESC:
250 break;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100254 delwin(dialog);
255 return -1; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}