106b28fa241104a55a80067e8a84f21221c6c582
[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                 if(w->cbfunc[TUI_FREE]) {
36                         w->cbfunc[TUI_FREE](w, 0);
37                 }
38                 free(w->title);
39                 free(w);
40         }
41 }
42
43 void tui_add_widget(struct tui_widget *par, struct tui_widget *w)
44 {
45         if(w->par == par) return;
46
47         w->next = par->child;
48         par->child = w;
49         w->par = par;
50 }
51
52 void tui_remove_widget(struct tui_widget *par, struct tui_widget *w)
53 {
54         struct tui_widget *iter, dummy;
55
56         if(w->par != par) {
57                 fprintf(stderr, "failed to remove widget %p from %p\n", (void*)w, (void*)par);
58                 return;
59         }
60
61         dummy.next = par->child;
62         iter = &dummy;
63         while(iter->next) {
64                 if(iter->next == w) {
65                         iter->next = w->next;
66                         break;
67                 }
68                 iter = iter->next;
69         }
70         par->child = dummy.next;
71
72         w->next = 0;
73         w->par = 0;
74 }
75
76 struct tui_widget *tui_parent(struct tui_widget *w)
77 {
78         return w->par;
79 }
80
81 void tui_draw(struct tui_widget *w)
82 {
83         struct tui_widget *iter;
84
85         if(w->cbfunc[TUI_DRAW]) {
86                 w->cbfunc[TUI_DRAW](w, 0);
87         }
88
89         iter = w->child;
90         while(iter) {
91                 tui_draw(iter);
92                 iter = iter->next;
93         }
94
95         tg_redraw();
96 }
97
98 void tui_set_callback(struct tui_widget *w, int type, tui_callback func, void *cls)
99 {
100         w->cbfunc[type] = func;
101         w->cbcls[type] = cls;
102 }
103
104 struct tui_widget *tui_window(const char *title, int x, int y, int width, int height)
105 {
106         struct tui_widget *w;
107
108         if(!(w = tui_widget(TUI_WINDOW))) {
109                 return 0;
110         }
111         w->title = strdup(title);
112         w->x = x;
113         w->y = y;
114         w->width = width;
115         w->height = height;
116         return w;
117 }
118
119 struct tui_widget *tui_button(const char *title, int x, int y, tui_callback cbfunc, void *cbdata)
120 {
121         struct tui_widget *w;
122
123         if(!(w = tui_widget(TUI_BUTTON))) {
124                 return 0;
125         }
126         w->title = strdup(title);
127         w->x = x;
128         w->y = y;
129         w->width = strlen(title) + 2;
130         w->height = 1;
131
132         if(cbfunc) {
133                 tui_set_callback(w, TUI_ONCLICK, cbfunc, cbdata);
134         }
135         return w;
136 }
137
138 void tui_wtoscr(struct tui_widget *w, int x, int y, int *retx, int *rety)
139 {
140         while(w) {
141                 x += w->x;
142                 y += w->y;
143                 w = w->par;
144         }
145         *retx = x;
146         *rety = y;
147 }
148
149 void tui_scrtow(struct tui_widget *w, int x, int y, int *retx, int *rety)
150 {
151         while(w) {
152                 x -= w->x;
153                 y -= w->y;
154                 w = w->par;
155         }
156         *retx = x;
157         *rety = y;
158 }
159
160
161 void tui_status(int type, const char *fmt, ...)
162 {
163         va_list ap;
164         va_start(ap, fmt);
165         tui_vstatus(type, fmt, ap);
166         va_end(ap);
167 }
168
169 void tui_vstatus(int type, const char *fmt, va_list ap)
170 {
171         /* TODO */
172         tg_color(15);
173         tg_bgcolor(0);
174         tg_vtext(0, 25, fmt, ap);
175 }
176
177 void tui_msgbox(int type, const char *title, const char *msg, ...)
178 {
179         va_list ap;
180         va_start(ap, msg);
181         tui_vmsgbox(type, title, msg, ap);
182         va_end(ap);
183 }
184
185 void tui_vmsgbox(int type, const char *title, const char *msg, va_list ap)
186 {
187         /* TODO */
188         tg_color(15);
189         tg_bgcolor(0);
190         tg_vtext(0, 25, msg, ap);
191 }