progress
[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", w, 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
96 void tui_set_callback(struct tui_widget *w, int type, tui_callback func, void *cls)
97 {
98         w->cbfunc[type] = func;
99         w->cbcls[type] = cls;
100 }
101
102 struct tui_widget *tui_window(const char *title, int x, int y, int width, int height)
103 {
104         struct tui_widget *w;
105
106         if(!(w = tui_widget(TUI_WINDOW))) {
107                 return 0;
108         }
109         w->title = strdup(title);
110         w->x = x;
111         w->y = y;
112         w->width = width;
113         w->height = height;
114         return w;
115 }
116
117 struct tui_widget *tui_button(const char *title, int x, int y, tui_callback cbfunc, void *cbdata)
118 {
119         struct tui_widget *w;
120
121         if(!(w = tui_widget(TUI_BUTTON))) {
122                 return 0;
123         }
124         w->title = strdup(title);
125         w->x = x;
126         w->y = y;
127         w->width = strlen(title) + 2;
128         w->height = 1;
129
130         if(cbfunc) {
131                 tui_set_callback(w, TUI_ONCLICK, cbfunc, cbdata);
132         }
133         return w;
134 }
135
136 void tui_wtoscr(struct tui_widget *w, int x, int y, int *retx, int *rety)
137 {
138         while(w) {
139                 x += w->x;
140                 y += w->y;
141                 w = w->par;
142         }
143         *retx = x;
144         *rety = y;
145 }
146
147 void tui_scrtow(struct tui_widget *w, int x, int y, int *retx, int *rety)
148 {
149         while(w) {
150                 x -= w->x;
151                 y -= w->y;
152                 w = w->par;
153         }
154         *retx = x;
155         *rety = y;
156 }
157
158
159 void tui_status(int type, const char *fmt, ...)
160 {
161         va_list ap;
162         va_start(ap, fmt);
163         tui_vstatus(type, fmt, ap);
164         va_end(ap);
165 }
166
167 void tui_vstatus(int type, const char *fmt, va_list ap)
168 {
169         /* TODO */
170         tg_color(15);
171         tg_bgcolor(0);
172         tg_vtext(0, 25, fmt, ap);
173 }
174
175 void tui_msgbox(int type, const char *title, const char *msg, ...)
176 {
177         va_list ap;
178         va_start(ap, msg);
179         tui_vmsgbox(type, title, msg, ap);
180         va_end(ap);
181 }
182
183 void tui_vmsgbox(int type, const char *title, const char *msg, va_list ap)
184 {
185         /* TODO */
186         tg_color(15);
187         tg_bgcolor(0);
188         tg_vtext(0, 25, msg, ap);
189 }