1217309Snwhitehorn/*
2255852Sdteske *  $Id: formbox.c,v 1.87 2013/09/02 17:02:05 tom Exp $
3217309Snwhitehorn *
4217309Snwhitehorn *  formbox.c -- implements the form (i.e, some pairs label/editbox)
5217309Snwhitehorn *
6251843Sbapt *  Copyright 2003-2012,2013	Thomas E. Dickey
7217309Snwhitehorn *
8217309Snwhitehorn *  This program is free software; you can redistribute it and/or modify
9217309Snwhitehorn *  it under the terms of the GNU Lesser General Public License, version 2.1
10217309Snwhitehorn *  as published by the Free Software Foundation.
11217309Snwhitehorn *
12217309Snwhitehorn *  This program is distributed in the hope that it will be useful, but
13217309Snwhitehorn *  WITHOUT ANY WARRANTY; without even the implied warranty of
14217309Snwhitehorn *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15217309Snwhitehorn *  Lesser General Public License for more details.
16217309Snwhitehorn *
17217309Snwhitehorn *  You should have received a copy of the GNU Lesser General Public
18217309Snwhitehorn *  License along with this program; if not, write to
19217309Snwhitehorn *	Free Software Foundation, Inc.
20217309Snwhitehorn *	51 Franklin St., Fifth Floor
21217309Snwhitehorn *	Boston, MA 02110, USA.
22217309Snwhitehorn *
23217309Snwhitehorn *  This is adapted from source contributed by
24217309Snwhitehorn *	Valery Reznic (valery_reznic@users.sourceforge.net)
25217309Snwhitehorn */
26217309Snwhitehorn
27217309Snwhitehorn#include <dialog.h>
28217309Snwhitehorn#include <dlg_keys.h>
29217309Snwhitehorn
30217309Snwhitehorn#define LLEN(n) ((n) * FORMBOX_TAGS)
31217309Snwhitehorn
32217309Snwhitehorn#define ItemName(i)     items[LLEN(i) + 0]
33217309Snwhitehorn#define ItemNameY(i)    items[LLEN(i) + 1]
34217309Snwhitehorn#define ItemNameX(i)    items[LLEN(i) + 2]
35217309Snwhitehorn#define ItemText(i)     items[LLEN(i) + 3]
36217309Snwhitehorn#define ItemTextY(i)    items[LLEN(i) + 4]
37217309Snwhitehorn#define ItemTextX(i)    items[LLEN(i) + 5]
38217309Snwhitehorn#define ItemTextFLen(i) items[LLEN(i) + 6]
39217309Snwhitehorn#define ItemTextILen(i) items[LLEN(i) + 7]
40217309Snwhitehorn#define ItemHelp(i)     (dialog_vars.item_help ? items[LLEN(i) + 8] : dlg_strempty())
41217309Snwhitehorn
42217309Snwhitehornstatic bool
43217309Snwhitehornis_readonly(DIALOG_FORMITEM * item)
44217309Snwhitehorn{
45217309Snwhitehorn    return ((item->type & 2) != 0) || (item->text_flen <= 0);
46217309Snwhitehorn}
47217309Snwhitehorn
48217309Snwhitehornstatic bool
49217309Snwhitehornis_hidden(DIALOG_FORMITEM * item)
50217309Snwhitehorn{
51217309Snwhitehorn    return ((item->type & 1) != 0);
52217309Snwhitehorn}
53217309Snwhitehorn
54217309Snwhitehornstatic bool
55217309Snwhitehornin_window(WINDOW *win, int scrollamt, int y)
56217309Snwhitehorn{
57217309Snwhitehorn    return (y >= scrollamt && y - scrollamt < getmaxy(win));
58217309Snwhitehorn}
59217309Snwhitehorn
60217309Snwhitehornstatic bool
61217309Snwhitehornok_move(WINDOW *win, int scrollamt, int y, int x)
62217309Snwhitehorn{
63217309Snwhitehorn    return in_window(win, scrollamt, y)
64217309Snwhitehorn	&& (wmove(win, y - scrollamt, x) != ERR);
65217309Snwhitehorn}
66217309Snwhitehorn
67217309Snwhitehornstatic void
68217309Snwhitehornmove_past(WINDOW *win, int y, int x)
69217309Snwhitehorn{
70217309Snwhitehorn    if (wmove(win, y, x) == ERR)
71217309Snwhitehorn	wmove(win, y, getmaxx(win) - 1);
72217309Snwhitehorn}
73217309Snwhitehorn
74217309Snwhitehorn/*
75217309Snwhitehorn * Print form item
76217309Snwhitehorn */
77217309Snwhitehornstatic int
78217309Snwhitehornprint_item(WINDOW *win, DIALOG_FORMITEM * item, int scrollamt, bool choice)
79217309Snwhitehorn{
80217309Snwhitehorn    int count = 0;
81217309Snwhitehorn    int len;
82217309Snwhitehorn
83217309Snwhitehorn    if (ok_move(win, scrollamt, item->name_y, item->name_x)) {
84217309Snwhitehorn	len = item->name_len;
85217309Snwhitehorn	len = MIN(len, getmaxx(win) - item->name_x);
86217309Snwhitehorn	if (len > 0) {
87217309Snwhitehorn	    dlg_show_string(win,
88217309Snwhitehorn			    item->name,
89217309Snwhitehorn			    0,
90217309Snwhitehorn			    menubox_attr,
91217309Snwhitehorn			    item->name_y - scrollamt,
92217309Snwhitehorn			    item->name_x,
93217309Snwhitehorn			    len,
94217309Snwhitehorn			    FALSE,
95217309Snwhitehorn			    FALSE);
96217309Snwhitehorn	    move_past(win, item->name_y - scrollamt, item->name_x + len);
97217309Snwhitehorn	    count = 1;
98217309Snwhitehorn	}
99217309Snwhitehorn    }
100217309Snwhitehorn    if (item->text_len && ok_move(win, scrollamt, item->text_y, item->text_x)) {
101217309Snwhitehorn	chtype this_item_attribute;
102217309Snwhitehorn
103217309Snwhitehorn	len = item->text_len;
104217309Snwhitehorn	len = MIN(len, getmaxx(win) - item->text_x);
105217309Snwhitehorn
106217309Snwhitehorn	if (!is_readonly(item)) {
107217309Snwhitehorn	    this_item_attribute = choice
108217309Snwhitehorn		? form_active_text_attr
109217309Snwhitehorn		: form_text_attr;
110217309Snwhitehorn	} else {
111217309Snwhitehorn	    this_item_attribute = form_item_readonly_attr;
112217309Snwhitehorn	}
113217309Snwhitehorn
114217309Snwhitehorn	if (len > 0) {
115217309Snwhitehorn	    dlg_show_string(win,
116217309Snwhitehorn			    item->text,
117217309Snwhitehorn			    0,
118217309Snwhitehorn			    this_item_attribute,
119217309Snwhitehorn			    item->text_y - scrollamt,
120217309Snwhitehorn			    item->text_x,
121217309Snwhitehorn			    len,
122217309Snwhitehorn			    is_hidden(item),
123217309Snwhitehorn			    FALSE);
124217309Snwhitehorn	    move_past(win, item->text_y - scrollamt, item->text_x + len);
125217309Snwhitehorn	    count = 1;
126217309Snwhitehorn	}
127217309Snwhitehorn    }
128217309Snwhitehorn    return count;
129217309Snwhitehorn}
130217309Snwhitehorn
131217309Snwhitehorn/*
132217309Snwhitehorn * Print the entire form.
133217309Snwhitehorn */
134217309Snwhitehornstatic void
135217309Snwhitehornprint_form(WINDOW *win, DIALOG_FORMITEM * item, int total, int scrollamt, int choice)
136217309Snwhitehorn{
137217309Snwhitehorn    int n;
138217309Snwhitehorn    int count = 0;
139217309Snwhitehorn
140217309Snwhitehorn    for (n = 0; n < total; ++n) {
141217309Snwhitehorn	count += print_item(win, item + n, scrollamt, n == choice);
142217309Snwhitehorn    }
143217309Snwhitehorn    if (count) {
144217309Snwhitehorn	wbkgdset(win, menubox_attr | ' ');
145217309Snwhitehorn	wclrtobot(win);
146217309Snwhitehorn	(void) wnoutrefresh(win);
147217309Snwhitehorn    }
148217309Snwhitehorn}
149217309Snwhitehorn
150217309Snwhitehornstatic int
151217309Snwhitehornset_choice(DIALOG_FORMITEM item[], int choice, int item_no, bool * noneditable)
152217309Snwhitehorn{
153217309Snwhitehorn    int result = -1;
154217309Snwhitehorn    int i;
155217309Snwhitehorn
156217309Snwhitehorn    *noneditable = FALSE;
157217309Snwhitehorn    if (!is_readonly(&item[choice])) {
158217309Snwhitehorn	result = choice;
159217309Snwhitehorn    } else {
160217309Snwhitehorn	for (i = 0; i < item_no; i++) {
161217309Snwhitehorn	    if (!is_readonly(&(item[i]))) {
162217309Snwhitehorn		result = i;
163217309Snwhitehorn		break;
164217309Snwhitehorn	    }
165217309Snwhitehorn	}
166217309Snwhitehorn	if (result < 0) {
167217309Snwhitehorn	    *noneditable = TRUE;
168217309Snwhitehorn	    result = 0;
169217309Snwhitehorn	}
170217309Snwhitehorn    }
171217309Snwhitehorn    return result;
172217309Snwhitehorn}
173217309Snwhitehorn
174217309Snwhitehorn/*
175217309Snwhitehorn * Find the last y-value in the form.
176217309Snwhitehorn */
177217309Snwhitehornstatic int
178217309Snwhitehornform_limit(DIALOG_FORMITEM item[])
179217309Snwhitehorn{
180217309Snwhitehorn    int n;
181217309Snwhitehorn    int limit = 0;
182217309Snwhitehorn    for (n = 0; item[n].name != 0; ++n) {
183217309Snwhitehorn	if (limit < item[n].name_y)
184217309Snwhitehorn	    limit = item[n].name_y;
185217309Snwhitehorn	if (limit < item[n].text_y)
186217309Snwhitehorn	    limit = item[n].text_y;
187217309Snwhitehorn    }
188217309Snwhitehorn    return limit;
189217309Snwhitehorn}
190217309Snwhitehorn
191251843Sbaptstatic int
192251843Sbaptis_first_field(DIALOG_FORMITEM item[], int choice)
193251843Sbapt{
194251843Sbapt    int count = 0;
195251843Sbapt    while (choice >= 0) {
196251843Sbapt	if (item[choice].text_flen > 0) {
197251843Sbapt	    ++count;
198251843Sbapt	}
199251843Sbapt	--choice;
200251843Sbapt    }
201251843Sbapt
202251843Sbapt    return (count == 1);
203251843Sbapt}
204251843Sbapt
205251843Sbaptstatic int
206251843Sbaptis_last_field(DIALOG_FORMITEM item[], int choice, int item_no)
207251843Sbapt{
208251843Sbapt    int count = 0;
209251843Sbapt    while (choice < item_no) {
210251843Sbapt	if (item[choice].text_flen > 0) {
211251843Sbapt	    ++count;
212251843Sbapt	}
213251843Sbapt	++choice;
214251843Sbapt    }
215251843Sbapt
216251843Sbapt    return (count == 1);
217251843Sbapt}
218251843Sbapt
219217309Snwhitehorn/*
220217309Snwhitehorn * Tab to the next field.
221217309Snwhitehorn */
222217309Snwhitehornstatic bool
223217309Snwhitehorntab_next(WINDOW *win,
224217309Snwhitehorn	 DIALOG_FORMITEM item[],
225217309Snwhitehorn	 int item_no,
226217309Snwhitehorn	 int stepsize,
227217309Snwhitehorn	 int *choice,
228217309Snwhitehorn	 int *scrollamt)
229217309Snwhitehorn{
230217309Snwhitehorn    int old_choice = *choice;
231217309Snwhitehorn    int old_scroll = *scrollamt;
232217309Snwhitehorn    bool wrapped = FALSE;
233217309Snwhitehorn
234217309Snwhitehorn    do {
235217309Snwhitehorn	do {
236217309Snwhitehorn	    *choice += stepsize;
237217309Snwhitehorn	    if (*choice < 0) {
238217309Snwhitehorn		*choice = item_no - 1;
239217309Snwhitehorn		wrapped = TRUE;
240217309Snwhitehorn	    } else if (*choice >= item_no) {
241217309Snwhitehorn		*choice = 0;
242217309Snwhitehorn		wrapped = TRUE;
243217309Snwhitehorn	    }
244217309Snwhitehorn	} while ((*choice != old_choice) && is_readonly(&(item[*choice])));
245217309Snwhitehorn
246217309Snwhitehorn	if (item[*choice].text_flen > 0) {
247217309Snwhitehorn	    int lo = MIN(item[*choice].name_y, item[*choice].text_y);
248217309Snwhitehorn	    int hi = MAX(item[*choice].name_y, item[*choice].text_y);
249217309Snwhitehorn
250217309Snwhitehorn	    if (old_choice == *choice)
251217309Snwhitehorn		break;
252217309Snwhitehorn	    print_item(win, item + old_choice, *scrollamt, FALSE);
253217309Snwhitehorn
254217309Snwhitehorn	    if (*scrollamt < lo + 1 - getmaxy(win))
255217309Snwhitehorn		*scrollamt = lo + 1 - getmaxy(win);
256217309Snwhitehorn	    if (*scrollamt > hi)
257217309Snwhitehorn		*scrollamt = hi;
258217309Snwhitehorn	    /*
259217309Snwhitehorn	     * If we have to scroll to show a wrap-around, it does get
260217309Snwhitehorn	     * confusing.  Just give up rather than scroll.  Tab'ing to the
261217309Snwhitehorn	     * next field in a multi-column form is a different matter.  Scroll
262217309Snwhitehorn	     * for that.
263217309Snwhitehorn	     */
264217309Snwhitehorn	    if (*scrollamt != old_scroll) {
265217309Snwhitehorn		if (wrapped) {
266217309Snwhitehorn		    beep();
267217309Snwhitehorn		    *scrollamt = old_scroll;
268217309Snwhitehorn		    *choice = old_choice;
269217309Snwhitehorn		} else {
270217309Snwhitehorn		    scrollok(win, TRUE);
271217309Snwhitehorn		    wscrl(win, *scrollamt - old_scroll);
272217309Snwhitehorn		    scrollok(win, FALSE);
273217309Snwhitehorn		}
274217309Snwhitehorn	    }
275217309Snwhitehorn	    break;
276217309Snwhitehorn	}
277217309Snwhitehorn    } while (*choice != old_choice);
278217309Snwhitehorn
279217309Snwhitehorn    return (old_choice != *choice) || (old_scroll != *scrollamt);
280217309Snwhitehorn}
281217309Snwhitehorn
282217309Snwhitehorn/*
283217309Snwhitehorn * Scroll to the next page, putting the choice at the first editable field
284217309Snwhitehorn * in that page.  Note that fields are not necessarily in top-to-bottom order,
285217309Snwhitehorn * nor is there necessarily a field on each row of the window.
286217309Snwhitehorn */
287217309Snwhitehornstatic bool
288217309Snwhitehornscroll_next(WINDOW *win, DIALOG_FORMITEM item[], int stepsize, int *choice, int *scrollamt)
289217309Snwhitehorn{
290251843Sbapt    bool result = TRUE;
291217309Snwhitehorn    int old_choice = *choice;
292217309Snwhitehorn    int old_scroll = *scrollamt;
293217309Snwhitehorn    int old_row = MIN(item[old_choice].text_y, item[old_choice].name_y);
294217309Snwhitehorn    int target = old_scroll + stepsize;
295217309Snwhitehorn    int n;
296217309Snwhitehorn
297217309Snwhitehorn    if (stepsize < 0) {
298217309Snwhitehorn	if (old_row != old_scroll)
299217309Snwhitehorn	    target = old_scroll;
300217309Snwhitehorn	else
301217309Snwhitehorn	    target = old_scroll + stepsize;
302251843Sbapt	if (target < 0) {
303251843Sbapt	    result = FALSE;
304251843Sbapt	}
305217309Snwhitehorn    } else {
306251843Sbapt	if (target > form_limit(item)) {
307251843Sbapt	    result = FALSE;
308251843Sbapt	}
309217309Snwhitehorn    }
310217309Snwhitehorn
311251843Sbapt    if (result) {
312251843Sbapt	for (n = 0; item[n].name != 0; ++n) {
313251843Sbapt	    if (item[n].text_flen > 0) {
314251843Sbapt		int new_row = MIN(item[n].text_y, item[n].name_y);
315251843Sbapt		if (abs(new_row - target) < abs(old_row - target)) {
316251843Sbapt		    old_row = new_row;
317251843Sbapt		    *choice = n;
318251843Sbapt		}
319217309Snwhitehorn	    }
320217309Snwhitehorn	}
321217309Snwhitehorn
322251843Sbapt	if (old_choice != *choice)
323251843Sbapt	    print_item(win, item + old_choice, *scrollamt, FALSE);
324217309Snwhitehorn
325251843Sbapt	*scrollamt = *choice;
326251843Sbapt	if (*scrollamt != old_scroll) {
327251843Sbapt	    scrollok(win, TRUE);
328251843Sbapt	    wscrl(win, *scrollamt - old_scroll);
329251843Sbapt	    scrollok(win, FALSE);
330251843Sbapt	}
331251843Sbapt	result = (old_choice != *choice) || (old_scroll != *scrollamt);
332217309Snwhitehorn    }
333251843Sbapt    if (!result)
334251843Sbapt	beep();
335251843Sbapt    return result;
336217309Snwhitehorn}
337217309Snwhitehorn
338217309Snwhitehorn/*
339217309Snwhitehorn * Do a sanity check on the field length, and return the "right" value.
340217309Snwhitehorn */
341217309Snwhitehornstatic int
342217309Snwhitehornreal_length(DIALOG_FORMITEM * item)
343217309Snwhitehorn{
344217309Snwhitehorn    return (item->text_flen > 0
345217309Snwhitehorn	    ? item->text_flen
346217309Snwhitehorn	    : (item->text_flen < 0
347217309Snwhitehorn	       ? -item->text_flen
348217309Snwhitehorn	       : item->text_len));
349217309Snwhitehorn}
350217309Snwhitehorn
351217309Snwhitehorn/*
352217309Snwhitehorn * Compute the form size, setup field buffers.
353217309Snwhitehorn */
354217309Snwhitehornstatic void
355217309Snwhitehornmake_FORM_ELTs(DIALOG_FORMITEM * item,
356217309Snwhitehorn	       int item_no,
357217309Snwhitehorn	       int *min_height,
358217309Snwhitehorn	       int *min_width)
359217309Snwhitehorn{
360217309Snwhitehorn    int i;
361217309Snwhitehorn    int min_w = 0;
362217309Snwhitehorn    int min_h = 0;
363217309Snwhitehorn
364217309Snwhitehorn    for (i = 0; i < item_no; ++i) {
365217309Snwhitehorn	int real_len = real_length(item + i);
366217309Snwhitehorn
367217309Snwhitehorn	/*
368217309Snwhitehorn	 * Special value '0' for text_flen: no input allowed
369217309Snwhitehorn	 * Special value '0' for text_ilen: 'be the same as text_flen'
370217309Snwhitehorn	 */
371217309Snwhitehorn	if (item[i].text_ilen == 0)
372217309Snwhitehorn	    item[i].text_ilen = real_len;
373217309Snwhitehorn
374217309Snwhitehorn	min_h = MAX(min_h, item[i].name_y + 1);
375217309Snwhitehorn	min_h = MAX(min_h, item[i].text_y + 1);
376217309Snwhitehorn	min_w = MAX(min_w, item[i].name_x + 1 + item[i].name_len);
377217309Snwhitehorn	min_w = MAX(min_w, item[i].text_x + 1 + real_len);
378217309Snwhitehorn
379217309Snwhitehorn	item[i].text_len = real_length(item + i);
380217309Snwhitehorn
381217309Snwhitehorn	/*
382217309Snwhitehorn	 * We do not know the actual length of .text, so we allocate it here
383217309Snwhitehorn	 * to ensure it is big enough.
384217309Snwhitehorn	 */
385217309Snwhitehorn	if (item[i].text_flen > 0) {
386217309Snwhitehorn	    int max_len = dlg_max_input(MAX(item[i].text_ilen + 1, MAX_LEN));
387217309Snwhitehorn	    char *old_text = item[i].text;
388217309Snwhitehorn
389217309Snwhitehorn	    item[i].text = dlg_malloc(char, (size_t) max_len + 1);
390217309Snwhitehorn	    assert_ptr(item[i].text, "make_FORM_ELTs");
391217309Snwhitehorn
392217309Snwhitehorn	    sprintf(item[i].text, "%.*s", item[i].text_ilen, old_text);
393217309Snwhitehorn
394217309Snwhitehorn	    if (item[i].text_free) {
395217309Snwhitehorn		item[i].text_free = FALSE;
396217309Snwhitehorn		free(old_text);
397217309Snwhitehorn	    }
398217309Snwhitehorn	    item[i].text_free = TRUE;
399217309Snwhitehorn	}
400217309Snwhitehorn    }
401217309Snwhitehorn
402217309Snwhitehorn    *min_height = min_h;
403217309Snwhitehorn    *min_width = min_w;
404217309Snwhitehorn}
405217309Snwhitehorn
406217309Snwhitehornint
407217309Snwhitehorndlg_default_formitem(DIALOG_FORMITEM * items)
408217309Snwhitehorn{
409217309Snwhitehorn    int result = 0;
410217309Snwhitehorn
411217309Snwhitehorn    if (dialog_vars.default_item != 0) {
412217309Snwhitehorn	int count = 0;
413217309Snwhitehorn	while (items->name != 0) {
414217309Snwhitehorn	    if (!strcmp(dialog_vars.default_item, items->name)) {
415217309Snwhitehorn		result = count;
416217309Snwhitehorn		break;
417217309Snwhitehorn	    }
418217309Snwhitehorn	    ++items;
419217309Snwhitehorn	    count++;
420217309Snwhitehorn	}
421217309Snwhitehorn    }
422217309Snwhitehorn    return result;
423217309Snwhitehorn}
424217309Snwhitehorn
425217309Snwhitehorn#define sTEXT -1
426217309Snwhitehorn
427217309Snwhitehornstatic int
428217309Snwhitehornnext_valid_buttonindex(int state, int extra, bool non_editable)
429217309Snwhitehorn{
430217309Snwhitehorn    state = dlg_next_ok_buttonindex(state, extra);
431217309Snwhitehorn    while (non_editable && state == sTEXT)
432217309Snwhitehorn	state = dlg_next_ok_buttonindex(state, sTEXT);
433217309Snwhitehorn    return state;
434217309Snwhitehorn}
435217309Snwhitehorn
436217309Snwhitehornstatic int
437217309Snwhitehornprev_valid_buttonindex(int state, int extra, bool non_editable)
438217309Snwhitehorn{
439217309Snwhitehorn    state = dlg_prev_ok_buttonindex(state, extra);
440217309Snwhitehorn    while (non_editable && state == sTEXT)
441217309Snwhitehorn	state = dlg_prev_ok_buttonindex(state, sTEXT);
442217309Snwhitehorn    return state;
443217309Snwhitehorn}
444217309Snwhitehorn
445217309Snwhitehorn#define NAVIGATE_BINDINGS \
446217309Snwhitehorn	DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), \
447217309Snwhitehorn	DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), \
448217309Snwhitehorn	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  CHR_NEXT ), \
449217309Snwhitehorn	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN ), \
450217309Snwhitehorn	DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NEXT ), \
451217309Snwhitehorn	DLG_KEYS_DATA( DLGK_ITEM_PREV,  CHR_PREVIOUS ), \
452217309Snwhitehorn	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PREVIOUS ), \
453217309Snwhitehorn	DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ), \
454217309Snwhitehorn	DLG_KEYS_DATA( DLGK_PAGE_NEXT,  KEY_NPAGE ), \
455217309Snwhitehorn	DLG_KEYS_DATA( DLGK_PAGE_PREV,  KEY_PPAGE )
456217309Snwhitehorn/*
457251843Sbapt * Display a form for entering a number of fields
458217309Snwhitehorn */
459217309Snwhitehornint
460217309Snwhitehorndlg_form(const char *title,
461217309Snwhitehorn	 const char *cprompt,
462217309Snwhitehorn	 int height,
463217309Snwhitehorn	 int width,
464217309Snwhitehorn	 int form_height,
465217309Snwhitehorn	 int item_no,
466217309Snwhitehorn	 DIALOG_FORMITEM * items,
467217309Snwhitehorn	 int *current_item)
468217309Snwhitehorn{
469217309Snwhitehorn    /* *INDENT-OFF* */
470217309Snwhitehorn    static DLG_KEYS_BINDING binding[] = {
471224014Snwhitehorn	HELPKEY_BINDINGS,
472217309Snwhitehorn	ENTERKEY_BINDINGS,
473217309Snwhitehorn	NAVIGATE_BINDINGS,
474217309Snwhitehorn	END_KEYS_BINDING
475217309Snwhitehorn    };
476217309Snwhitehorn    static DLG_KEYS_BINDING binding2[] = {
477217309Snwhitehorn	INPUTSTR_BINDINGS,
478224014Snwhitehorn	HELPKEY_BINDINGS,
479217309Snwhitehorn	ENTERKEY_BINDINGS,
480217309Snwhitehorn	NAVIGATE_BINDINGS,
481217309Snwhitehorn	END_KEYS_BINDING
482217309Snwhitehorn    };
483217309Snwhitehorn    /* *INDENT-ON* */
484217309Snwhitehorn
485217309Snwhitehorn#ifdef KEY_RESIZE
486217309Snwhitehorn    int old_height = height;
487217309Snwhitehorn    int old_width = width;
488217309Snwhitehorn#endif
489217309Snwhitehorn
490217309Snwhitehorn    int form_width;
491217309Snwhitehorn    int first = TRUE;
492251843Sbapt    int first_trace = TRUE;
493217309Snwhitehorn    int chr_offset = 0;
494251843Sbapt    int state = dialog_vars.default_button >= 0 ? dlg_default_button() : sTEXT;
495217309Snwhitehorn    int x, y, cur_x, cur_y, box_x, box_y;
496217309Snwhitehorn    int code;
497217309Snwhitehorn    int key = 0;
498217309Snwhitehorn    int fkey;
499217309Snwhitehorn    int choice = dlg_default_formitem(items);
500217309Snwhitehorn    int new_choice, new_scroll;
501217309Snwhitehorn    int scrollamt = 0;
502217309Snwhitehorn    int result = DLG_EXIT_UNKNOWN;
503217309Snwhitehorn    int min_width = 0, min_height = 0;
504217309Snwhitehorn    bool was_autosize = (height == 0 || width == 0);
505217309Snwhitehorn    bool show_buttons = FALSE;
506217309Snwhitehorn    bool scroll_changed = FALSE;
507217309Snwhitehorn    bool field_changed = FALSE;
508217309Snwhitehorn    bool non_editable = FALSE;
509217309Snwhitehorn    WINDOW *dialog, *form;
510217309Snwhitehorn    char *prompt = dlg_strclone(cprompt);
511217309Snwhitehorn    const char **buttons = dlg_ok_labels();
512217309Snwhitehorn    DIALOG_FORMITEM *current;
513217309Snwhitehorn
514217309Snwhitehorn    make_FORM_ELTs(items, item_no, &min_height, &min_width);
515217309Snwhitehorn    dlg_button_layout(buttons, &min_width);
516217309Snwhitehorn    dlg_does_output();
517217309Snwhitehorn    dlg_tab_correct_str(prompt);
518217309Snwhitehorn
519217309Snwhitehorn#ifdef KEY_RESIZE
520217309Snwhitehorn  retry:
521217309Snwhitehorn#endif
522217309Snwhitehorn
523217309Snwhitehorn    dlg_auto_size(title, prompt, &height, &width,
524217309Snwhitehorn		  1 + 3 * MARGIN,
525217309Snwhitehorn		  MAX(26, 2 + min_width));
526217309Snwhitehorn
527217309Snwhitehorn    if (form_height == 0)
528217309Snwhitehorn	form_height = min_height;
529217309Snwhitehorn
530217309Snwhitehorn    if (was_autosize) {
531217309Snwhitehorn	form_height = MIN(SLINES - height, form_height);
532217309Snwhitehorn	height += form_height;
533217309Snwhitehorn    } else {
534217309Snwhitehorn	int thigh = 0;
535217309Snwhitehorn	int twide = 0;
536217309Snwhitehorn	dlg_auto_size(title, prompt, &thigh, &twide, 0, width);
537217309Snwhitehorn	thigh = SLINES - (height - (thigh + 1 + 3 * MARGIN));
538217309Snwhitehorn	form_height = MIN(thigh, form_height);
539217309Snwhitehorn    }
540217309Snwhitehorn
541217309Snwhitehorn    dlg_print_size(height, width);
542217309Snwhitehorn    dlg_ctl_size(height, width);
543217309Snwhitehorn
544217309Snwhitehorn    x = dlg_box_x_ordinate(width);
545217309Snwhitehorn    y = dlg_box_y_ordinate(height);
546217309Snwhitehorn
547217309Snwhitehorn    dialog = dlg_new_window(height, width, y, x);
548217309Snwhitehorn    dlg_register_window(dialog, "formbox", binding);
549217309Snwhitehorn    dlg_register_buttons(dialog, "formbox", buttons);
550217309Snwhitehorn
551217309Snwhitehorn    dlg_mouse_setbase(x, y);
552217309Snwhitehorn
553251843Sbapt    dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
554251843Sbapt    dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
555217309Snwhitehorn    dlg_draw_title(dialog, title);
556217309Snwhitehorn
557251843Sbapt    (void) wattrset(dialog, dialog_attr);
558217309Snwhitehorn    dlg_print_autowrap(dialog, prompt, height, width);
559217309Snwhitehorn
560217309Snwhitehorn    form_width = width - 6;
561217309Snwhitehorn    getyx(dialog, cur_y, cur_x);
562251843Sbapt    (void) cur_x;
563217309Snwhitehorn    box_y = cur_y + 1;
564217309Snwhitehorn    box_x = (width - form_width) / 2 - 1;
565217309Snwhitehorn
566217309Snwhitehorn    /* create new window for the form */
567217309Snwhitehorn    form = dlg_sub_window(dialog, form_height, form_width, y + box_y + 1,
568217309Snwhitehorn			  x + box_x + 1);
569251843Sbapt    dlg_register_window(form, "formfield", binding2);
570217309Snwhitehorn
571217309Snwhitehorn    /* draw a box around the form items */
572217309Snwhitehorn    dlg_draw_box(dialog, box_y, box_x, form_height + 2, form_width + 2,
573251843Sbapt		 menubox_border_attr, menubox_border2_attr);
574217309Snwhitehorn
575217309Snwhitehorn    /* register the new window, along with its borders */
576217309Snwhitehorn    dlg_mouse_mkbigregion(getbegy(form) - getbegy(dialog),
577217309Snwhitehorn			  getbegx(form) - getbegx(dialog),
578217309Snwhitehorn			  getmaxy(form),
579217309Snwhitehorn			  getmaxx(form),
580217309Snwhitehorn			  KEY_MAX, 1, 1, 3 /* by cells */ );
581217309Snwhitehorn
582217309Snwhitehorn    show_buttons = TRUE;
583217309Snwhitehorn    scroll_changed = TRUE;
584217309Snwhitehorn
585217309Snwhitehorn    choice = set_choice(items, choice, item_no, &non_editable);
586217309Snwhitehorn    current = &items[choice];
587217309Snwhitehorn    if (non_editable)
588217309Snwhitehorn	state = next_valid_buttonindex(state, sTEXT, non_editable);
589217309Snwhitehorn
590217309Snwhitehorn    while (result == DLG_EXIT_UNKNOWN) {
591217309Snwhitehorn	int edit = FALSE;
592217309Snwhitehorn
593217309Snwhitehorn	if (scroll_changed) {
594217309Snwhitehorn	    print_form(form, items, item_no, scrollamt, choice);
595217309Snwhitehorn	    dlg_draw_scrollbar(dialog,
596217309Snwhitehorn			       scrollamt,
597217309Snwhitehorn			       scrollamt,
598217309Snwhitehorn			       scrollamt + form_height + 1,
599217309Snwhitehorn			       min_height,
600217309Snwhitehorn			       box_x + 1,
601217309Snwhitehorn			       box_x + form_width,
602217309Snwhitehorn			       box_y,
603217309Snwhitehorn			       box_y + form_height + 1,
604251843Sbapt			       menubox_border2_attr,
605217309Snwhitehorn			       menubox_border_attr);
606217309Snwhitehorn	    scroll_changed = FALSE;
607217309Snwhitehorn	}
608217309Snwhitehorn
609217309Snwhitehorn	if (show_buttons) {
610217309Snwhitehorn	    dlg_item_help("");
611217309Snwhitehorn	    dlg_draw_buttons(dialog, height - 2, 0, buttons,
612217309Snwhitehorn			     ((state < 0)
613217309Snwhitehorn			      ? 1000	/* no such button, not highlighted */
614217309Snwhitehorn			      : state),
615217309Snwhitehorn			     FALSE, width);
616217309Snwhitehorn	    show_buttons = FALSE;
617217309Snwhitehorn	}
618217309Snwhitehorn
619251843Sbapt	if (first_trace) {
620251843Sbapt	    first_trace = FALSE;
621251843Sbapt	    dlg_trace_win(dialog);
622251843Sbapt	}
623251843Sbapt
624217309Snwhitehorn	if (field_changed || state == sTEXT) {
625217309Snwhitehorn	    if (field_changed)
626217309Snwhitehorn		chr_offset = 0;
627217309Snwhitehorn	    current = &items[choice];
628217309Snwhitehorn	    dialog_vars.max_input = current->text_ilen;
629217309Snwhitehorn	    dlg_item_help(current->help);
630217309Snwhitehorn	    dlg_show_string(form, current->text, chr_offset,
631217309Snwhitehorn			    form_active_text_attr,
632217309Snwhitehorn			    current->text_y - scrollamt,
633217309Snwhitehorn			    current->text_x,
634217309Snwhitehorn			    current->text_len,
635217309Snwhitehorn			    is_hidden(current), first);
636251843Sbapt	    wsyncup(form);
637251843Sbapt	    wcursyncup(form);
638217309Snwhitehorn	    field_changed = FALSE;
639217309Snwhitehorn	}
640217309Snwhitehorn
641251843Sbapt	key = dlg_mouse_wgetch((state == sTEXT) ? form : dialog, &fkey);
642217309Snwhitehorn	if (dlg_result_key(key, fkey, &result))
643217309Snwhitehorn	    break;
644217309Snwhitehorn
645217309Snwhitehorn	/* handle non-functionkeys */
646217309Snwhitehorn	if (!fkey) {
647217309Snwhitehorn	    if (state != sTEXT) {
648217309Snwhitehorn		code = dlg_char_to_button(key, buttons);
649217309Snwhitehorn		if (code >= 0) {
650217309Snwhitehorn		    dlg_del_window(dialog);
651217309Snwhitehorn		    result = dlg_ok_buttoncode(code);
652217309Snwhitehorn		    continue;
653217309Snwhitehorn		}
654217309Snwhitehorn		if (key == ' ') {
655217309Snwhitehorn		    fkey = TRUE;
656217309Snwhitehorn		    key = DLGK_ENTER;
657217309Snwhitehorn		}
658217309Snwhitehorn	    }
659217309Snwhitehorn	}
660217309Snwhitehorn
661217309Snwhitehorn	/* handle functionkeys */
662217309Snwhitehorn	if (fkey) {
663217309Snwhitehorn	    bool do_scroll = FALSE;
664217309Snwhitehorn	    bool do_tab = FALSE;
665217309Snwhitehorn	    int move_by = 0;
666217309Snwhitehorn
667217309Snwhitehorn	    switch (key) {
668217309Snwhitehorn	    case DLGK_MOUSE(KEY_PPAGE):
669217309Snwhitehorn	    case DLGK_PAGE_PREV:
670217309Snwhitehorn		do_scroll = TRUE;
671217309Snwhitehorn		move_by = -form_height;
672217309Snwhitehorn		break;
673217309Snwhitehorn
674217309Snwhitehorn	    case DLGK_MOUSE(KEY_NPAGE):
675217309Snwhitehorn	    case DLGK_PAGE_NEXT:
676217309Snwhitehorn		do_scroll = TRUE;
677217309Snwhitehorn		move_by = form_height;
678217309Snwhitehorn		break;
679217309Snwhitehorn
680217309Snwhitehorn	    case DLGK_ENTER:
681217309Snwhitehorn		dlg_del_window(dialog);
682224014Snwhitehorn		result = (state >= 0) ? dlg_enter_buttoncode(state) : DLG_EXIT_OK;
683217309Snwhitehorn		continue;
684217309Snwhitehorn
685217309Snwhitehorn	    case DLGK_GRID_LEFT:
686217309Snwhitehorn		if (state == sTEXT)
687217309Snwhitehorn		    break;
688217309Snwhitehorn		/* FALLTHRU */
689217309Snwhitehorn	    case DLGK_ITEM_PREV:
690217309Snwhitehorn		if (state == sTEXT) {
691217309Snwhitehorn		    do_tab = TRUE;
692217309Snwhitehorn		    move_by = -1;
693217309Snwhitehorn		    break;
694217309Snwhitehorn		} else {
695217309Snwhitehorn		    state = prev_valid_buttonindex(state, 0, non_editable);
696217309Snwhitehorn		    show_buttons = TRUE;
697217309Snwhitehorn		    continue;
698217309Snwhitehorn		}
699217309Snwhitehorn
700251843Sbapt	    case DLGK_FORM_PREV:
701251843Sbapt		if (state == sTEXT && !is_first_field(items, choice)) {
702251843Sbapt		    do_tab = TRUE;
703251843Sbapt		    move_by = -1;
704251843Sbapt		    break;
705251843Sbapt		} else {
706251843Sbapt		    int old_state = state;
707251843Sbapt		    state = prev_valid_buttonindex(state, sTEXT, non_editable);
708251843Sbapt		    show_buttons = TRUE;
709251843Sbapt		    if (old_state >= 0 && state == sTEXT) {
710251843Sbapt			new_choice = item_no - 1;
711251843Sbapt			if (choice != new_choice) {
712251843Sbapt			    print_item(form, items + choice, scrollamt, FALSE);
713251843Sbapt			    choice = new_choice;
714251843Sbapt			}
715251843Sbapt		    }
716251843Sbapt		    continue;
717251843Sbapt		}
718251843Sbapt
719217309Snwhitehorn	    case DLGK_FIELD_PREV:
720217309Snwhitehorn		state = prev_valid_buttonindex(state, sTEXT, non_editable);
721217309Snwhitehorn		show_buttons = TRUE;
722217309Snwhitehorn		continue;
723217309Snwhitehorn
724217309Snwhitehorn	    case DLGK_FIELD_NEXT:
725217309Snwhitehorn		state = next_valid_buttonindex(state, sTEXT, non_editable);
726217309Snwhitehorn		show_buttons = TRUE;
727217309Snwhitehorn		continue;
728217309Snwhitehorn
729217309Snwhitehorn	    case DLGK_GRID_RIGHT:
730217309Snwhitehorn		if (state == sTEXT)
731217309Snwhitehorn		    break;
732217309Snwhitehorn		/* FALLTHRU */
733217309Snwhitehorn
734217309Snwhitehorn	    case DLGK_ITEM_NEXT:
735217309Snwhitehorn		if (state == sTEXT) {
736217309Snwhitehorn		    do_tab = TRUE;
737217309Snwhitehorn		    move_by = 1;
738217309Snwhitehorn		    break;
739217309Snwhitehorn		} else {
740217309Snwhitehorn		    state = next_valid_buttonindex(state, 0, non_editable);
741217309Snwhitehorn		    show_buttons = TRUE;
742217309Snwhitehorn		    continue;
743217309Snwhitehorn		}
744217309Snwhitehorn
745251843Sbapt	    case DLGK_FORM_NEXT:
746251843Sbapt		if (state == sTEXT && !is_last_field(items, choice, item_no)) {
747251843Sbapt		    do_tab = TRUE;
748251843Sbapt		    move_by = 1;
749251843Sbapt		    break;
750251843Sbapt		} else {
751251843Sbapt		    state = next_valid_buttonindex(state, sTEXT, non_editable);
752251843Sbapt		    show_buttons = TRUE;
753251843Sbapt		    if (state == sTEXT && choice) {
754251843Sbapt			print_item(form, items + choice, scrollamt, FALSE);
755251843Sbapt			choice = 0;
756251843Sbapt		    }
757251843Sbapt		    continue;
758251843Sbapt		}
759251843Sbapt
760217309Snwhitehorn#ifdef KEY_RESIZE
761217309Snwhitehorn	    case KEY_RESIZE:
762217309Snwhitehorn		/* reset data */
763217309Snwhitehorn		height = old_height;
764217309Snwhitehorn		width = old_width;
765217309Snwhitehorn		/* repaint */
766217309Snwhitehorn		dlg_clear();
767217309Snwhitehorn		dlg_del_window(dialog);
768217309Snwhitehorn		refresh();
769217309Snwhitehorn		dlg_mouse_free_regions();
770217309Snwhitehorn		goto retry;
771217309Snwhitehorn#endif
772217309Snwhitehorn	    default:
773217309Snwhitehorn#if USE_MOUSE
774217309Snwhitehorn		if (is_DLGK_MOUSE(key)) {
775217309Snwhitehorn		    if (key >= DLGK_MOUSE(KEY_MAX)) {
776217309Snwhitehorn			int cell = key - DLGK_MOUSE(KEY_MAX);
777217309Snwhitehorn			int row = (cell / getmaxx(form)) + scrollamt;
778217309Snwhitehorn			int col = (cell % getmaxx(form));
779217309Snwhitehorn			int n;
780217309Snwhitehorn
781217309Snwhitehorn			for (n = 0; n < item_no; ++n) {
782217309Snwhitehorn			    if (items[n].name_y == row
783217309Snwhitehorn				&& items[n].name_x <= col
784217309Snwhitehorn				&& (items[n].name_x + items[n].name_len > col
785217309Snwhitehorn				    || (items[n].name_y == items[n].text_y
786217309Snwhitehorn					&& items[n].text_x > col))) {
787217309Snwhitehorn				if (!is_readonly(&(items[n]))) {
788217309Snwhitehorn				    field_changed = TRUE;
789217309Snwhitehorn				    break;
790217309Snwhitehorn				}
791217309Snwhitehorn			    }
792217309Snwhitehorn			    if (items[n].text_y == row
793217309Snwhitehorn				&& items[n].text_x <= col
794217309Snwhitehorn				&& items[n].text_x + items[n].text_ilen > col) {
795217309Snwhitehorn				if (!is_readonly(&(items[n]))) {
796217309Snwhitehorn				    field_changed = TRUE;
797217309Snwhitehorn				    break;
798217309Snwhitehorn				}
799217309Snwhitehorn			    }
800217309Snwhitehorn			}
801217309Snwhitehorn			if (field_changed) {
802217309Snwhitehorn			    print_item(form, items + choice, scrollamt, FALSE);
803217309Snwhitehorn			    choice = n;
804217309Snwhitehorn			    continue;
805217309Snwhitehorn			}
806217309Snwhitehorn			beep();
807217309Snwhitehorn		    } else if ((code = dlg_ok_buttoncode(key - M_EVENT)) >= 0) {
808217309Snwhitehorn			result = code;
809217309Snwhitehorn		    }
810217309Snwhitehorn		    continue;
811217309Snwhitehorn		}
812217309Snwhitehorn#endif
813217309Snwhitehorn		break;
814217309Snwhitehorn	    }
815217309Snwhitehorn
816217309Snwhitehorn	    new_scroll = scrollamt;
817217309Snwhitehorn	    new_choice = choice;
818217309Snwhitehorn	    if (do_scroll) {
819217309Snwhitehorn		if (scroll_next(form, items, move_by, &new_choice, &new_scroll)) {
820217309Snwhitehorn		    if (choice != new_choice) {
821217309Snwhitehorn			choice = new_choice;
822217309Snwhitehorn			field_changed = TRUE;
823217309Snwhitehorn		    }
824217309Snwhitehorn		    if (scrollamt != new_scroll) {
825217309Snwhitehorn			scrollamt = new_scroll;
826217309Snwhitehorn			scroll_changed = TRUE;
827217309Snwhitehorn		    }
828217309Snwhitehorn		}
829217309Snwhitehorn		continue;
830217309Snwhitehorn	    }
831217309Snwhitehorn	    if (do_tab) {
832217309Snwhitehorn		if (tab_next(form, items, item_no, move_by, &new_choice, &new_scroll)) {
833217309Snwhitehorn		    if (choice != new_choice) {
834217309Snwhitehorn			choice = new_choice;
835217309Snwhitehorn			field_changed = TRUE;
836217309Snwhitehorn		    }
837217309Snwhitehorn		    if (scrollamt != new_scroll) {
838217309Snwhitehorn			scrollamt = new_scroll;
839217309Snwhitehorn			scroll_changed = TRUE;
840217309Snwhitehorn		    }
841217309Snwhitehorn		}
842217309Snwhitehorn		continue;
843217309Snwhitehorn	    }
844217309Snwhitehorn	}
845217309Snwhitehorn
846217309Snwhitehorn	if (state == sTEXT) {	/* Input box selected */
847217309Snwhitehorn	    if (!is_readonly(current))
848217309Snwhitehorn		edit = dlg_edit_string(current->text, &chr_offset, key,
849217309Snwhitehorn				       fkey, first);
850217309Snwhitehorn	    if (edit) {
851217309Snwhitehorn		dlg_show_string(form, current->text, chr_offset,
852217309Snwhitehorn				form_active_text_attr,
853217309Snwhitehorn				current->text_y - scrollamt,
854217309Snwhitehorn				current->text_x,
855217309Snwhitehorn				current->text_len,
856217309Snwhitehorn				is_hidden(current), first);
857217309Snwhitehorn		continue;
858217309Snwhitehorn	    }
859217309Snwhitehorn	}
860217309Snwhitehorn
861217309Snwhitehorn    }
862217309Snwhitehorn
863217309Snwhitehorn    dlg_mouse_free_regions();
864217309Snwhitehorn    dlg_del_window(dialog);
865217309Snwhitehorn    free(prompt);
866217309Snwhitehorn
867217309Snwhitehorn    *current_item = choice;
868217309Snwhitehorn    return result;
869217309Snwhitehorn}
870217309Snwhitehorn
871217309Snwhitehorn/*
872217309Snwhitehorn * Free memory owned by a list of DIALOG_FORMITEM's.
873217309Snwhitehorn */
874217309Snwhitehornvoid
875217309Snwhitehorndlg_free_formitems(DIALOG_FORMITEM * items)
876217309Snwhitehorn{
877217309Snwhitehorn    int n;
878217309Snwhitehorn    for (n = 0; items[n].name != 0; ++n) {
879217309Snwhitehorn	if (items[n].name_free)
880217309Snwhitehorn	    free(items[n].name);
881217309Snwhitehorn	if (items[n].text_free)
882217309Snwhitehorn	    free(items[n].text);
883217309Snwhitehorn	if (items[n].help_free && items[n].help != dlg_strempty())
884217309Snwhitehorn	    free(items[n].help);
885217309Snwhitehorn    }
886217309Snwhitehorn    free(items);
887217309Snwhitehorn}
888217309Snwhitehorn
889217309Snwhitehorn/*
890217309Snwhitehorn * The script accepts values beginning at 1, while curses starts at 0.
891217309Snwhitehorn */
892217309Snwhitehornint
893217309Snwhitehorndlg_ordinate(const char *s)
894217309Snwhitehorn{
895217309Snwhitehorn    int result = atoi(s);
896217309Snwhitehorn    if (result > 0)
897217309Snwhitehorn	--result;
898217309Snwhitehorn    else
899217309Snwhitehorn	result = 0;
900217309Snwhitehorn    return result;
901217309Snwhitehorn}
902217309Snwhitehorn
903217309Snwhitehornint
904217309Snwhitehorndialog_form(const char *title,
905217309Snwhitehorn	    const char *cprompt,
906217309Snwhitehorn	    int height,
907217309Snwhitehorn	    int width,
908217309Snwhitehorn	    int form_height,
909217309Snwhitehorn	    int item_no,
910217309Snwhitehorn	    char **items)
911217309Snwhitehorn{
912217309Snwhitehorn    int result;
913217309Snwhitehorn    int choice;
914217309Snwhitehorn    int i;
915217309Snwhitehorn    DIALOG_FORMITEM *listitems;
916217309Snwhitehorn    DIALOG_VARS save_vars;
917217309Snwhitehorn    bool show_status = FALSE;
918255852Sdteske    char *help_result;
919217309Snwhitehorn
920217309Snwhitehorn    dlg_save_vars(&save_vars);
921217309Snwhitehorn    dialog_vars.separate_output = TRUE;
922217309Snwhitehorn
923217309Snwhitehorn    listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
924217309Snwhitehorn    assert_ptr(listitems, "dialog_form");
925217309Snwhitehorn
926217309Snwhitehorn    for (i = 0; i < item_no; ++i) {
927217309Snwhitehorn	listitems[i].type = dialog_vars.formitem_type;
928217309Snwhitehorn	listitems[i].name = ItemName(i);
929217309Snwhitehorn	listitems[i].name_len = (int) strlen(ItemName(i));
930217309Snwhitehorn	listitems[i].name_y = dlg_ordinate(ItemNameY(i));
931217309Snwhitehorn	listitems[i].name_x = dlg_ordinate(ItemNameX(i));
932217309Snwhitehorn	listitems[i].text = ItemText(i);
933217309Snwhitehorn	listitems[i].text_len = (int) strlen(ItemText(i));
934217309Snwhitehorn	listitems[i].text_y = dlg_ordinate(ItemTextY(i));
935217309Snwhitehorn	listitems[i].text_x = dlg_ordinate(ItemTextX(i));
936217309Snwhitehorn	listitems[i].text_flen = atoi(ItemTextFLen(i));
937217309Snwhitehorn	listitems[i].text_ilen = atoi(ItemTextILen(i));
938217309Snwhitehorn	listitems[i].help = ((dialog_vars.item_help)
939217309Snwhitehorn			     ? ItemHelp(i)
940217309Snwhitehorn			     : dlg_strempty());
941217309Snwhitehorn    }
942217309Snwhitehorn
943217309Snwhitehorn    result = dlg_form(title,
944217309Snwhitehorn		      cprompt,
945217309Snwhitehorn		      height,
946217309Snwhitehorn		      width,
947217309Snwhitehorn		      form_height,
948217309Snwhitehorn		      item_no,
949217309Snwhitehorn		      listitems,
950217309Snwhitehorn		      &choice);
951217309Snwhitehorn
952217309Snwhitehorn    switch (result) {
953217309Snwhitehorn    case DLG_EXIT_OK:		/* FALLTHRU */
954217309Snwhitehorn    case DLG_EXIT_EXTRA:
955217309Snwhitehorn	show_status = TRUE;
956217309Snwhitehorn	break;
957217309Snwhitehorn    case DLG_EXIT_HELP:
958255852Sdteske	dlg_add_help_formitem(&result, &help_result, &listitems[choice]);
959217309Snwhitehorn	show_status = dialog_vars.help_status;
960255852Sdteske	dlg_add_string(help_result);
961217309Snwhitehorn	if (show_status)
962217309Snwhitehorn	    dlg_add_separator();
963217309Snwhitehorn	break;
964217309Snwhitehorn    }
965217309Snwhitehorn    if (show_status) {
966217309Snwhitehorn	for (i = 0; i < item_no; i++) {
967217309Snwhitehorn	    if (listitems[i].text_flen > 0) {
968217309Snwhitehorn		dlg_add_string(listitems[i].text);
969217309Snwhitehorn		dlg_add_separator();
970217309Snwhitehorn	    }
971217309Snwhitehorn	}
972251843Sbapt	dlg_add_last_key(-1);
973217309Snwhitehorn    }
974217309Snwhitehorn
975217309Snwhitehorn    dlg_free_formitems(listitems);
976217309Snwhitehorn    dlg_restore_vars(&save_vars);
977217309Snwhitehorn
978217309Snwhitehorn    return result;
979217309Snwhitehorn}
980