xref: /freebsd/contrib/dialog/mixedform.c (revision a0b9e2e854027e6ff61fb075a1309dbc71c42b54)
1 /*
2  *  $Id: mixedform.c,v 1.13 2018/06/15 01:23:33 tom Exp $
3  *
4  *  mixedform.c -- implements the mixed form (i.e, typed pairs label/editbox)
5  *
6  *  Copyright 2007-2013,2018	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 = 0;
53     int i;
54     DIALOG_FORMITEM *listitems;
55     DIALOG_VARS save_vars;
56     bool show_status = FALSE;
57     char *help_result;
58 
59     dlg_save_vars(&save_vars);
60     dialog_vars.separate_output = TRUE;
61 
62     listitems = dlg_calloc(DIALOG_FORMITEM, (size_t) item_no + 1);
63     assert_ptr(listitems, "dialog_mixedform");
64 
65     for (i = 0; i < item_no; ++i) {
66 	listitems[i].type = dialog_vars.formitem_type;
67 	listitems[i].name = ItemName(i);
68 	listitems[i].name_len = (int) strlen(ItemName(i));
69 	listitems[i].name_y = dlg_ordinate(ItemNameY(i));
70 	listitems[i].name_x = dlg_ordinate(ItemNameX(i));
71 	listitems[i].text = ItemText(i);
72 	listitems[i].text_len = (int) strlen(ItemText(i));
73 	listitems[i].text_y = dlg_ordinate(ItemTextY(i));
74 	listitems[i].text_x = dlg_ordinate(ItemTextX(i));
75 	listitems[i].text_flen = atoi(ItemTextFLen(i));
76 	listitems[i].text_ilen = atoi(ItemTextILen(i));
77 	listitems[i].help = (dialog_vars.item_help ? ItemHelp(i) :
78 			     dlg_strempty());
79 	listitems[i].type = (unsigned) atoi(ItemTypep(i));
80     }
81 
82     result = dlg_form(title,
83 		      cprompt,
84 		      height,
85 		      width,
86 		      form_height,
87 		      item_no,
88 		      listitems,
89 		      &choice);
90 
91     switch (result) {
92     case DLG_EXIT_OK:		/* FALLTHRU */
93     case DLG_EXIT_EXTRA:
94 	show_status = TRUE;
95 	break;
96     case DLG_EXIT_HELP:
97 	dlg_add_help_formitem(&result, &help_result, &listitems[choice]);
98 	show_status = dialog_vars.help_status;
99 	dlg_add_string(help_result);
100 	if (show_status)
101 	    dlg_add_separator();
102 	break;
103     }
104     if (show_status) {
105 	for (i = 0; i < item_no; i++) {
106 	    if (listitems[i].text_flen > 0) {
107 		dlg_add_string(listitems[i].text);
108 		dlg_add_separator();
109 	    }
110 	}
111 	dlg_add_last_key(-1);
112     }
113 
114     dlg_free_formitems(listitems);
115     dlg_restore_vars(&save_vars);
116 
117     return result;
118 }
119