xref: /dragonfly/contrib/dialog/formbox.c (revision e98bdfd3)
1 /*
2  *  $Id: formbox.c,v 1.87 2013/09/02 17:02:05 tom Exp $
3  *
4  *  formbox.c -- implements the form (i.e, some pairs label/editbox)
5  *
6  *  Copyright 2003-2012,2013	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 
42 static bool
43 is_readonly(DIALOG_FORMITEM * item)
44 {
45     return ((item->type & 2) != 0) || (item->text_flen <= 0);
46 }
47 
48 static bool
49 is_hidden(DIALOG_FORMITEM * item)
50 {
51     return ((item->type & 1) != 0);
52 }
53 
54 static bool
55 in_window(WINDOW *win, int scrollamt, int y)
56 {
57     return (y >= scrollamt && y - scrollamt < getmaxy(win));
58 }
59 
60 static bool
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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_NEXT ), \
451 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  CHR_PREVIOUS ), \
452 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PREVIOUS ), \
453 	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ), \
454 	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE ), \
455 	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE )
456 /*
457  * Display a form for entering a number of fields
458  */
459 int
460 dlg_form(const char *title,
461 	 const char *cprompt,
462 	 int height,
463 	 int width,
464 	 int form_height,
465 	 int item_no,
466 	 DIALOG_FORMITEM * items,
467 	 int *current_item)
468 {
469     /* *INDENT-OFF* */
470     static DLG_KEYS_BINDING binding[] = {
471 	HELPKEY_BINDINGS,
472 	ENTERKEY_BINDINGS,
473 	NAVIGATE_BINDINGS,
474 	END_KEYS_BINDING
475     };
476     static DLG_KEYS_BINDING binding2[] = {
477 	INPUTSTR_BINDINGS,
478 	HELPKEY_BINDINGS,
479 	ENTERKEY_BINDINGS,
480 	NAVIGATE_BINDINGS,
481 	END_KEYS_BINDING
482     };
483     /* *INDENT-ON* */
484 
485 #ifdef KEY_RESIZE
486     int old_height = height;
487     int old_width = width;
488 #endif
489 
490     int form_width;
491     int first = TRUE;
492     int first_trace = TRUE;
493     int chr_offset = 0;
494     int state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
495     int x, y, cur_x, cur_y, box_x, box_y;
496     int code;
497     int key = 0;
498     int fkey;
499     int choice = dlg_default_formitem(items);
500     int new_choice, new_scroll;
501     int scrollamt = 0;
502     int result = DLG_EXIT_UNKNOWN;
503     int min_width = 0, min_height = 0;
504     bool was_autosize = (height == 0 || width == 0);
505     bool show_buttons = FALSE;
506     bool scroll_changed = FALSE;
507     bool field_changed = FALSE;
508     bool non_editable = FALSE;
509     WINDOW *dialog, *form;
510     char *prompt = dlg_strclone(cprompt);
511     const char **buttons = dlg_ok_labels();
512     DIALOG_FORMITEM *current;
513 
514     make_FORM_ELTs(items, item_no, &min_height, &min_width);
515     dlg_button_layout(buttons, &min_width);
516     dlg_does_output();
517     dlg_tab_correct_str(prompt);
518 
519 #ifdef KEY_RESIZE
520   retry:
521 #endif
522 
523     dlg_auto_size(title, prompt, &height, &width,
524 		  1 + 3 * MARGIN,
525 		  MAX(26, 2 + min_width));
526 
527     if (form_height == 0)
528 	form_height = min_height;
529 
530     if (was_autosize) {
531 	form_height = MIN(SLINES - height, form_height);
532 	height += form_height;
533     } else {
534 	int thigh = 0;
535 	int twide = 0;
536 	dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
537 	thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
538 	form_height = MIN(thigh, form_height);
539     }
540 
541     dlg_print_size(height, width);
542     dlg_ctl_size(height, width);
543 
544     x = dlg_box_x_ordinate(width);
545     y = dlg_box_y_ordinate(height);
546 
547     dialog = dlg_new_window(height, width, y, x);
548     dlg_register_window(dialog, "formbox", binding);
549     dlg_register_buttons(dialog, "formbox", buttons);
550 
551     dlg_mouse_setbase(x, y);
552 
553     dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
554     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
555     dlg_draw_title(dialog, title);
556 
557     (void) wattrset(dialog, dialog_attr);
558     dlg_print_autowrap(dialog, prompt, height, width);
559 
560     form_width = width - 6;
561     getyx(dialog, cur_y, cur_x);
562     (void) cur_x;
563     box_y = cur_y + 1;
564     box_x = (width - form_width) / 2 - 1;
565 
566     /* create new window for the form */
567     form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
568 			  x + box_x + 1);
569     dlg_register_window(form, "formfield", binding2);
570 
571     /* draw a box around the form items */
572     dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
573 		 menubox_border_attr, menubox_border2_attr);
574 
575     /* register the new window, along with its borders */
576     dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
577 			  getbegx(form) - getbegx(dialog),
578 			  getmaxy(form),
579 			  getmaxx(form),
580 			  KEY_MAX, 1, 1, 3 /* by cells */ );
581 
582     show_buttons = TRUE;
583     scroll_changed = TRUE;
584 
585     choice = set_choice(items, choice, item_no, &non_editable);
586     current = &items[choice];
587     if (non_editable)
588 	state = next_valid_buttonindex(state, sTEXT, non_editable);
589 
590     while (result == DLG_EXIT_UNKNOWN) {
591 	int edit = FALSE;
592 
593 	if (scroll_changed) {
594 	    print_form(form, items, item_no, scrollamt, choice);
595 	    dlg_draw_scrollbar(dialog,
596 			       scrollamt,
597 			       scrollamt,
598 			       scrollamt + form_height + 1,
599 			       min_height,
600 			       box_x + 1,
601 			       box_x + form_width,
602 			       box_y,
603 			       box_y + form_height + 1,
604 			       menubox_border2_attr,
605 			       menubox_border_attr);
606 	    scroll_changed = FALSE;
607 	}
608 
609 	if (show_buttons) {
610 	    dlg_item_help("");
611 	    dlg_draw_buttons(dialog, height - 2, 0, buttons,
612 			     ((state < 0)
613 			      ? 1000	/* no such button, not highlighted */
614 			      : state),
615 			     FALSE, width);
616 	    show_buttons = FALSE;
617 	}
618 
619 	if (first_trace) {
620 	    first_trace = FALSE;
621 	    dlg_trace_win(dialog);
622 	}
623 
624 	if (field_changed || state == sTEXT) {
625 	    if (field_changed)
626 		chr_offset = 0;
627 	    current = &items[choice];
628 	    dialog_vars.max_input = current->text_ilen;
629 	    dlg_item_help(current->help);
630 	    dlg_show_string(form, current->text, chr_offset,
631 			    form_active_text_attr,
632 			    current->text_y - scrollamt,
633 			    current->text_x,
634 			    current->text_len,
635 			    is_hidden(current), first);
636 	    wsyncup(form);
637 	    wcursyncup(form);
638 	    field_changed = FALSE;
639 	}
640 
641 	key = dlg_mouse_wgetch((state == sTEXT) ? form : dialog, &fkey);
642 	if (dlg_result_key(key, fkey, &result))
643 	    break;
644 
645 	/* handle non-functionkeys */
646 	if (!fkey) {
647 	    if (state != sTEXT) {
648 		code = dlg_char_to_button(key, buttons);
649 		if (code >= 0) {
650 		    dlg_del_window(dialog);
651 		    result = dlg_ok_buttoncode(code);
652 		    continue;
653 		}
654 		if (key == ' ') {
655 		    fkey = TRUE;
656 		    key = DLGK_ENTER;
657 		}
658 	    }
659 	}
660 
661 	/* handle functionkeys */
662 	if (fkey) {
663 	    bool do_scroll = FALSE;
664 	    bool do_tab = FALSE;
665 	    int move_by = 0;
666 
667 	    switch (key) {
668 	    case DLGK_MOUSE(KEY_PPAGE):
669 	    case DLGK_PAGE_PREV:
670 		do_scroll = TRUE;
671 		move_by = -form_height;
672 		break;
673 
674 	    case DLGK_MOUSE(KEY_NPAGE):
675 	    case DLGK_PAGE_NEXT:
676 		do_scroll = TRUE;
677 		move_by = form_height;
678 		break;
679 
680 	    case DLGK_ENTER:
681 		dlg_del_window(dialog);
682 		result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
683 		continue;
684 
685 	    case DLGK_GRID_LEFT:
686 		if (state == sTEXT)
687 		    break;
688 		/* FALLTHRU */
689 	    case DLGK_ITEM_PREV:
690 		if (state == sTEXT) {
691 		    do_tab = TRUE;
692 		    move_by = -1;
693 		    break;
694 		} else {
695 		    state = prev_valid_buttonindex(state, 0, non_editable);
696 		    show_buttons = TRUE;
697 		    continue;
698 		}
699 
700 	    case DLGK_FORM_PREV:
701 		if (state == sTEXT && !is_first_field(items, choice)) {
702 		    do_tab = TRUE;
703 		    move_by = -1;
704 		    break;
705 		} else {
706 		    int old_state = state;
707 		    state = prev_valid_buttonindex(state, sTEXT, non_editable);
708 		    show_buttons = TRUE;
709 		    if (old_state >= 0 && state == sTEXT) {
710 			new_choice = item_no - 1;
711 			if (choice != new_choice) {
712 			    print_item(form, items + choice, scrollamt, FALSE);
713 			    choice = new_choice;
714 			}
715 		    }
716 		    continue;
717 		}
718 
719 	    case DLGK_FIELD_PREV:
720 		state = prev_valid_buttonindex(state, sTEXT, non_editable);
721 		show_buttons = TRUE;
722 		continue;
723 
724 	    case DLGK_FIELD_NEXT:
725 		state = next_valid_buttonindex(state, sTEXT, non_editable);
726 		show_buttons = TRUE;
727 		continue;
728 
729 	    case DLGK_GRID_RIGHT:
730 		if (state == sTEXT)
731 		    break;
732 		/* FALLTHRU */
733 
734 	    case DLGK_ITEM_NEXT:
735 		if (state == sTEXT) {
736 		    do_tab = TRUE;
737 		    move_by = 1;
738 		    break;
739 		} else {
740 		    state = next_valid_buttonindex(state, 0, non_editable);
741 		    show_buttons = TRUE;
742 		    continue;
743 		}
744 
745 	    case DLGK_FORM_NEXT:
746 		if (state == sTEXT && !is_last_field(items, choice, item_no)) {
747 		    do_tab = TRUE;
748 		    move_by = 1;
749 		    break;
750 		} else {
751 		    state = next_valid_buttonindex(state, sTEXT, non_editable);
752 		    show_buttons = TRUE;
753 		    if (state == sTEXT && choice) {
754 			print_item(form, items + choice, scrollamt, FALSE);
755 			choice = 0;
756 		    }
757 		    continue;
758 		}
759 
760 #ifdef KEY_RESIZE
761 	    case KEY_RESIZE:
762 		/* reset data */
763 		height = old_height;
764 		width = old_width;
765 		/* repaint */
766 		dlg_clear();
767 		dlg_del_window(dialog);
768 		refresh();
769 		dlg_mouse_free_regions();
770 		goto retry;
771 #endif
772 	    default:
773 #if USE_MOUSE
774 		if (is_DLGK_MOUSE(key)) {
775 		    if (key >= DLGK_MOUSE(KEY_MAX)) {
776 			int cell = key - DLGK_MOUSE(KEY_MAX);
777 			int row = (cell / getmaxx(form)) + scrollamt;
778 			int col = (cell % getmaxx(form));
779 			int n;
780 
781 			for (n = 0; n < item_no; ++n) {
782 			    if (items[n].name_y == row
783 				&& items[n].name_x <= col
784 				&& (items[n].name_x + items[n].name_len > col
785 				    || (items[n].name_y == items[n].text_y
786 					&& items[n].text_x > col))) {
787 				if (!is_readonly(&(items[n]))) {
788 				    field_changed = TRUE;
789 				    break;
790 				}
791 			    }
792 			    if (items[n].text_y == row
793 				&& items[n].text_x <= col
794 				&& items[n].text_x + items[n].text_ilen > col) {
795 				if (!is_readonly(&(items[n]))) {
796 				    field_changed = TRUE;
797 				    break;
798 				}
799 			    }
800 			}
801 			if (field_changed) {
802 			    print_item(form, items + choice, scrollamt, FALSE);
803 			    choice = n;
804 			    continue;
805 			}
806 			beep();
807 		    } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
808 			result = code;
809 		    }
810 		    continue;
811 		}
812 #endif
813 		break;
814 	    }
815 
816 	    new_scroll = scrollamt;
817 	    new_choice = choice;
818 	    if (do_scroll) {
819 		if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
820 		    if (choice != new_choice) {
821 			choice = new_choice;
822 			field_changed = TRUE;
823 		    }
824 		    if (scrollamt != new_scroll) {
825 			scrollamt = new_scroll;
826 			scroll_changed = TRUE;
827 		    }
828 		}
829 		continue;
830 	    }
831 	    if (do_tab) {
832 		if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
833 		    if (choice != new_choice) {
834 			choice = new_choice;
835 			field_changed = TRUE;
836 		    }
837 		    if (scrollamt != new_scroll) {
838 			scrollamt = new_scroll;
839 			scroll_changed = TRUE;
840 		    }
841 		}
842 		continue;
843 	    }
844 	}
845 
846 	if (state == sTEXT) {	/* Input box selected */
847 	    if (!is_readonly(current))
848 		edit = dlg_edit_string(current->text, &chr_offset, key,
849 				       fkey, first);
850 	    if (edit) {
851 		dlg_show_string(form, current->text, chr_offset,
852 				form_active_text_attr,
853 				current->text_y - scrollamt,
854 				current->text_x,
855 				current->text_len,
856 				is_hidden(current), first);
857 		continue;
858 	    }
859 	}
860 
861     }
862 
863     dlg_mouse_free_regions();
864     dlg_del_window(dialog);
865     free(prompt);
866 
867     *current_item = choice;
868     return result;
869 }
870 
871 /*
872  * Free memory owned by a list of DIALOG_FORMITEM's.
873  */
874 void
875 dlg_free_formitems(DIALOG_FORMITEM * items)
876 {
877     int n;
878     for (n = 0; items[n].name != 0; ++n) {
879 	if (items[n].name_free)
880 	    free(items[n].name);
881 	if (items[n].text_free)
882 	    free(items[n].text);
883 	if (items[n].help_free && items[n].help != dlg_strempty())
884 	    free(items[n].help);
885     }
886     free(items);
887 }
888 
889 /*
890  * The script accepts values beginning at 1, while curses starts at 0.
891  */
892 int
893 dlg_ordinate(const char *s)
894 {
895     int result = atoi(s);
896     if (result > 0)
897 	--result;
898     else
899 	result = 0;
900     return result;
901 }
902 
903 int
904 dialog_form(const char *title,
905 	    const char *cprompt,
906 	    int height,
907 	    int width,
908 	    int form_height,
909 	    int item_no,
910 	    char **items)
911 {
912     int result;
913     int choice;
914     int i;
915     DIALOG_FORMITEM *listitems;
916     DIALOG_VARS save_vars;
917     bool show_status = FALSE;
918     char *help_result;
919 
920     dlg_save_vars(&save_vars);
921     dialog_vars.separate_output = TRUE;
922 
923     listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
924     assert_ptr(listitems, "dialog_form");
925 
926     for (i = 0; i < item_no; ++i) {
927 	listitems[i].type = dialog_vars.formitem_type;
928 	listitems[i].name = ItemName(i);
929 	listitems[i].name_len = (int) strlen(ItemName(i));
930 	listitems[i].name_y = dlg_ordinate(ItemNameY(i));
931 	listitems[i].name_x = dlg_ordinate(ItemNameX(i));
932 	listitems[i].text = ItemText(i);
933 	listitems[i].text_len = (int) strlen(ItemText(i));
934 	listitems[i].text_y = dlg_ordinate(ItemTextY(i));
935 	listitems[i].text_x = dlg_ordinate(ItemTextX(i));
936 	listitems[i].text_flen = atoi(ItemTextFLen(i));
937 	listitems[i].text_ilen = atoi(ItemTextILen(i));
938 	listitems[i].help = ((dialog_vars.item_help)
939 			     ? ItemHelp(i)
940 			     : dlg_strempty());
941     }
942 
943     result = dlg_form(title,
944 		      cprompt,
945 		      height,
946 		      width,
947 		      form_height,
948 		      item_no,
949 		      listitems,
950 		      &choice);
951 
952     switch (result) {
953     case DLG_EXIT_OK:		/* FALLTHRU */
954     case DLG_EXIT_EXTRA:
955 	show_status = TRUE;
956 	break;
957     case DLG_EXIT_HELP:
958 	dlg_add_help_formitem(&result, &help_result, &listitems[choice]);
959 	show_status = dialog_vars.help_status;
960 	dlg_add_string(help_result);
961 	if (show_status)
962 	    dlg_add_separator();
963 	break;
964     }
965     if (show_status) {
966 	for (i = 0; i < item_no; i++) {
967 	    if (listitems[i].text_flen > 0) {
968 		dlg_add_string(listitems[i].text);
969 		dlg_add_separator();
970 	    }
971 	}
972 	dlg_add_last_key(-1);
973     }
974 
975     dlg_free_formitems(listitems);
976     dlg_restore_vars(&save_vars);
977 
978     return result;
979 }
980