From: John Tsiombikas Date: Mon, 22 Nov 2021 23:35:19 +0000 (+0200) Subject: theme X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=commitdiff_plain;h=b012c112e391964e63247dcbb55988855b0fc95e;p=windtk theme --- diff --git a/src/widget.h b/src/widget.h index 0a978cf..c174f43 100644 --- a/src/widget.h +++ b/src/widget.h @@ -16,11 +16,13 @@ struct wt_widget { wt_callback_func cb[WT_NUM_CALLBACKS]; void *cbcls[WT_NUM_CALLBACKS]; - void (*draw)(struct wt_widget *w); + wt_draw_func draw; void (*click)(struct wt_widget *w); void (*keypress)(struct wt_widget *w, int key); void (*mbutton)(struct wt_widget *w, int bn, int st, int x, int y); void (*mmotion)(struct wt_widget *w, int x, int y); + + void (*use_theme)(struct wt_widget *w, struct wt_theme *theme); }; #endif /* WIDGET_H_ */ diff --git a/src/window.c b/src/window.c index a103ed4..d0d8d74 100644 --- a/src/window.c +++ b/src/window.c @@ -1,6 +1,7 @@ #include "wtimpl.h" -static void draw_win(wt_widget *w); +static void draw_win(wt_widget *w, struct wt_graphics *gfx); +static void use_theme(wt_widget *w, struct wt_theme *theme); wt_widget *wt_window(wt_widget *par, const char *title, int style, int x, int y, int xsz, int ysz) { @@ -15,12 +16,17 @@ wt_widget *wt_window(wt_widget *par, const char *title, int style, int x, int y, wt_resize(w, xsz, ysz); /* TODO: style */ - w->draw = draw_win; + use_theme(w, wt->theme); return w; } -static void draw_win(wt_widget *w) +static void draw_win(wt_widget *w, struct wt_graphics *gfx) { wt_gfx_color(COL_BG); wt_gfx_fillrect(&w->rect); } + +static void use_theme(wt_widget *w, struct wt_theme *theme) +{ + w->draw = theme && theme->draw_window ? theme->draw_window : draw_win; +} diff --git a/src/windtk.c b/src/windtk.c index ea916c3..d6b4d64 100644 --- a/src/windtk.c +++ b/src/windtk.c @@ -57,6 +57,34 @@ void wt_destroy(void) wt->root = 0; } +struct wt_theme *wt_load_theme(const char *path) +{ + return 0; /* TODO */ +} + +void wt_unload_theme(struct wt_theme *theme) +{ +} + +static void use_theme(wt_widget *w, struct wt_theme *theme) +{ + int i; + + if(w->use_theme) { + w->use_theme(w, theme); + } + + for(i=0; inum_child; i++) { + use_theme(w->child[i], theme); + } +} + +void wt_use_theme(struct wt_theme *theme) +{ + wt->theme = theme; + use_theme(wt->root, theme); +} + void wt_viewport(int x, int y, int w, int h) { wt_setrect(&wt->vp, x, y, w, h); @@ -96,7 +124,7 @@ void wt_draw_tree(wt_widget *tree) int i; if(tree->draw) { - tree->draw(tree); + tree->draw(tree, &wt->gfx); } for(i=0; inum_child; i++) { diff --git a/src/windtk.h b/src/windtk.h index 6b6724c..56535c9 100644 --- a/src/windtk.h +++ b/src/windtk.h @@ -68,11 +68,29 @@ struct wt_graphics { int (*baseline)(int font); }; +typedef void (*wt_draw_func)(wt_widget*, struct wt_graphics*); + +struct wt_theme { + char *name; + void *so; + wt_draw_func draw_window; + wt_draw_func draw_label; + wt_draw_func draw_button; + wt_draw_func draw_checkbox; + wt_draw_func draw_textfield; + + struct wt_theme *next; +}; + void wt_allocator(void *(*allocfunc)(size_t), void (*freefunc)(void*)); int wt_init(int w, int h, struct wt_graphics *gfx); void wt_destroy(void); +struct wt_theme *wt_load_theme(const char *path); /* load dynamically, where applicable */ +void wt_unload_theme(struct wt_theme *theme); +void wt_use_theme(struct wt_theme *theme); + void wt_viewport(int x, int y, int w, int h); void wt_graphics(struct wt_graphics *gfx); diff --git a/src/wtimpl.h b/src/wtimpl.h index d9f3410..f4956ac 100644 --- a/src/wtimpl.h +++ b/src/wtimpl.h @@ -26,6 +26,7 @@ struct wt_context { struct wt_graphics gfx; struct wt_rect vp; wt_widget *root; + struct wt_theme *theme; int colors[NUM_COLORS]; };