Chameleon

Chameleon Svn Source Tree

Root/branches/rewrite/i386/config/inputbox.c

Source at commit 1146 created 12 years 11 months ago.
By azimutz, Sync with trunk (r1145). Add nVidia dev id's, 0DF4 for "GeForce GT 450M" (issue 99) and 1251 for "GeForce GTX 560M" (thanks to oSxFr33k for testing).
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
24char dialog_input_result[MAX_LEN + 1];
25
26/*
27 * Print the termination buttons
28 */
29static void print_buttons(WINDOW * dialog, int height, int width, int selected)
30{
31int x = width / 2 - 11;
32int y = height - 2;
33
34print_button(dialog, gettext(" Ok "), y, x, selected == 0);
35print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
36
37wmove(dialog, y, x + 1 + 14 * selected);
38wrefresh(dialog);
39}
40
41/*
42 * Display a dialog box for inputing a string
43 */
44int dialog_inputbox(const char *title, const char *prompt, int height, int width,
45 const char *init)
46{
47int i, x, y, box_y, box_x, box_width;
48int input_x = 0, scroll = 0, key = 0, button = -1;
49char *instr = dialog_input_result;
50WINDOW *dialog;
51
52if (!init)
53instr[0] = '\0';
54else
55strcpy(instr, init);
56
57do_resize:
58if (getmaxy(stdscr) <= (height - 2))
59return -ERRDISPLAYTOOSMALL;
60if (getmaxx(stdscr) <= (width - 2))
61return -ERRDISPLAYTOOSMALL;
62
63/* center dialog box on screen */
64x = (COLS - width) / 2;
65y = (LINES - height) / 2;
66
67draw_shadow(stdscr, y, x, height, width);
68
69dialog = newwin(height, width, y, x);
70keypad(dialog, TRUE);
71
72draw_box(dialog, 0, 0, height, width,
73 dlg.dialog.atr, dlg.border.atr);
74wattrset(dialog, dlg.border.atr);
75mvwaddch(dialog, height - 3, 0, ACS_LTEE);
76for (i = 0; i < width - 2; i++)
77waddch(dialog, ACS_HLINE);
78wattrset(dialog, dlg.dialog.atr);
79waddch(dialog, ACS_RTEE);
80
81print_title(dialog, title, width);
82
83wattrset(dialog, dlg.dialog.atr);
84print_autowrap(dialog, prompt, width - 2, 1, 3);
85
86/* Draw the input field box */
87box_width = width - 6;
88getyx(dialog, y, x);
89box_y = y + 2;
90box_x = (width - box_width) / 2;
91draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
92 dlg.dialog.atr, dlg.border.atr);
93
94print_buttons(dialog, height, width, 0);
95
96/* Set up the initial value */
97wmove(dialog, box_y, box_x);
98wattrset(dialog, dlg.inputbox.atr);
99
100input_x = strlen(instr);
101
102if (input_x >= box_width) {
103scroll = input_x - box_width + 1;
104input_x = box_width - 1;
105for (i = 0; i < box_width - 1; i++)
106waddch(dialog, instr[scroll + i]);
107} else {
108waddstr(dialog, instr);
109}
110
111wmove(dialog, box_y, box_x + input_x);
112
113wrefresh(dialog);
114
115while (key != KEY_ESC) {
116key = wgetch(dialog);
117
118if (button == -1) {/* Input box selected */
119switch (key) {
120case TAB:
121case KEY_UP:
122case KEY_DOWN:
123break;
124case KEY_LEFT:
125continue;
126case KEY_RIGHT:
127continue;
128case KEY_BACKSPACE:
129case 127:
130if (input_x || scroll) {
131wattrset(dialog, dlg.inputbox.atr);
132if (!input_x) {
133scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1);
134wmove(dialog, box_y, box_x);
135for (i = 0; i < box_width; i++)
136waddch(dialog,
137 instr[scroll + input_x + i] ?
138 instr[scroll + input_x + i] : ' ');
139input_x = strlen(instr) - scroll;
140} else
141input_x--;
142instr[scroll + input_x] = '\0';
143mvwaddch(dialog, box_y, input_x + box_x, ' ');
144wmove(dialog, box_y, input_x + box_x);
145wrefresh(dialog);
146}
147continue;
148default:
149if (key < 0x100 && isprint(key)) {
150if (scroll + input_x < MAX_LEN) {
151wattrset(dialog, dlg.inputbox.atr);
152instr[scroll + input_x] = key;
153instr[scroll + input_x + 1] = '\0';
154if (input_x == box_width - 1) {
155scroll++;
156wmove(dialog, box_y, box_x);
157for (i = 0; i < box_width - 1; i++)
158waddch(dialog, instr [scroll + i]);
159} else {
160wmove(dialog, box_y, input_x++ + box_x);
161waddch(dialog, key);
162}
163wrefresh(dialog);
164} else
165flash();/* Alarm user about overflow */
166continue;
167}
168}
169}
170switch (key) {
171case 'O':
172case 'o':
173delwin(dialog);
174return 0;
175case 'H':
176case 'h':
177delwin(dialog);
178return 1;
179case KEY_UP:
180case KEY_LEFT:
181switch (button) {
182case -1:
183button = 1;/* Indicates "Help" button is selected */
184print_buttons(dialog, height, width, 1);
185break;
186case 0:
187button = -1;/* Indicates input box is selected */
188print_buttons(dialog, height, width, 0);
189wmove(dialog, box_y, box_x + input_x);
190wrefresh(dialog);
191break;
192case 1:
193button = 0;/* Indicates "OK" button is selected */
194print_buttons(dialog, height, width, 0);
195break;
196}
197break;
198case TAB:
199case KEY_DOWN:
200case KEY_RIGHT:
201switch (button) {
202case -1:
203button = 0;/* Indicates "OK" button is selected */
204print_buttons(dialog, height, width, 0);
205break;
206case 0:
207button = 1;/* Indicates "Help" button is selected */
208print_buttons(dialog, height, width, 1);
209break;
210case 1:
211button = -1;/* Indicates input box is selected */
212print_buttons(dialog, height, width, 0);
213wmove(dialog, box_y, box_x + input_x);
214wrefresh(dialog);
215break;
216}
217break;
218case ' ':
219case '\n':
220delwin(dialog);
221return (button == -1 ? 0 : button);
222case 'X':
223case 'x':
224key = KEY_ESC;
225break;
226case KEY_ESC:
227key = on_key_esc(dialog);
228break;
229case KEY_RESIZE:
230delwin(dialog);
231on_key_resize();
232goto do_resize;
233}
234}
235
236delwin(dialog);
237return KEY_ESC;/* ESC pressed */
238}
239

Archive Download this file

Revision: 1146