istereo2

diff libs/goatkit/theme.cc @ 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
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