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