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