download, progress, improved screen updates...
[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_invalidate(struct tui_widget *w)
83 {
84         w->dirty = 1;
85 }
86
87 int tui_isdirty(struct tui_widget *w)
88 {
89         return w->dirty;
90 }
91
92 void tui_draw(struct tui_widget *w)
93 {
94         struct tui_widget *iter;
95
96         if(w->cbfunc[TUI_DRAW]) {
97                 w->cbfunc[TUI_DRAW](w, 0);
98         }
99         w->dirty = 0;
100
101         iter = w->child;
102         while(iter) {
103                 tui_draw(iter);
104                 iter = iter->next;
105         }
106
107         tg_redraw();
108 }
109
110 void tui_set_callback(struct tui_widget *w, int type, tui_callback func, void *cls)
111 {
112         w->cbfunc[type] = func;
113         w->cbcls[type] = cls;
114 }
115
116 void tui_call_callback(struct tui_widget *w, int type)
117 {
118         if(w->cbfunc[type]) {
119                 w->cbfunc[type](w, w->cbcls[type]);
120         }
121 }
122
123 void tui_focus(struct tui_widget *w, int focus)
124 {
125         focus = focus ? 1 : 0;
126         if(w->focus == focus) {
127                 return;
128         }
129         w->focus = focus;
130         w->dirty = 1;
131         tui_call_callback(w, TUI_ONFOCUS);
132 }
133
134 int tui_set_title(struct tui_widget *w, const char *s)
135 {
136         free(w->title);
137         w->title = strdup_nf(s);
138         return 0;
139 }
140
141 const char *tui_get_title(struct tui_widget *w)
142 {
143         return w->title;
144 }
145
146 struct tui_widget *tui_window(const char *title, int x, int y, int width, int height)
147 {
148         struct tui_widget *w;
149
150         if(!(w = tui_widget(TUI_WINDOW))) {
151                 return 0;
152         }
153         w->title = strdup(title);
154         w->x = x;
155         w->y = y;
156         w->width = width;
157         w->height = height;
158         return w;
159 }
160
161 struct tui_widget *tui_button(const char *title, int x, int y, tui_callback cbfunc, void *cbdata)
162 {
163         struct tui_widget *w;
164
165         if(!(w = tui_widget(TUI_BUTTON))) {
166                 return 0;
167         }
168         w->title = strdup(title);
169         w->x = x;
170         w->y = y;
171         w->width = strlen(title) + 2;
172         w->height = 1;
173
174         if(cbfunc) {
175                 tui_set_callback(w, TUI_ONCLICK, cbfunc, cbdata);
176         }
177         return w;
178 }
179
180 void tui_wtoscr(struct tui_widget *w, int x, int y, int *retx, int *rety)
181 {
182         while(w) {
183                 x += w->x;
184                 y += w->y;
185                 w = w->par;
186         }
187         *retx = x;
188         *rety = y;
189 }
190
191 void tui_scrtow(struct tui_widget *w, int x, int y, int *retx, int *rety)
192 {
193         while(w) {
194                 x -= w->x;
195                 y -= w->y;
196                 w = w->par;
197         }
198         *retx = x;
199         *rety = y;
200 }
201
202
203 void tui_status(int type, const char *fmt, ...)
204 {
205         va_list ap;
206         va_start(ap, fmt);
207         tui_vstatus(type, fmt, ap);
208         va_end(ap);
209 }
210
211 void tui_vstatus(int type, const char *fmt, va_list ap)
212 {
213         /* TODO */
214 }
215
216 void tui_msgbox(int type, const char *title, const char *msg, ...)
217 {
218         va_list ap;
219         va_start(ap, msg);
220         tui_vmsgbox(type, title, msg, ap);
221         va_end(ap);
222 }
223
224 void tui_vmsgbox(int type, const char *title, const char *msg, va_list ap)
225 {
226         /* TODO */
227 }