xref: /freebsd/contrib/bsddialog/examples_library/mixedlist.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*-
2  * SPDX-License-Identifier: CC0-1.0
3  *
4  * Written in 2021 by Alfonso Sabato Siciliano.
5  * To the extent possible under law, the author has dedicated all copyright
6  * and related and neighboring rights to this software to the public domain
7  * worldwide. This software is distributed without any warranty, see:
8  *   <http://creativecommons.org/publicdomain/zero/1.0/>.
9  */
10 
11 #include <bsddialog.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 int main()
16 {
17 	int output;
18 	unsigned int i, j;
19 	struct bsddialog_conf conf;
20 	struct bsddialog_menuitem item;
21 	struct bsddialog_menuitem sep1[1] = {
22 	    { "", true, 0, "Checklist", "(desc)", "" }
23 	};
24 	struct bsddialog_menuitem check[5] = {
25 	    { "+", true,  0, "Name 1", "Desc 1", "Bottom Desc 1" },
26 	    { "" , false, 0, "Name 2", "Desc 2", "Bottom Desc 2" },
27 	    { "+", true,  0, "Name 3", "Desc 3", "Bottom Desc 3" },
28 	    { "" , false, 0, "Name 4", "Desc 4", "Bottom Desc 4" },
29 	    { "+", true,  0, "Name 5", "Desc 5", "Bottom Desc 5" }
30 	};
31 	struct bsddialog_menuitem sep2[2] = {
32 	    { "", true, 0, "Radiolist", "(desc)", "" },
33 	    { "", true, 0, "Subtitle",  "(desc)", "" }
34 	};
35 	struct bsddialog_menuitem radio[5] = {
36 	    { "",  true,  0, "Name 1", "Desc 1", "Bottom Desc 1" },
37 	    { "+", false, 0, "Name 2", "Desc 2", "Bottom Desc 2" },
38 	    { "",  false, 0, "Name 3", "Desc 3", "Bottom Desc 3" },
39 	    { "+", false, 0, "Name 4", "Desc 4", "Bottom Desc 4" },
40 	    { "",  false, 0, "Name 5", "Desc 5", "Bottom Desc 5" }
41 	};
42 	struct bsddialog_menugroup group[4] = {
43 	    { BSDDIALOG_SEPARATOR, 1, sep1  },
44 	    { BSDDIALOG_CHECKLIST, 5, check },
45 	    { BSDDIALOG_SEPARATOR, 2, sep2  },
46 	    { BSDDIALOG_RADIOLIST, 5, radio }
47 	};
48 
49 	if (bsddialog_init() == BSDDIALOG_ERROR) {
50 		printf("Error: %s\n", bsddialog_geterror());
51 		return (1);
52 	}
53 
54 	bsddialog_initconf(&conf);
55 	conf.title = "mixedlist";
56 	output = bsddialog_mixedlist(&conf, "Example", 20, 0, 13, 4, group,
57 	    NULL, NULL);
58 
59 	bsddialog_end();
60 
61 	if (output == BSDDIALOG_ERROR) {
62 		printf("Error: %s\n", bsddialog_geterror());
63 		return (1);
64 	}
65 
66 	if (output == BSDDIALOG_CANCEL) {
67 		printf("Cancel\n");
68 		return (0);
69 	}
70 
71 	printf("Mixedlist:\n");
72 	for (i = 0; i < 4; i++) {
73 		for (j = 0; j < group[i].nitems; j++) {
74 			item = group[i].items[j];
75 			if (group[i].type == BSDDIALOG_SEPARATOR)
76 				printf("----- %s -----\n", item.name);
77 			else if (group[i].type == BSDDIALOG_RADIOLIST)
78 				printf(" (%c) %s\n",
79 				    item.on ? '*' : ' ', item.name);
80 			else /* BSDDIALOG_CHECKLIST */
81 				printf(" [%c] %s\n",
82 				    item.on ? 'X' : ' ', item.name);
83 		}
84 	}
85 
86 	return (output);
87 }