2 * $Id: formbox.c,v 1.101 2020/03/27 20:42:19 tom Exp $
4 * formbox.c -- implements the form (i.e., some pairs label/editbox)
6 * Copyright 2003-2019,2020 Thomas E. Dickey
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.
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.
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.
23 * This is adapted from source contributed by
24 * Valery Reznic (valery_reznic@users.sourceforge.net)
30 #define LLEN(n) ((n) * FORMBOX_TAGS)
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())
43 is_readonly(DIALOG_FORMITEM * item)
45 return ((item->type & 2) != 0) || (item->text_flen <= 0);
49 is_hidden(DIALOG_FORMITEM * item)
51 return ((item->type & 1) != 0);
55 in_window(WINDOW *win, int scrollamt, int y)
57 return (y >= scrollamt && y - scrollamt < getmaxy(win));
61 ok_move(WINDOW *win, int scrollamt, int y, int x)
63 return in_window(win, scrollamt, y)
64 && (wmove(win, y - scrollamt, x) != ERR);
68 move_past(WINDOW *win, int y, int x)
70 if (wmove(win, y, x) == ERR)
71 wmove(win, y, getmaxx(win) - 1);
78 print_item(WINDOW *win, DIALOG_FORMITEM * item, int scrollamt, bool choice)
83 if (ok_move(win, scrollamt, item->name_y, item->name_x)) {
85 len = MIN(len, getmaxx(win) - item->name_x);
91 item->name_y - scrollamt,
96 move_past(win, item->name_y - scrollamt, item->name_x + len);
100 if (item->text_len && ok_move(win, scrollamt, item->text_y, item->text_x)) {
101 chtype this_item_attribute;
103 len = item->text_len;
104 len = MIN(len, getmaxx(win) - item->text_x);
106 if (!is_readonly(item)) {
107 this_item_attribute = choice
108 ? form_active_text_attr
111 this_item_attribute = form_item_readonly_attr;
119 item->text_y - scrollamt,
124 move_past(win, item->text_y - scrollamt, item->text_x + len);
132 * Print the entire form.
135 print_form(WINDOW *win, DIALOG_FORMITEM * item, int total, int scrollamt, int choice)
140 for (n = 0; n < total; ++n) {
141 count += print_item(win, item + n, scrollamt, n == choice);
144 wbkgdset(win, menubox_attr | ' ');
146 (void) wnoutrefresh(win);
151 set_choice(DIALOG_FORMITEM item[], int choice, int item_no, bool * noneditable)
155 *noneditable = FALSE;
156 if (!is_readonly(&item[choice])) {
161 for (i = 0; i < item_no; i++) {
162 if (!is_readonly(&(item[i]))) {
176 * Find the last y-value in the form.
179 form_limit(DIALOG_FORMITEM item[])
183 for (n = 0; item[n].name != 0; ++n) {
184 if (limit < item[n].name_y)
185 limit = item[n].name_y;
186 if (limit < item[n].text_y)
187 limit = item[n].text_y;
193 is_first_field(DIALOG_FORMITEM item[], int choice)
196 while (choice >= 0) {
197 if (item[choice].text_flen > 0) {
207 is_last_field(DIALOG_FORMITEM item[], int choice, int item_no)
210 while (choice < item_no) {
211 if (item[choice].text_flen > 0) {
221 * Tab to the next field.
224 tab_next(WINDOW *win,
225 DIALOG_FORMITEM item[],
231 int old_choice = *choice;
232 int old_scroll = *scrollamt;
233 bool wrapped = FALSE;
239 *choice = item_no - 1;
241 } else if (*choice >= item_no) {
245 } while ((*choice != old_choice) && is_readonly(&(item[*choice])));
247 if (item[*choice].text_flen > 0) {
248 int lo = MIN(item[*choice].name_y, item[*choice].text_y);
249 int hi = MAX(item[*choice].name_y, item[*choice].text_y);
251 if (old_choice == *choice)
253 print_item(win, item + old_choice, *scrollamt, FALSE);
255 if (*scrollamt < lo + 1 - getmaxy(win))
256 *scrollamt = lo + 1 - getmaxy(win);
260 * If we have to scroll to show a wrap-around, it does get
261 * confusing. Just give up rather than scroll. Tab'ing to the
262 * next field in a multi-column form is a different matter. Scroll
265 if (*scrollamt != old_scroll) {
268 *scrollamt = old_scroll;
269 *choice = old_choice;
272 wscrl(win, *scrollamt - old_scroll);
273 scrollok(win, FALSE);
278 } while (*choice != old_choice);
280 return (old_choice != *choice) || (old_scroll != *scrollamt);
284 * Scroll to the next page, putting the choice at the first editable field
285 * in that page. Note that fields are not necessarily in top-to-bottom order,
286 * nor is there necessarily a field on each row of the window.
289 scroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
292 int old_choice = *choice;
293 int old_scroll = *scrollamt;
294 int old_row = MIN(item[old_choice].text_y, item[old_choice].name_y);
295 int target = old_scroll + stepsize;
298 if (old_row != old_scroll)
301 target = old_scroll + stepsize;
306 if (target > form_limit(item)) {
314 for (n = 0; item[n].name != 0; ++n) {
315 if (item[n].text_flen > 0) {
316 int new_row = MIN(item[n].text_y, item[n].name_y);
317 if (abs(new_row - target) < abs(old_row - target)) {
324 if (old_choice != *choice)
325 print_item(win, item + old_choice, *scrollamt, FALSE);
327 *scrollamt = *choice;
328 if (*scrollamt != old_scroll) {
330 wscrl(win, *scrollamt - old_scroll);
331 scrollok(win, FALSE);
333 result = (old_choice != *choice) || (old_scroll != *scrollamt);
341 * Do a sanity check on the field length, and return the "right" value.
344 real_length(DIALOG_FORMITEM * item)
346 return (item->text_flen > 0
348 : (item->text_flen < 0
354 * Compute the form size, setup field buffers.
357 make_FORM_ELTs(DIALOG_FORMITEM * item,
366 for (i = 0; i < item_no; ++i) {
367 int real_len = real_length(item + i);
370 * Special value '0' for text_flen: no input allowed
371 * Special value '0' for text_ilen: 'be the same as text_flen'
373 if (item[i].text_ilen == 0)
374 item[i].text_ilen = real_len;
376 min_h = MAX(min_h, item[i].name_y + 1);
377 min_h = MAX(min_h, item[i].text_y + 1);
378 min_w = MAX(min_w, item[i].name_x + 1 + item[i].name_len);
379 min_w = MAX(min_w, item[i].text_x + 1 + real_len);
381 item[i].text_len = real_length(item + i);
384 * We do not know the actual length of .text, so we allocate it here
385 * to ensure it is big enough.
387 if (item[i].text_flen > 0) {
388 int max_len = dlg_max_input(MAX(item[i].text_ilen + 1, MAX_LEN));
389 char *old_text = item[i].text;
391 item[i].text = dlg_malloc(char, (size_t) max_len + 1);
392 assert_ptr(item[i].text, "make_FORM_ELTs");
394 sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
396 if (item[i].text_free) {
397 item[i].text_free = FALSE;
400 item[i].text_free = TRUE;
409 dlg_default_formitem(DIALOG_FORMITEM * items)
413 if (dialog_vars.default_item != 0) {
415 while (items->name != 0) {
416 if (!strcmp(dialog_vars.default_item, items->name)) {
430 next_valid_buttonindex(int state, int extra, bool non_editable)
432 state = dlg_next_ok_buttonindex(state, extra);
433 while (non_editable && state == sTEXT)
434 state = dlg_next_ok_buttonindex(state, sTEXT);
439 prev_valid_buttonindex(int state, int extra, bool non_editable)
441 state = dlg_prev_ok_buttonindex(state, extra);
442 while (non_editable && state == sTEXT)
443 state = dlg_prev_ok_buttonindex(state, sTEXT);
447 #define NAVIGATE_BINDINGS \
448 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
449 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
450 DLG_KEYS_DATA( DLGK_ITEM_NEXT, CHR_NEXT ), \
451 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN ), \
452 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_RIGHT ), \
453 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT ), \
454 DLG_KEYS_DATA( DLGK_ITEM_PREV, CHR_PREVIOUS ), \
455 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ), \
456 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_LEFT ), \
457 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), \
458 DLG_KEYS_DATA( DLGK_PAGE_NEXT, KEY_NPAGE ), \
459 DLG_KEYS_DATA( DLGK_PAGE_PREV, KEY_PPAGE )
461 * Display a form for entering a number of fields
464 dlg_form(const char *title,
470 DIALOG_FORMITEM * items,
474 static DLG_KEYS_BINDING binding[] = {
481 static DLG_KEYS_BINDING binding2[] = {
486 /* no TOGGLEKEY_BINDINGS, since that includes space... */
492 int old_height = height;
493 int old_width = width;
498 bool first_trace = TRUE;
500 int state = (dialog_vars.default_button >= 0
501 ? dlg_default_button()
503 int x, y, cur_x, cur_y, box_x, box_y;
506 int choice = dlg_default_formitem(items);
507 int new_choice, new_scroll;
509 int result = DLG_EXIT_UNKNOWN;
510 int min_width = 0, min_height = 0;
511 bool was_autosize = (height == 0 || width == 0);
512 bool show_buttons = FALSE;
513 bool scroll_changed = FALSE;
514 bool field_changed = FALSE;
515 bool non_editable = FALSE;
516 WINDOW *dialog, *form;
518 const char **buttons = dlg_ok_labels();
519 DIALOG_FORMITEM *current;
521 DLG_TRACE(("# %sform args:\n", (dialog_vars.formitem_type
524 DLG_TRACE2S("title", title);
525 DLG_TRACE2S("message", cprompt);
526 DLG_TRACE2N("height", height);
527 DLG_TRACE2N("width", width);
528 DLG_TRACE2N("lheight", form_height);
529 DLG_TRACE2N("llength", item_no);
530 /* FIXME dump the items[][] too */
531 DLG_TRACE2N("current", *current_item);
533 make_FORM_ELTs(items, item_no, &min_height, &min_width);
534 dlg_button_layout(buttons, &min_width);
541 prompt = dlg_strclone(cprompt);
542 dlg_tab_correct_str(prompt);
543 dlg_auto_size(title, prompt, &height, &width,
545 MAX(26, 2 + min_width));
547 if (form_height == 0)
548 form_height = min_height;
551 form_height = MIN(SLINES - height, form_height);
552 height += form_height;
556 dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
557 thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
558 form_height = MIN(thigh, form_height);
561 dlg_print_size(height, width);
562 dlg_ctl_size(height, width);
564 x = dlg_box_x_ordinate(width);
565 y = dlg_box_y_ordinate(height);
567 dialog = dlg_new_window(height, width, y, x);
568 dlg_register_window(dialog, "formbox", binding);
569 dlg_register_buttons(dialog, "formbox", buttons);
571 dlg_mouse_setbase(x, y);
573 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
574 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
575 dlg_draw_title(dialog, title);
577 dlg_attrset(dialog, dialog_attr);
578 dlg_print_autowrap(dialog, prompt, height, width);
580 form_width = width - 6;
581 getyx(dialog, cur_y, cur_x);
584 box_x = (width - form_width) / 2 - 1;
586 /* create new window for the form */
587 form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
589 dlg_register_window(form, "formfield", binding2);
591 /* draw a box around the form items */
592 dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
593 menubox_border_attr, menubox_border2_attr);
595 /* register the new window, along with its borders */
596 dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
597 getbegx(form) - getbegx(dialog),
600 KEY_MAX, 1, 1, 3 /* by cells */ );
603 scroll_changed = TRUE;
605 choice = set_choice(items, choice, item_no, &non_editable);
606 current = &items[choice];
608 state = next_valid_buttonindex(state, sTEXT, non_editable);
610 while (result == DLG_EXIT_UNKNOWN) {
614 if (scroll_changed) {
615 print_form(form, items, item_no, scrollamt, choice);
616 dlg_draw_scrollbar(dialog,
619 scrollamt + form_height + 1,
624 box_y + form_height + 1,
625 menubox_border2_attr,
626 menubox_border_attr);
627 scroll_changed = FALSE;
632 dlg_draw_buttons(dialog, height - 2, 0, buttons,
634 ? 1000 /* no such button, not highlighted */
637 show_buttons = FALSE;
642 dlg_trace_win(dialog);
645 if (field_changed || state == sTEXT) {
648 current = &items[choice];
649 dialog_vars.max_input = current->text_ilen;
650 dlg_item_help(current->help);
651 dlg_show_string(form, current->text, chr_offset,
652 form_active_text_attr,
653 current->text_y - scrollamt,
656 is_hidden(current), first);
659 field_changed = FALSE;
662 key = dlg_mouse_wgetch((state == sTEXT) ? form : dialog, &fkey);
663 if (dlg_result_key(key, fkey, &result)) {
667 /* handle non-functionkeys */
669 if (state != sTEXT) {
670 code = dlg_char_to_button(key, buttons);
672 dlg_del_window(dialog);
673 result = dlg_ok_buttoncode(code);
679 /* handle functionkeys */
681 bool do_scroll = FALSE;
686 case DLGK_MOUSE(KEY_PPAGE):
689 move_by = -form_height;
692 case DLGK_MOUSE(KEY_NPAGE):
695 move_by = form_height;
700 dlg_del_window(dialog);
701 result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
709 if (state == sTEXT) {
714 state = prev_valid_buttonindex(state, 0, non_editable);
720 if (state == sTEXT && !is_first_field(items, choice)) {
725 int old_state = state;
726 state = prev_valid_buttonindex(state, sTEXT, non_editable);
728 if (old_state >= 0 && state == sTEXT) {
729 new_choice = item_no - 1;
730 if (choice != new_choice) {
731 print_item(form, items + choice, scrollamt, FALSE);
738 case DLGK_FIELD_PREV:
739 state = prev_valid_buttonindex(state, sTEXT, non_editable);
743 case DLGK_FIELD_NEXT:
744 state = next_valid_buttonindex(state, sTEXT, non_editable);
748 case DLGK_GRID_RIGHT:
754 if (state == sTEXT) {
759 state = next_valid_buttonindex(state, 0, non_editable);
765 if (state == sTEXT && !is_last_field(items, choice, item_no)) {
770 state = next_valid_buttonindex(state, sTEXT, non_editable);
772 if (state == sTEXT && choice) {
773 print_item(form, items + choice, scrollamt, FALSE);
781 dlg_will_resize(dialog);
786 _dlg_resize_cleanup(dialog);
787 dlg_unregister_window(form);
793 if (is_DLGK_MOUSE(key)) {
794 if (key >= DLGK_MOUSE(KEY_MAX)) {
795 int cell = key - DLGK_MOUSE(KEY_MAX);
796 int row = (cell / getmaxx(form)) + scrollamt;
797 int col = (cell % getmaxx(form));
800 for (n = 0; n < item_no; ++n) {
801 if (items[n].name_y == row
802 && items[n].name_x <= col
803 && (items[n].name_x + items[n].name_len > col
804 || (items[n].name_y == items[n].text_y
805 && items[n].text_x > col))) {
806 if (!is_readonly(&(items[n]))) {
807 field_changed = TRUE;
811 if (items[n].text_y == row
812 && items[n].text_x <= col
813 && items[n].text_x + items[n].text_ilen > col) {
814 if (!is_readonly(&(items[n]))) {
815 field_changed = TRUE;
821 print_item(form, items + choice, scrollamt, FALSE);
826 } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
835 new_scroll = scrollamt;
838 if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
839 if (choice != new_choice) {
841 field_changed = TRUE;
843 if (scrollamt != new_scroll) {
844 scrollamt = new_scroll;
845 scroll_changed = TRUE;
851 if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
852 if (choice != new_choice) {
854 field_changed = TRUE;
856 if (scrollamt != new_scroll) {
857 scrollamt = new_scroll;
858 scroll_changed = TRUE;
865 if (state == sTEXT) { /* Input box selected */
866 if (!is_readonly(current))
867 edit = dlg_edit_string(current->text, &chr_offset, key,
870 dlg_show_string(form, current->text, chr_offset,
871 form_active_text_attr,
872 current->text_y - scrollamt,
875 is_hidden(current), first);
882 dlg_mouse_free_regions();
883 dlg_unregister_window(form);
884 dlg_del_window(dialog);
887 *current_item = choice;
892 * Free memory owned by a list of DIALOG_FORMITEM's.
895 dlg_free_formitems(DIALOG_FORMITEM * items)
898 for (n = 0; items[n].name != 0; ++n) {
899 if (items[n].name_free)
901 if (items[n].text_free)
903 if (items[n].help_free && items[n].help != dlg_strempty())
910 * The script accepts values beginning at 1, while curses starts at 0.
913 dlg_ordinate(const char *s)
915 int result = atoi(s);
924 dialog_form(const char *title,
935 DIALOG_FORMITEM *listitems;
936 DIALOG_VARS save_vars;
937 bool show_status = FALSE;
940 dlg_save_vars(&save_vars);
941 dialog_vars.separate_output = TRUE;
943 listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
944 assert_ptr(listitems, "dialog_form");
946 for (i = 0; i < item_no; ++i) {
947 listitems[i].type = dialog_vars.formitem_type;
948 listitems[i].name = ItemName(i);
949 listitems[i].name_len = (int) strlen(ItemName(i));
950 listitems[i].name_y = dlg_ordinate(ItemNameY(i));
951 listitems[i].name_x = dlg_ordinate(ItemNameX(i));
952 listitems[i].text = ItemText(i);
953 listitems[i].text_len = (int) strlen(ItemText(i));
954 listitems[i].text_y = dlg_ordinate(ItemTextY(i));
955 listitems[i].text_x = dlg_ordinate(ItemTextX(i));
956 listitems[i].text_flen = atoi(ItemTextFLen(i));
957 listitems[i].text_ilen = atoi(ItemTextILen(i));
958 listitems[i].help = ((dialog_vars.item_help)
963 result = dlg_form(title,
973 case DLG_EXIT_OK: /* FALLTHRU */
978 dlg_add_help_formitem(&result, &help_result, &listitems[choice]);
979 show_status = dialog_vars.help_status;
980 dlg_add_string(help_result);
986 for (i = 0; i < item_no; i++) {
987 if (listitems[i].text_flen > 0) {
988 dlg_add_string(listitems[i].text);
992 dlg_add_last_key(-1);
995 dlg_free_formitems(listitems);
996 dlg_restore_vars(&save_vars);