2 * $Id: formbox.c,v 1.73 2011/06/29 09:48:08 tom Exp $
4 * formbox.c -- implements the form (i.e, some pairs label/editbox)
6 * Copyright 2003-2010,2011 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)
156 *noneditable = FALSE;
157 if (!is_readonly(&item[choice])) {
160 for (i = 0; i < item_no; i++) {
161 if (!is_readonly(&(item[i]))) {
175 * Find the last y-value in the form.
178 form_limit(DIALOG_FORMITEM item[])
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;
192 * Tab to the next field.
195 tab_next(WINDOW *win,
196 DIALOG_FORMITEM item[],
202 int old_choice = *choice;
203 int old_scroll = *scrollamt;
204 bool wrapped = FALSE;
210 *choice = item_no - 1;
212 } else if (*choice >= item_no) {
216 } while ((*choice != old_choice) && is_readonly(&(item[*choice])));
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);
222 if (old_choice == *choice)
224 print_item(win, item + old_choice, *scrollamt, FALSE);
226 if (*scrollamt < lo + 1 - getmaxy(win))
227 *scrollamt = lo + 1 - getmaxy(win);
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
236 if (*scrollamt != old_scroll) {
239 *scrollamt = old_scroll;
240 *choice = old_choice;
243 wscrl(win, *scrollamt - old_scroll);
244 scrollok(win, FALSE);
249 } while (*choice != old_choice);
251 return (old_choice != *choice) || (old_scroll != *scrollamt);
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.
260 scroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
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;
269 if (old_row != old_scroll)
272 target = old_scroll + stepsize;
276 int limit = form_limit(item);
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)) {
291 if (old_choice != *choice)
292 print_item(win, item + old_choice, *scrollamt, FALSE);
294 *scrollamt = *choice;
295 if (*scrollamt != old_scroll) {
297 wscrl(win, *scrollamt - old_scroll);
298 scrollok(win, FALSE);
300 return (old_choice != *choice) || (old_scroll != *scrollamt);
304 * Do a sanity check on the field length, and return the "right" value.
307 real_length(DIALOG_FORMITEM * item)
309 return (item->text_flen > 0
311 : (item->text_flen < 0
317 * Compute the form size, setup field buffers.
320 make_FORM_ELTs(DIALOG_FORMITEM * item,
329 for (i = 0; i < item_no; ++i) {
330 int real_len = real_length(item + i);
333 * Special value '0' for text_flen: no input allowed
334 * Special value '0' for text_ilen: 'be the same as text_flen'
336 if (item[i].text_ilen == 0)
337 item[i].text_ilen = real_len;
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);
344 item[i].text_len = real_length(item + i);
347 * We do not know the actual length of .text, so we allocate it here
348 * to ensure it is big enough.
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;
354 item[i].text = dlg_malloc(char, (size_t) max_len + 1);
355 assert_ptr(item[i].text, "make_FORM_ELTs");
357 sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
359 if (item[i].text_free) {
360 item[i].text_free = FALSE;
363 item[i].text_free = TRUE;
372 dlg_default_formitem(DIALOG_FORMITEM * items)
376 if (dialog_vars.default_item != 0) {
378 while (items->name != 0) {
379 if (!strcmp(dialog_vars.default_item, items->name)) {
393 next_valid_buttonindex(int state, int extra, bool non_editable)
395 state = dlg_next_ok_buttonindex(state, extra);
396 while (non_editable && state == sTEXT)
397 state = dlg_next_ok_buttonindex(state, sTEXT);
402 prev_valid_buttonindex(int state, int extra, bool non_editable)
404 state = dlg_prev_ok_buttonindex(state, extra);
405 while (non_editable && state == sTEXT)
406 state = dlg_prev_ok_buttonindex(state, sTEXT);
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 )
422 * Display a form for fulfill a number of fields
425 dlg_form(const char *title,
431 DIALOG_FORMITEM * items,
435 static DLG_KEYS_BINDING binding[] = {
441 static DLG_KEYS_BINDING binding2[] = {
451 int old_height = height;
452 int old_width = width;
458 int state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
459 int x, y, cur_x, cur_y, box_x, box_y;
463 int choice = dlg_default_formitem(items);
464 int new_choice, new_scroll;
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;
478 make_FORM_ELTs(items, item_no, &min_height, &min_width);
479 dlg_button_layout(buttons, &min_width);
481 dlg_tab_correct_str(prompt);
487 dlg_auto_size(title, prompt, &height, &width,
489 MAX(26, 2 + min_width));
491 if (form_height == 0)
492 form_height = min_height;
495 form_height = MIN(SLINES - height, form_height);
496 height += form_height;
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);
505 dlg_print_size(height, width);
506 dlg_ctl_size(height, width);
508 x = dlg_box_x_ordinate(width);
509 y = dlg_box_y_ordinate(height);
511 dialog = dlg_new_window(height, width, y, x);
512 dlg_register_window(dialog, "formbox", binding);
513 dlg_register_window(dialog, "formfield", binding2);
514 dlg_register_buttons(dialog, "formbox", buttons);
516 dlg_mouse_setbase(x, y);
518 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
519 dlg_draw_bottom_box(dialog);
520 dlg_draw_title(dialog, title);
522 wattrset(dialog, dialog_attr);
523 dlg_print_autowrap(dialog, prompt, height, width);
525 form_width = width - 6;
526 getyx(dialog, cur_y, cur_x);
528 box_x = (width - form_width) / 2 - 1;
530 /* create new window for the form */
531 form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
534 /* draw a box around the form items */
535 dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
536 menubox_border_attr, menubox_attr);
538 /* register the new window, along with its borders */
539 dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
540 getbegx(form) - getbegx(dialog),
543 KEY_MAX, 1, 1, 3 /* by cells */ );
546 scroll_changed = TRUE;
548 choice = set_choice(items, choice, item_no, &non_editable);
549 current = &items[choice];
551 state = next_valid_buttonindex(state, sTEXT, non_editable);
553 while (result == DLG_EXIT_UNKNOWN) {
556 if (scroll_changed) {
557 print_form(form, items, item_no, scrollamt, choice);
558 dlg_draw_scrollbar(dialog,
561 scrollamt + form_height + 1,
566 box_y + form_height + 1,
568 menubox_border_attr);
569 scroll_changed = FALSE;
574 dlg_draw_buttons(dialog, height - 2, 0, buttons,
576 ? 1000 /* no such button, not highlighted */
579 show_buttons = FALSE;
582 if (field_changed || state == sTEXT) {
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,
593 is_hidden(current), first);
594 field_changed = FALSE;
597 key = dlg_mouse_wgetch(dialog, &fkey);
598 if (dlg_result_key(key, fkey, &result))
601 /* handle non-functionkeys */
603 if (state != sTEXT) {
604 code = dlg_char_to_button(key, buttons);
606 dlg_del_window(dialog);
607 result = dlg_ok_buttoncode(code);
617 /* handle functionkeys */
619 bool do_scroll = FALSE;
624 case DLGK_MOUSE(KEY_PPAGE):
627 move_by = -form_height;
630 case DLGK_MOUSE(KEY_NPAGE):
633 move_by = form_height;
637 dlg_del_window(dialog);
638 result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
646 if (state == sTEXT) {
651 state = prev_valid_buttonindex(state, 0, non_editable);
656 case DLGK_FIELD_PREV:
657 state = prev_valid_buttonindex(state, sTEXT, non_editable);
661 case DLGK_FIELD_NEXT:
662 state = next_valid_buttonindex(state, sTEXT, non_editable);
666 case DLGK_GRID_RIGHT:
672 if (state == sTEXT) {
677 state = next_valid_buttonindex(state, 0, non_editable);
689 dlg_del_window(dialog);
691 dlg_mouse_free_regions();
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));
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;
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;
724 print_item(form, items + choice, scrollamt, FALSE);
729 } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
738 new_scroll = scrollamt;
741 if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
742 if (choice != new_choice) {
744 field_changed = TRUE;
746 if (scrollamt != new_scroll) {
747 scrollamt = new_scroll;
748 scroll_changed = TRUE;
754 if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
755 if (choice != new_choice) {
757 field_changed = TRUE;
759 if (scrollamt != new_scroll) {
760 scrollamt = new_scroll;
761 scroll_changed = TRUE;
768 if (state == sTEXT) { /* Input box selected */
769 if (!is_readonly(current))
770 edit = dlg_edit_string(current->text, &chr_offset, key,
773 dlg_show_string(form, current->text, chr_offset,
774 form_active_text_attr,
775 current->text_y - scrollamt,
778 is_hidden(current), first);
785 dlg_mouse_free_regions();
786 dlg_del_window(dialog);
789 *current_item = choice;
794 * Free memory owned by a list of DIALOG_FORMITEM's.
797 dlg_free_formitems(DIALOG_FORMITEM * items)
800 for (n = 0; items[n].name != 0; ++n) {
801 if (items[n].name_free)
803 if (items[n].text_free)
805 if (items[n].help_free && items[n].help != dlg_strempty())
812 * The script accepts values beginning at 1, while curses starts at 0.
815 dlg_ordinate(const char *s)
817 int result = atoi(s);
826 dialog_form(const char *title,
837 DIALOG_FORMITEM *listitems;
838 DIALOG_VARS save_vars;
839 bool show_status = FALSE;
841 dlg_save_vars(&save_vars);
842 dialog_vars.separate_output = TRUE;
844 listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
845 assert_ptr(listitems, "dialog_form");
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)
864 result = dlg_form(title,
874 case DLG_EXIT_OK: /* FALLTHRU */
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;
885 dlg_add_string(listitems[choice].name);
892 for (i = 0; i < item_no; i++) {
893 if (listitems[i].text_flen > 0) {
894 dlg_add_string(listitems[i].text);
900 dlg_free_formitems(listitems);
901 dlg_restore_vars(&save_vars);