blob: bc135c7093d921ce5a98415829a8f58a1074fb6a [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 */
Sam Ravnborgdec69da2005-11-19 21:56:20 +010044int dialog_inputbox(const char *title, const char *prompt, int height, int width,
45 const char *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010047 int i, x, y, box_y, box_x, box_width;
48 int input_x = 0, scroll = 0, key = 0, button = -1;
49 char *instr = dialog_input_result;
50 WINDOW *dialog;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010052 /* center dialog box on screen */
53 x = (COLS - width) / 2;
54 y = (LINES - height) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010056 draw_shadow(stdscr, y, x, height, width);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010058 dialog = newwin(height, width, y, x);
59 keypad(dialog, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010061 draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
62 wattrset(dialog, border_attr);
63 mvwaddch(dialog, height - 3, 0, ACS_LTEE);
64 for (i = 0; i < width - 2; i++)
65 waddch(dialog, ACS_HLINE);
66 wattrset(dialog, dialog_attr);
67 waddch(dialog, ACS_RTEE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010069 if (title != NULL && strlen(title) >= width - 2) {
70 /* truncate long title -- mec */
71 char *title2 = malloc(width - 2 + 1);
72 memcpy(title2, title, width - 2);
73 title2[width - 2] = '\0';
74 title = title2;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010077 if (title != NULL) {
78 wattrset(dialog, title_attr);
79 mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' ');
80 waddstr(dialog, (char *)title);
81 waddch(dialog, ' ');
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010084 wattrset(dialog, dialog_attr);
85 print_autowrap(dialog, prompt, width - 2, 1, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010087 /* Draw the input field box */
88 box_width = width - 6;
89 getyx(dialog, y, x);
90 box_y = y + 2;
91 box_x = (width - box_width) / 2;
Sam Ravnborgdec69da2005-11-19 21:56:20 +010092 draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2, border_attr, dialog_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010094 print_buttons(dialog, height, width, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010096 /* Set up the initial value */
97 wmove(dialog, box_y, box_x);
98 wattrset(dialog, inputbox_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100100 if (!init)
101 instr[0] = '\0';
102 else
103 strcpy(instr, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100105 input_x = strlen(instr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100107 if (input_x >= box_width) {
108 scroll = input_x - box_width + 1;
109 input_x = box_width - 1;
110 for (i = 0; i < box_width - 1; i++)
111 waddch(dialog, instr[scroll + i]);
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100112 } else {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100113 waddstr(dialog, instr);
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100116 wmove(dialog, box_y, box_x + input_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100118 wrefresh(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100120 while (key != ESC) {
121 key = wgetch(dialog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100123 if (button == -1) { /* Input box selected */
124 switch (key) {
125 case TAB:
126 case KEY_UP:
127 case KEY_DOWN:
128 break;
129 case KEY_LEFT:
130 continue;
131 case KEY_RIGHT:
132 continue;
133 case KEY_BACKSPACE:
134 case 127:
135 if (input_x || scroll) {
136 wattrset(dialog, inputbox_attr);
137 if (!input_x) {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100138 scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100139 wmove(dialog, box_y, box_x);
140 for (i = 0; i < box_width; i++)
141 waddch(dialog,
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100142 instr[scroll + input_x + i] ?
143 instr[scroll + input_x + i] : ' ');
144 input_x = strlen(instr) - scroll;
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100145 } else
146 input_x--;
147 instr[scroll + input_x] = '\0';
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100148 mvwaddch(dialog, box_y, input_x + box_x, ' ');
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100149 wmove(dialog, box_y, input_x + box_x);
150 wrefresh(dialog);
151 }
152 continue;
153 default:
154 if (key < 0x100 && isprint(key)) {
155 if (scroll + input_x < MAX_LEN) {
156 wattrset(dialog, inputbox_attr);
157 instr[scroll + input_x] = key;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100158 instr[scroll + input_x + 1] = '\0';
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100159 if (input_x == box_width - 1) {
160 scroll++;
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100161 wmove(dialog, box_y, box_x);
162 for (i = 0; i < box_width - 1; i++)
163 waddch(dialog, instr [scroll + i]);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100164 } else {
Sam Ravnborgdec69da2005-11-19 21:56:20 +0100165 wmove(dialog, box_y, input_x++ + box_x);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100166 waddch(dialog, key);
167 }
168 wrefresh(dialog);
169 } else
170 flash(); /* Alarm user about overflow */
171 continue;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100175 switch (key) {
176 case 'O':
177 case 'o':
178 delwin(dialog);
179 return 0;
180 case 'H':
181 case 'h':
182 delwin(dialog);
183 return 1;
184 case KEY_UP:
185 case KEY_LEFT:
186 switch (button) {
187 case -1:
188 button = 1; /* Indicates "Cancel" button is selected */
189 print_buttons(dialog, height, width, 1);
190 break;
191 case 0:
192 button = -1; /* Indicates input box is selected */
193 print_buttons(dialog, height, width, 0);
194 wmove(dialog, box_y, box_x + input_x);
195 wrefresh(dialog);
196 break;
197 case 1:
198 button = 0; /* Indicates "OK" button is selected */
199 print_buttons(dialog, height, width, 0);
200 break;
201 }
202 break;
203 case TAB:
204 case KEY_DOWN:
205 case KEY_RIGHT:
206 switch (button) {
207 case -1:
208 button = 0; /* Indicates "OK" button is selected */
209 print_buttons(dialog, height, width, 0);
210 break;
211 case 0:
212 button = 1; /* Indicates "Cancel" button is selected */
213 print_buttons(dialog, height, width, 1);
214 break;
215 case 1:
216 button = -1; /* Indicates input box is selected */
217 print_buttons(dialog, height, width, 0);
218 wmove(dialog, box_y, box_x + input_x);
219 wrefresh(dialog);
220 break;
221 }
222 break;
223 case ' ':
224 case '\n':
225 delwin(dialog);
226 return (button == -1 ? 0 : button);
227 case 'X':
228 case 'x':
229 key = ESC;
230 case ESC:
231 break;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100235 delwin(dialog);
236 return -1; /* ESC pressed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}