# HG changeset patch # User John Tsiombikas # Date 1443412386 -10800 # Node ID 03cc3b1884d1773d2cb69f341b36c1e8332e2425 # Parent 64e15874f3bd4bf8381da507d9c2682a6e0c0caf implemented builtin themes registration and lookup in goatkit diff -r 64e15874f3bd -r 03cc3b1884d1 libs/goatkit/theme.cc --- a/libs/goatkit/theme.cc Sat Sep 26 02:56:07 2015 +0300 +++ b/libs/goatkit/theme.cc Mon Sep 28 06:53:06 2015 +0300 @@ -70,6 +70,10 @@ 0 }; +typedef std::map ThemeMap; +static ThemeMap *themes; + + void add_theme_path(const char *path) { if(!path || !*path) return; @@ -87,6 +91,37 @@ search_paths.push_back(s); } +void register_theme(const char *name, Theme *theme) +{ + if(!themes) { + themes = new ThemeMap; + } + + Theme *prev = (*themes)[name]; + if(prev) { + delete prev; + } + (*themes)[name] = theme; +} + +Theme *get_theme(const char *name) +{ + // first search in the already registered themes + ThemeMap::const_iterator it = themes->find(name); + if(it != themes->end()) { + return it->second; + } + + // then try loading it from a theme plugin + Theme *theme = new Theme; + if(theme->load(name)) { + return theme; + } + + fprintf(stderr, "[goatkit] theme \"%s\" not found!\n", name); + return 0; +} + Theme::Theme() { impl = new ThemeImpl; @@ -94,6 +129,15 @@ impl->lookup_theme_draw_func = 0; } +Theme::Theme(const char *name, WidgetLookupFunc func) +{ + impl = new ThemeImpl; + impl->so = 0; + impl->lookup_theme_draw_func = func; + + register_theme(name, this); +} + Theme::~Theme() { unload(); @@ -106,34 +150,30 @@ { unload(); - if(strcmp(name, "GOATKIT_THEME_BUILTIN") == 0) { - impl->so = RTLD_DEFAULT; - } else { - std::string fname = std::string(name) + ".gtheme"; - if(!(impl->so = dlopen(fname.c_str(), RTLD_LAZY))) { - for(size_t i=0; iso = dlopen(fname.c_str(), RTLD_LAZY))) { + for(size_t i=0; iso = dlopen(path.c_str(), RTLD_LAZY))) { + break; + } + } + + // try the fallback paths + if(!impl->so) { + for(int i=0; fallback_paths[i]; i++) { + std::string path = std::string(fallback_paths[i]) + "/" + fname; if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) { break; } } + } - // try the fallback paths - if(!impl->so) { - for(int i=0; fallback_paths[i]; i++) { - std::string path = std::string(fallback_paths[i]) + "/" + fname; - - if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) { - break; - } - } - } - - if(!impl->so) { - fprintf(stderr, "%s: failed to load theme plugin: %s\n", __func__, name); - return false; - } + if(!impl->so) { + fprintf(stderr, "%s: failed to load theme plugin: %s\n", __func__, name); + return false; } } @@ -145,6 +185,7 @@ return false; } + register_theme(name, this); return true; } diff -r 64e15874f3bd -r 03cc3b1884d1 libs/goatkit/theme.h --- a/libs/goatkit/theme.h Sat Sep 26 02:56:07 2015 +0300 +++ b/libs/goatkit/theme.h Mon Sep 28 06:53:06 2015 +0300 @@ -18,18 +18,23 @@ #ifndef THEME_H_ #define THEME_H_ -#define GOATKIT_THEME_BUILTIN "GOATKIT_THEME_BUILTIN" +#define GOATKIT_BUILTIN_THEME(n, f) \ + static goatkit::Theme goatkit_theme##__LINE__(n, f) namespace goatkit { class Widget; +class Theme; +struct ThemeImpl; typedef void (*WidgetDrawFunc)(const Widget*); +typedef WidgetDrawFunc (*WidgetLookupFunc)(const char*); void add_theme_path(const char *path); void default_draw_func(const Widget *w); -struct ThemeImpl; +void register_theme(const char *name, Theme *theme); +Theme *get_theme(const char *name); class Theme { private: @@ -37,6 +42,7 @@ public: Theme(); + Theme(const char *name, WidgetLookupFunc func); ~Theme(); bool load(const char *name); diff -r 64e15874f3bd -r 03cc3b1884d1 src/ui.cc --- a/src/ui.cc Sat Sep 26 02:56:07 2015 +0300 +++ b/src/ui.cc Mon Sep 28 06:53:06 2015 +0300 @@ -66,9 +66,7 @@ // load the theme //goatkit::add_theme_path("themes/simple"); - goatkit::theme = new goatkit::Theme; - if(!goatkit::theme->load(GOATKIT_THEME_BUILTIN)) { - fprintf(stderr, "no builitn theme\n"); + if(!(goatkit::theme = goatkit::get_theme("istereo"))) { return -1; } diff -r 64e15874f3bd -r 03cc3b1884d1 src/uitheme.cc --- a/src/uitheme.cc Sat Sep 26 02:56:07 2015 +0300 +++ b/src/uitheme.cc Mon Sep 28 06:53:06 2015 +0300 @@ -42,6 +42,9 @@ return funcmap[name]; } +// register ourselves as a built-in theme +GOATKIT_BUILTIN_THEME("istereo", get_widget_func); + static void begin_drawing(const Widget *w) { Vec2 pos = w->get_position();