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