From 487b4f073cfbec280ed91e001c51cb32814ee4b5 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Tue, 23 Nov 2021 01:06:06 +0200 Subject: [PATCH] wt_draw --- example.c | 3 +++ src/widget.c | 7 ++++--- src/windtk.c | 19 +++++++++++++++++++ src/windtk.h | 8 +++++--- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/example.c b/example.c index 2d13dc5..8e7c8e7 100644 --- a/example.c +++ b/example.c @@ -39,6 +39,7 @@ int main(int argc, char **argv) if(wt_init(800, 600, &gfx)) { return 1; } + wt_window(0, "foo", WT_WS_DEFAULT, 10, 10, 200, 200); glutMainLoop(); return 0; @@ -48,6 +49,8 @@ static void display(void) { glClear(GL_COLOR_BUFFER_BIT); + wt_draw(); + glutSwapBuffers(); } diff --git a/src/widget.c b/src/widget.c index 07643db..96be755 100644 --- a/src/widget.c +++ b/src/widget.c @@ -10,9 +10,8 @@ wt_widget *wt_alloc_widget(wt_widget *par) } w->type = WT_TYPE_WIDGET; - if(par) { - wt_add_child(par, w); - } + if(!par) par = wt->root; + wt_add_child(par, w); return w; } @@ -73,6 +72,8 @@ static int find_child(wt_widget *w, wt_widget *c) int wt_add_child(wt_widget *w, wt_widget *c) { + if(!w || !c) return -1; + if(find_child(w, c) != -1) { return 0; } diff --git a/src/windtk.c b/src/windtk.c index 1e4de86..ea916c3 100644 --- a/src/windtk.c +++ b/src/windtk.c @@ -41,6 +41,7 @@ void wt_allocator(void *(*allocfunc)(size_t), void (*freefunc)(void *p)) int wt_init(int w, int h, struct wt_graphics *gfx) { + wt->root = 0; if(!(wt->root = wt_alloc_widget(0))) { return -1; } @@ -90,6 +91,24 @@ void wt_inp_motion(int x, int y) { } +void wt_draw_tree(wt_widget *tree) +{ + int i; + + if(tree->draw) { + tree->draw(tree); + } + + for(i=0; inum_child; i++) { + wt_draw_tree(tree->child[i]); + } +} + +void wt_draw(void) +{ + wt_draw_tree(wt->root); +} + void wt_gfx_color(int cidx) { wt->gfx.color(wt->colors[cidx]); diff --git a/src/windtk.h b/src/windtk.h index 56ca8a6..6b6724c 100644 --- a/src/windtk.h +++ b/src/windtk.h @@ -80,6 +80,8 @@ void wt_inp_key(int key, int press); void wt_inp_mouse(int bn, int st, int x, int y); void wt_inp_motion(int x, int y); +void wt_draw(void); + wt_widget *wt_alloc_widget(wt_widget *par); void wt_free_widget(wt_widget *w); void wt_free_tree(wt_widget *tree); @@ -111,14 +113,14 @@ void wt_resize(wt_widget *w, int x, int y); int *wt_position(wt_widget *w, int *xret, int *yret); int *wt_size(wt_widget *w, int *xret, int *yret); +int wt_hittest(wt_widget *w, int x, int y); +wt_widget *wt_widget_at(int x, int y); + void wt_layout(wt_widget *w, int layout); void wt_padding(wt_widget *w, int pad); /* calculates layout of child widgets and updates dimensions */ void wt_relayout(wt_widget *w); -int wt_hittest(wt_widget *w, int x, int y); -wt_widget *wt_widget_at(int x, int y); - void wt_focus(wt_widget *w); void wt_unfocus(wt_widget *w); int wt_isfocused(wt_widget *w); -- 1.7.10.4