Deleted Added
full compact
formbox.c (224014) formbox.c (241818)
1/*
1/*
2 * $Id: formbox.c,v 1.73 2011/06/29 09:48:08 tom Exp $
2 * $Id: formbox.c,v 1.81 2012/07/01 18:13:51 Zoltan.Kelemen Exp $
3 *
4 * formbox.c -- implements the form (i.e, some pairs label/editbox)
5 *
3 *
4 * formbox.c -- implements the form (i.e, some pairs label/editbox)
5 *
6 * Copyright 2003-2010,2011 Thomas E. Dickey
6 * Copyright 2003-2011,2012 Thomas E. Dickey
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
22 *
23 * This is adapted from source contributed by
24 * Valery Reznic (valery_reznic@users.sourceforge.net)
25 */
26
27#include <dialog.h>
28#include <dlg_keys.h>
29
30#define LLEN(n) ((n) * FORMBOX_TAGS)
31
32#define ItemName(i) items[LLEN(i) + 0]
33#define ItemNameY(i) items[LLEN(i) + 1]
34#define ItemNameX(i) items[LLEN(i) + 2]
35#define ItemText(i) items[LLEN(i) + 3]
36#define ItemTextY(i) items[LLEN(i) + 4]
37#define ItemTextX(i) items[LLEN(i) + 5]
38#define ItemTextFLen(i) items[LLEN(i) + 6]
39#define ItemTextILen(i) items[LLEN(i) + 7]
40#define ItemHelp(i) (dialog_vars.item_help ? items[LLEN(i) + 8] : dlg_strempty())
41
42static bool
43is_readonly(DIALOG_FORMITEM * item)
44{
45 return ((item->type & 2) != 0) || (item->text_flen <= 0);
46}
47
48static bool
49is_hidden(DIALOG_FORMITEM * item)
50{
51 return ((item->type & 1) != 0);
52}
53
54static bool
55in_window(WINDOW *win, int scrollamt, int y)
56{
57 return (y >= scrollamt && y - scrollamt < getmaxy(win));
58}
59
60static bool
61ok_move(WINDOW *win, int scrollamt, int y, int x)
62{
63 return in_window(win, scrollamt, y)
64 && (wmove(win, y - scrollamt, x) != ERR);
65}
66
67static void
68move_past(WINDOW *win, int y, int x)
69{
70 if (wmove(win, y, x) == ERR)
71 wmove(win, y, getmaxx(win) - 1);
72}
73
74/*
75 * Print form item
76 */
77static int
78print_item(WINDOW *win, DIALOG_FORMITEM * item, int scrollamt, bool choice)
79{
80 int count = 0;
81 int len;
82
83 if (ok_move(win, scrollamt, item->name_y, item->name_x)) {
84 len = item->name_len;
85 len = MIN(len, getmaxx(win) - item->name_x);
86 if (len > 0) {
87 dlg_show_string(win,
88 item->name,
89 0,
90 menubox_attr,
91 item->name_y - scrollamt,
92 item->name_x,
93 len,
94 FALSE,
95 FALSE);
96 move_past(win, item->name_y - scrollamt, item->name_x + len);
97 count = 1;
98 }
99 }
100 if (item->text_len && ok_move(win, scrollamt, item->text_y, item->text_x)) {
101 chtype this_item_attribute;
102
103 len = item->text_len;
104 len = MIN(len, getmaxx(win) - item->text_x);
105
106 if (!is_readonly(item)) {
107 this_item_attribute = choice
108 ? form_active_text_attr
109 : form_text_attr;
110 } else {
111 this_item_attribute = form_item_readonly_attr;
112 }
113
114 if (len > 0) {
115 dlg_show_string(win,
116 item->text,
117 0,
118 this_item_attribute,
119 item->text_y - scrollamt,
120 item->text_x,
121 len,
122 is_hidden(item),
123 FALSE);
124 move_past(win, item->text_y - scrollamt, item->text_x + len);
125 count = 1;
126 }
127 }
128 return count;
129}
130
131/*
132 * Print the entire form.
133 */
134static void
135print_form(WINDOW *win, DIALOG_FORMITEM * item, int total, int scrollamt, int choice)
136{
137 int n;
138 int count = 0;
139
140 for (n = 0; n < total; ++n) {
141 count += print_item(win, item + n, scrollamt, n == choice);
142 }
143 if (count) {
144 wbkgdset(win, menubox_attr | ' ');
145 wclrtobot(win);
146 (void) wnoutrefresh(win);
147 }
148}
149
150static int
151set_choice(DIALOG_FORMITEM item[], int choice, int item_no, bool * noneditable)
152{
153 int result = -1;
154 int i;
155
156 *noneditable = FALSE;
157 if (!is_readonly(&item[choice])) {
158 result = choice;
159 } else {
160 for (i = 0; i < item_no; i++) {
161 if (!is_readonly(&(item[i]))) {
162 result = i;
163 break;
164 }
165 }
166 if (result < 0) {
167 *noneditable = TRUE;
168 result = 0;
169 }
170 }
171 return result;
172}
173
174/*
175 * Find the last y-value in the form.
176 */
177static int
178form_limit(DIALOG_FORMITEM item[])
179{
180 int n;
181 int limit = 0;
182 for (n = 0; item[n].name != 0; ++n) {
183 if (limit < item[n].name_y)
184 limit = item[n].name_y;
185 if (limit < item[n].text_y)
186 limit = item[n].text_y;
187 }
188 return limit;
189}
190
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
22 *
23 * This is adapted from source contributed by
24 * Valery Reznic (valery_reznic@users.sourceforge.net)
25 */
26
27#include <dialog.h>
28#include <dlg_keys.h>
29
30#define LLEN(n) ((n) * FORMBOX_TAGS)
31
32#define ItemName(i) items[LLEN(i) + 0]
33#define ItemNameY(i) items[LLEN(i) + 1]
34#define ItemNameX(i) items[LLEN(i) + 2]
35#define ItemText(i) items[LLEN(i) + 3]
36#define ItemTextY(i) items[LLEN(i) + 4]
37#define ItemTextX(i) items[LLEN(i) + 5]
38#define ItemTextFLen(i) items[LLEN(i) + 6]
39#define ItemTextILen(i) items[LLEN(i) + 7]
40#define ItemHelp(i) (dialog_vars.item_help ? items[LLEN(i) + 8] : dlg_strempty())
41
42static bool
43is_readonly(DIALOG_FORMITEM * item)
44{
45 return ((item->type & 2) != 0) || (item->text_flen <= 0);
46}
47
48static bool
49is_hidden(DIALOG_FORMITEM * item)
50{
51 return ((item->type & 1) != 0);
52}
53
54static bool
55in_window(WINDOW *win, int scrollamt, int y)
56{
57 return (y >= scrollamt && y - scrollamt < getmaxy(win));
58}
59
60static bool
61ok_move(WINDOW *win, int scrollamt, int y, int x)
62{
63 return in_window(win, scrollamt, y)
64 && (wmove(win, y - scrollamt, x) != ERR);
65}
66
67static void
68move_past(WINDOW *win, int y, int x)
69{
70 if (wmove(win, y, x) == ERR)
71 wmove(win, y, getmaxx(win) - 1);
72}
73
74/*
75 * Print form item
76 */
77static int
78print_item(WINDOW *win, DIALOG_FORMITEM * item, int scrollamt, bool choice)
79{
80 int count = 0;
81 int len;
82
83 if (ok_move(win, scrollamt, item->name_y, item->name_x)) {
84 len = item->name_len;
85 len = MIN(len, getmaxx(win) - item->name_x);
86 if (len > 0) {
87 dlg_show_string(win,
88 item->name,
89 0,
90 menubox_attr,
91 item->name_y - scrollamt,
92 item->name_x,
93 len,
94 FALSE,
95 FALSE);
96 move_past(win, item->name_y - scrollamt, item->name_x + len);
97 count = 1;
98 }
99 }
100 if (item->text_len && ok_move(win, scrollamt, item->text_y, item->text_x)) {
101 chtype this_item_attribute;
102
103 len = item->text_len;
104 len = MIN(len, getmaxx(win) - item->text_x);
105
106 if (!is_readonly(item)) {
107 this_item_attribute = choice
108 ? form_active_text_attr
109 : form_text_attr;
110 } else {
111 this_item_attribute = form_item_readonly_attr;
112 }
113
114 if (len > 0) {
115 dlg_show_string(win,
116 item->text,
117 0,
118 this_item_attribute,
119 item->text_y - scrollamt,
120 item->text_x,
121 len,
122 is_hidden(item),
123 FALSE);
124 move_past(win, item->text_y - scrollamt, item->text_x + len);
125 count = 1;
126 }
127 }
128 return count;
129}
130
131/*
132 * Print the entire form.
133 */
134static void
135print_form(WINDOW *win, DIALOG_FORMITEM * item, int total, int scrollamt, int choice)
136{
137 int n;
138 int count = 0;
139
140 for (n = 0; n < total; ++n) {
141 count += print_item(win, item + n, scrollamt, n == choice);
142 }
143 if (count) {
144 wbkgdset(win, menubox_attr | ' ');
145 wclrtobot(win);
146 (void) wnoutrefresh(win);
147 }
148}
149
150static int
151set_choice(DIALOG_FORMITEM item[], int choice, int item_no, bool * noneditable)
152{
153 int result = -1;
154 int i;
155
156 *noneditable = FALSE;
157 if (!is_readonly(&item[choice])) {
158 result = choice;
159 } else {
160 for (i = 0; i < item_no; i++) {
161 if (!is_readonly(&(item[i]))) {
162 result = i;
163 break;
164 }
165 }
166 if (result < 0) {
167 *noneditable = TRUE;
168 result = 0;
169 }
170 }
171 return result;
172}
173
174/*
175 * Find the last y-value in the form.
176 */
177static int
178form_limit(DIALOG_FORMITEM item[])
179{
180 int n;
181 int limit = 0;
182 for (n = 0; item[n].name != 0; ++n) {
183 if (limit < item[n].name_y)
184 limit = item[n].name_y;
185 if (limit < item[n].text_y)
186 limit = item[n].text_y;
187 }
188 return limit;
189}
190
191static int
192is_first_field(DIALOG_FORMITEM item[], int choice)
193{
194 int count = 0;
195 while (choice >= 0) {
196 if (item[choice].text_flen > 0) {
197 ++count;
198 }
199 --choice;
200 }
201
202 return (count == 1);
203}
204
205static int
206is_last_field(DIALOG_FORMITEM item[], int choice, int item_no)
207{
208 int count = 0;
209 while (choice < item_no) {
210 if (item[choice].text_flen > 0) {
211 ++count;
212 }
213 ++choice;
214 }
215
216 return (count == 1);
217}
218
191/*
192 * Tab to the next field.
193 */
194static bool
195tab_next(WINDOW *win,
196 DIALOG_FORMITEM item[],
197 int item_no,
198 int stepsize,
199 int *choice,
200 int *scrollamt)
201{
202 int old_choice = *choice;
203 int old_scroll = *scrollamt;
204 bool wrapped = FALSE;
205
206 do {
207 do {
208 *choice += stepsize;
209 if (*choice < 0) {
210 *choice = item_no - 1;
211 wrapped = TRUE;
212 } else if (*choice >= item_no) {
213 *choice = 0;
214 wrapped = TRUE;
215 }
216 } while ((*choice != old_choice) && is_readonly(&(item[*choice])));
217
218 if (item[*choice].text_flen > 0) {
219 int lo = MIN(item[*choice].name_y, item[*choice].text_y);
220 int hi = MAX(item[*choice].name_y, item[*choice].text_y);
221
222 if (old_choice == *choice)
223 break;
224 print_item(win, item + old_choice, *scrollamt, FALSE);
225
226 if (*scrollamt < lo + 1 - getmaxy(win))
227 *scrollamt = lo + 1 - getmaxy(win);
228 if (*scrollamt > hi)
229 *scrollamt = hi;
230 /*
231 * If we have to scroll to show a wrap-around, it does get
232 * confusing. Just give up rather than scroll. Tab'ing to the
233 * next field in a multi-column form is a different matter. Scroll
234 * for that.
235 */
236 if (*scrollamt != old_scroll) {
237 if (wrapped) {
238 beep();
239 *scrollamt = old_scroll;
240 *choice = old_choice;
241 } else {
242 scrollok(win, TRUE);
243 wscrl(win, *scrollamt - old_scroll);
244 scrollok(win, FALSE);
245 }
246 }
247 break;
248 }
249 } while (*choice != old_choice);
250
251 return (old_choice != *choice) || (old_scroll != *scrollamt);
252}
253
254/*
255 * Scroll to the next page, putting the choice at the first editable field
256 * in that page. Note that fields are not necessarily in top-to-bottom order,
257 * nor is there necessarily a field on each row of the window.
258 */
259static bool
260scroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
261{
262 int old_choice = *choice;
263 int old_scroll = *scrollamt;
264 int old_row = MIN(item[old_choice].text_y, item[old_choice].name_y);
265 int target = old_scroll + stepsize;
266 int n;
267
268 if (stepsize < 0) {
269 if (old_row != old_scroll)
270 target = old_scroll;
271 else
272 target = old_scroll + stepsize;
273 if (target < 0)
274 target = 0;
275 } else {
276 int limit = form_limit(item);
277 if (target > limit)
278 target = limit;
279 }
280
281 for (n = 0; item[n].name != 0; ++n) {
282 if (item[n].text_flen > 0) {
283 int new_row = MIN(item[n].text_y, item[n].name_y);
284 if (abs(new_row - target) < abs(old_row - target)) {
285 old_row = new_row;
286 *choice = n;
287 }
288 }
289 }
290
291 if (old_choice != *choice)
292 print_item(win, item + old_choice, *scrollamt, FALSE);
293
294 *scrollamt = *choice;
295 if (*scrollamt != old_scroll) {
296 scrollok(win, TRUE);
297 wscrl(win, *scrollamt - old_scroll);
298 scrollok(win, FALSE);
299 }
300 return (old_choice != *choice) || (old_scroll != *scrollamt);
301}
302
303/*
304 * Do a sanity check on the field length, and return the "right" value.
305 */
306static int
307real_length(DIALOG_FORMITEM * item)
308{
309 return (item->text_flen > 0
310 ? item->text_flen
311 : (item->text_flen < 0
312 ? -item->text_flen
313 : item->text_len));
314}
315
316/*
317 * Compute the form size, setup field buffers.
318 */
319static void
320make_FORM_ELTs(DIALOG_FORMITEM * item,
321 int item_no,
322 int *min_height,
323 int *min_width)
324{
325 int i;
326 int min_w = 0;
327 int min_h = 0;
328
329 for (i = 0; i < item_no; ++i) {
330 int real_len = real_length(item + i);
331
332 /*
333 * Special value '0' for text_flen: no input allowed
334 * Special value '0' for text_ilen: 'be the same as text_flen'
335 */
336 if (item[i].text_ilen == 0)
337 item[i].text_ilen = real_len;
338
339 min_h = MAX(min_h, item[i].name_y + 1);
340 min_h = MAX(min_h, item[i].text_y + 1);
341 min_w = MAX(min_w, item[i].name_x + 1 + item[i].name_len);
342 min_w = MAX(min_w, item[i].text_x + 1 + real_len);
343
344 item[i].text_len = real_length(item + i);
345
346 /*
347 * We do not know the actual length of .text, so we allocate it here
348 * to ensure it is big enough.
349 */
350 if (item[i].text_flen > 0) {
351 int max_len = dlg_max_input(MAX(item[i].text_ilen + 1, MAX_LEN));
352 char *old_text = item[i].text;
353
354 item[i].text = dlg_malloc(char, (size_t) max_len + 1);
355 assert_ptr(item[i].text, "make_FORM_ELTs");
356
357 sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
358
359 if (item[i].text_free) {
360 item[i].text_free = FALSE;
361 free(old_text);
362 }
363 item[i].text_free = TRUE;
364 }
365 }
366
367 *min_height = min_h;
368 *min_width = min_w;
369}
370
371int
372dlg_default_formitem(DIALOG_FORMITEM * items)
373{
374 int result = 0;
375
376 if (dialog_vars.default_item != 0) {
377 int count = 0;
378 while (items->name != 0) {
379 if (!strcmp(dialog_vars.default_item, items->name)) {
380 result = count;
381 break;
382 }
383 ++items;
384 count++;
385 }
386 }
387 return result;
388}
389
390#define sTEXT -1
391
392static int
393next_valid_buttonindex(int state, int extra, bool non_editable)
394{
395 state = dlg_next_ok_buttonindex(state, extra);
396 while (non_editable && state == sTEXT)
397 state = dlg_next_ok_buttonindex(state, sTEXT);
398 return state;
399}
400
401static int
402prev_valid_buttonindex(int state, int extra, bool non_editable)
403{
404 state = dlg_prev_ok_buttonindex(state, extra);
405 while (non_editable && state == sTEXT)
406 state = dlg_prev_ok_buttonindex(state, sTEXT);
407 return state;
408}
409
410#define NAVIGATE_BINDINGS \
411 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
412 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
413 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
414 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
415 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
416 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
417 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ), \
418 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
419 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
420 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
421/*
422 * Display a form for fulfill a number of fields
423 */
424int
425dlg_form(const char *title,
426 const char *cprompt,
427 int height,
428 int width,
429 int form_height,
430 int item_no,
431 DIALOG_FORMITEM * items,
432 int *current_item)
433{
434 /* *INDENT-OFF* */
435 static DLG_KEYS_BINDING binding[] = {
436 HELPKEY_BINDINGS,
437 ENTERKEY_BINDINGS,
438 NAVIGATE_BINDINGS,
439 END_KEYS_BINDING
440 };
441 static DLG_KEYS_BINDING binding2[] = {
442 INPUTSTR_BINDINGS,
443 HELPKEY_BINDINGS,
444 ENTERKEY_BINDINGS,
445 NAVIGATE_BINDINGS,
446 END_KEYS_BINDING
447 };
448 /* *INDENT-ON* */
449
450#ifdef KEY_RESIZE
451 int old_height = height;
452 int old_width = width;
453#endif
454
455 int form_width;
456 int first = TRUE;
219/*
220 * Tab to the next field.
221 */
222static bool
223tab_next(WINDOW *win,
224 DIALOG_FORMITEM item[],
225 int item_no,
226 int stepsize,
227 int *choice,
228 int *scrollamt)
229{
230 int old_choice = *choice;
231 int old_scroll = *scrollamt;
232 bool wrapped = FALSE;
233
234 do {
235 do {
236 *choice += stepsize;
237 if (*choice < 0) {
238 *choice = item_no - 1;
239 wrapped = TRUE;
240 } else if (*choice >= item_no) {
241 *choice = 0;
242 wrapped = TRUE;
243 }
244 } while ((*choice != old_choice) && is_readonly(&(item[*choice])));
245
246 if (item[*choice].text_flen > 0) {
247 int lo = MIN(item[*choice].name_y, item[*choice].text_y);
248 int hi = MAX(item[*choice].name_y, item[*choice].text_y);
249
250 if (old_choice == *choice)
251 break;
252 print_item(win, item + old_choice, *scrollamt, FALSE);
253
254 if (*scrollamt < lo + 1 - getmaxy(win))
255 *scrollamt = lo + 1 - getmaxy(win);
256 if (*scrollamt > hi)
257 *scrollamt = hi;
258 /*
259 * If we have to scroll to show a wrap-around, it does get
260 * confusing. Just give up rather than scroll. Tab'ing to the
261 * next field in a multi-column form is a different matter. Scroll
262 * for that.
263 */
264 if (*scrollamt != old_scroll) {
265 if (wrapped) {
266 beep();
267 *scrollamt = old_scroll;
268 *choice = old_choice;
269 } else {
270 scrollok(win, TRUE);
271 wscrl(win, *scrollamt - old_scroll);
272 scrollok(win, FALSE);
273 }
274 }
275 break;
276 }
277 } while (*choice != old_choice);
278
279 return (old_choice != *choice) || (old_scroll != *scrollamt);
280}
281
282/*
283 * Scroll to the next page, putting the choice at the first editable field
284 * in that page. Note that fields are not necessarily in top-to-bottom order,
285 * nor is there necessarily a field on each row of the window.
286 */
287static bool
288scroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
289{
290 int old_choice = *choice;
291 int old_scroll = *scrollamt;
292 int old_row = MIN(item[old_choice].text_y, item[old_choice].name_y);
293 int target = old_scroll + stepsize;
294 int n;
295
296 if (stepsize < 0) {
297 if (old_row != old_scroll)
298 target = old_scroll;
299 else
300 target = old_scroll + stepsize;
301 if (target < 0)
302 target = 0;
303 } else {
304 int limit = form_limit(item);
305 if (target > limit)
306 target = limit;
307 }
308
309 for (n = 0; item[n].name != 0; ++n) {
310 if (item[n].text_flen > 0) {
311 int new_row = MIN(item[n].text_y, item[n].name_y);
312 if (abs(new_row - target) < abs(old_row - target)) {
313 old_row = new_row;
314 *choice = n;
315 }
316 }
317 }
318
319 if (old_choice != *choice)
320 print_item(win, item + old_choice, *scrollamt, FALSE);
321
322 *scrollamt = *choice;
323 if (*scrollamt != old_scroll) {
324 scrollok(win, TRUE);
325 wscrl(win, *scrollamt - old_scroll);
326 scrollok(win, FALSE);
327 }
328 return (old_choice != *choice) || (old_scroll != *scrollamt);
329}
330
331/*
332 * Do a sanity check on the field length, and return the "right" value.
333 */
334static int
335real_length(DIALOG_FORMITEM * item)
336{
337 return (item->text_flen > 0
338 ? item->text_flen
339 : (item->text_flen < 0
340 ? -item->text_flen
341 : item->text_len));
342}
343
344/*
345 * Compute the form size, setup field buffers.
346 */
347static void
348make_FORM_ELTs(DIALOG_FORMITEM * item,
349 int item_no,
350 int *min_height,
351 int *min_width)
352{
353 int i;
354 int min_w = 0;
355 int min_h = 0;
356
357 for (i = 0; i < item_no; ++i) {
358 int real_len = real_length(item + i);
359
360 /*
361 * Special value '0' for text_flen: no input allowed
362 * Special value '0' for text_ilen: 'be the same as text_flen'
363 */
364 if (item[i].text_ilen == 0)
365 item[i].text_ilen = real_len;
366
367 min_h = MAX(min_h, item[i].name_y + 1);
368 min_h = MAX(min_h, item[i].text_y + 1);
369 min_w = MAX(min_w, item[i].name_x + 1 + item[i].name_len);
370 min_w = MAX(min_w, item[i].text_x + 1 + real_len);
371
372 item[i].text_len = real_length(item + i);
373
374 /*
375 * We do not know the actual length of .text, so we allocate it here
376 * to ensure it is big enough.
377 */
378 if (item[i].text_flen > 0) {
379 int max_len = dlg_max_input(MAX(item[i].text_ilen + 1, MAX_LEN));
380 char *old_text = item[i].text;
381
382 item[i].text = dlg_malloc(char, (size_t) max_len + 1);
383 assert_ptr(item[i].text, "make_FORM_ELTs");
384
385 sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
386
387 if (item[i].text_free) {
388 item[i].text_free = FALSE;
389 free(old_text);
390 }
391 item[i].text_free = TRUE;
392 }
393 }
394
395 *min_height = min_h;
396 *min_width = min_w;
397}
398
399int
400dlg_default_formitem(DIALOG_FORMITEM * items)
401{
402 int result = 0;
403
404 if (dialog_vars.default_item != 0) {
405 int count = 0;
406 while (items->name != 0) {
407 if (!strcmp(dialog_vars.default_item, items->name)) {
408 result = count;
409 break;
410 }
411 ++items;
412 count++;
413 }
414 }
415 return result;
416}
417
418#define sTEXT -1
419
420static int
421next_valid_buttonindex(int state, int extra, bool non_editable)
422{
423 state = dlg_next_ok_buttonindex(state, extra);
424 while (non_editable && state == sTEXT)
425 state = dlg_next_ok_buttonindex(state, sTEXT);
426 return state;
427}
428
429static int
430prev_valid_buttonindex(int state, int extra, bool non_editable)
431{
432 state = dlg_prev_ok_buttonindex(state, extra);
433 while (non_editable && state == sTEXT)
434 state = dlg_prev_ok_buttonindex(state, sTEXT);
435 return state;
436}
437
438#define NAVIGATE_BINDINGS \
439 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
440 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
441 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
442 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
443 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
444 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
445 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ), \
446 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
447 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
448 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
449/*
450 * Display a form for fulfill a number of fields
451 */
452int
453dlg_form(const char *title,
454 const char *cprompt,
455 int height,
456 int width,
457 int form_height,
458 int item_no,
459 DIALOG_FORMITEM * items,
460 int *current_item)
461{
462 /* *INDENT-OFF* */
463 static DLG_KEYS_BINDING binding[] = {
464 HELPKEY_BINDINGS,
465 ENTERKEY_BINDINGS,
466 NAVIGATE_BINDINGS,
467 END_KEYS_BINDING
468 };
469 static DLG_KEYS_BINDING binding2[] = {
470 INPUTSTR_BINDINGS,
471 HELPKEY_BINDINGS,
472 ENTERKEY_BINDINGS,
473 NAVIGATE_BINDINGS,
474 END_KEYS_BINDING
475 };
476 /* *INDENT-ON* */
477
478#ifdef KEY_RESIZE
479 int old_height = height;
480 int old_width = width;
481#endif
482
483 int form_width;
484 int first = TRUE;
485 int first_trace = TRUE;
457 int chr_offset = 0;
486 int chr_offset = 0;
458 int state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
487 int state = dialog_vars.default_button >=0 ? dlg_default_button() : sTEXT;
459 int x, y, cur_x, cur_y, box_x, box_y;
460 int code;
461 int key = 0;
462 int fkey;
463 int choice = dlg_default_formitem(items);
464 int new_choice, new_scroll;
465 int scrollamt = 0;
466 int result = DLG_EXIT_UNKNOWN;
467 int min_width = 0, min_height = 0;
468 bool was_autosize = (height == 0 || width == 0);
469 bool show_buttons = FALSE;
470 bool scroll_changed = FALSE;
471 bool field_changed = FALSE;
472 bool non_editable = FALSE;
473 WINDOW *dialog, *form;
474 char *prompt = dlg_strclone(cprompt);
475 const char **buttons = dlg_ok_labels();
476 DIALOG_FORMITEM *current;
477
478 make_FORM_ELTs(items, item_no, &min_height, &min_width);
479 dlg_button_layout(buttons, &min_width);
480 dlg_does_output();
481 dlg_tab_correct_str(prompt);
482
483#ifdef KEY_RESIZE
484 retry:
485#endif
486
487 dlg_auto_size(title, prompt, &height, &width,
488 1 + 3 * MARGIN,
489 MAX(26, 2 + min_width));
490
491 if (form_height == 0)
492 form_height = min_height;
493
494 if (was_autosize) {
495 form_height = MIN(SLINES - height, form_height);
496 height += form_height;
497 } else {
498 int thigh = 0;
499 int twide = 0;
500 dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
501 thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
502 form_height = MIN(thigh, form_height);
503 }
504
505 dlg_print_size(height, width);
506 dlg_ctl_size(height, width);
507
508 x = dlg_box_x_ordinate(width);
509 y = dlg_box_y_ordinate(height);
510
511 dialog = dlg_new_window(height, width, y, x);
512 dlg_register_window(dialog, "formbox", binding);
488 int x, y, cur_x, cur_y, box_x, box_y;
489 int code;
490 int key = 0;
491 int fkey;
492 int choice = dlg_default_formitem(items);
493 int new_choice, new_scroll;
494 int scrollamt = 0;
495 int result = DLG_EXIT_UNKNOWN;
496 int min_width = 0, min_height = 0;
497 bool was_autosize = (height == 0 || width == 0);
498 bool show_buttons = FALSE;
499 bool scroll_changed = FALSE;
500 bool field_changed = FALSE;
501 bool non_editable = FALSE;
502 WINDOW *dialog, *form;
503 char *prompt = dlg_strclone(cprompt);
504 const char **buttons = dlg_ok_labels();
505 DIALOG_FORMITEM *current;
506
507 make_FORM_ELTs(items, item_no, &min_height, &min_width);
508 dlg_button_layout(buttons, &min_width);
509 dlg_does_output();
510 dlg_tab_correct_str(prompt);
511
512#ifdef KEY_RESIZE
513 retry:
514#endif
515
516 dlg_auto_size(title, prompt, &height, &width,
517 1 + 3 * MARGIN,
518 MAX(26, 2 + min_width));
519
520 if (form_height == 0)
521 form_height = min_height;
522
523 if (was_autosize) {
524 form_height = MIN(SLINES - height, form_height);
525 height += form_height;
526 } else {
527 int thigh = 0;
528 int twide = 0;
529 dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
530 thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
531 form_height = MIN(thigh, form_height);
532 }
533
534 dlg_print_size(height, width);
535 dlg_ctl_size(height, width);
536
537 x = dlg_box_x_ordinate(width);
538 y = dlg_box_y_ordinate(height);
539
540 dialog = dlg_new_window(height, width, y, x);
541 dlg_register_window(dialog, "formbox", binding);
513 dlg_register_window(dialog, "formfield", binding2);
514 dlg_register_buttons(dialog, "formbox", buttons);
515
516 dlg_mouse_setbase(x, y);
517
542 dlg_register_buttons(dialog, "formbox", buttons);
543
544 dlg_mouse_setbase(x, y);
545
518 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
519 dlg_draw_bottom_box(dialog);
546 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
547 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
520 dlg_draw_title(dialog, title);
521
548 dlg_draw_title(dialog, title);
549
522 wattrset(dialog, dialog_attr);
550 (void) wattrset(dialog, dialog_attr);
523 dlg_print_autowrap(dialog, prompt, height, width);
524
525 form_width = width - 6;
526 getyx(dialog, cur_y, cur_x);
551 dlg_print_autowrap(dialog, prompt, height, width);
552
553 form_width = width - 6;
554 getyx(dialog, cur_y, cur_x);
555 (void) cur_x;
527 box_y = cur_y + 1;
528 box_x = (width - form_width) / 2 - 1;
529
530 /* create new window for the form */
531 form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
532 x + box_x + 1);
556 box_y = cur_y + 1;
557 box_x = (width - form_width) / 2 - 1;
558
559 /* create new window for the form */
560 form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
561 x + box_x + 1);
562 dlg_register_window(form, "formfield", binding2);
533
534 /* draw a box around the form items */
535 dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
563
564 /* draw a box around the form items */
565 dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
536 menubox_border_attr, menubox_attr);
566 menubox_border_attr, menubox_border2_attr);
537
538 /* register the new window, along with its borders */
539 dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
540 getbegx(form) - getbegx(dialog),
541 getmaxy(form),
542 getmaxx(form),
543 KEY_MAX, 1, 1, 3 /* by cells */ );
544
545 show_buttons = TRUE;
546 scroll_changed = TRUE;
547
548 choice = set_choice(items, choice, item_no, &non_editable);
549 current = &items[choice];
550 if (non_editable)
551 state = next_valid_buttonindex(state, sTEXT, non_editable);
552
553 while (result == DLG_EXIT_UNKNOWN) {
554 int edit = FALSE;
555
556 if (scroll_changed) {
557 print_form(form, items, item_no, scrollamt, choice);
558 dlg_draw_scrollbar(dialog,
559 scrollamt,
560 scrollamt,
561 scrollamt + form_height + 1,
562 min_height,
563 box_x + 1,
564 box_x + form_width,
565 box_y,
566 box_y + form_height + 1,
567
568 /* register the new window, along with its borders */
569 dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
570 getbegx(form) - getbegx(dialog),
571 getmaxy(form),
572 getmaxx(form),
573 KEY_MAX, 1, 1, 3 /* by cells */ );
574
575 show_buttons = TRUE;
576 scroll_changed = TRUE;
577
578 choice = set_choice(items, choice, item_no, &non_editable);
579 current = &items[choice];
580 if (non_editable)
581 state = next_valid_buttonindex(state, sTEXT, non_editable);
582
583 while (result == DLG_EXIT_UNKNOWN) {
584 int edit = FALSE;
585
586 if (scroll_changed) {
587 print_form(form, items, item_no, scrollamt, choice);
588 dlg_draw_scrollbar(dialog,
589 scrollamt,
590 scrollamt,
591 scrollamt + form_height + 1,
592 min_height,
593 box_x + 1,
594 box_x + form_width,
595 box_y,
596 box_y + form_height + 1,
567 menubox_attr,
597 menubox_border2_attr,
568 menubox_border_attr);
569 scroll_changed = FALSE;
570 }
571
572 if (show_buttons) {
573 dlg_item_help("");
574 dlg_draw_buttons(dialog, height - 2, 0, buttons,
575 ((state < 0)
576 ? 1000 /* no such button, not highlighted */
577 : state),
578 FALSE, width);
579 show_buttons = FALSE;
580 }
581
598 menubox_border_attr);
599 scroll_changed = FALSE;
600 }
601
602 if (show_buttons) {
603 dlg_item_help("");
604 dlg_draw_buttons(dialog, height - 2, 0, buttons,
605 ((state < 0)
606 ? 1000 /* no such button, not highlighted */
607 : state),
608 FALSE, width);
609 show_buttons = FALSE;
610 }
611
612 if (first_trace) {
613 first_trace = FALSE;
614 dlg_trace_win(dialog);
615 }
616
582 if (field_changed || state == sTEXT) {
583 if (field_changed)
584 chr_offset = 0;
585 current = &items[choice];
586 dialog_vars.max_input = current->text_ilen;
587 dlg_item_help(current->help);
588 dlg_show_string(form, current->text, chr_offset,
589 form_active_text_attr,
590 current->text_y - scrollamt,
591 current->text_x,
592 current->text_len,
593 is_hidden(current), first);
594 field_changed = FALSE;
595 }
596
617 if (field_changed || state == sTEXT) {
618 if (field_changed)
619 chr_offset = 0;
620 current = &items[choice];
621 dialog_vars.max_input = current->text_ilen;
622 dlg_item_help(current->help);
623 dlg_show_string(form, current->text, chr_offset,
624 form_active_text_attr,
625 current->text_y - scrollamt,
626 current->text_x,
627 current->text_len,
628 is_hidden(current), first);
629 field_changed = FALSE;
630 }
631
597 key = dlg_mouse_wgetch(dialog, &fkey);
632 key = dlg_mouse_wgetch((state == sTEXT) ? form : dialog, &fkey);
598 if (dlg_result_key(key, fkey, &result))
599 break;
600
601 /* handle non-functionkeys */
602 if (!fkey) {
603 if (state != sTEXT) {
604 code = dlg_char_to_button(key, buttons);
605 if (code >= 0) {
606 dlg_del_window(dialog);
607 result = dlg_ok_buttoncode(code);
608 continue;
609 }
610 if (key == ' ') {
611 fkey = TRUE;
612 key = DLGK_ENTER;
613 }
614 }
615 }
616
617 /* handle functionkeys */
618 if (fkey) {
619 bool do_scroll = FALSE;
620 bool do_tab = FALSE;
621 int move_by = 0;
622
623 switch (key) {
624 case DLGK_MOUSE(KEY_PPAGE):
625 case DLGK_PAGE_PREV:
626 do_scroll = TRUE;
627 move_by = -form_height;
628 break;
629
630 case DLGK_MOUSE(KEY_NPAGE):
631 case DLGK_PAGE_NEXT:
632 do_scroll = TRUE;
633 move_by = form_height;
634 break;
635
636 case DLGK_ENTER:
637 dlg_del_window(dialog);
638 result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
639 continue;
640
641 case DLGK_GRID_LEFT:
642 if (state == sTEXT)
643 break;
644 /* FALLTHRU */
645 case DLGK_ITEM_PREV:
646 if (state == sTEXT) {
647 do_tab = TRUE;
648 move_by = -1;
649 break;
650 } else {
651 state = prev_valid_buttonindex(state, 0, non_editable);
652 show_buttons = TRUE;
653 continue;
654 }
655
633 if (dlg_result_key(key, fkey, &result))
634 break;
635
636 /* handle non-functionkeys */
637 if (!fkey) {
638 if (state != sTEXT) {
639 code = dlg_char_to_button(key, buttons);
640 if (code >= 0) {
641 dlg_del_window(dialog);
642 result = dlg_ok_buttoncode(code);
643 continue;
644 }
645 if (key == ' ') {
646 fkey = TRUE;
647 key = DLGK_ENTER;
648 }
649 }
650 }
651
652 /* handle functionkeys */
653 if (fkey) {
654 bool do_scroll = FALSE;
655 bool do_tab = FALSE;
656 int move_by = 0;
657
658 switch (key) {
659 case DLGK_MOUSE(KEY_PPAGE):
660 case DLGK_PAGE_PREV:
661 do_scroll = TRUE;
662 move_by = -form_height;
663 break;
664
665 case DLGK_MOUSE(KEY_NPAGE):
666 case DLGK_PAGE_NEXT:
667 do_scroll = TRUE;
668 move_by = form_height;
669 break;
670
671 case DLGK_ENTER:
672 dlg_del_window(dialog);
673 result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
674 continue;
675
676 case DLGK_GRID_LEFT:
677 if (state == sTEXT)
678 break;
679 /* FALLTHRU */
680 case DLGK_ITEM_PREV:
681 if (state == sTEXT) {
682 do_tab = TRUE;
683 move_by = -1;
684 break;
685 } else {
686 state = prev_valid_buttonindex(state, 0, non_editable);
687 show_buttons = TRUE;
688 continue;
689 }
690
691 case DLGK_FORM_PREV:
692 if (state == sTEXT && !is_first_field(items, choice)) {
693 do_tab = TRUE;
694 move_by = -1;
695 break;
696 } else {
697 int old_state = state;
698 state = prev_valid_buttonindex(state, sTEXT, non_editable);
699 show_buttons = TRUE;
700 if (old_state >= 0 && state == sTEXT) {
701 new_choice = item_no - 1;
702 if (choice != new_choice) {
703 print_item(form, items + choice, scrollamt, FALSE);
704 choice = new_choice;
705 }
706 }
707 continue;
708 }
709
656 case DLGK_FIELD_PREV:
657 state = prev_valid_buttonindex(state, sTEXT, non_editable);
658 show_buttons = TRUE;
659 continue;
660
661 case DLGK_FIELD_NEXT:
662 state = next_valid_buttonindex(state, sTEXT, non_editable);
663 show_buttons = TRUE;
664 continue;
665
666 case DLGK_GRID_RIGHT:
667 if (state == sTEXT)
668 break;
669 /* FALLTHRU */
670
671 case DLGK_ITEM_NEXT:
672 if (state == sTEXT) {
673 do_tab = TRUE;
674 move_by = 1;
675 break;
676 } else {
677 state = next_valid_buttonindex(state, 0, non_editable);
678 show_buttons = TRUE;
679 continue;
680 }
681
710 case DLGK_FIELD_PREV:
711 state = prev_valid_buttonindex(state, sTEXT, non_editable);
712 show_buttons = TRUE;
713 continue;
714
715 case DLGK_FIELD_NEXT:
716 state = next_valid_buttonindex(state, sTEXT, non_editable);
717 show_buttons = TRUE;
718 continue;
719
720 case DLGK_GRID_RIGHT:
721 if (state == sTEXT)
722 break;
723 /* FALLTHRU */
724
725 case DLGK_ITEM_NEXT:
726 if (state == sTEXT) {
727 do_tab = TRUE;
728 move_by = 1;
729 break;
730 } else {
731 state = next_valid_buttonindex(state, 0, non_editable);
732 show_buttons = TRUE;
733 continue;
734 }
735
736 case DLGK_FORM_NEXT:
737 if (state == sTEXT && !is_last_field(items, choice, item_no)) {
738 do_tab = TRUE;
739 move_by = 1;
740 break;
741 } else {
742 state = next_valid_buttonindex(state, sTEXT, non_editable);
743 show_buttons = TRUE;
744 if (state == sTEXT && choice) {
745 print_item(form, items + choice, scrollamt, FALSE);
746 choice = 0;
747 }
748 continue;
749 }
750
682#ifdef KEY_RESIZE
683 case KEY_RESIZE:
684 /* reset data */
685 height = old_height;
686 width = old_width;
687 /* repaint */
688 dlg_clear();
689 dlg_del_window(dialog);
690 refresh();
691 dlg_mouse_free_regions();
692 goto retry;
693#endif
694 default:
695#if USE_MOUSE
696 if (is_DLGK_MOUSE(key)) {
697 if (key >= DLGK_MOUSE(KEY_MAX)) {
698 int cell = key - DLGK_MOUSE(KEY_MAX);
699 int row = (cell / getmaxx(form)) + scrollamt;
700 int col = (cell % getmaxx(form));
701 int n;
702
703 for (n = 0; n < item_no; ++n) {
704 if (items[n].name_y == row
705 && items[n].name_x <= col
706 && (items[n].name_x + items[n].name_len > col
707 || (items[n].name_y == items[n].text_y
708 && items[n].text_x > col))) {
709 if (!is_readonly(&(items[n]))) {
710 field_changed = TRUE;
711 break;
712 }
713 }
714 if (items[n].text_y == row
715 && items[n].text_x <= col
716 && items[n].text_x + items[n].text_ilen > col) {
717 if (!is_readonly(&(items[n]))) {
718 field_changed = TRUE;
719 break;
720 }
721 }
722 }
723 if (field_changed) {
724 print_item(form, items + choice, scrollamt, FALSE);
725 choice = n;
726 continue;
727 }
728 beep();
729 } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
730 result = code;
731 }
732 continue;
733 }
734#endif
735 break;
736 }
737
738 new_scroll = scrollamt;
739 new_choice = choice;
740 if (do_scroll) {
741 if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
742 if (choice != new_choice) {
743 choice = new_choice;
744 field_changed = TRUE;
745 }
746 if (scrollamt != new_scroll) {
747 scrollamt = new_scroll;
748 scroll_changed = TRUE;
749 }
750 }
751 continue;
752 }
753 if (do_tab) {
754 if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
755 if (choice != new_choice) {
756 choice = new_choice;
757 field_changed = TRUE;
758 }
759 if (scrollamt != new_scroll) {
760 scrollamt = new_scroll;
761 scroll_changed = TRUE;
762 }
763 }
764 continue;
765 }
766 }
767
768 if (state == sTEXT) { /* Input box selected */
769 if (!is_readonly(current))
770 edit = dlg_edit_string(current->text, &chr_offset, key,
771 fkey, first);
772 if (edit) {
773 dlg_show_string(form, current->text, chr_offset,
774 form_active_text_attr,
775 current->text_y - scrollamt,
776 current->text_x,
777 current->text_len,
778 is_hidden(current), first);
779 continue;
780 }
781 }
782
783 }
784
785 dlg_mouse_free_regions();
786 dlg_del_window(dialog);
787 free(prompt);
788
789 *current_item = choice;
790 return result;
791}
792
793/*
794 * Free memory owned by a list of DIALOG_FORMITEM's.
795 */
796void
797dlg_free_formitems(DIALOG_FORMITEM * items)
798{
799 int n;
800 for (n = 0; items[n].name != 0; ++n) {
801 if (items[n].name_free)
802 free(items[n].name);
803 if (items[n].text_free)
804 free(items[n].text);
805 if (items[n].help_free && items[n].help != dlg_strempty())
806 free(items[n].help);
807 }
808 free(items);
809}
810
811/*
812 * The script accepts values beginning at 1, while curses starts at 0.
813 */
814int
815dlg_ordinate(const char *s)
816{
817 int result = atoi(s);
818 if (result > 0)
819 --result;
820 else
821 result = 0;
822 return result;
823}
824
825int
826dialog_form(const char *title,
827 const char *cprompt,
828 int height,
829 int width,
830 int form_height,
831 int item_no,
832 char **items)
833{
834 int result;
835 int choice;
836 int i;
837 DIALOG_FORMITEM *listitems;
838 DIALOG_VARS save_vars;
839 bool show_status = FALSE;
840
841 dlg_save_vars(&save_vars);
842 dialog_vars.separate_output = TRUE;
843
844 listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
845 assert_ptr(listitems, "dialog_form");
846
847 for (i = 0; i < item_no; ++i) {
848 listitems[i].type = dialog_vars.formitem_type;
849 listitems[i].name = ItemName(i);
850 listitems[i].name_len = (int) strlen(ItemName(i));
851 listitems[i].name_y = dlg_ordinate(ItemNameY(i));
852 listitems[i].name_x = dlg_ordinate(ItemNameX(i));
853 listitems[i].text = ItemText(i);
854 listitems[i].text_len = (int) strlen(ItemText(i));
855 listitems[i].text_y = dlg_ordinate(ItemTextY(i));
856 listitems[i].text_x = dlg_ordinate(ItemTextX(i));
857 listitems[i].text_flen = atoi(ItemTextFLen(i));
858 listitems[i].text_ilen = atoi(ItemTextILen(i));
859 listitems[i].help = ((dialog_vars.item_help)
860 ? ItemHelp(i)
861 : dlg_strempty());
862 }
863
864 result = dlg_form(title,
865 cprompt,
866 height,
867 width,
868 form_height,
869 item_no,
870 listitems,
871 &choice);
872
873 switch (result) {
874 case DLG_EXIT_OK: /* FALLTHRU */
875 case DLG_EXIT_EXTRA:
876 show_status = TRUE;
877 break;
878 case DLG_EXIT_HELP:
879 dlg_add_result("HELP ");
880 show_status = dialog_vars.help_status;
881 if (USE_ITEM_HELP(listitems[choice].help)) {
882 dlg_add_string(listitems[choice].help);
883 result = DLG_EXIT_ITEM_HELP;
884 } else {
885 dlg_add_string(listitems[choice].name);
886 }
887 if (show_status)
888 dlg_add_separator();
889 break;
890 }
891 if (show_status) {
892 for (i = 0; i < item_no; i++) {
893 if (listitems[i].text_flen > 0) {
894 dlg_add_string(listitems[i].text);
895 dlg_add_separator();
896 }
897 }
898 }
899
900 dlg_free_formitems(listitems);
901 dlg_restore_vars(&save_vars);
902
903 return result;
904}
751#ifdef KEY_RESIZE
752 case KEY_RESIZE:
753 /* reset data */
754 height = old_height;
755 width = old_width;
756 /* repaint */
757 dlg_clear();
758 dlg_del_window(dialog);
759 refresh();
760 dlg_mouse_free_regions();
761 goto retry;
762#endif
763 default:
764#if USE_MOUSE
765 if (is_DLGK_MOUSE(key)) {
766 if (key >= DLGK_MOUSE(KEY_MAX)) {
767 int cell = key - DLGK_MOUSE(KEY_MAX);
768 int row = (cell / getmaxx(form)) + scrollamt;
769 int col = (cell % getmaxx(form));
770 int n;
771
772 for (n = 0; n < item_no; ++n) {
773 if (items[n].name_y == row
774 && items[n].name_x <= col
775 && (items[n].name_x + items[n].name_len > col
776 || (items[n].name_y == items[n].text_y
777 && items[n].text_x > col))) {
778 if (!is_readonly(&(items[n]))) {
779 field_changed = TRUE;
780 break;
781 }
782 }
783 if (items[n].text_y == row
784 && items[n].text_x <= col
785 && items[n].text_x + items[n].text_ilen > col) {
786 if (!is_readonly(&(items[n]))) {
787 field_changed = TRUE;
788 break;
789 }
790 }
791 }
792 if (field_changed) {
793 print_item(form, items + choice, scrollamt, FALSE);
794 choice = n;
795 continue;
796 }
797 beep();
798 } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
799 result = code;
800 }
801 continue;
802 }
803#endif
804 break;
805 }
806
807 new_scroll = scrollamt;
808 new_choice = choice;
809 if (do_scroll) {
810 if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
811 if (choice != new_choice) {
812 choice = new_choice;
813 field_changed = TRUE;
814 }
815 if (scrollamt != new_scroll) {
816 scrollamt = new_scroll;
817 scroll_changed = TRUE;
818 }
819 }
820 continue;
821 }
822 if (do_tab) {
823 if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
824 if (choice != new_choice) {
825 choice = new_choice;
826 field_changed = TRUE;
827 }
828 if (scrollamt != new_scroll) {
829 scrollamt = new_scroll;
830 scroll_changed = TRUE;
831 }
832 }
833 continue;
834 }
835 }
836
837 if (state == sTEXT) { /* Input box selected */
838 if (!is_readonly(current))
839 edit = dlg_edit_string(current->text, &chr_offset, key,
840 fkey, first);
841 if (edit) {
842 dlg_show_string(form, current->text, chr_offset,
843 form_active_text_attr,
844 current->text_y - scrollamt,
845 current->text_x,
846 current->text_len,
847 is_hidden(current), first);
848 continue;
849 }
850 }
851
852 }
853
854 dlg_mouse_free_regions();
855 dlg_del_window(dialog);
856 free(prompt);
857
858 *current_item = choice;
859 return result;
860}
861
862/*
863 * Free memory owned by a list of DIALOG_FORMITEM's.
864 */
865void
866dlg_free_formitems(DIALOG_FORMITEM * items)
867{
868 int n;
869 for (n = 0; items[n].name != 0; ++n) {
870 if (items[n].name_free)
871 free(items[n].name);
872 if (items[n].text_free)
873 free(items[n].text);
874 if (items[n].help_free && items[n].help != dlg_strempty())
875 free(items[n].help);
876 }
877 free(items);
878}
879
880/*
881 * The script accepts values beginning at 1, while curses starts at 0.
882 */
883int
884dlg_ordinate(const char *s)
885{
886 int result = atoi(s);
887 if (result > 0)
888 --result;
889 else
890 result = 0;
891 return result;
892}
893
894int
895dialog_form(const char *title,
896 const char *cprompt,
897 int height,
898 int width,
899 int form_height,
900 int item_no,
901 char **items)
902{
903 int result;
904 int choice;
905 int i;
906 DIALOG_FORMITEM *listitems;
907 DIALOG_VARS save_vars;
908 bool show_status = FALSE;
909
910 dlg_save_vars(&save_vars);
911 dialog_vars.separate_output = TRUE;
912
913 listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
914 assert_ptr(listitems, "dialog_form");
915
916 for (i = 0; i < item_no; ++i) {
917 listitems[i].type = dialog_vars.formitem_type;
918 listitems[i].name = ItemName(i);
919 listitems[i].name_len = (int) strlen(ItemName(i));
920 listitems[i].name_y = dlg_ordinate(ItemNameY(i));
921 listitems[i].name_x = dlg_ordinate(ItemNameX(i));
922 listitems[i].text = ItemText(i);
923 listitems[i].text_len = (int) strlen(ItemText(i));
924 listitems[i].text_y = dlg_ordinate(ItemTextY(i));
925 listitems[i].text_x = dlg_ordinate(ItemTextX(i));
926 listitems[i].text_flen = atoi(ItemTextFLen(i));
927 listitems[i].text_ilen = atoi(ItemTextILen(i));
928 listitems[i].help = ((dialog_vars.item_help)
929 ? ItemHelp(i)
930 : dlg_strempty());
931 }
932
933 result = dlg_form(title,
934 cprompt,
935 height,
936 width,
937 form_height,
938 item_no,
939 listitems,
940 &choice);
941
942 switch (result) {
943 case DLG_EXIT_OK: /* FALLTHRU */
944 case DLG_EXIT_EXTRA:
945 show_status = TRUE;
946 break;
947 case DLG_EXIT_HELP:
948 dlg_add_result("HELP ");
949 show_status = dialog_vars.help_status;
950 if (USE_ITEM_HELP(listitems[choice].help)) {
951 dlg_add_string(listitems[choice].help);
952 result = DLG_EXIT_ITEM_HELP;
953 } else {
954 dlg_add_string(listitems[choice].name);
955 }
956 if (show_status)
957 dlg_add_separator();
958 break;
959 }
960 if (show_status) {
961 for (i = 0; i < item_no; i++) {
962 if (listitems[i].text_flen > 0) {
963 dlg_add_string(listitems[i].text);
964 dlg_add_separator();
965 }
966 }
967 }
968
969 dlg_free_formitems(listitems);
970 dlg_restore_vars(&save_vars);
971
972 return result;
973}