5 wt_widget *wt_alloc_widget(wt_widget *par)
8 if(!(w = wt_zalloc(sizeof *w))) {
11 w->type = WT_TYPE_WIDGET;
14 if(!par) par = wt->root;
19 void wt_free_widget(wt_widget *w)
28 void wt_free_tree(wt_widget *tree)
34 for(i=0; i<tree->num_child; i++) {
35 wt_free_tree(tree->child[i]);
40 void wt_dirty_widget(wt_widget *w)
46 for(i=0; i<w->num_child; i++) {
47 wt_dirty_widget(w->child[i]);
51 int wt_type(wt_widget *w)
56 int wt_set_text(wt_widget *w, const char *text)
60 if(!(s = wt_alloc(strlen(text) + 1))) {
69 const char *wt_text(wt_widget *w)
74 static int find_child(wt_widget *w, wt_widget *c)
77 for(i=0; i<w->num_child; i++) {
78 if(w->child[i] == c) {
85 int wt_add_child(wt_widget *w, wt_widget *c)
87 if(!w || !c) return -1;
89 if(find_child(w, c) != -1) {
93 wt_remove_child(c->parent, c);
96 if(w->num_child >= w->max_child) {
98 int newsz = w->max_child ? w->max_child << 1 : 8;
99 if(!(newarr = wt_alloc(newsz * sizeof *w->child))) {
102 memcpy(newarr, w->child, w->num_child * sizeof *w->child);
105 w->max_child = newsz;
108 w->child[w->num_child++] = c;
112 int wt_remove_child(wt_widget *w, wt_widget *c)
116 if(!w->num_child || (idx = find_child(w, c)) == -1) {
119 w->child[idx] = w->child[--w->num_child];
121 if(w->max_child > 8 && w->num_child < w->max_child / 3) {
123 int newsz = w->max_child >> 1;
124 if(!(newarr = wt_alloc(newsz * sizeof *w->child))) {
127 memcpy(newarr, w->child, w->num_child * sizeof *w->child);
130 w->max_child = newsz;
135 wt_widget *wt_parent(wt_widget *w)
140 wt_widget *wt_widget_window(wt_widget *w)
143 while(par && par->type != WT_TYPE_WINDOW) {
149 int wt_child_count(wt_widget *w)
154 wt_widget *wt_child(wt_widget *w, int idx)
156 if(idx < 0 || idx >= w->num_child) {
159 return w->child[idx];
162 void wt_move(wt_widget *w, int x, int y)
166 /* TODO: invalidate something */
169 void wt_resize(wt_widget *w, int x, int y)
173 /* TODO: invalidate something */
176 int *wt_position(wt_widget *w, int *xret, int *yret)
178 if(xret) *xret = w->rect.x;
179 if(yret) *yret = w->rect.y;
183 int *wt_size(wt_widget *w, int *xret, int *yret)
185 if(xret) *xret = w->rect.w;
186 if(yret) *yret = w->rect.h;
190 int wt_hittest(wt_widget *w, int x, int y)
192 return x >= w->rect.x && y >= w->rect.y && x < w->rect.x + w->rect.w &&
193 y < w->rect.y + w->rect.h;
196 wt_widget *wt_widget_at(int x, int y)
201 if(!wt_hittest(wt->root, x, y)) {
210 for(i=0; i<w->num_child; i++) {
211 if(wt_hittest(w->child[i], x, y)) {
222 void wt_layout(wt_widget *w, int layout);
223 void wt_padding(wt_widget *w, int pad);
224 void wt_relayout(wt_widget *w);
226 void wt_focus(wt_widget *w);
227 void wt_unfocus(wt_widget *w);
228 int wt_isfocused(wt_widget *w);
230 void wt_hover(wt_widget *w);
231 void wt_unhover(wt_widget *w);
232 int wt_ishover(wt_widget *w);
234 void wt_enable(wt_widget *w);
235 void wt_disable(wt_widget *w);
236 int wt_isenabled(wt_widget *w);
239 void wt_callback(wt_widget *w, int type, wt_callback_func func, void *cls)
242 w->cbcls[type] = cls;