diff options
Diffstat (limited to 'scripts/lxdialog/inputbox.c')
-rw-r--r-- | scripts/lxdialog/inputbox.c | 408 |
1 files changed, 212 insertions, 196 deletions
diff --git a/scripts/lxdialog/inputbox.c b/scripts/lxdialog/inputbox.c index 074d2d68bd31..9e9691567269 100644 --- a/scripts/lxdialog/inputbox.c +++ b/scripts/lxdialog/inputbox.c | |||
@@ -26,215 +26,231 @@ char dialog_input_result[MAX_LEN + 1]; | |||
26 | /* | 26 | /* |
27 | * Print the termination buttons | 27 | * Print the termination buttons |
28 | */ | 28 | */ |
29 | static void | 29 | static void print_buttons(WINDOW * dialog, int height, int width, int selected) |
30 | print_buttons(WINDOW *dialog, int height, int width, int selected) | ||
31 | { | 30 | { |
32 | int x = width / 2 - 11; | 31 | int x = width / 2 - 11; |
33 | int y = height - 2; | 32 | int y = height - 2; |
34 | 33 | ||
35 | print_button (dialog, " Ok ", y, x, selected==0); | 34 | print_button(dialog, " Ok ", y, x, selected == 0); |
36 | print_button (dialog, " Help ", y, x + 14, selected==1); | 35 | print_button(dialog, " Help ", y, x + 14, selected == 1); |
37 | 36 | ||
38 | wmove(dialog, y, x+1+14*selected); | 37 | wmove(dialog, y, x + 1 + 14 * selected); |
39 | wrefresh(dialog); | 38 | wrefresh(dialog); |
40 | } | 39 | } |
41 | 40 | ||
42 | /* | 41 | /* |
43 | * Display a dialog box for inputing a string | 42 | * Display a dialog box for inputing a string |
44 | */ | 43 | */ |
45 | int | 44 | int |
46 | dialog_inputbox (const char *title, const char *prompt, int height, int width, | 45 | dialog_inputbox(const char *title, const char *prompt, int height, int width, |
47 | const char *init) | 46 | const char *init) |
48 | { | 47 | { |
49 | int i, x, y, box_y, box_x, box_width; | 48 | int i, x, y, box_y, box_x, box_width; |
50 | int input_x = 0, scroll = 0, key = 0, button = -1; | 49 | int input_x = 0, scroll = 0, key = 0, button = -1; |
51 | char *instr = dialog_input_result; | 50 | char *instr = dialog_input_result; |
52 | WINDOW *dialog; | 51 | WINDOW *dialog; |
53 | 52 | ||
54 | /* center dialog box on screen */ | 53 | /* center dialog box on screen */ |
55 | x = (COLS - width) / 2; | 54 | x = (COLS - width) / 2; |
56 | y = (LINES - height) / 2; | 55 | y = (LINES - height) / 2; |
57 | 56 | ||
58 | 57 | draw_shadow(stdscr, y, x, height, width); | |
59 | draw_shadow (stdscr, y, x, height, width); | 58 | |
60 | 59 | dialog = newwin(height, width, y, x); | |
61 | dialog = newwin (height, width, y, x); | 60 | keypad(dialog, TRUE); |
62 | keypad (dialog, TRUE); | 61 | |
63 | 62 | draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); | |
64 | draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr); | 63 | wattrset(dialog, border_attr); |
65 | wattrset (dialog, border_attr); | 64 | mvwaddch(dialog, height - 3, 0, ACS_LTEE); |
66 | mvwaddch (dialog, height-3, 0, ACS_LTEE); | 65 | for (i = 0; i < width - 2; i++) |
67 | for (i = 0; i < width - 2; i++) | 66 | waddch(dialog, ACS_HLINE); |
68 | waddch (dialog, ACS_HLINE); | 67 | wattrset(dialog, dialog_attr); |
69 | wattrset (dialog, dialog_attr); | 68 | waddch(dialog, ACS_RTEE); |
70 | waddch (dialog, ACS_RTEE); | 69 | |
71 | 70 | if (title != NULL && strlen(title) >= width - 2) { | |
72 | if (title != NULL && strlen(title) >= width-2 ) { | 71 | /* truncate long title -- mec */ |
73 | /* truncate long title -- mec */ | 72 | char *title2 = malloc(width - 2 + 1); |
74 | char * title2 = malloc(width-2+1); | 73 | memcpy(title2, title, width - 2); |
75 | memcpy( title2, title, width-2 ); | 74 | title2[width - 2] = '\0'; |
76 | title2[width-2] = '\0'; | 75 | title = title2; |
77 | title = title2; | 76 | } |
78 | } | 77 | |
79 | 78 | if (title != NULL) { | |
80 | if (title != NULL) { | 79 | wattrset(dialog, title_attr); |
81 | wattrset (dialog, title_attr); | 80 | mvwaddch(dialog, 0, (width - strlen(title)) / 2 - 1, ' '); |
82 | mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' '); | 81 | waddstr(dialog, (char *)title); |
83 | waddstr (dialog, (char *)title); | 82 | waddch(dialog, ' '); |
84 | waddch (dialog, ' '); | 83 | } |
85 | } | 84 | |
86 | 85 | wattrset(dialog, dialog_attr); | |
87 | wattrset (dialog, dialog_attr); | 86 | print_autowrap(dialog, prompt, width - 2, 1, 3); |
88 | print_autowrap (dialog, prompt, width - 2, 1, 3); | 87 | |
89 | 88 | /* Draw the input field box */ | |
90 | /* Draw the input field box */ | 89 | box_width = width - 6; |
91 | box_width = width - 6; | 90 | getyx(dialog, y, x); |
92 | getyx (dialog, y, x); | 91 | box_y = y + 2; |
93 | box_y = y + 2; | 92 | box_x = (width - box_width) / 2; |
94 | box_x = (width - box_width) / 2; | 93 | draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2, |
95 | draw_box (dialog, y + 1, box_x - 1, 3, box_width + 2, | 94 | border_attr, dialog_attr); |
96 | border_attr, dialog_attr); | 95 | |
97 | 96 | print_buttons(dialog, height, width, 0); | |
98 | print_buttons(dialog, height, width, 0); | 97 | |
99 | 98 | /* Set up the initial value */ | |
100 | /* Set up the initial value */ | 99 | wmove(dialog, box_y, box_x); |
101 | wmove (dialog, box_y, box_x); | 100 | wattrset(dialog, inputbox_attr); |
102 | wattrset (dialog, inputbox_attr); | 101 | |
103 | 102 | if (!init) | |
104 | if (!init) | 103 | instr[0] = '\0'; |
105 | instr[0] = '\0'; | 104 | else |
106 | else | 105 | strcpy(instr, init); |
107 | strcpy (instr, init); | 106 | |
108 | 107 | input_x = strlen(instr); | |
109 | input_x = strlen (instr); | 108 | |
110 | 109 | if (input_x >= box_width) { | |
111 | if (input_x >= box_width) { | 110 | scroll = input_x - box_width + 1; |
112 | scroll = input_x - box_width + 1; | 111 | input_x = box_width - 1; |
113 | input_x = box_width - 1; | 112 | for (i = 0; i < box_width - 1; i++) |
114 | for (i = 0; i < box_width - 1; i++) | 113 | waddch(dialog, instr[scroll + i]); |
115 | waddch (dialog, instr[scroll + i]); | 114 | } else |
116 | } else | 115 | waddstr(dialog, instr); |
117 | waddstr (dialog, instr); | 116 | |
118 | 117 | wmove(dialog, box_y, box_x + input_x); | |
119 | wmove (dialog, box_y, box_x + input_x); | 118 | |
120 | 119 | wrefresh(dialog); | |
121 | wrefresh (dialog); | 120 | |
122 | 121 | while (key != ESC) { | |
123 | while (key != ESC) { | 122 | key = wgetch(dialog); |
124 | key = wgetch (dialog); | 123 | |
125 | 124 | if (button == -1) { /* Input box selected */ | |
126 | if (button == -1) { /* Input box selected */ | 125 | switch (key) { |
127 | switch (key) { | 126 | case TAB: |
128 | case TAB: | 127 | case KEY_UP: |
129 | case KEY_UP: | 128 | case KEY_DOWN: |
130 | case KEY_DOWN: | 129 | break; |
131 | break; | 130 | case KEY_LEFT: |
132 | case KEY_LEFT: | 131 | continue; |
133 | continue; | 132 | case KEY_RIGHT: |
134 | case KEY_RIGHT: | 133 | continue; |
135 | continue; | 134 | case KEY_BACKSPACE: |
136 | case KEY_BACKSPACE: | 135 | case 127: |
137 | case 127: | 136 | if (input_x || scroll) { |
138 | if (input_x || scroll) { | 137 | wattrset(dialog, inputbox_attr); |
139 | wattrset (dialog, inputbox_attr); | 138 | if (!input_x) { |
140 | if (!input_x) { | 139 | scroll = |
141 | scroll = scroll < box_width - 1 ? | 140 | scroll < |
142 | 0 : scroll - (box_width - 1); | 141 | box_width - 1 ? 0 : scroll - |
143 | wmove (dialog, box_y, box_x); | 142 | (box_width - 1); |
144 | for (i = 0; i < box_width; i++) | 143 | wmove(dialog, box_y, box_x); |
145 | waddch (dialog, instr[scroll + input_x + i] ? | 144 | for (i = 0; i < box_width; i++) |
146 | instr[scroll + input_x + i] : ' '); | 145 | waddch(dialog, |
147 | input_x = strlen (instr) - scroll; | 146 | instr[scroll + |
148 | } else | 147 | input_x + |
149 | input_x--; | 148 | i] ? |
150 | instr[scroll + input_x] = '\0'; | 149 | instr[scroll + |
151 | mvwaddch (dialog, box_y, input_x + box_x, ' '); | 150 | input_x + |
152 | wmove (dialog, box_y, input_x + box_x); | 151 | i] : ' '); |
153 | wrefresh (dialog); | 152 | input_x = |
153 | strlen(instr) - scroll; | ||
154 | } else | ||
155 | input_x--; | ||
156 | instr[scroll + input_x] = '\0'; | ||
157 | mvwaddch(dialog, box_y, input_x + box_x, | ||
158 | ' '); | ||
159 | wmove(dialog, box_y, input_x + box_x); | ||
160 | wrefresh(dialog); | ||
161 | } | ||
162 | continue; | ||
163 | default: | ||
164 | if (key < 0x100 && isprint(key)) { | ||
165 | if (scroll + input_x < MAX_LEN) { | ||
166 | wattrset(dialog, inputbox_attr); | ||
167 | instr[scroll + input_x] = key; | ||
168 | instr[scroll + input_x + 1] = | ||
169 | '\0'; | ||
170 | if (input_x == box_width - 1) { | ||
171 | scroll++; | ||
172 | wmove(dialog, box_y, | ||
173 | box_x); | ||
174 | for (i = 0; | ||
175 | i < box_width - 1; | ||
176 | i++) | ||
177 | waddch(dialog, | ||
178 | instr | ||
179 | [scroll + | ||
180 | i]); | ||
181 | } else { | ||
182 | wmove(dialog, box_y, | ||
183 | input_x++ + | ||
184 | box_x); | ||
185 | waddch(dialog, key); | ||
186 | } | ||
187 | wrefresh(dialog); | ||
188 | } else | ||
189 | flash(); /* Alarm user about overflow */ | ||
190 | continue; | ||
191 | } | ||
192 | } | ||
154 | } | 193 | } |
155 | continue; | 194 | switch (key) { |
156 | default: | 195 | case 'O': |
157 | if (key < 0x100 && isprint (key)) { | 196 | case 'o': |
158 | if (scroll + input_x < MAX_LEN) { | 197 | delwin(dialog); |
159 | wattrset (dialog, inputbox_attr); | 198 | return 0; |
160 | instr[scroll + input_x] = key; | 199 | case 'H': |
161 | instr[scroll + input_x + 1] = '\0'; | 200 | case 'h': |
162 | if (input_x == box_width - 1) { | 201 | delwin(dialog); |
163 | scroll++; | 202 | return 1; |
164 | wmove (dialog, box_y, box_x); | 203 | case KEY_UP: |
165 | for (i = 0; i < box_width - 1; i++) | 204 | case KEY_LEFT: |
166 | waddch (dialog, instr[scroll + i]); | 205 | switch (button) { |
167 | } else { | 206 | case -1: |
168 | wmove (dialog, box_y, input_x++ + box_x); | 207 | button = 1; /* Indicates "Cancel" button is selected */ |
169 | waddch (dialog, key); | 208 | print_buttons(dialog, height, width, 1); |
209 | break; | ||
210 | case 0: | ||
211 | button = -1; /* Indicates input box is selected */ | ||
212 | print_buttons(dialog, height, width, 0); | ||
213 | wmove(dialog, box_y, box_x + input_x); | ||
214 | wrefresh(dialog); | ||
215 | break; | ||
216 | case 1: | ||
217 | button = 0; /* Indicates "OK" button is selected */ | ||
218 | print_buttons(dialog, height, width, 0); | ||
219 | break; | ||
170 | } | 220 | } |
171 | wrefresh (dialog); | 221 | break; |
172 | } else | 222 | case TAB: |
173 | flash (); /* Alarm user about overflow */ | 223 | case KEY_DOWN: |
174 | continue; | 224 | case KEY_RIGHT: |
225 | switch (button) { | ||
226 | case -1: | ||
227 | button = 0; /* Indicates "OK" button is selected */ | ||
228 | print_buttons(dialog, height, width, 0); | ||
229 | break; | ||
230 | case 0: | ||
231 | button = 1; /* Indicates "Cancel" button is selected */ | ||
232 | print_buttons(dialog, height, width, 1); | ||
233 | break; | ||
234 | case 1: | ||
235 | button = -1; /* Indicates input box is selected */ | ||
236 | print_buttons(dialog, height, width, 0); | ||
237 | wmove(dialog, box_y, box_x + input_x); | ||
238 | wrefresh(dialog); | ||
239 | break; | ||
240 | } | ||
241 | break; | ||
242 | case ' ': | ||
243 | case '\n': | ||
244 | delwin(dialog); | ||
245 | return (button == -1 ? 0 : button); | ||
246 | case 'X': | ||
247 | case 'x': | ||
248 | key = ESC; | ||
249 | case ESC: | ||
250 | break; | ||
175 | } | 251 | } |
176 | } | ||
177 | } | ||
178 | switch (key) { | ||
179 | case 'O': | ||
180 | case 'o': | ||
181 | delwin (dialog); | ||
182 | return 0; | ||
183 | case 'H': | ||
184 | case 'h': | ||
185 | delwin (dialog); | ||
186 | return 1; | ||
187 | case KEY_UP: | ||
188 | case KEY_LEFT: | ||
189 | switch (button) { | ||
190 | case -1: | ||
191 | button = 1; /* Indicates "Cancel" button is selected */ | ||
192 | print_buttons(dialog, height, width, 1); | ||
193 | break; | ||
194 | case 0: | ||
195 | button = -1; /* Indicates input box is selected */ | ||
196 | print_buttons(dialog, height, width, 0); | ||
197 | wmove (dialog, box_y, box_x + input_x); | ||
198 | wrefresh (dialog); | ||
199 | break; | ||
200 | case 1: | ||
201 | button = 0; /* Indicates "OK" button is selected */ | ||
202 | print_buttons(dialog, height, width, 0); | ||
203 | break; | ||
204 | } | ||
205 | break; | ||
206 | case TAB: | ||
207 | case KEY_DOWN: | ||
208 | case KEY_RIGHT: | ||
209 | switch (button) { | ||
210 | case -1: | ||
211 | button = 0; /* Indicates "OK" button is selected */ | ||
212 | print_buttons(dialog, height, width, 0); | ||
213 | break; | ||
214 | case 0: | ||
215 | button = 1; /* Indicates "Cancel" button is selected */ | ||
216 | print_buttons(dialog, height, width, 1); | ||
217 | break; | ||
218 | case 1: | ||
219 | button = -1; /* Indicates input box is selected */ | ||
220 | print_buttons(dialog, height, width, 0); | ||
221 | wmove (dialog, box_y, box_x + input_x); | ||
222 | wrefresh (dialog); | ||
223 | break; | ||
224 | } | ||
225 | break; | ||
226 | case ' ': | ||
227 | case '\n': | ||
228 | delwin (dialog); | ||
229 | return (button == -1 ? 0 : button); | ||
230 | case 'X': | ||
231 | case 'x': | ||
232 | key = ESC; | ||
233 | case ESC: | ||
234 | break; | ||
235 | } | 252 | } |
236 | } | ||
237 | 253 | ||
238 | delwin (dialog); | 254 | delwin(dialog); |
239 | return -1; /* ESC pressed */ | 255 | return -1; /* ESC pressed */ |
240 | } | 256 | } |