diff options
Diffstat (limited to 'src/FbTk/ThemeItems.hh')
-rw-r--r-- | src/FbTk/ThemeItems.hh | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/src/FbTk/ThemeItems.hh b/src/FbTk/ThemeItems.hh new file mode 100644 index 0000000..63c17df --- /dev/null +++ b/src/FbTk/ThemeItems.hh | |||
@@ -0,0 +1,218 @@ | |||
1 | // ThemeItems.cc for FbTk - Fluxbox ToolKit | ||
2 | // Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
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: ThemeItems.hh,v 1.5 2004/02/10 19:03:04 fluxgen Exp $ | ||
23 | |||
24 | /// @file implements common theme items | ||
25 | |||
26 | #ifndef THEMEITEMS_HH | ||
27 | #define THEMEITEMS_HH | ||
28 | |||
29 | #include "Theme.hh" | ||
30 | #include "Color.hh" | ||
31 | #include "Texture.hh" | ||
32 | #include "Font.hh" | ||
33 | #include "PixmapWithMask.hh" | ||
34 | #include "Image.hh" | ||
35 | |||
36 | #include <string> | ||
37 | #include <cstdio> | ||
38 | #include <iostream> | ||
39 | namespace FbTk { | ||
40 | |||
41 | using namespace std; | ||
42 | |||
43 | // create default handlers for Color, Font, Texture, int and string | ||
44 | template <> | ||
45 | void FbTk::ThemeItem<std::string>::load() { } | ||
46 | |||
47 | template <> | ||
48 | void FbTk::ThemeItem<std::string>::setDefaultValue() { | ||
49 | *(*this) = ""; | ||
50 | } | ||
51 | |||
52 | template <> | ||
53 | void FbTk::ThemeItem<std::string>::setFromString(const char *str) { | ||
54 | *(*this) = (str ? str : ""); | ||
55 | } | ||
56 | |||
57 | template <> | ||
58 | void FbTk::ThemeItem<int>::load() { } | ||
59 | |||
60 | template <> | ||
61 | void FbTk::ThemeItem<int>::setDefaultValue() { | ||
62 | *(*this) = 0; | ||
63 | } | ||
64 | |||
65 | template <> | ||
66 | void FbTk::ThemeItem<int>::setFromString(const char *str) { | ||
67 | if (str == 0) { | ||
68 | setDefaultValue(); | ||
69 | return; | ||
70 | } | ||
71 | |||
72 | if (sscanf(str, "%d", &m_value) < 1) | ||
73 | setDefaultValue(); | ||
74 | } | ||
75 | |||
76 | template <> | ||
77 | void ThemeItem<FbTk::Font>::setDefaultValue() { | ||
78 | if (!m_value.load("fixed")) { | ||
79 | cerr<<"FbTk::ThemeItem<FbTk::Font>: Warning! Failed to load default value 'fixed'"<<endl; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | template <> | ||
84 | void ThemeItem<FbTk::Font>::setFromString(const char *str) { | ||
85 | |||
86 | if (str == 0 || m_value.load(str) == false) { | ||
87 | if (FbTk::ThemeManager::instance().verbose()) { | ||
88 | cerr<<"FbTk::Theme: Error loading font "<< | ||
89 | ((m_value.isAntialias() || m_value.utf8()) ? "(" : "")<< | ||
90 | |||
91 | (m_value.isAntialias() ? "antialias" : "")<< | ||
92 | (m_value.utf8() ? " utf8" : "")<< | ||
93 | |||
94 | ((m_value.isAntialias() || m_value.utf8()) ? ") " : "")<< | ||
95 | "for \""<<name()<<"\" or \""<<altName()<<"\": "<<str<<endl; | ||
96 | |||
97 | cerr<<"FbTk::Theme: Setting default value"<<endl; | ||
98 | } | ||
99 | setDefaultValue(); | ||
100 | } | ||
101 | |||
102 | } | ||
103 | |||
104 | // do nothing | ||
105 | template <> | ||
106 | void ThemeItem<FbTk::Font>::load() { | ||
107 | } | ||
108 | |||
109 | |||
110 | template <> | ||
111 | void ThemeItem<FbTk::Texture>::load() { | ||
112 | string color_name(ThemeManager::instance(). | ||
113 | resourceValue(name()+".color", altName()+".Color")); | ||
114 | string colorto_name(ThemeManager::instance(). | ||
115 | resourceValue(name()+".colorTo", altName()+".ColorTo")); | ||
116 | string pixmap_name(ThemeManager::instance(). | ||
117 | resourceValue(name()+".pixmap", altName()+".Pixmap")); | ||
118 | |||
119 | |||
120 | // set default value if we failed to load color | ||
121 | if (!m_value.color().setFromString(color_name.c_str(), | ||
122 | m_tm.screenNum())) | ||
123 | m_value.color().setFromString("darkgray", m_tm.screenNum()); | ||
124 | |||
125 | if (!m_value.colorTo().setFromString(colorto_name.c_str(), | ||
126 | m_tm.screenNum())) | ||
127 | m_value.colorTo().setFromString("white", m_tm.screenNum()); | ||
128 | |||
129 | StringUtil::removeFirstWhitespace(pixmap_name); | ||
130 | StringUtil::removeTrailingWhitespace(pixmap_name); | ||
131 | if (pixmap_name.empty()) { | ||
132 | m_value.pixmap() = 0; | ||
133 | return; | ||
134 | } | ||
135 | |||
136 | std::auto_ptr<FbTk::PixmapWithMask> pm(FbTk::Image::load(pixmap_name, | ||
137 | m_tm.screenNum())); | ||
138 | if (pm.get() == 0) { | ||
139 | if (FbTk::ThemeManager::instance().verbose()) { | ||
140 | cerr<<"Resource("<<name()+".pixmap" | ||
141 | <<"): Failed to load image: "<<pixmap_name<<endl; | ||
142 | } | ||
143 | m_value.pixmap() = 0; | ||
144 | } else | ||
145 | m_value.pixmap() = pm->pixmap().release(); | ||
146 | |||
147 | } | ||
148 | |||
149 | template <> | ||
150 | void ThemeItem<FbTk::Texture>::setDefaultValue() { | ||
151 | m_value.setType(FbTk::Texture::FLAT | FbTk::Texture::SOLID); | ||
152 | load(); // one might forget to add line something: so we try to load something.*: too | ||
153 | } | ||
154 | |||
155 | template <> | ||
156 | void ThemeItem<FbTk::Texture>::setFromString(const char *str) { | ||
157 | m_value.setFromString(str); | ||
158 | if (m_value.type() == 0) // failed to set value | ||
159 | setDefaultValue(); | ||
160 | } | ||
161 | |||
162 | |||
163 | |||
164 | // not used | ||
165 | template <> | ||
166 | void FbTk::ThemeItem<PixmapWithMask>:: | ||
167 | load() { } | ||
168 | |||
169 | template <> | ||
170 | void FbTk::ThemeItem<PixmapWithMask>:: | ||
171 | setDefaultValue() { | ||
172 | // create empty pixmap | ||
173 | (*this)->pixmap() = 0; | ||
174 | (*this)->mask() = 0; | ||
175 | } | ||
176 | |||
177 | template <> | ||
178 | void FbTk::ThemeItem<PixmapWithMask>:: | ||
179 | setFromString(const char *str) { | ||
180 | if (str == 0) | ||
181 | setDefaultValue(); | ||
182 | else { | ||
183 | std::string filename(str); | ||
184 | |||
185 | StringUtil::removeFirstWhitespace(filename); | ||
186 | StringUtil::removeTrailingWhitespace(filename); | ||
187 | |||
188 | std::auto_ptr<FbTk::PixmapWithMask> pm(Image::load(filename, m_tm.screenNum())); | ||
189 | if (pm.get() == 0) | ||
190 | setDefaultValue(); | ||
191 | else { | ||
192 | (*this)->pixmap() = pm->pixmap().release(); | ||
193 | (*this)->mask() = pm->mask().release(); | ||
194 | } | ||
195 | } | ||
196 | } | ||
197 | |||
198 | template <> | ||
199 | void ThemeItem<FbTk::Color>::setDefaultValue() { | ||
200 | m_value.setFromString("white", m_tm.screenNum()); | ||
201 | } | ||
202 | |||
203 | template <> | ||
204 | void ThemeItem<FbTk::Color>::setFromString(const char *str) { | ||
205 | if (!m_value.setFromString(str, m_tm.screenNum())) { | ||
206 | if (FbTk::ThemeManager::instance().verbose()) | ||
207 | cerr<<"FbTk::Theme: Error loading color value for \""<<name()<<"\" or \""<<altName()<<"\"."<<endl; | ||
208 | setDefaultValue(); | ||
209 | } | ||
210 | } | ||
211 | |||
212 | // does nothing | ||
213 | template <> | ||
214 | void ThemeItem<FbTk::Color>::load() { } | ||
215 | |||
216 | } // end namespace FbTk | ||
217 | |||
218 | #endif // THEMEITEMS_HH | ||