xref: /freebsd/contrib/dialog/formbox.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*
2  *  $Id: formbox.c,v 1.71 2010/02/24 10:45:57 Samuel.Martin.Moro Exp $
3  *
4  *  formbox.c -- implements the form (i.e, some pairs label/editbox)
5  *
6  *  Copyright 2003-2009,2010	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 /*
192  * Tab to the next field.
193  */
194 static bool
195 tab_next(WINDOW *win,
196 	 DIALOG_FORMITEM item[],
197 	 int item_no,
198 	 int stepsize,
199 	 int *choice,
200 	 int *scrollamt)
201 {
202     int old_choice = *choice;
203     int old_scroll = *scrollamt;
204     bool wrapped = FALSE;
205 
206     do {
207 	do {
208 	    *choice += stepsize;
209 	    if (*choice < 0) {
210 		*choice = item_no - 1;
211 		wrapped = TRUE;
212 	    } else if (*choice >= item_no) {
213 		*choice = 0;
214 		wrapped = TRUE;
215 	    }
216 	} while ((*choice != old_choice) && is_readonly(&(item[*choice])));
217 
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);
221 
222 	    if (old_choice == *choice)
223 		break;
224 	    print_item(win, item + old_choice, *scrollamt, FALSE);
225 
226 	    if (*scrollamt < lo + 1 - getmaxy(win))
227 		*scrollamt = lo + 1 - getmaxy(win);
228 	    if (*scrollamt > hi)
229 		*scrollamt = hi;
230 	    /*
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
234 	     * for that.
235 	     */
236 	    if (*scrollamt != old_scroll) {
237 		if (wrapped) {
238 		    beep();
239 		    *scrollamt = old_scroll;
240 		    *choice = old_choice;
241 		} else {
242 		    scrollok(win, TRUE);
243 		    wscrl(win, *scrollamt - old_scroll);
244 		    scrollok(win, FALSE);
245 		}
246 	    }
247 	    break;
248 	}
249     } while (*choice != old_choice);
250 
251     return (old_choice != *choice) || (old_scroll != *scrollamt);
252 }
253 
254 /*
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.
258  */
259 static bool
260 scroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
261 {
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;
266     int n;
267 
268     if (stepsize < 0) {
269 	if (old_row != old_scroll)
270 	    target = old_scroll;
271 	else
272 	    target = old_scroll + stepsize;
273 	if (target < 0)
274 	    target = 0;
275     } else {
276 	int limit = form_limit(item);
277 	if (target > limit)
278 	    target = limit;
279     }
280 
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)) {
285 		old_row = new_row;
286 		*choice = n;
287 	    }
288 	}
289     }
290 
291     if (old_choice != *choice)
292 	print_item(win, item + old_choice, *scrollamt, FALSE);
293 
294     *scrollamt = *choice;
295     if (*scrollamt != old_scroll) {
296 	scrollok(win, TRUE);
297 	wscrl(win, *scrollamt - old_scroll);
298 	scrollok(win, FALSE);
299     }
300     return (old_choice != *choice) || (old_scroll != *scrollamt);
301 }
302 
303 /*
304  * Do a sanity check on the field length, and return the "right" value.
305  */
306 static int
307 real_length(DIALOG_FORMITEM * item)
308 {
309     return (item->text_flen > 0
310 	    ? item->text_flen
311 	    : (item->text_flen < 0
312 	       ? -item->text_flen
313 	       : item->text_len));
314 }
315 
316 /*
317  * Compute the form size, setup field buffers.
318  */
319 static void
320 make_FORM_ELTs(DIALOG_FORMITEM * item,
321 	       int item_no,
322 	       int *min_height,
323 	       int *min_width)
324 {
325     int i;
326     int min_w = 0;
327     int min_h = 0;
328 
329     for (i = 0; i < item_no; ++i) {
330 	int real_len = real_length(item + i);
331 
332 	/*
333 	 * Special value '0' for text_flen: no input allowed
334 	 * Special value '0' for text_ilen: 'be the same as text_flen'
335 	 */
336 	if (item[i].text_ilen == 0)
337 	    item[i].text_ilen = real_len;
338 
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);
343 
344 	item[i].text_len = real_length(item + i);
345 
346 	/*
347 	 * We do not know the actual length of .text, so we allocate it here
348 	 * to ensure it is big enough.
349 	 */
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;
353 
354 	    item[i].text = dlg_malloc(char, (size_t) max_len + 1);
355 	    assert_ptr(item[i].text, "make_FORM_ELTs");
356 
357 	    sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
358 
359 	    if (item[i].text_free) {
360 		item[i].text_free = FALSE;
361 		free(old_text);
362 	    }
363 	    item[i].text_free = TRUE;
364 	}
365     }
366 
367     *min_height = min_h;
368     *min_width = min_w;
369 }
370 
371 int
372 dlg_default_formitem(DIALOG_FORMITEM * items)
373 {
374     int result = 0;
375 
376     if (dialog_vars.default_item != 0) {
377 	int count = 0;
378 	while (items->name != 0) {
379 	    if (!strcmp(dialog_vars.default_item, items->name)) {
380 		result = count;
381 		break;
382 	    }
383 	    ++items;
384 	    count++;
385 	}
386     }
387     return result;
388 }
389 
390 #define sTEXT -1
391 
392 static int
393 next_valid_buttonindex(int state, int extra, bool non_editable)
394 {
395     state = dlg_next_ok_buttonindex(state, extra);
396     while (non_editable && state == sTEXT)
397 	state = dlg_next_ok_buttonindex(state, sTEXT);
398     return state;
399 }
400 
401 static int
402 prev_valid_buttonindex(int state, int extra, bool non_editable)
403 {
404     state = dlg_prev_ok_buttonindex(state, extra);
405     while (non_editable && state == sTEXT)
406 	state = dlg_prev_ok_buttonindex(state, sTEXT);
407     return state;
408 }
409 
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 )
421 /*
422  * Display a form for fulfill a number of fields
423  */
424 int
425 dlg_form(const char *title,
426 	 const char *cprompt,
427 	 int height,
428 	 int width,
429 	 int form_height,
430 	 int item_no,
431 	 DIALOG_FORMITEM * items,
432 	 int *current_item)
433 {
434     /* *INDENT-OFF* */
435     static DLG_KEYS_BINDING binding[] = {
436 	ENTERKEY_BINDINGS,
437 	NAVIGATE_BINDINGS,
438 	END_KEYS_BINDING
439     };
440     static DLG_KEYS_BINDING binding2[] = {
441 	INPUTSTR_BINDINGS,
442 	ENTERKEY_BINDINGS,
443 	NAVIGATE_BINDINGS,
444 	END_KEYS_BINDING
445     };
446     /* *INDENT-ON* */
447 
448 #ifdef KEY_RESIZE
449     int old_height = height;
450     int old_width = width;
451 #endif
452 
453     int form_width;
454     int first = TRUE;
455     int chr_offset = 0;
456     int state = dialog_vars.defaultno ? dlg_defaultno_button() : sTEXT;
457     int x, y, cur_x, cur_y, box_x, box_y;
458     int code;
459     int key = 0;
460     int fkey;
461     int choice = dlg_default_formitem(items);
462     int new_choice, new_scroll;
463     int scrollamt = 0;
464     int result = DLG_EXIT_UNKNOWN;
465     int min_width = 0, min_height = 0;
466     bool was_autosize = (height == 0 || width == 0);
467     bool show_buttons = FALSE;
468     bool scroll_changed = FALSE;
469     bool field_changed = FALSE;
470     bool non_editable = FALSE;
471     WINDOW *dialog, *form;
472     char *prompt = dlg_strclone(cprompt);
473     const char **buttons = dlg_ok_labels();
474     DIALOG_FORMITEM *current;
475 
476     make_FORM_ELTs(items, item_no, &min_height, &min_width);
477     dlg_button_layout(buttons, &min_width);
478     dlg_does_output();
479     dlg_tab_correct_str(prompt);
480 
481 #ifdef KEY_RESIZE
482   retry:
483 #endif
484 
485     dlg_auto_size(title, prompt, &height, &width,
486 		  1 + 3 * MARGIN,
487 		  MAX(26, 2 + min_width));
488 
489     if (form_height == 0)
490 	form_height = min_height;
491 
492     if (was_autosize) {
493 	form_height = MIN(SLINES - height, form_height);
494 	height += form_height;
495     } else {
496 	int thigh = 0;
497 	int twide = 0;
498 	dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
499 	thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
500 	form_height = MIN(thigh, form_height);
501     }
502 
503     dlg_print_size(height, width);
504     dlg_ctl_size(height, width);
505 
506     x = dlg_box_x_ordinate(width);
507     y = dlg_box_y_ordinate(height);
508 
509     dialog = dlg_new_window(height, width, y, x);
510     dlg_register_window(dialog, "formbox", binding);
511     dlg_register_window(dialog, "formfield", binding2);
512     dlg_register_buttons(dialog, "formbox", buttons);
513 
514     dlg_mouse_setbase(x, y);
515 
516     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
517     dlg_draw_bottom_box(dialog);
518     dlg_draw_title(dialog, title);
519 
520     wattrset(dialog, dialog_attr);
521     dlg_print_autowrap(dialog, prompt, height, width);
522 
523     form_width = width - 6;
524     getyx(dialog, cur_y, cur_x);
525     box_y = cur_y + 1;
526     box_x = (width - form_width) / 2 - 1;
527 
528     /* create new window for the form */
529     form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
530 			  x + box_x + 1);
531 
532     /* draw a box around the form items */
533     dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
534 		 menubox_border_attr, menubox_attr);
535 
536     /* register the new window, along with its borders */
537     dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
538 			  getbegx(form) - getbegx(dialog),
539 			  getmaxy(form),
540 			  getmaxx(form),
541 			  KEY_MAX, 1, 1, 3 /* by cells */ );
542 
543     show_buttons = TRUE;
544     scroll_changed = TRUE;
545 
546     choice = set_choice(items, choice, item_no, &non_editable);
547     current = &items[choice];
548     if (non_editable)
549 	state = next_valid_buttonindex(state, sTEXT, non_editable);
550 
551     while (result == DLG_EXIT_UNKNOWN) {
552 	int edit = FALSE;
553 
554 	if (scroll_changed) {
555 	    print_form(form, items, item_no, scrollamt, choice);
556 	    dlg_draw_scrollbar(dialog,
557 			       scrollamt,
558 			       scrollamt,
559 			       scrollamt + form_height + 1,
560 			       min_height,
561 			       box_x + 1,
562 			       box_x + form_width,
563 			       box_y,
564 			       box_y + form_height + 1,
565 			       menubox_attr,
566 			       menubox_border_attr);
567 	    scroll_changed = FALSE;
568 	}
569 
570 	if (show_buttons) {
571 	    dlg_item_help("");
572 	    dlg_draw_buttons(dialog, height - 2, 0, buttons,
573 			     ((state < 0)
574 			      ? 1000	/* no such button, not highlighted */
575 			      : state),
576 			     FALSE, width);
577 	    show_buttons = FALSE;
578 	}
579 
580 	if (field_changed || state == sTEXT) {
581 	    if (field_changed)
582 		chr_offset = 0;
583 	    current = &items[choice];
584 	    dialog_vars.max_input = current->text_ilen;
585 	    dlg_item_help(current->help);
586 	    dlg_show_string(form, current->text, chr_offset,
587 			    form_active_text_attr,
588 			    current->text_y - scrollamt,
589 			    current->text_x,
590 			    current->text_len,
591 			    is_hidden(current), first);
592 	    field_changed = FALSE;
593 	}
594 
595 	key = dlg_mouse_wgetch(dialog, &fkey);
596 	if (dlg_result_key(key, fkey, &result))
597 	    break;
598 
599 	/* handle non-functionkeys */
600 	if (!fkey) {
601 	    if (state != sTEXT) {
602 		code = dlg_char_to_button(key, buttons);
603 		if (code >= 0) {
604 		    dlg_del_window(dialog);
605 		    result = dlg_ok_buttoncode(code);
606 		    continue;
607 		}
608 		if (key == ' ') {
609 		    fkey = TRUE;
610 		    key = DLGK_ENTER;
611 		}
612 	    }
613 	}
614 
615 	/* handle functionkeys */
616 	if (fkey) {
617 	    bool do_scroll = FALSE;
618 	    bool do_tab = FALSE;
619 	    int move_by = 0;
620 
621 	    switch (key) {
622 	    case DLGK_MOUSE(KEY_PPAGE):
623 	    case DLGK_PAGE_PREV:
624 		do_scroll = TRUE;
625 		move_by = -form_height;
626 		break;
627 
628 	    case DLGK_MOUSE(KEY_NPAGE):
629 	    case DLGK_PAGE_NEXT:
630 		do_scroll = TRUE;
631 		move_by = form_height;
632 		break;
633 
634 	    case DLGK_ENTER:
635 		dlg_del_window(dialog);
636 		result = (state >= 0) ? dlg_ok_buttoncode(state) : DLG_EXIT_OK;
637 		continue;
638 
639 	    case DLGK_GRID_LEFT:
640 		if (state == sTEXT)
641 		    break;
642 		/* FALLTHRU */
643 	    case DLGK_ITEM_PREV:
644 		if (state == sTEXT) {
645 		    do_tab = TRUE;
646 		    move_by = -1;
647 		    break;
648 		} else {
649 		    state = prev_valid_buttonindex(state, 0, non_editable);
650 		    show_buttons = TRUE;
651 		    continue;
652 		}
653 
654 	    case DLGK_FIELD_PREV:
655 		state = prev_valid_buttonindex(state, sTEXT, non_editable);
656 		show_buttons = TRUE;
657 		continue;
658 
659 	    case DLGK_FIELD_NEXT:
660 		state = next_valid_buttonindex(state, sTEXT, non_editable);
661 		show_buttons = TRUE;
662 		continue;
663 
664 	    case DLGK_GRID_RIGHT:
665 		if (state == sTEXT)
666 		    break;
667 		/* FALLTHRU */
668 
669 	    case DLGK_ITEM_NEXT:
670 		if (state == sTEXT) {
671 		    do_tab = TRUE;
672 		    move_by = 1;
673 		    break;
674 		} else {
675 		    state = next_valid_buttonindex(state, 0, non_editable);
676 		    show_buttons = TRUE;
677 		    continue;
678 		}
679 
680 #ifdef KEY_RESIZE
681 	    case KEY_RESIZE:
682 		/* reset data */
683 		height = old_height;
684 		width = old_width;
685 		/* repaint */
686 		dlg_clear();
687 		dlg_del_window(dialog);
688 		refresh();
689 		dlg_mouse_free_regions();
690 		goto retry;
691 #endif
692 	    default:
693 #if USE_MOUSE
694 		if (is_DLGK_MOUSE(key)) {
695 		    if (key >= DLGK_MOUSE(KEY_MAX)) {
696 			int cell = key - DLGK_MOUSE(KEY_MAX);
697 			int row = (cell / getmaxx(form)) + scrollamt;
698 			int col = (cell % getmaxx(form));
699 			int n;
700 
701 			for (n = 0; n < item_no; ++n) {
702 			    if (items[n].name_y == row
703 				&& items[n].name_x <= col
704 				&& (items[n].name_x + items[n].name_len > col
705 				    || (items[n].name_y == items[n].text_y
706 					&& items[n].text_x > col))) {
707 				if (!is_readonly(&(items[n]))) {
708 				    field_changed = TRUE;
709 				    break;
710 				}
711 			    }
712 			    if (items[n].text_y == row
713 				&& items[n].text_x <= col
714 				&& items[n].text_x + items[n].text_ilen > col) {
715 				if (!is_readonly(&(items[n]))) {
716 				    field_changed = TRUE;
717 				    break;
718 				}
719 			    }
720 			}
721 			if (field_changed) {
722 			    print_item(form, items + choice, scrollamt, FALSE);
723 			    choice = n;
724 			    continue;
725 			}
726 			beep();
727 		    } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
728 			result = code;
729 		    }
730 		    continue;
731 		}
732 #endif
733 		break;
734 	    }
735 
736 	    new_scroll = scrollamt;
737 	    new_choice = choice;
738 	    if (do_scroll) {
739 		if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
740 		    if (choice != new_choice) {
741 			choice = new_choice;
742 			field_changed = TRUE;
743 		    }
744 		    if (scrollamt != new_scroll) {
745 			scrollamt = new_scroll;
746 			scroll_changed = TRUE;
747 		    }
748 		}
749 		continue;
750 	    }
751 	    if (do_tab) {
752 		if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
753 		    if (choice != new_choice) {
754 			choice = new_choice;
755 			field_changed = TRUE;
756 		    }
757 		    if (scrollamt != new_scroll) {
758 			scrollamt = new_scroll;
759 			scroll_changed = TRUE;
760 		    }
761 		}
762 		continue;
763 	    }
764 	}
765 
766 	if (state == sTEXT) {	/* Input box selected */
767 	    if (!is_readonly(current))
768 		edit = dlg_edit_string(current->text, &chr_offset, key,
769 				       fkey, first);
770 	    if (edit) {
771 		dlg_show_string(form, current->text, chr_offset,
772 				form_active_text_attr,
773 				current->text_y - scrollamt,
774 				current->text_x,
775 				current->text_len,
776 				is_hidden(current), first);
777 		continue;
778 	    }
779 	}
780 
781     }
782 
783     dlg_mouse_free_regions();
784     dlg_del_window(dialog);
785     free(prompt);
786 
787     *current_item = choice;
788     return result;
789 }
790 
791 /*
792  * Free memory owned by a list of DIALOG_FORMITEM's.
793  */
794 void
795 dlg_free_formitems(DIALOG_FORMITEM * items)
796 {
797     int n;
798     for (n = 0; items[n].name != 0; ++n) {
799 	if (items[n].name_free)
800 	    free(items[n].name);
801 	if (items[n].text_free)
802 	    free(items[n].text);
803 	if (items[n].help_free && items[n].help != dlg_strempty())
804 	    free(items[n].help);
805     }
806     free(items);
807 }
808 
809 /*
810  * The script accepts values beginning at 1, while curses starts at 0.
811  */
812 int
813 dlg_ordinate(const char *s)
814 {
815     int result = atoi(s);
816     if (result > 0)
817 	--result;
818     else
819 	result = 0;
820     return result;
821 }
822 
823 int
824 dialog_form(const char *title,
825 	    const char *cprompt,
826 	    int height,
827 	    int width,
828 	    int form_height,
829 	    int item_no,
830 	    char **items)
831 {
832     int result;
833     int choice;
834     int i;
835     DIALOG_FORMITEM *listitems;
836     DIALOG_VARS save_vars;
837     bool show_status = FALSE;
838 
839     dlg_save_vars(&save_vars);
840     dialog_vars.separate_output = TRUE;
841 
842     listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
843     assert_ptr(listitems, "dialog_form");
844 
845     for (i = 0; i < item_no; ++i) {
846 	listitems[i].type = dialog_vars.formitem_type;
847 	listitems[i].name = ItemName(i);
848 	listitems[i].name_len = (int) strlen(ItemName(i));
849 	listitems[i].name_y = dlg_ordinate(ItemNameY(i));
850 	listitems[i].name_x = dlg_ordinate(ItemNameX(i));
851 	listitems[i].text = ItemText(i);
852 	listitems[i].text_len = (int) strlen(ItemText(i));
853 	listitems[i].text_y = dlg_ordinate(ItemTextY(i));
854 	listitems[i].text_x = dlg_ordinate(ItemTextX(i));
855 	listitems[i].text_flen = atoi(ItemTextFLen(i));
856 	listitems[i].text_ilen = atoi(ItemTextILen(i));
857 	listitems[i].help = ((dialog_vars.item_help)
858 			     ? ItemHelp(i)
859 			     : dlg_strempty());
860     }
861 
862     result = dlg_form(title,
863 		      cprompt,
864 		      height,
865 		      width,
866 		      form_height,
867 		      item_no,
868 		      listitems,
869 		      &choice);
870 
871     switch (result) {
872     case DLG_EXIT_OK:		/* FALLTHRU */
873     case DLG_EXIT_EXTRA:
874 	show_status = TRUE;
875 	break;
876     case DLG_EXIT_HELP:
877 	dlg_add_result("HELP ");
878 	show_status = dialog_vars.help_status;
879 	if (USE_ITEM_HELP(listitems[choice].help)) {
880 	    dlg_add_string(listitems[choice].help);
881 	    result = DLG_EXIT_ITEM_HELP;
882 	} else {
883 	    dlg_add_string(listitems[choice].name);
884 	}
885 	if (show_status)
886 	    dlg_add_separator();
887 	break;
888     }
889     if (show_status) {
890 	for (i = 0; i < item_no; i++) {
891 	    if (listitems[i].text_flen > 0) {
892 		dlg_add_string(listitems[i].text);
893 		dlg_add_separator();
894 	    }
895 	}
896     }
897 
898     dlg_free_formitems(listitems);
899     dlg_restore_vars(&save_vars);
900 
901     return result;
902 }
903