diff options
Diffstat (limited to 'src/FbTk/Theme.cc')
-rw-r--r-- | src/FbTk/Theme.cc | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/src/FbTk/Theme.cc b/src/FbTk/Theme.cc new file mode 100644 index 0000000..1e2ea3a --- /dev/null +++ b/src/FbTk/Theme.cc | |||
@@ -0,0 +1,187 @@ | |||
1 | // Theme.cc for FbTk - Fluxbox ToolKit | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: Theme.cc,v 1.1 2002/12/02 19:34:54 fluxgen Exp $ | ||
23 | |||
24 | #include "Theme.hh" | ||
25 | |||
26 | #include "../XrmDatabaseHelper.hh" | ||
27 | #include "Font.hh" | ||
28 | #include "Color.hh" | ||
29 | #include "Texture.hh" | ||
30 | #include "App.hh" | ||
31 | |||
32 | #include <iostream> | ||
33 | |||
34 | using namespace std; | ||
35 | namespace FbTk { | ||
36 | |||
37 | // create default handlers for Color, Font, and Texture | ||
38 | |||
39 | template <> | ||
40 | void ThemeItem<FbTk::Font>::setDefaultValue() { | ||
41 | if (!m_value.load("fixed")) { | ||
42 | cerr<<"FbTk::ThemeItem<FbTk::Font>: Warning! Failed to load default value 'fixed'"<<endl; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | template <> | ||
47 | void ThemeItem<FbTk::Font>::setFromString(const char *str) { | ||
48 | if (m_value.load(str) == false) { | ||
49 | cerr<<"Failed to load font: "<<str<<endl; | ||
50 | cerr<<"Setting default value"<<endl; | ||
51 | setDefaultValue(); | ||
52 | } | ||
53 | |||
54 | } | ||
55 | |||
56 | // do nothing | ||
57 | template <> | ||
58 | void ThemeItem<FbTk::Font>::load() { | ||
59 | } | ||
60 | |||
61 | |||
62 | template <> | ||
63 | void ThemeItem<FbTk::Texture>::setFromString(const char *str) { | ||
64 | m_value.setFromString(str); | ||
65 | } | ||
66 | |||
67 | template <> | ||
68 | void ThemeItem<FbTk::Texture>::setDefaultValue() { | ||
69 | m_value.setType(0); | ||
70 | } | ||
71 | |||
72 | template <> | ||
73 | void ThemeItem<FbTk::Texture>::load() { | ||
74 | string color_name(ThemeManager::instance().resourceValue(name()+".color", altName()+".Color")); | ||
75 | string colorto_name(ThemeManager::instance().resourceValue(name()+".colorTo", altName()+".ColorTo")); | ||
76 | m_value.color().setFromString(color_name.c_str(), m_tm.screenNum()); | ||
77 | m_value.colorTo().setFromString(colorto_name.c_str(), m_tm.screenNum()); | ||
78 | } | ||
79 | |||
80 | template <> | ||
81 | void ThemeItem<FbTk::Color>::setFromString(const char *str) { | ||
82 | m_value.setFromString(str, m_tm.screenNum()); | ||
83 | } | ||
84 | |||
85 | template <> | ||
86 | void ThemeItem<FbTk::Color>::setDefaultValue() { | ||
87 | m_value.setPixel(0xFFFFFFFF); | ||
88 | } | ||
89 | |||
90 | // does nothing | ||
91 | template <> | ||
92 | void ThemeItem<FbTk::Color>::load() { } | ||
93 | |||
94 | Theme::Theme(int screen_num):m_screen_num(screen_num) { | ||
95 | |||
96 | if (!ThemeManager::instance().registerTheme(*this)) { | ||
97 | // should it be fatal or not? | ||
98 | cerr<<"FbTk::Theme Warning: Failed to register Theme"<<endl; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | Theme::~Theme() { | ||
103 | if (!ThemeManager::instance().unregisterTheme(*this)) { | ||
104 | #ifdef DEBUG | ||
105 | cerr<<"Warning: Theme not registered!"<<endl; | ||
106 | #endif // DEBUG | ||
107 | } | ||
108 | } | ||
109 | |||
110 | ThemeManager &ThemeManager::instance() { | ||
111 | static ThemeManager tm; | ||
112 | return tm; | ||
113 | } | ||
114 | |||
115 | ThemeManager::ThemeManager(): | ||
116 | m_max_screens(ScreenCount(FbTk::App::instance()->display())) { | ||
117 | |||
118 | } | ||
119 | |||
120 | bool ThemeManager::registerTheme(Theme &tm) { | ||
121 | // valid screen num? | ||
122 | if (m_max_screens < tm.screenNum() || tm.screenNum() < 0) | ||
123 | return false; | ||
124 | // TODO: use find and return false if it's already there | ||
125 | // instead of unique | ||
126 | m_themelist.push_back(&tm); | ||
127 | m_themelist.unique(); | ||
128 | return true; | ||
129 | } | ||
130 | |||
131 | bool ThemeManager::unregisterTheme(Theme &tm) { | ||
132 | m_themelist.remove(&tm); | ||
133 | return true; | ||
134 | } | ||
135 | |||
136 | bool ThemeManager::load(const char *filename) { | ||
137 | |||
138 | if (!m_database.load(filename)) | ||
139 | return false; | ||
140 | |||
141 | //get list and go throu all the resources and load them | ||
142 | ThemeList::iterator theme_it = m_themelist.begin(); | ||
143 | const ThemeList::iterator theme_it_end = m_themelist.end(); | ||
144 | for (; theme_it != theme_it_end; ++theme_it) { | ||
145 | loadTheme(**theme_it); | ||
146 | } | ||
147 | |||
148 | return true; | ||
149 | } | ||
150 | |||
151 | void ThemeManager::loadTheme(Theme &tm) { | ||
152 | |||
153 | XrmValue value; | ||
154 | char *value_type; | ||
155 | |||
156 | std::list<ThemeItem_base *>::iterator i = tm.itemList().begin(); | ||
157 | std::list<ThemeItem_base *>::iterator i_end = tm.itemList().end(); | ||
158 | for (; i != i_end; ++i) { | ||
159 | ThemeItem_base *resource = *i; | ||
160 | if (XrmGetResource(*m_database, resource->name().c_str(), | ||
161 | resource->altName().c_str(), &value_type, &value)) { | ||
162 | resource->setFromString(value.addr); | ||
163 | resource->load(); // load additional stuff by the ThemeItem | ||
164 | } else { | ||
165 | cerr<<"Failed to read theme item: "<<resource->name()<<endl; | ||
166 | cerr<<"Setting default value"<<endl; | ||
167 | resource->setDefaultValue(); | ||
168 | } | ||
169 | } | ||
170 | // send reconfiguration signal to theme | ||
171 | tm.reconfigTheme(); | ||
172 | |||
173 | } | ||
174 | |||
175 | std::string ThemeManager::resourceValue(const std::string &name, const std::string &altname) { | ||
176 | XrmValue value; | ||
177 | char *value_type; | ||
178 | |||
179 | if (*m_database != 0 && XrmGetResource(*m_database, name.c_str(), | ||
180 | altname.c_str(), &value_type, &value) && value.addr != 0) { | ||
181 | return string(value.addr); | ||
182 | } | ||
183 | return ""; | ||
184 | } | ||
185 | |||
186 | |||
187 | }; // end namespace FbTk | ||