2 * inputbox.c -- implements the input box
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
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.
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.
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.
24 char dialog_input_result
[MAX_LEN
+ 1];
27 * Print the termination buttons
29 static void print_buttons(WINDOW
* dialog
, int height
, int width
, int selected
)
31 int x
= width
/ 2 - 11;
34 print_button(dialog
, " Ok ", y
, x
, selected
== 0);
35 print_button(dialog
, " Help ", y
, x
+ 14, selected
== 1);
37 wmove(dialog
, y
, x
+ 1 + 14 * selected
);
42 * Display a dialog box for inputing a string
44 int dialog_inputbox(const char *title
, const char *prompt
, int height
, int width
,
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
;
58 if (getmaxy(stdscr
) <= (height
- 2))
59 return -ERRDISPLAYTOOSMALL
;
60 if (getmaxx(stdscr
) <= (width
- 2))
61 return -ERRDISPLAYTOOSMALL
;
63 /* center dialog box on screen */
64 x
= (COLS
- width
) / 2;
65 y
= (LINES
- height
) / 2;
67 draw_shadow(stdscr
, y
, x
, height
, width
);
69 dialog
= newwin(height
, width
, y
, x
);
72 draw_box(dialog
, 0, 0, height
, width
,
73 dlg
.dialog
.atr
, dlg
.border
.atr
);
74 wattrset(dialog
, dlg
.border
.atr
);
75 mvwaddch(dialog
, height
- 3, 0, ACS_LTEE
);
76 for (i
= 0; i
< width
- 2; i
++)
77 waddch(dialog
, ACS_HLINE
);
78 wattrset(dialog
, dlg
.dialog
.atr
);
79 waddch(dialog
, ACS_RTEE
);
81 print_title(dialog
, title
, width
);
83 wattrset(dialog
, dlg
.dialog
.atr
);
84 print_autowrap(dialog
, prompt
, width
- 2, 1, 3);
86 /* Draw the input field box */
87 box_width
= width
- 6;
90 box_x
= (width
- box_width
) / 2;
91 draw_box(dialog
, y
+ 1, box_x
- 1, 3, box_width
+ 2,
92 dlg
.border
.atr
, dlg
.dialog
.atr
);
94 print_buttons(dialog
, height
, width
, 0);
96 /* Set up the initial value */
97 wmove(dialog
, box_y
, box_x
);
98 wattrset(dialog
, dlg
.inputbox
.atr
);
100 input_x
= strlen(instr
);
102 if (input_x
>= box_width
) {
103 scroll
= input_x
- box_width
+ 1;
104 input_x
= box_width
- 1;
105 for (i
= 0; i
< box_width
- 1; i
++)
106 waddch(dialog
, instr
[scroll
+ i
]);
108 waddstr(dialog
, instr
);
111 wmove(dialog
, box_y
, box_x
+ input_x
);
115 while (key
!= KEY_ESC
) {
116 key
= wgetch(dialog
);
118 if (button
== -1) { /* Input box selected */
130 if (input_x
|| scroll
) {
131 wattrset(dialog
, dlg
.inputbox
.atr
);
133 scroll
= scroll
< box_width
- 1 ? 0 : scroll
- (box_width
- 1);
134 wmove(dialog
, box_y
, box_x
);
135 for (i
= 0; i
< box_width
; i
++)
137 instr
[scroll
+ input_x
+ i
] ?
138 instr
[scroll
+ input_x
+ i
] : ' ');
139 input_x
= strlen(instr
) - scroll
;
142 instr
[scroll
+ input_x
] = '\0';
143 mvwaddch(dialog
, box_y
, input_x
+ box_x
, ' ');
144 wmove(dialog
, box_y
, input_x
+ box_x
);
149 if (key
< 0x100 && isprint(key
)) {
150 if (scroll
+ input_x
< MAX_LEN
) {
151 wattrset(dialog
, dlg
.inputbox
.atr
);
152 instr
[scroll
+ input_x
] = key
;
153 instr
[scroll
+ input_x
+ 1] = '\0';
154 if (input_x
== box_width
- 1) {
156 wmove(dialog
, box_y
, box_x
);
157 for (i
= 0; i
< box_width
- 1; i
++)
158 waddch(dialog
, instr
[scroll
+ i
]);
160 wmove(dialog
, box_y
, input_x
++ + box_x
);
165 flash(); /* Alarm user about overflow */
183 button
= 1; /* Indicates "Cancel" button is selected */
184 print_buttons(dialog
, height
, width
, 1);
187 button
= -1; /* Indicates input box is selected */
188 print_buttons(dialog
, height
, width
, 0);
189 wmove(dialog
, box_y
, box_x
+ input_x
);
193 button
= 0; /* Indicates "OK" button is selected */
194 print_buttons(dialog
, height
, width
, 0);
203 button
= 0; /* Indicates "OK" button is selected */
204 print_buttons(dialog
, height
, width
, 0);
207 button
= 1; /* Indicates "Cancel" button is selected */
208 print_buttons(dialog
, height
, width
, 1);
211 button
= -1; /* Indicates input box is selected */
212 print_buttons(dialog
, height
, width
, 0);
213 wmove(dialog
, box_y
, box_x
+ input_x
);
221 return (button
== -1 ? 0 : button
);
227 key
= on_key_esc(dialog
);
237 return KEY_ESC
; /* ESC pressed */