istereo2

changeset 11:03cc3b1884d1

implemented builtin themes registration and lookup in goatkit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Sep 2015 06:53:06 +0300
parents 64e15874f3bd
children 57188f7d9304
files libs/goatkit/theme.cc libs/goatkit/theme.h src/ui.cc src/uitheme.cc
diffstat 4 files changed, 75 insertions(+), 27 deletions(-) [+]
line diff
     1.1 --- a/libs/goatkit/theme.cc	Sat Sep 26 02:56:07 2015 +0300
     1.2 +++ b/libs/goatkit/theme.cc	Mon Sep 28 06:53:06 2015 +0300
     1.3 @@ -70,6 +70,10 @@
     1.4  	0
     1.5  };
     1.6  
     1.7 +typedef std::map<std::string, Theme*> ThemeMap;
     1.8 +static ThemeMap *themes;
     1.9 +
    1.10 +
    1.11  void add_theme_path(const char *path)
    1.12  {
    1.13  	if(!path || !*path) return;
    1.14 @@ -87,6 +91,37 @@
    1.15  	search_paths.push_back(s);
    1.16  }
    1.17  
    1.18 +void register_theme(const char *name, Theme *theme)
    1.19 +{
    1.20 +	if(!themes) {
    1.21 +		themes = new ThemeMap;
    1.22 +	}
    1.23 +
    1.24 +	Theme *prev = (*themes)[name];
    1.25 +	if(prev) {
    1.26 +		delete prev;
    1.27 +	}
    1.28 +	(*themes)[name] = theme;
    1.29 +}
    1.30 +
    1.31 +Theme *get_theme(const char *name)
    1.32 +{
    1.33 +	// first search in the already registered themes
    1.34 +	ThemeMap::const_iterator it = themes->find(name);
    1.35 +	if(it != themes->end()) {
    1.36 +		return it->second;
    1.37 +	}
    1.38 +
    1.39 +	// then try loading it from a theme plugin
    1.40 +	Theme *theme = new Theme;
    1.41 +	if(theme->load(name)) {
    1.42 +		return theme;
    1.43 +	}
    1.44 +
    1.45 +	fprintf(stderr, "[goatkit] theme \"%s\" not found!\n", name);
    1.46 +	return 0;
    1.47 +}
    1.48 +
    1.49  Theme::Theme()
    1.50  {
    1.51  	impl = new ThemeImpl;
    1.52 @@ -94,6 +129,15 @@
    1.53  	impl->lookup_theme_draw_func = 0;
    1.54  }
    1.55  
    1.56 +Theme::Theme(const char *name, WidgetLookupFunc func)
    1.57 +{
    1.58 +	impl = new ThemeImpl;
    1.59 +	impl->so = 0;
    1.60 +	impl->lookup_theme_draw_func = func;
    1.61 +
    1.62 +	register_theme(name, this);
    1.63 +}
    1.64 +
    1.65  Theme::~Theme()
    1.66  {
    1.67  	unload();
    1.68 @@ -106,34 +150,30 @@
    1.69  {
    1.70  	unload();
    1.71  
    1.72 -	if(strcmp(name, "GOATKIT_THEME_BUILTIN") == 0) {
    1.73 -		impl->so = RTLD_DEFAULT;
    1.74 -	} else {
    1.75 -		std::string fname = std::string(name) + ".gtheme";
    1.76 -		if(!(impl->so = dlopen(fname.c_str(), RTLD_LAZY))) {
    1.77 -			for(size_t i=0; i<search_paths.size(); i++) {
    1.78 -				std::string path = search_paths[i] + "/" + fname;
    1.79 +	std::string fname = std::string(name) + ".gtheme";
    1.80 +	if(!(impl->so = dlopen(fname.c_str(), RTLD_LAZY))) {
    1.81 +		for(size_t i=0; i<search_paths.size(); i++) {
    1.82 +			std::string path = search_paths[i] + "/" + fname;
    1.83 +
    1.84 +			if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) {
    1.85 +				break;
    1.86 +			}
    1.87 +		}
    1.88 +
    1.89 +		// try the fallback paths
    1.90 +		if(!impl->so) {
    1.91 +			for(int i=0; fallback_paths[i]; i++) {
    1.92 +				std::string path = std::string(fallback_paths[i]) + "/" + fname;
    1.93  
    1.94  				if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) {
    1.95  					break;
    1.96  				}
    1.97  			}
    1.98 +		}
    1.99  
   1.100 -			// try the fallback paths
   1.101 -			if(!impl->so) {
   1.102 -				for(int i=0; fallback_paths[i]; i++) {
   1.103 -					std::string path = std::string(fallback_paths[i]) + "/" + fname;
   1.104 -
   1.105 -					if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) {
   1.106 -						break;
   1.107 -					}
   1.108 -				}
   1.109 -			}
   1.110 -
   1.111 -			if(!impl->so) {
   1.112 -				fprintf(stderr, "%s: failed to load theme plugin: %s\n", __func__, name);
   1.113 -				return false;
   1.114 -			}
   1.115 +		if(!impl->so) {
   1.116 +			fprintf(stderr, "%s: failed to load theme plugin: %s\n", __func__, name);
   1.117 +			return false;
   1.118  		}
   1.119  	}
   1.120  
   1.121 @@ -145,6 +185,7 @@
   1.122  		return false;
   1.123  	}
   1.124  
   1.125 +	register_theme(name, this);
   1.126  	return true;
   1.127  }
   1.128  
     2.1 --- a/libs/goatkit/theme.h	Sat Sep 26 02:56:07 2015 +0300
     2.2 +++ b/libs/goatkit/theme.h	Mon Sep 28 06:53:06 2015 +0300
     2.3 @@ -18,18 +18,23 @@
     2.4  #ifndef THEME_H_
     2.5  #define THEME_H_
     2.6  
     2.7 -#define GOATKIT_THEME_BUILTIN	"GOATKIT_THEME_BUILTIN"
     2.8 +#define GOATKIT_BUILTIN_THEME(n, f)	\
     2.9 +	static goatkit::Theme goatkit_theme##__LINE__(n, f)
    2.10  
    2.11  namespace goatkit {
    2.12  
    2.13  class Widget;
    2.14 +class Theme;
    2.15 +struct ThemeImpl;
    2.16  
    2.17  typedef void (*WidgetDrawFunc)(const Widget*);
    2.18 +typedef WidgetDrawFunc (*WidgetLookupFunc)(const char*);
    2.19  
    2.20  void add_theme_path(const char *path);
    2.21  void default_draw_func(const Widget *w);
    2.22  
    2.23 -struct ThemeImpl;
    2.24 +void register_theme(const char *name, Theme *theme);
    2.25 +Theme *get_theme(const char *name);
    2.26  
    2.27  class Theme {
    2.28  private:
    2.29 @@ -37,6 +42,7 @@
    2.30  
    2.31  public:
    2.32  	Theme();
    2.33 +	Theme(const char *name, WidgetLookupFunc func);
    2.34  	~Theme();
    2.35  
    2.36  	bool load(const char *name);
     3.1 --- a/src/ui.cc	Sat Sep 26 02:56:07 2015 +0300
     3.2 +++ b/src/ui.cc	Mon Sep 28 06:53:06 2015 +0300
     3.3 @@ -66,9 +66,7 @@
     3.4  	// load the theme
     3.5  	//goatkit::add_theme_path("themes/simple");
     3.6  
     3.7 -	goatkit::theme = new goatkit::Theme;
     3.8 -	if(!goatkit::theme->load(GOATKIT_THEME_BUILTIN)) {
     3.9 -		fprintf(stderr, "no builitn theme\n");
    3.10 +	if(!(goatkit::theme = goatkit::get_theme("istereo"))) {
    3.11  		return -1;
    3.12  	}
    3.13  
     4.1 --- a/src/uitheme.cc	Sat Sep 26 02:56:07 2015 +0300
     4.2 +++ b/src/uitheme.cc	Mon Sep 28 06:53:06 2015 +0300
     4.3 @@ -42,6 +42,9 @@
     4.4  	return funcmap[name];
     4.5  }
     4.6  
     4.7 +// register ourselves as a built-in theme
     4.8 +GOATKIT_BUILTIN_THEME("istereo", get_widget_func);
     4.9 +
    4.10  static void begin_drawing(const Widget *w)
    4.11  {
    4.12  	Vec2 pos = w->get_position();