]> Pileus Git - lackey/blob - src/form.h
Add basic forms
[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,...)    {.type=FORM_TEXT,   .hotkey=k, __VA_ARGS__}
20 #define NUMBER(k,...)  {.type=FORM_NUMBER, .hotkey=k, __VA_ARGS__}
21 #define DATE(k,...)    {.type=FORM_DATE,   .hotkey=k, __VA_ARGS__}
22 #define BUTTON(k,...)  {.type=FORM_BUTTON, .hotkey=k, __VA_ARGS__}
23 #define LIST(k,...)    {.type=FORM_LIST,   .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,...)     {.type=FORM_BUTTON, .label=t}
29 #define TOGGLE(t,...)    {.type=FORM_BUTTON, .label=t}
30 #define RADIO(t,...)     {.type=FORM_BUTTON, .label=t}
31 #define BUTTONS(t,...)   {.type=FORM_BUTTON, .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 data type */
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         union {
57                 char    text[256];
58                 date_t  date;
59                 int     number;
60                 int     button;
61                 struct {
62                         char **map;
63                         int    num;
64                         int    idx;
65                 } list;
66         };
67 } form_field_t;
68
69 typedef struct {
70         int rows;
71         int cols;
72         form_field_t *fields[][10];
73 } form_t;
74
75 void form_init(void);
76
77 int form_run(form_t *form, int key, mmask_t btn, int row, int col);