5eb7a29b49b08771a6e6cf54609ab2e544cb9dc8
[oftp] / src / tui.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "tui.h"
6 #include "tuipriv.h"
7 #include "tgfx.h"
8 #include "darray.h"
9
10
11 int tui_init(void)
12 {
13         return 0;
14 }
15
16 void tui_shutdown(void)
17 {
18 }
19
20
21 struct tui_widget *tui_widget(int type)
22 {
23         struct tui_widget *w;
24
25         if(!(w = calloc(1, sizeof *w))) {
26                 return 0;
27         }
28         w->type = type;
29         return w;
30 }
31
32 void tui_free(struct tui_widget *w)
33 {
34         if(w) {
35                 free(w->title);
36                 free(w);
37         }
38 }
39
40 void tui_set_callback(struct tui_widget *w, int type, tui_callback func, void *cls)
41 {
42         w->cbfunc[type] = func;
43         w->cbcls[type] = cls;
44 }
45
46 struct tui_widget *tui_window(const char *title, int x, int y, int width, int height)
47 {
48         struct tui_widget *w;
49
50         if(!(w = tui_widget(TUI_WINDOW))) {
51                 return 0;
52         }
53         w->title = strdup(title);
54         w->x = x;
55         w->y = y;
56         w->width = width;
57         w->height = height;
58         return w;
59 }
60
61 struct tui_widget *tui_button(const char *title, int x, int y, tui_callback cbfunc, void *cbdata)
62 {
63         struct tui_widget *w;
64
65         if(!(w = tui_widget(TUI_BUTTON))) {
66                 return 0;
67         }
68         w->title = strdup(title);
69         w->x = x;
70         w->y = y;
71         w->width = strlen(title) + 2;
72         w->height = 1;
73
74         if(cbfunc) {
75                 tui_set_callback(w, TUI_ONCLICK, cbfunc, cbdata);
76         }
77         return w;
78 }
79
80 struct tui_widget *tui_list(const char *title, int x, int y, int width, int height, tui_callback cbfunc, void *cbdata)
81 {
82         struct tui_list *w;
83
84         if(!(w = calloc(1, sizeof *w))) {
85                 return 0;
86         }
87         w->type = TUI_LIST;
88         w->title = strdup(title);
89         w->x = x;
90         w->y = y;
91         w->width = width;
92         w->height = height;
93
94         if(cbfunc) {
95                 tui_set_callback((struct tui_widget*)w, TUI_ONMODIFY, cbfunc, cbdata);
96         }
97         return (struct tui_widget*)w;
98 }
99
100
101 void tui_clear_list(struct tui_widget *w)
102 {
103         struct tui_list *wl = (struct tui_list*)w;
104         assert(wl->type == TUI_LIST);
105         darr_clear(wl->entries);
106 }
107
108 void tui_add_list_item(struct tui_widget *w, const char *text)
109 {
110         struct tui_list *wl = (struct tui_list*)w;
111         assert(wl->type == TUI_LIST);
112         darr_push(wl->entries, &text);
113 }