diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-06-11 11:00:45 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2013-02-16 23:49:23 (GMT) |
commit | eb0eef14134ee667c60f75f53de4e24950f3c117 (patch) | |
tree | 58b35842e1f84666373478bca3386be037a7a019 /src/Resources.cc | |
parent | 3b7c1a3c220b7ccec0dee7804d36dabfe81449c4 (diff) | |
download | fluxbox_pavel-eb0eef14134ee667c60f75f53de4e24950f3c117.zip fluxbox_pavel-eb0eef14134ee667c60f75f53de4e24950f3c117.tar.bz2 |
Simplify FbTk::Resource template class
by outsourcing the conversion from string/lua to the specific type (and back) to a separate
class. This change touches a lot of files because the interface of FbTk::Resource changed
slightly. However, the changes are minor.
Diffstat (limited to 'src/Resources.cc')
-rw-r--r-- | src/Resources.cc | 376 |
1 files changed, 36 insertions, 340 deletions
diff --git a/src/Resources.cc b/src/Resources.cc index af6b5c9..2cae2b6 100644 --- a/src/Resources.cc +++ b/src/Resources.cc | |||
@@ -48,345 +48,41 @@ using std::vector; | |||
48 | namespace FbTk { | 48 | namespace FbTk { |
49 | 49 | ||
50 | template<> | 50 | template<> |
51 | string FbTk::Resource<int>:: | 51 | const EnumTraits<WinButton::Type>::Pair EnumTraits<WinButton::Type>::s_map[] = { |
52 | getString() const { | 52 | { "Shade", WinButton::SHADE }, |
53 | return FbTk::StringUtil::number2String(**this); | 53 | { "Minimize", WinButton::MINIMIZE }, |
54 | } | 54 | { "Maximize", WinButton::MAXIMIZE }, |
55 | 55 | { "Close", WinButton::CLOSE }, | |
56 | template<> | 56 | { "Stick", WinButton::STICK }, |
57 | void FbTk::Resource<int>:: | 57 | { "MenuIcon", WinButton::MENUICON }, |
58 | setFromString(const char* strval) { | 58 | { "LHalf", WinButton::LEFT_HALF }, |
59 | FbTk::StringUtil::extractNumber(strval, get()); | 59 | { "RHalf", WinButton::RIGHT_HALF }, |
60 | } | 60 | { NULL, WinButton::MENUICON } |
61 | 61 | }; | |
62 | template<> | 62 | |
63 | void FbTk::Resource<int>::setFromLua(lua::state &l) { | 63 | template<> |
64 | lua::stack_sentry s(l, -1); | 64 | const EnumTraits<Fluxbox::TabsAttachArea>::Pair EnumTraits<Fluxbox::TabsAttachArea>::s_map[] = { |
65 | if(l.isnumber(-1)) | 65 | { "Titlebar", Fluxbox::ATTACH_AREA_TITLEBAR }, |
66 | *this = l.tonumber(-1); | 66 | { "Window", Fluxbox::ATTACH_AREA_WINDOW }, |
67 | else if(l.isstring(-1)) | 67 | { NULL, Fluxbox::ATTACH_AREA_WINDOW } |
68 | setFromString(l.tostring(-1).c_str()); | 68 | }; |
69 | l.pop(); | 69 | |
70 | } | 70 | template<> |
71 | 71 | const EnumTraits<ResourceLayer::Type>::Pair EnumTraits<ResourceLayer::Type>::s_map[] = { | |
72 | template<> | 72 | { "Menu", ResourceLayer::MENU }, |
73 | void FbTk::Resource<int>::pushToLua(lua::state &l) const { | 73 | { "1", ResourceLayer::LAYER1 }, |
74 | l.pushnumber(*this); | 74 | { "AboveDock",ResourceLayer::ABOVE_DOCK }, |
75 | } | 75 | { "3", ResourceLayer::LAYER3 }, |
76 | 76 | { "Dock", ResourceLayer::DOCK }, | |
77 | 77 | { "5", ResourceLayer::LAYER5 }, | |
78 | template<> | 78 | { "Top", ResourceLayer::TOP }, |
79 | string FbTk::Resource<string>:: | 79 | { "7", ResourceLayer::LAYER7 }, |
80 | getString() const { return **this; } | 80 | { "Normal", ResourceLayer::NORMAL }, |
81 | 81 | { "9", ResourceLayer::LAYER9 }, | |
82 | template<> | 82 | { "Bottom", ResourceLayer::BOTTOM }, |
83 | void FbTk::Resource<string>:: | 83 | { "11", ResourceLayer::LAYER11 }, |
84 | setFromString(const char *strval) { | 84 | { "Desktop", ResourceLayer::DESKTOP }, |
85 | *this = strval; | 85 | { NULL, ResourceLayer::DESKTOP } |
86 | } | 86 | }; |
87 | |||
88 | template<> | ||
89 | void FbTk::Resource<string>::setFromLua(lua::state &l) { | ||
90 | lua::stack_sentry s(l, -1); | ||
91 | if(l.isstring(-1)) | ||
92 | *this = l.tostring(-1); | ||
93 | l.pop(); | ||
94 | } | ||
95 | |||
96 | template<> | ||
97 | void FbTk::Resource<string>::pushToLua(lua::state &l) const { | ||
98 | l.pushstring(*this); | ||
99 | } | ||
100 | |||
101 | |||
102 | template<> | ||
103 | string FbTk::Resource<bool>:: | ||
104 | getString() const { | ||
105 | return string(**this == true ? "true" : "false"); | ||
106 | } | ||
107 | |||
108 | template<> | ||
109 | void FbTk::Resource<bool>:: | ||
110 | setFromString(char const *strval) { | ||
111 | *this = (bool)!strcasecmp(strval, "true"); | ||
112 | } | ||
113 | |||
114 | template<> | ||
115 | void FbTk::Resource<bool>::setFromLua(lua::state &l) { | ||
116 | lua::stack_sentry s(l, -1); | ||
117 | if(l.isstring(-1)) | ||
118 | setFromString(l.tostring(-1).c_str()); | ||
119 | else if(l.isnumber(-1)) | ||
120 | *this = l.tointeger(-1) != 0; | ||
121 | else | ||
122 | *this = l.toboolean(-1); | ||
123 | l.pop(); | ||
124 | } | ||
125 | |||
126 | template<> | ||
127 | void FbTk::Resource<bool>::pushToLua(lua::state &l) const { | ||
128 | l.pushboolean(*this); | ||
129 | } | ||
130 | |||
131 | |||
132 | namespace { | ||
133 | struct ButtonPair { | ||
134 | WinButton::Type type; | ||
135 | const char *name; | ||
136 | }; | ||
137 | const ButtonPair button_map[] = { | ||
138 | { WinButton::SHADE, "Shade" }, | ||
139 | { WinButton::MINIMIZE, "Minimize" }, | ||
140 | { WinButton::MAXIMIZE, "Maximize" }, | ||
141 | { WinButton::CLOSE, "Close" }, | ||
142 | { WinButton::STICK, "Stick" }, | ||
143 | { WinButton::MENUICON, "MenuIcon" }, | ||
144 | { WinButton::LEFT_HALF, "LHalf" }, | ||
145 | { WinButton::RIGHT_HALF, "RHalf" } | ||
146 | }; | ||
147 | |||
148 | const char *buttonToStr(WinButton::Type t) { | ||
149 | for(size_t i = 0; i < sizeof(button_map)/sizeof(button_map[0]); ++i) { | ||
150 | if(button_map[i].type == t) | ||
151 | return button_map[i].name; | ||
152 | } | ||
153 | assert(0); | ||
154 | } | ||
155 | |||
156 | WinButton::Type strToButton(const char *v) { | ||
157 | for(size_t i = 0; i < sizeof(button_map)/sizeof(button_map[0]); ++i) { | ||
158 | if(strcasecmp(v, button_map[i].name) == 0) | ||
159 | return button_map[i].type; | ||
160 | } | ||
161 | throw std::runtime_error("bad button"); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | template<> | ||
166 | string FbTk::Resource<vector<WinButton::Type> >:: | ||
167 | getString() const { | ||
168 | string retval; | ||
169 | for (size_t i = 0; i < m_value.size(); i++) { | ||
170 | retval.append(buttonToStr(m_value[i])); | ||
171 | retval.append(" "); | ||
172 | } | ||
173 | |||
174 | return retval; | ||
175 | } | ||
176 | |||
177 | template<> | ||
178 | void FbTk::Resource<vector<WinButton::Type> >:: | ||
179 | setFromString(char const *strval) { | ||
180 | vector<string> val; | ||
181 | StringUtil::stringtok(val, strval); | ||
182 | //clear old values | ||
183 | m_value.clear(); | ||
184 | |||
185 | for (size_t i = 0; i < val.size(); i++) { | ||
186 | try { | ||
187 | m_value.push_back(strToButton(val[i].c_str())); | ||
188 | } | ||
189 | catch(std::runtime_error &) { | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | |||
194 | template<> | ||
195 | void FbTk::Resource<vector<WinButton::Type> >::setFromLua(lua::state &l) { | ||
196 | l.checkstack(1); | ||
197 | lua::stack_sentry s(l, -1); | ||
198 | |||
199 | if(l.type(-1) == lua::TTABLE) { | ||
200 | for(size_t i = 0; l.rawgeti(-1, i), !l.isnil(-1); l.pop(), ++i) { | ||
201 | if(l.isstring(-1)) { | ||
202 | try { | ||
203 | m_value.push_back(strToButton(l.tostring(-1).c_str())); | ||
204 | } | ||
205 | catch(std::runtime_error &) { | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | l.pop(); | ||
210 | } | ||
211 | l.pop(); | ||
212 | } | ||
213 | |||
214 | template<> | ||
215 | void FbTk::Resource<vector<WinButton::Type> >::pushToLua(lua::state &l) const { | ||
216 | l.checkstack(2); | ||
217 | l.newtable(); | ||
218 | lua::stack_sentry s(l); | ||
219 | |||
220 | for (size_t i = 0; i < m_value.size(); ++i) { | ||
221 | l.pushstring(buttonToStr(m_value[i])); | ||
222 | l.rawseti(-2, i); | ||
223 | } | ||
224 | } | ||
225 | |||
226 | |||
227 | template<> | ||
228 | string FbTk::Resource<Fluxbox::TabsAttachArea>:: | ||
229 | getString() const { | ||
230 | if (m_value == Fluxbox::ATTACH_AREA_TITLEBAR) | ||
231 | return "Titlebar"; | ||
232 | else | ||
233 | return "Window"; | ||
234 | } | ||
235 | |||
236 | template<> | ||
237 | void FbTk::Resource<Fluxbox::TabsAttachArea>:: | ||
238 | setFromString(char const *strval) { | ||
239 | if (strcasecmp(strval, "Titlebar")==0) | ||
240 | m_value= Fluxbox::ATTACH_AREA_TITLEBAR; | ||
241 | else | ||
242 | m_value= Fluxbox::ATTACH_AREA_WINDOW; | ||
243 | } | ||
244 | |||
245 | template<> | ||
246 | void FbTk::Resource<Fluxbox::TabsAttachArea>::setFromLua(lua::state &l) { | ||
247 | lua::stack_sentry s(l, -1); | ||
248 | |||
249 | if(l.isstring(-1) && strcasecmp(l.tostring(-1).c_str(), "Titlebar") == 0) | ||
250 | m_value = Fluxbox::ATTACH_AREA_TITLEBAR; | ||
251 | else | ||
252 | m_value = Fluxbox::ATTACH_AREA_WINDOW; | ||
253 | |||
254 | l.pop(); | ||
255 | } | ||
256 | |||
257 | template<> | ||
258 | void FbTk::Resource<Fluxbox::TabsAttachArea>::pushToLua(lua::state &l) const { | ||
259 | l.checkstack(1); | ||
260 | |||
261 | l.pushstring(getString()); | ||
262 | } | ||
263 | |||
264 | |||
265 | template<> | ||
266 | string FbTk::Resource<unsigned int>:: | ||
267 | getString() const { | ||
268 | return FbTk::StringUtil::number2String(m_value); | ||
269 | } | ||
270 | |||
271 | template<> | ||
272 | void FbTk::Resource<unsigned int>:: | ||
273 | setFromString(const char *strval) { | ||
274 | if (!FbTk::StringUtil::extractNumber(strval, m_value)) | ||
275 | setDefaultValue(); | ||
276 | } | ||
277 | |||
278 | template<> | ||
279 | void FbTk::Resource<unsigned int>::setFromLua(lua::state &l) { | ||
280 | lua::stack_sentry s(l, -1); | ||
281 | if(l.isnumber(-1)) | ||
282 | *this = l.tonumber(-1); | ||
283 | else if(l.isstring(-1)) | ||
284 | setFromString(l.tostring(-1).c_str()); | ||
285 | l.pop(); | ||
286 | } | ||
287 | |||
288 | template<> | ||
289 | void FbTk::Resource<unsigned int>::pushToLua(lua::state &l) const { | ||
290 | l.pushnumber(*this); | ||
291 | } | ||
292 | |||
293 | |||
294 | template<> | ||
295 | string FbTk::Resource<long long>:: | ||
296 | getString() const { | ||
297 | return FbTk::StringUtil::number2String(m_value); | ||
298 | } | ||
299 | |||
300 | template<> | ||
301 | void FbTk::Resource<long long>:: | ||
302 | setFromString(const char *strval) { | ||
303 | if (!FbTk::StringUtil::extractNumber(strval, m_value)) | ||
304 | setDefaultValue(); | ||
305 | } | ||
306 | |||
307 | template<> | ||
308 | void FbTk::Resource<long long>::setFromLua(lua::state &l) { | ||
309 | lua::stack_sentry s(l, -1); | ||
310 | if(l.isnumber(-1)) | ||
311 | *this = l.tonumber(-1); | ||
312 | else if(l.isstring(-1)) | ||
313 | setFromString(l.tostring(-1).c_str()); | ||
314 | l.pop(); | ||
315 | } | ||
316 | |||
317 | template<> | ||
318 | void FbTk::Resource<long long>::pushToLua(lua::state &l) const { | ||
319 | l.pushnumber(*this); | ||
320 | } | ||
321 | |||
322 | |||
323 | template<> | ||
324 | string FbTk::Resource<ResourceLayer>:: | ||
325 | getString() const { | ||
326 | return ::ResourceLayer::getString(m_value.getNum()); | ||
327 | } | ||
328 | |||
329 | template<> | ||
330 | void FbTk::Resource<ResourceLayer>:: | ||
331 | setFromString(const char *strval) { | ||
332 | string str(strval); | ||
333 | int tempnum = ::ResourceLayer::getNumFromString(str); | ||
334 | if (tempnum >= 0 && tempnum < ::ResourceLayer::NUM_LAYERS) | ||
335 | m_value = tempnum; | ||
336 | else | ||
337 | setDefaultValue(); | ||
338 | } | ||
339 | |||
340 | template<> | ||
341 | void FbTk::Resource<ResourceLayer>::setFromLua(lua::state &l) { | ||
342 | lua::stack_sentry s(l, -1); | ||
343 | |||
344 | int tempnum = -1; | ||
345 | if(l.isnumber(-1)) | ||
346 | tempnum = l.tonumber(-1); | ||
347 | else if(l.isstring(-1)) | ||
348 | tempnum = ::ResourceLayer::getNumFromString(l.tostring(-1)); | ||
349 | |||
350 | if (tempnum >= 0 && tempnum < ::ResourceLayer::NUM_LAYERS) | ||
351 | m_value = tempnum; | ||
352 | else | ||
353 | setDefaultValue(); | ||
354 | |||
355 | l.pop(); | ||
356 | } | ||
357 | |||
358 | template<> | ||
359 | void FbTk::Resource<ResourceLayer>::pushToLua(lua::state &l) const { | ||
360 | l.pushstring(getString()); | ||
361 | } | ||
362 | |||
363 | |||
364 | template<> | ||
365 | string FbTk::Resource<long>:: | ||
366 | getString() const { | ||
367 | return FbTk::StringUtil::number2String(m_value); | ||
368 | } | ||
369 | |||
370 | template<> | ||
371 | void FbTk::Resource<long>:: | ||
372 | setFromString(const char *strval) { | ||
373 | if (!FbTk::StringUtil::extractNumber(strval, m_value)) | ||
374 | setDefaultValue(); | ||
375 | } | ||
376 | |||
377 | template<> | ||
378 | void FbTk::Resource<long>::setFromLua(lua::state &l) { | ||
379 | lua::stack_sentry s(l, -1); | ||
380 | if(l.isnumber(-1)) | ||
381 | *this = l.tonumber(-1); | ||
382 | else if(l.isstring(-1)) | ||
383 | setFromString(l.tostring(-1).c_str()); | ||
384 | l.pop(); | ||
385 | } | ||
386 | |||
387 | template<> | ||
388 | void FbTk::Resource<long>::pushToLua(lua::state &l) const { | ||
389 | l.pushnumber(*this); | ||
390 | } | ||
391 | 87 | ||
392 | } // end namespace FbTk | 88 | } // end namespace FbTk |