Dash:
[t2.git] / misc / confdialog / inputbox.c
blob3f23208149a94f1b9f09aa0de83494b619b16e00
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/confdialog/inputbox.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
8 *
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 * inputbox.c -- implements the input box
20 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
21 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
22 * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation; either version 2
27 * of the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include "dialog.h"
41 char dialog_input_result[MAX_LEN + 1];
44 * Print the termination buttons
46 static void
47 print_buttons(WINDOW *dialog, int height, int width, int selected)
49 int x = width / 2 - 5;
50 int y = height - 2;
52 print_button (dialog, " Ok ", y, x, selected==0);
53 // print_button (dialog, " Help ", y, x + 14, selected==1);
55 wmove(dialog, y, x+1+14*selected);
56 wrefresh(dialog);
60 * Display a dialog box for inputing a string
62 int
63 dialog_inputbox (const char *title, const char *prompt, int height, int width,
64 const char *init)
66 int i, x, y, box_y, box_x, box_width;
67 int input_x = 0, scroll = 0, key = 0, button = -1;
68 char *instr = dialog_input_result;
69 WINDOW *dialog;
71 /* center dialog box on screen */
72 x = (COLS - width) / 2;
73 y = (LINES - height) / 2;
76 draw_shadow (stdscr, y, x, height, width);
78 dialog = newwin (height, width, y, x);
79 keypad (dialog, TRUE);
81 draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
82 wattrset (dialog, border_attr);
83 mvwaddch (dialog, height-3, 0, ACS_LTEE);
84 for (i = 0; i < width - 2; i++)
85 waddch (dialog, ACS_HLINE);
86 wattrset (dialog, dialog_attr);
87 waddch (dialog, ACS_RTEE);
89 if (title != NULL && strlen(title) >= width-2 ) {
90 /* truncate long title -- mec */
91 char * title2 = malloc(width-2+1);
92 memcpy( title2, title, width-2 );
93 title2[width-2] = '\0';
94 title = title2;
97 if (title != NULL) {
98 wattrset (dialog, title_attr);
99 mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
100 waddstr (dialog, (char *)title);
101 waddch (dialog, ' ');
104 wattrset (dialog, dialog_attr);
105 print_autowrap (dialog, prompt, width - 2, 1, 3);
107 /* Draw the input field box */
108 box_width = width - 6;
109 getyx (dialog, y, x);
110 box_y = y + 2;
111 box_x = (width - box_width) / 2;
112 draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2,
113 border_attr, dialog_attr);
115 print_buttons(dialog, height, width, 0);
117 /* Set up the initial value */
118 wmove (dialog, box_y, box_x);
119 wattrset (dialog, inputbox_attr);
121 if (!init)
122 instr[0] = '\0';
123 else
124 strcpy (instr, init);
126 input_x = strlen (instr);
128 if (input_x >= box_width) {
129 scroll = input_x - box_width + 1;
130 input_x = box_width - 1;
131 for (i = 0; i < box_width - 1; i++)
132 waddch (dialog, instr[scroll + i]);
133 } else
134 waddstr (dialog, instr);
136 wmove (dialog, box_y, box_x + input_x);
138 wrefresh (dialog);
140 while (key != ESC) {
141 key = wgetch (dialog);
143 if (button == -1) { /* Input box selected */
144 switch (key) {
145 case TAB:
146 case KEY_UP:
147 case KEY_DOWN:
148 break;
149 case KEY_LEFT:
150 continue;
151 case KEY_RIGHT:
152 continue;
153 case KEY_BACKSPACE:
154 case 127:
155 if (input_x || scroll) {
156 wattrset (dialog, inputbox_attr);
157 if (!input_x) {
158 scroll = scroll < box_width - 1 ?
159 0 : scroll - (box_width - 1);
160 wmove (dialog, box_y, box_x);
161 for (i = 0; i < box_width; i++)
162 waddch (dialog, instr[scroll + input_x + i] ?
163 instr[scroll + input_x + i] : ' ');
164 input_x = strlen (instr) - scroll;
165 } else
166 input_x--;
167 instr[scroll + input_x] = '\0';
168 mvwaddch (dialog, box_y, input_x + box_x, ' ');
169 wmove (dialog, box_y, input_x + box_x);
170 wrefresh (dialog);
172 continue;
173 default:
174 if (key < 0x100 && isprint (key)) {
175 if (scroll + input_x < MAX_LEN) {
176 wattrset (dialog, inputbox_attr);
177 instr[scroll + input_x] = key;
178 instr[scroll + input_x + 1] = '\0';
179 if (input_x == box_width - 1) {
180 scroll++;
181 wmove (dialog, box_y, box_x);
182 for (i = 0; i < box_width - 1; i++)
183 waddch (dialog, instr[scroll + i]);
184 } else {
185 wmove (dialog, box_y, input_x++ + box_x);
186 waddch (dialog, key);
188 wrefresh (dialog);
189 } else
190 flash (); /* Alarm user about overflow */
191 continue;
195 switch (key) {
196 case 'O':
197 case 'o':
198 delwin (dialog);
199 return 0;
200 case KEY_UP:
201 case KEY_LEFT:
202 switch (button) {
203 case -1:
204 button = 1; /* Indicates "Cancel" button is selected */
205 print_buttons(dialog, height, width, 1);
206 break;
207 case 0:
208 button = -1; /* Indicates input box is selected */
209 print_buttons(dialog, height, width, 0);
210 wmove (dialog, box_y, box_x + input_x);
211 wrefresh (dialog);
212 break;
213 case 1:
214 button = 0; /* Indicates "OK" button is selected */
215 print_buttons(dialog, height, width, 0);
216 break;
218 break;
219 case TAB:
220 case KEY_DOWN:
221 case KEY_RIGHT:
222 switch (button) {
223 case -1:
224 button = 1; /* Indicates "OK" button is selected */
225 print_buttons(dialog, height, width, 0);
226 break;
227 case 0:
228 button = 1; /* Indicates "Cancel" button is selected */
229 print_buttons(dialog, height, width, 1);
230 break;
231 case 1:
232 button = -1; /* Indicates input box is selected */
233 print_buttons(dialog, height, width, 0);
234 wmove (dialog, box_y, box_x + input_x);
235 wrefresh (dialog);
236 break;
238 break;
239 case ' ':
240 case '\n':
241 delwin (dialog);
242 return (button == -1 ? 0 : button);
243 case 'X':
244 case 'x':
245 key = ESC;
246 case ESC:
247 break;
251 delwin (dialog);
252 return -1; /* ESC pressed */