]> Pileus Git - lackey/blob - src/form.h
Make form field types separate structures
[lackey] / src / form.h
1 /*
2  * Copyright (C) 2015 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /* Widget Macros */
19 #define TEXT(k,...)    {.f.type=FORM_TEXT,   .f.hotkey=k, __VA_ARGS__}
20 #define NUMBER(k,...)  {.f.type=FORM_NUMBER, .f.hotkey=k, __VA_ARGS__}
21 #define DATE(k,...)    {.f.type=FORM_DATE,   .f.hotkey=k, __VA_ARGS__}
22 #define BUTTON(k,...)  {.f.type=FORM_BUTTON, .f.hotkey=k, __VA_ARGS__}
23 #define LIST(k,...)    {.f.type=FORM_LIST,   .f.hotkey=k, __VA_ARGS__}
24
25 #define HEAD(t,...)    &(form_field_t){.type=FORM_LABEL, .label=t, .attr.bold=1}
26 #define LABEL(t,...)   &(form_field_t){.type=FORM_LABEL, .label=t}
27
28 #define CHECK(t,...)   {.f.type=FORM_BUTTON, .f.label=t}
29 #define TOGGLE(t,...)  {.f.type=FORM_BUTTON, .f.label=t}
30 #define RADIO(t,...)   {.f.type=FORM_BUTTON, .f.label=t}
31 #define BUTTONS(t,...) {.f.type=FORM_BUTTON, .f.label=t} // LIST[buttons]
32
33 /* Widget types */
34 typedef enum {
35         FORM_LABEL,
36         FORM_TEXT,
37         FORM_DATE,
38         FORM_NUMBER,
39         FORM_BUTTON,
40         FORM_LIST
41 } form_type_t;
42
43 /* Text attributes */
44 typedef struct {
45         int bold : 1;
46 } form_attr_t;
47
48 /* Form header */
49 typedef struct {
50         form_type_t type;
51         form_attr_t attr;
52         int         hotkey;
53         char       *before;
54         char       *label;
55         char       *after;
56 } form_field_t;
57
58 /* Form types */
59 typedef struct {
60         form_field_t f;
61         char   *text;
62 } form_text_t;
63
64 typedef struct {
65         form_field_t f;
66         date_t  date;
67 } form_date_t;
68
69 typedef struct {
70         form_field_t f;
71         int     number;
72 } form_number_t;
73
74 typedef struct {
75         form_field_t f;
76         int     button;
77 } form_button_t;
78
79 typedef struct {
80         form_field_t f;
81         char **map;
82         int    num;
83         int    idx;
84 } form_list_t;
85
86 /* Form structure */
87 typedef struct {
88         int rows;
89         int cols;
90         form_field_t *fields[][10];
91 } form_t;
92
93 void form_init(void);
94
95 int form_run(form_t *form, int key, mmask_t btn, int row, int col);