Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
84c2a2eb | 24 | char dialog_input_result[MAX_LEN + 1]; |
1da177e4 LT |
25 | |
26 | /* | |
27 | * Print the termination buttons | |
28 | */ | |
b1c5f1c6 | 29 | static void print_buttons(WINDOW * dialog, int height, int width, int selected) |
1da177e4 | 30 | { |
b1c5f1c6 SR |
31 | int x = width / 2 - 11; |
32 | int y = height - 2; | |
1da177e4 | 33 | |
b1c5f1c6 SR |
34 | print_button(dialog, " Ok ", y, x, selected == 0); |
35 | print_button(dialog, " Help ", y, x + 14, selected == 1); | |
1da177e4 | 36 | |
b1c5f1c6 SR |
37 | wmove(dialog, y, x + 1 + 14 * selected); |
38 | wrefresh(dialog); | |
1da177e4 LT |
39 | } |
40 | ||
41 | /* | |
42 | * Display a dialog box for inputing a string | |
43 | */ | |
dec69da8 SR |
44 | int dialog_inputbox(const char *title, const char *prompt, int height, int width, |
45 | const char *init) | |
1da177e4 | 46 | { |
b1c5f1c6 SR |
47 | 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; | |
51 | ||
52 | /* center dialog box on screen */ | |
53 | x = (COLS - width) / 2; | |
54 | y = (LINES - height) / 2; | |
55 | ||
56 | draw_shadow(stdscr, y, x, height, width); | |
57 | ||
58 | dialog = newwin(height, width, y, x); | |
59 | keypad(dialog, TRUE); | |
60 | ||
61 | 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); | |
68 | ||
fa7009d5 | 69 | print_title(dialog, title, width); |
b1c5f1c6 SR |
70 | |
71 | wattrset(dialog, dialog_attr); | |
72 | print_autowrap(dialog, prompt, width - 2, 1, 3); | |
73 | ||
74 | /* Draw the input field box */ | |
75 | box_width = width - 6; | |
76 | getyx(dialog, y, x); | |
77 | box_y = y + 2; | |
78 | box_x = (width - box_width) / 2; | |
dec69da8 | 79 | draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2, border_attr, dialog_attr); |
b1c5f1c6 SR |
80 | |
81 | print_buttons(dialog, height, width, 0); | |
82 | ||
83 | /* Set up the initial value */ | |
84 | wmove(dialog, box_y, box_x); | |
85 | wattrset(dialog, inputbox_attr); | |
86 | ||
87 | if (!init) | |
88 | instr[0] = '\0'; | |
89 | else | |
90 | strcpy(instr, init); | |
91 | ||
92 | input_x = strlen(instr); | |
93 | ||
94 | if (input_x >= box_width) { | |
95 | scroll = input_x - box_width + 1; | |
96 | input_x = box_width - 1; | |
97 | for (i = 0; i < box_width - 1; i++) | |
98 | waddch(dialog, instr[scroll + i]); | |
dec69da8 | 99 | } else { |
b1c5f1c6 | 100 | waddstr(dialog, instr); |
dec69da8 | 101 | } |
b1c5f1c6 SR |
102 | |
103 | wmove(dialog, box_y, box_x + input_x); | |
104 | ||
105 | wrefresh(dialog); | |
106 | ||
107 | while (key != ESC) { | |
108 | key = wgetch(dialog); | |
109 | ||
110 | if (button == -1) { /* Input box selected */ | |
111 | switch (key) { | |
112 | case TAB: | |
113 | case KEY_UP: | |
114 | case KEY_DOWN: | |
115 | break; | |
116 | case KEY_LEFT: | |
117 | continue; | |
118 | case KEY_RIGHT: | |
119 | continue; | |
120 | case KEY_BACKSPACE: | |
121 | case 127: | |
122 | if (input_x || scroll) { | |
123 | wattrset(dialog, inputbox_attr); | |
124 | if (!input_x) { | |
dec69da8 | 125 | scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1); |
b1c5f1c6 SR |
126 | wmove(dialog, box_y, box_x); |
127 | for (i = 0; i < box_width; i++) | |
128 | waddch(dialog, | |
dec69da8 SR |
129 | instr[scroll + input_x + i] ? |
130 | instr[scroll + input_x + i] : ' '); | |
131 | input_x = strlen(instr) - scroll; | |
b1c5f1c6 SR |
132 | } else |
133 | input_x--; | |
134 | instr[scroll + input_x] = '\0'; | |
dec69da8 | 135 | mvwaddch(dialog, box_y, input_x + box_x, ' '); |
b1c5f1c6 SR |
136 | wmove(dialog, box_y, input_x + box_x); |
137 | wrefresh(dialog); | |
138 | } | |
139 | continue; | |
140 | default: | |
141 | if (key < 0x100 && isprint(key)) { | |
142 | if (scroll + input_x < MAX_LEN) { | |
143 | wattrset(dialog, inputbox_attr); | |
144 | instr[scroll + input_x] = key; | |
dec69da8 | 145 | instr[scroll + input_x + 1] = '\0'; |
b1c5f1c6 SR |
146 | if (input_x == box_width - 1) { |
147 | scroll++; | |
dec69da8 SR |
148 | wmove(dialog, box_y, box_x); |
149 | for (i = 0; i < box_width - 1; i++) | |
150 | waddch(dialog, instr [scroll + i]); | |
b1c5f1c6 | 151 | } else { |
dec69da8 | 152 | wmove(dialog, box_y, input_x++ + box_x); |
b1c5f1c6 SR |
153 | waddch(dialog, key); |
154 | } | |
155 | wrefresh(dialog); | |
156 | } else | |
157 | flash(); /* Alarm user about overflow */ | |
158 | continue; | |
159 | } | |
160 | } | |
1da177e4 | 161 | } |
b1c5f1c6 SR |
162 | switch (key) { |
163 | case 'O': | |
164 | case 'o': | |
165 | delwin(dialog); | |
166 | return 0; | |
167 | case 'H': | |
168 | case 'h': | |
169 | delwin(dialog); | |
170 | return 1; | |
171 | case KEY_UP: | |
172 | case KEY_LEFT: | |
173 | switch (button) { | |
174 | case -1: | |
175 | button = 1; /* Indicates "Cancel" button is selected */ | |
176 | print_buttons(dialog, height, width, 1); | |
177 | break; | |
178 | case 0: | |
179 | button = -1; /* Indicates input box is selected */ | |
180 | print_buttons(dialog, height, width, 0); | |
181 | wmove(dialog, box_y, box_x + input_x); | |
182 | wrefresh(dialog); | |
183 | break; | |
184 | case 1: | |
185 | button = 0; /* Indicates "OK" button is selected */ | |
186 | print_buttons(dialog, height, width, 0); | |
187 | break; | |
1da177e4 | 188 | } |
b1c5f1c6 SR |
189 | break; |
190 | case TAB: | |
191 | case KEY_DOWN: | |
192 | case KEY_RIGHT: | |
193 | switch (button) { | |
194 | case -1: | |
195 | button = 0; /* Indicates "OK" button is selected */ | |
196 | print_buttons(dialog, height, width, 0); | |
197 | break; | |
198 | case 0: | |
199 | button = 1; /* Indicates "Cancel" button is selected */ | |
200 | print_buttons(dialog, height, width, 1); | |
201 | break; | |
202 | case 1: | |
203 | button = -1; /* Indicates input box is selected */ | |
204 | print_buttons(dialog, height, width, 0); | |
205 | wmove(dialog, box_y, box_x + input_x); | |
206 | wrefresh(dialog); | |
207 | break; | |
208 | } | |
209 | break; | |
210 | case ' ': | |
211 | case '\n': | |
212 | delwin(dialog); | |
213 | return (button == -1 ? 0 : button); | |
214 | case 'X': | |
215 | case 'x': | |
216 | key = ESC; | |
217 | case ESC: | |
218 | break; | |
1da177e4 | 219 | } |
1da177e4 | 220 | } |
1da177e4 | 221 | |
b1c5f1c6 SR |
222 | delwin(dialog); |
223 | return -1; /* ESC pressed */ | |
1da177e4 | 224 | } |