1 /*
2 * $Id: formbox.c,v 1.95 2018/06/21 08:23:31 tom Exp $
3 *
4 * formbox.c -- implements the form (i.e., some pairs label/editbox)
5 *
6 * Copyright 2003-2016,2018 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 ([email protected])
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
42 static bool
is_readonly(DIALOG_FORMITEM * item)43 is_readonly(DIALOG_FORMITEM * item)
44 {
45 return ((item->type & 2) != 0) || (item->text_flen <= 0);
46 }
47
48 static bool
is_hidden(DIALOG_FORMITEM * item)49 is_hidden(DIALOG_FORMITEM * item)
50 {
51 return ((item->type & 1) != 0);
52 }
53
54 static bool
in_window(WINDOW * win,int scrollamt,int y)55 in_window(WINDOW *win, int scrollamt, int y)
56 {
57 return (y >= scrollamt && y - scrollamt < getmaxy(win));
58 }
59
60 static bool
ok_move(WINDOW * win,int scrollamt,int y,int x)61 ok_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
67 static void
move_past(WINDOW * win,int y,int x)68 move_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 */
77 static int
print_item(WINDOW * win,DIALOG_FORMITEM * item,int scrollamt,bool choice)78 print_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 */
134 static void
print_form(WINDOW * win,DIALOG_FORMITEM * item,int total,int scrollamt,int choice)135 print_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
150 static int
set_choice(DIALOG_FORMITEM item[],int choice,int item_no,bool * noneditable)151 set_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 */
177 static int
form_limit(DIALOG_FORMITEM item[])178 form_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
191 static int
is_first_field(DIALOG_FORMITEM item[],int choice)192 is_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
205 static int
is_last_field(DIALOG_FORMITEM item[],int choice,int item_no)206 is_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
219 /*
220 * Tab to the next field.
221 */
222 static bool
tab_next(WINDOW * win,DIALOG_FORMITEM item[],int item_no,int stepsize,int * choice,int * scrollamt)223 tab_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 */
287 static bool
scroll_next(WINDOW * win,DIALOG_FORMITEM item[],int stepsize,int * choice,int * scrollamt)288 scroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
289 {
290 bool result = TRUE;
291 int old_choice = *choice;
292 int old_scroll = *scrollamt;
293 int old_row = MIN(item[old_choice].text_y, item[old_choice].name_y);
294 int target = old_scroll + stepsize;
295 int n;
296
297 if (stepsize < 0) {
298 if (old_row != old_scroll)
299 target = old_scroll;
300 else
301 target = old_scroll + stepsize;
302 if (target < 0) {
303 result = FALSE;
304 }
305 } else {
306 if (target > form_limit(item)) {
307 result = FALSE;
308 }
309 }
310
311 if (result) {
312 for (n = 0; item[n].name != 0; ++n) {
313 if (item[n].text_flen > 0) {
314 int new_row = MIN(item[n].text_y, item[n].name_y);
315 if (abs(new_row - target) < abs(old_row - target)) {
316 old_row = new_row;
317 *choice = n;
318 }
319 }
320 }
321
322 if (old_choice != *choice)
323 print_item(win, item + old_choice, *scrollamt, FALSE);
324
325 *scrollamt = *choice;
326 if (*scrollamt != old_scroll) {
327 scrollok(win, TRUE);
328 wscrl(win, *scrollamt - old_scroll);
329 scrollok(win, FALSE);
330 }
331 result = (old_choice != *choice) || (old_scroll != *scrollamt);
332 }
333 if (!result)
334 beep();
335 return result;
336 }
337
338 /*
339 * Do a sanity check on the field length, and return the "right" value.
340 */
341 static int
real_length(DIALOG_FORMITEM * item)342 real_length(DIALOG_FORMITEM * item)
343 {
344 return (item->text_flen > 0
345 ? item->text_flen
346 : (item->text_flen < 0
347 ? -item->text_flen
348 : item->text_len));
349 }
350
351 /*
352 * Compute the form size, setup field buffers.
353 */
354 static void
make_FORM_ELTs(DIALOG_FORMITEM * item,int item_no,int * min_height,int * min_width)355 make_FORM_ELTs(DIALOG_FORMITEM * item,
356 int item_no,
357 int *min_height,
358 int *min_width)
359 {
360 int i;
361 int min_w = 0;
362 int min_h = 0;
363
364 for (i = 0; i < item_no; ++i) {
365 int real_len = real_length(item + i);
366
367 /*
368 * Special value '0' for text_flen: no input allowed
369 * Special value '0' for text_ilen: 'be the same as text_flen'
370 */
371 if (item[i].text_ilen == 0)
372 item[i].text_ilen = real_len;
373
374 min_h = MAX(min_h, item[i].name_y + 1);
375 min_h = MAX(min_h, item[i].text_y + 1);
376 min_w = MAX(min_w, item[i].name_x + 1 + item[i].name_len);
377 min_w = MAX(min_w, item[i].text_x + 1 + real_len);
378
379 item[i].text_len = real_length(item + i);
380
381 /*
382 * We do not know the actual length of .text, so we allocate it here
383 * to ensure it is big enough.
384 */
385 if (item[i].text_flen > 0) {
386 int max_len = dlg_max_input(MAX(item[i].text_ilen + 1, MAX_LEN));
387 char *old_text = item[i].text;
388
389 item[i].text = dlg_malloc(char, (size_t) max_len + 1);
390 assert_ptr(item[i].text, "make_FORM_ELTs");
391
392 sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
393
394 if (item[i].text_free) {
395 item[i].text_free = FALSE;
396 free(old_text);
397 }
398 item[i].text_free = TRUE;
399 }
400 }
401
402 *min_height = min_h;
403 *min_width = min_w;
404 }
405
406 int
dlg_default_formitem(DIALOG_FORMITEM * items)407 dlg_default_formitem(DIALOG_FORMITEM * items)
408 {
409 int result = 0;
410
411 if (dialog_vars.default_item != 0) {
412 int count = 0;
413 while (items->name != 0) {
414 if (!strcmp(dialog_vars.default_item, items->name)) {
415 result = count;
416 break;
417 }
418 ++items;
419 count++;
420 }
421 }
422 return result;
423 }
424
425 #define sTEXT -1
426
427 static int
next_valid_buttonindex(int state,int extra,bool non_editable)428 next_valid_buttonindex(int state, int extra, bool non_editable)
429 {
430 state = dlg_next_ok_buttonindex(state, extra);
431 while (non_editable && state == sTEXT)
432 state = dlg_next_ok_buttonindex(state, sTEXT);
433 return state;
434 }
435
436 static int
prev_valid_buttonindex(int state,int extra,bool non_editable)437 prev_valid_buttonindex(int state, int extra, bool non_editable)
438 {
439 state = dlg_prev_ok_buttonindex(state, extra);
440 while (non_editable && state == sTEXT)
441 state = dlg_prev_ok_buttonindex(state, sTEXT);
442 return state;
443 }
444
445 #define NAVIGATE_BINDINGS \
446 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
447 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
448 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
449 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
450 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_RIGHT ), \
451 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
452 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
453 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ), \
454 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_LEFT ), \
455 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
456 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
457 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
458 /*
459 * Display a form for entering a number of fields
460 */
461 int
dlg_form(const char * title,const char * cprompt,int height,int width,int form_height,int item_no,DIALOG_FORMITEM * items,int * current_item)462 dlg_form(const char *title,
463 const char *cprompt,
464 int height,
465 int width,
466 int form_height,
467 int item_no,
468 DIALOG_FORMITEM * items,
469 int *current_item)
470 {
471 /* *INDENT-OFF* */
472 static DLG_KEYS_BINDING binding[] = {
473 HELPKEY_BINDINGS,
474 ENTERKEY_BINDINGS,
475 NAVIGATE_BINDINGS,
476 TOGGLEKEY_BINDINGS,
477 END_KEYS_BINDING
478 };
479 static DLG_KEYS_BINDING binding2[] = {
480 INPUTSTR_BINDINGS,
481 HELPKEY_BINDINGS,
482 ENTERKEY_BINDINGS,
483 NAVIGATE_BINDINGS,
484 /* no TOGGLEKEY_BINDINGS, since that includes space... */
485 END_KEYS_BINDING
486 };
487 /* *INDENT-ON* */
488
489 #ifdef KEY_RESIZE
490 int old_height = height;
491 int old_width = width;
492 #endif
493
494 int form_width;
495 bool first = TRUE;
496 bool first_trace = TRUE;
497 int chr_offset = 0;
498 int state = (dialog_vars.default_button >= 0
499 ? dlg_default_button()
500 : sTEXT);
501 int x, y, cur_x, cur_y, box_x, box_y;
502 int code;
503 int key = 0;
504 int fkey;
505 int choice = dlg_default_formitem(items);
506 int new_choice, new_scroll;
507 int scrollamt = 0;
508 int result = DLG_EXIT_UNKNOWN;
509 int min_width = 0, min_height = 0;
510 bool was_autosize = (height == 0 || width == 0);
511 bool show_buttons = FALSE;
512 bool scroll_changed = FALSE;
513 bool field_changed = FALSE;
514 bool non_editable = FALSE;
515 WINDOW *dialog, *form;
516 char *prompt;
517 const char **buttons = dlg_ok_labels();
518 DIALOG_FORMITEM *current;
519
520 DLG_TRACE(("# %sform args:\n", (dialog_vars.formitem_type
521 ? "password"
522 : "")));
523 DLG_TRACE2S("title", title);
524 DLG_TRACE2S("message", cprompt);
525 DLG_TRACE2N("height", height);
526 DLG_TRACE2N("width", width);
527 DLG_TRACE2N("lheight", form_height);
528 DLG_TRACE2N("llength", item_no);
529 /* FIXME dump the items[][] too */
530 DLG_TRACE2N("current", *current_item);
531
532 make_FORM_ELTs(items, item_no, &min_height, &min_width);
533 dlg_button_layout(buttons, &min_width);
534 dlg_does_output();
535
536 #ifdef KEY_RESIZE
537 retry:
538 #endif
539
540 prompt = dlg_strclone(cprompt);
541 dlg_tab_correct_str(prompt);
542 dlg_auto_size(title, prompt, &height, &width,
543 1 + 3 * MARGIN,
544 MAX(26, 2 + min_width));
545
546 if (form_height == 0)
547 form_height = min_height;
548
549 if (was_autosize) {
550 form_height = MIN(SLINES - height, form_height);
551 height += form_height;
552 } else {
553 int thigh = 0;
554 int twide = 0;
555 dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
556 thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
557 form_height = MIN(thigh, form_height);
558 }
559
560 dlg_print_size(height, width);
561 dlg_ctl_size(height, width);
562
563 x = dlg_box_x_ordinate(width);
564 y = dlg_box_y_ordinate(height);
565
566 dialog = dlg_new_window(height, width, y, x);
567 dlg_register_window(dialog, "formbox", binding);
568 dlg_register_buttons(dialog, "formbox", buttons);
569
570 dlg_mouse_setbase(x, y);
571
572 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
573 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
574 dlg_draw_title(dialog, title);
575
576 dlg_attrset(dialog, dialog_attr);
577 dlg_print_autowrap(dialog, prompt, height, width);
578
579 form_width = width - 6;
580 getyx(dialog, cur_y, cur_x);
581 (void) cur_x;
582 box_y = cur_y + 1;
583 box_x = (width - form_width) / 2 - 1;
584
585 /* create new window for the form */
586 form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
587 x + box_x + 1);
588 dlg_register_window(form, "formfield", binding2);
589
590 /* draw a box around the form items */
591 dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
592 menubox_border_attr, menubox_border2_attr);
593
594 /* register the new window, along with its borders */
595 dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
596 getbegx(form) - getbegx(dialog),
597 getmaxy(form),
598 getmaxx(form),
599 KEY_MAX, 1, 1, 3 /* by cells */ );
600
601 show_buttons = TRUE;
602 scroll_changed = TRUE;
603
604 choice = set_choice(items, choice, item_no, &non_editable);
605 current = &items[choice];
606 if (non_editable)
607 state = next_valid_buttonindex(state, sTEXT, non_editable);
608
609 while (result == DLG_EXIT_UNKNOWN) {
610 int edit = FALSE;
611
612 if (scroll_changed) {
613 print_form(form, items, item_no, scrollamt, choice);
614 dlg_draw_scrollbar(dialog,
615 scrollamt,
616 scrollamt,
617 scrollamt + form_height + 1,
618 min_height,
619 box_x + 1,
620 box_x + form_width,
621 box_y,
622 box_y + form_height + 1,
623 menubox_border2_attr,
624 menubox_border_attr);
625 scroll_changed = FALSE;
626 }
627
628 if (show_buttons) {
629 dlg_item_help("");
630 dlg_draw_buttons(dialog, height - 2, 0, buttons,
631 ((state < 0)
632 ? 1000 /* no such button, not highlighted */
633 : state),
634 FALSE, width);
635 show_buttons = FALSE;
636 }
637
638 if (first_trace) {
639 first_trace = FALSE;
640 dlg_trace_win(dialog);
641 }
642
643 if (field_changed || state == sTEXT) {
644 if (field_changed)
645 chr_offset = 0;
646 current = &items[choice];
647 dialog_vars.max_input = current->text_ilen;
648 dlg_item_help(current->help);
649 dlg_show_string(form, current->text, chr_offset,
650 form_active_text_attr,
651 current->text_y - scrollamt,
652 current->text_x,
653 current->text_len,
654 is_hidden(current), first);
655 wsyncup(form);
656 wcursyncup(form);
657 field_changed = FALSE;
658 }
659
660 key = dlg_mouse_wgetch((state == sTEXT) ? form : dialog, &fkey);
661 if (dlg_result_key(key, fkey, &result))
662 break;
663
664 /* handle non-functionkeys */
665 if (!fkey) {
666 if (state != sTEXT) {
667 code = dlg_char_to_button(key, buttons);
668 if (code >= 0) {
669 dlg_del_window(dialog);
670 result = dlg_ok_buttoncode(code);
671 continue;
672 }
673 }
674 }
675
676 /* handle functionkeys */
677 if (fkey) {
678 bool do_scroll = FALSE;
679 bool do_tab = FALSE;
680 int move_by = 0;
681
682 switch (key) {
683 case DLGK_MOUSE(KEY_PPAGE):
684 case DLGK_PAGE_PREV:
685 do_scroll = TRUE;
686 move_by = -form_height;
687 break;
688
689 case DLGK_MOUSE(KEY_NPAGE):
690 case DLGK_PAGE_NEXT:
691 do_scroll = TRUE;
692 move_by = form_height;
693 break;
694
695 case DLGK_TOGGLE:
696 case DLGK_ENTER:
697 dlg_del_window(dialog);
698 result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
699 continue;
700
701 case DLGK_GRID_LEFT:
702 if (state == sTEXT)
703 break;
704 /* FALLTHRU */
705 case DLGK_ITEM_PREV:
706 if (state == sTEXT) {
707 do_tab = TRUE;
708 move_by = -1;
709 break;
710 } else {
711 state = prev_valid_buttonindex(state, 0, non_editable);
712 show_buttons = TRUE;
713 continue;
714 }
715
716 case DLGK_FORM_PREV:
717 if (state == sTEXT && !is_first_field(items, choice)) {
718 do_tab = TRUE;
719 move_by = -1;
720 break;
721 } else {
722 int old_state = state;
723 state = prev_valid_buttonindex(state, sTEXT, non_editable);
724 show_buttons = TRUE;
725 if (old_state >= 0 && state == sTEXT) {
726 new_choice = item_no - 1;
727 if (choice != new_choice) {
728 print_item(form, items + choice, scrollamt, FALSE);
729 choice = new_choice;
730 }
731 }
732 continue;
733 }
734
735 case DLGK_FIELD_PREV:
736 state = prev_valid_buttonindex(state, sTEXT, non_editable);
737 show_buttons = TRUE;
738 continue;
739
740 case DLGK_FIELD_NEXT:
741 state = next_valid_buttonindex(state, sTEXT, non_editable);
742 show_buttons = TRUE;
743 continue;
744
745 case DLGK_GRID_RIGHT:
746 if (state == sTEXT)
747 break;
748 /* FALLTHRU */
749
750 case DLGK_ITEM_NEXT:
751 if (state == sTEXT) {
752 do_tab = TRUE;
753 move_by = 1;
754 break;
755 } else {
756 state = next_valid_buttonindex(state, 0, non_editable);
757 show_buttons = TRUE;
758 continue;
759 }
760
761 case DLGK_FORM_NEXT:
762 if (state == sTEXT && !is_last_field(items, choice, item_no)) {
763 do_tab = TRUE;
764 move_by = 1;
765 break;
766 } else {
767 state = next_valid_buttonindex(state, sTEXT, non_editable);
768 show_buttons = TRUE;
769 if (state == sTEXT && choice) {
770 print_item(form, items + choice, scrollamt, FALSE);
771 choice = 0;
772 }
773 continue;
774 }
775
776 #ifdef KEY_RESIZE
777 case KEY_RESIZE:
778 dlg_will_resize(dialog);
779 /* reset data */
780 height = old_height;
781 width = old_width;
782 free(prompt);
783 dlg_clear();
784 dlg_unregister_window(form);
785 dlg_del_window(dialog);
786 dlg_mouse_free_regions();
787 /* repaint */
788 goto retry;
789 #endif
790 default:
791 #if USE_MOUSE
792 if (is_DLGK_MOUSE(key)) {
793 if (key >= DLGK_MOUSE(KEY_MAX)) {
794 int cell = key - DLGK_MOUSE(KEY_MAX);
795 int row = (cell / getmaxx(form)) + scrollamt;
796 int col = (cell % getmaxx(form));
797 int n;
798
799 for (n = 0; n < item_no; ++n) {
800 if (items[n].name_y == row
801 && items[n].name_x <= col
802 && (items[n].name_x + items[n].name_len > col
803 || (items[n].name_y == items[n].text_y
804 && items[n].text_x > col))) {
805 if (!is_readonly(&(items[n]))) {
806 field_changed = TRUE;
807 break;
808 }
809 }
810 if (items[n].text_y == row
811 && items[n].text_x <= col
812 && items[n].text_x + items[n].text_ilen > col) {
813 if (!is_readonly(&(items[n]))) {
814 field_changed = TRUE;
815 break;
816 }
817 }
818 }
819 if (field_changed) {
820 print_item(form, items + choice, scrollamt, FALSE);
821 choice = n;
822 continue;
823 }
824 beep();
825 } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
826 result = code;
827 }
828 continue;
829 }
830 #endif
831 break;
832 }
833
834 new_scroll = scrollamt;
835 new_choice = choice;
836 if (do_scroll) {
837 if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
838 if (choice != new_choice) {
839 choice = new_choice;
840 field_changed = TRUE;
841 }
842 if (scrollamt != new_scroll) {
843 scrollamt = new_scroll;
844 scroll_changed = TRUE;
845 }
846 }
847 continue;
848 }
849 if (do_tab) {
850 if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
851 if (choice != new_choice) {
852 choice = new_choice;
853 field_changed = TRUE;
854 }
855 if (scrollamt != new_scroll) {
856 scrollamt = new_scroll;
857 scroll_changed = TRUE;
858 }
859 }
860 continue;
861 }
862 }
863
864 if (state == sTEXT) { /* Input box selected */
865 if (!is_readonly(current))
866 edit = dlg_edit_string(current->text, &chr_offset, key,
867 fkey, first);
868 if (edit) {
869 dlg_show_string(form, current->text, chr_offset,
870 form_active_text_attr,
871 current->text_y - scrollamt,
872 current->text_x,
873 current->text_len,
874 is_hidden(current), first);
875 continue;
876 }
877 }
878
879 }
880
881 dlg_mouse_free_regions();
882 dlg_unregister_window(form);
883 dlg_del_window(dialog);
884 free(prompt);
885
886 *current_item = choice;
887 return result;
888 }
889
890 /*
891 * Free memory owned by a list of DIALOG_FORMITEM's.
892 */
893 void
dlg_free_formitems(DIALOG_FORMITEM * items)894 dlg_free_formitems(DIALOG_FORMITEM * items)
895 {
896 int n;
897 for (n = 0; items[n].name != 0; ++n) {
898 if (items[n].name_free)
899 free(items[n].name);
900 if (items[n].text_free)
901 free(items[n].text);
902 if (items[n].help_free && items[n].help != dlg_strempty())
903 free(items[n].help);
904 }
905 free(items);
906 }
907
908 /*
909 * The script accepts values beginning at 1, while curses starts at 0.
910 */
911 int
dlg_ordinate(const char * s)912 dlg_ordinate(const char *s)
913 {
914 int result = atoi(s);
915 if (result > 0)
916 --result;
917 else
918 result = 0;
919 return result;
920 }
921
922 int
dialog_form(const char * title,const char * cprompt,int height,int width,int form_height,int item_no,char ** items)923 dialog_form(const char *title,
924 const char *cprompt,
925 int height,
926 int width,
927 int form_height,
928 int item_no,
929 char **items)
930 {
931 int result;
932 int choice = 0;
933 int i;
934 DIALOG_FORMITEM *listitems;
935 DIALOG_VARS save_vars;
936 bool show_status = FALSE;
937 char *help_result;
938
939 dlg_save_vars(&save_vars);
940 dialog_vars.separate_output = TRUE;
941
942 listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
943 assert_ptr(listitems, "dialog_form");
944
945 for (i = 0; i < item_no; ++i) {
946 listitems[i].type = dialog_vars.formitem_type;
947 listitems[i].name = ItemName(i);
948 listitems[i].name_len = (int) strlen(ItemName(i));
949 listitems[i].name_y = dlg_ordinate(ItemNameY(i));
950 listitems[i].name_x = dlg_ordinate(ItemNameX(i));
951 listitems[i].text = ItemText(i);
952 listitems[i].text_len = (int) strlen(ItemText(i));
953 listitems[i].text_y = dlg_ordinate(ItemTextY(i));
954 listitems[i].text_x = dlg_ordinate(ItemTextX(i));
955 listitems[i].text_flen = atoi(ItemTextFLen(i));
956 listitems[i].text_ilen = atoi(ItemTextILen(i));
957 listitems[i].help = ((dialog_vars.item_help)
958 ? ItemHelp(i)
959 : dlg_strempty());
960 }
961
962 result = dlg_form(title,
963 cprompt,
964 height,
965 width,
966 form_height,
967 item_no,
968 listitems,
969 &choice);
970
971 switch (result) {
972 case DLG_EXIT_OK: /* FALLTHRU */
973 case DLG_EXIT_EXTRA:
974 show_status = TRUE;
975 break;
976 case DLG_EXIT_HELP:
977 dlg_add_help_formitem(&result, &help_result, &listitems[choice]);
978 show_status = dialog_vars.help_status;
979 dlg_add_string(help_result);
980 if (show_status)
981 dlg_add_separator();
982 break;
983 }
984 if (show_status) {
985 for (i = 0; i < item_no; i++) {
986 if (listitems[i].text_flen > 0) {
987 dlg_add_string(listitems[i].text);
988 dlg_add_separator();
989 }
990 }
991 dlg_add_last_key(-1);
992 }
993
994 dlg_free_formitems(listitems);
995 dlg_restore_vars(&save_vars);
996
997 return result;
998 }
999