2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
5 * T2 SDE: misc/confdialog/inputbox.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
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.
41 char dialog_input_result
[MAX_LEN
+ 1];
44 * Print the termination buttons
47 print_buttons(WINDOW
*dialog
, int height
, int width
, int selected
)
49 int x
= width
/ 2 - 5;
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
);
60 * Display a dialog box for inputing a string
63 dialog_inputbox (const char *title
, const char *prompt
, int height
, int width
,
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
;
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';
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
);
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
);
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
]);
134 waddstr (dialog
, instr
);
136 wmove (dialog
, box_y
, box_x
+ input_x
);
141 key
= wgetch (dialog
);
143 if (button
== -1) { /* Input box selected */
155 if (input_x
|| scroll
) {
156 wattrset (dialog
, inputbox_attr
);
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
;
167 instr
[scroll
+ input_x
] = '\0';
168 mvwaddch (dialog
, box_y
, input_x
+ box_x
, ' ');
169 wmove (dialog
, box_y
, input_x
+ box_x
);
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) {
181 wmove (dialog
, box_y
, box_x
);
182 for (i
= 0; i
< box_width
- 1; i
++)
183 waddch (dialog
, instr
[scroll
+ i
]);
185 wmove (dialog
, box_y
, input_x
++ + box_x
);
186 waddch (dialog
, key
);
190 flash (); /* Alarm user about overflow */
204 button
= 1; /* Indicates "Cancel" button is selected */
205 print_buttons(dialog
, height
, width
, 1);
208 button
= -1; /* Indicates input box is selected */
209 print_buttons(dialog
, height
, width
, 0);
210 wmove (dialog
, box_y
, box_x
+ input_x
);
214 button
= 0; /* Indicates "OK" button is selected */
215 print_buttons(dialog
, height
, width
, 0);
224 button
= 1; /* Indicates "OK" button is selected */
225 print_buttons(dialog
, height
, width
, 0);
228 button
= 1; /* Indicates "Cancel" button is selected */
229 print_buttons(dialog
, height
, width
, 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
);
242 return (button
== -1 ? 0 : button
);
252 return -1; /* ESC pressed */