xref: /freebsd/contrib/dialog/mixedform.c (revision 730cecb05aaf016ac52ef7cfc691ccec3a0408cd)
1 /*
2  *  $Id: mixedform.c,v 1.8 2010/04/28 20:54:11 tom Exp $
3  *
4  *  formbox.c -- implements the form (i.e, some pairs label/editbox)
5  *
6  *  Copyright 2007-2008,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 inspired by a patch from Kiran Cherupally
24  *  (but different interface design).
25  */
26 
27 #include <dialog.h>
28 
29 #define LLEN(n) ((n) * MIXEDFORM_TAGS)
30 
31 #define ItemName(i)     items[LLEN(i) + 0]
32 #define ItemNameY(i)    items[LLEN(i) + 1]
33 #define ItemNameX(i)    items[LLEN(i) + 2]
34 #define ItemText(i)     items[LLEN(i) + 3]
35 #define ItemTextY(i)    items[LLEN(i) + 4]
36 #define ItemTextX(i)    items[LLEN(i) + 5]
37 #define ItemTextFLen(i) items[LLEN(i) + 6]
38 #define ItemTextILen(i) items[LLEN(i) + 7]
39 #define ItemTypep(i)    items[LLEN(i) + 8]
40 #define ItemHelp(i)     (dialog_vars.item_help ? items[LLEN(i) + 9] : dlg_strempty())
41 
42 int
43 dialog_mixedform(const char *title,
44 		 const char *cprompt,
45 		 int height,
46 		 int width,
47 		 int form_height,
48 		 int item_no,
49 		 char **items)
50 {
51     int result;
52     int choice;
53     int i;
54     DIALOG_FORMITEM *listitems;
55     DIALOG_VARS save_vars;
56     bool show_status = FALSE;
57 
58     dlg_save_vars(&save_vars);
59     dialog_vars.separate_output = TRUE;
60 
61     listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
62     assert_ptr(listitems, "dialog_mixedform");
63 
64     for (i = 0; i < item_no; ++i) {
65 	listitems[i].type = dialog_vars.formitem_type;
66 	listitems[i].name = ItemName(i);
67 	listitems[i].name_len = (int) strlen(ItemName(i));
68 	listitems[i].name_y = dlg_ordinate(ItemNameY(i));
69 	listitems[i].name_x = dlg_ordinate(ItemNameX(i));
70 	listitems[i].text = ItemText(i);
71 	listitems[i].text_len = (int) strlen(ItemText(i));
72 	listitems[i].text_y = dlg_ordinate(ItemTextY(i));
73 	listitems[i].text_x = dlg_ordinate(ItemTextX(i));
74 	listitems[i].text_flen = atoi(ItemTextFLen(i));
75 	listitems[i].text_ilen = atoi(ItemTextILen(i));
76 	listitems[i].help = (dialog_vars.item_help ? ItemHelp(i) :
77 			     dlg_strempty());
78 	listitems[i].type = (unsigned) atoi(ItemTypep(i));
79     }
80 
81     result = dlg_form(title,
82 		      cprompt,
83 		      height,
84 		      width,
85 		      form_height,
86 		      item_no,
87 		      listitems,
88 		      &choice);
89 
90     switch (result) {
91     case DLG_EXIT_OK:		/* FALLTHRU */
92     case DLG_EXIT_EXTRA:
93 	show_status = TRUE;
94 	break;
95     case DLG_EXIT_HELP:
96 	dlg_add_result("HELP ");
97 	show_status = dialog_vars.help_status;
98 	if (USE_ITEM_HELP(listitems[choice].help)) {
99 	    dlg_add_string(listitems[choice].help);
100 	    result = DLG_EXIT_ITEM_HELP;
101 	} else {
102 	    dlg_add_string(listitems[choice].name);
103 	}
104 	if (show_status)
105 	    dlg_add_separator();
106 	break;
107     }
108     if (show_status) {
109 	for (i = 0; i < item_no; i++) {
110 	    if (listitems[i].text_flen > 0) {
111 		dlg_add_string(listitems[i].text);
112 		dlg_add_separator();
113 	    }
114 	}
115     }
116 
117     dlg_free_formitems(listitems);
118     dlg_restore_vars(&save_vars);
119 
120     return result;
121 }
122