aboutsummaryrefslogtreecommitdiff
path: root/src/Resources.cc
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-06-11 11:00:45 (GMT)
committerPavel Labath <pavelo@centrum.sk>2013-02-16 23:49:23 (GMT)
commiteb0eef14134ee667c60f75f53de4e24950f3c117 (patch)
tree58b35842e1f84666373478bca3386be037a7a019 /src/Resources.cc
parent3b7c1a3c220b7ccec0dee7804d36dabfe81449c4 (diff)
downloadfluxbox_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.cc376
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;
48namespace FbTk { 48namespace FbTk {
49 49
50template<> 50template<>
51string FbTk::Resource<int>:: 51const EnumTraits<WinButton::Type>::Pair EnumTraits<WinButton::Type>::s_map[] = {
52getString() 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 },
56template<> 56 { "Stick", WinButton::STICK },
57void FbTk::Resource<int>:: 57 { "MenuIcon", WinButton::MENUICON },
58setFromString(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};
62template<> 62
63void FbTk::Resource<int>::setFromLua(lua::state &l) { 63template<>
64 lua::stack_sentry s(l, -1); 64const 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} 70template<>
71 71const EnumTraits<ResourceLayer::Type>::Pair EnumTraits<ResourceLayer::Type>::s_map[] = {
72template<> 72 { "Menu", ResourceLayer::MENU },
73void 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 },
78template<> 78 { "Top", ResourceLayer::TOP },
79string FbTk::Resource<string>:: 79 { "7", ResourceLayer::LAYER7 },
80getString() const { return **this; } 80 { "Normal", ResourceLayer::NORMAL },
81 81 { "9", ResourceLayer::LAYER9 },
82template<> 82 { "Bottom", ResourceLayer::BOTTOM },
83void FbTk::Resource<string>:: 83 { "11", ResourceLayer::LAYER11 },
84setFromString(const char *strval) { 84 { "Desktop", ResourceLayer::DESKTOP },
85 *this = strval; 85 { NULL, ResourceLayer::DESKTOP }
86} 86};
87
88template<>
89void 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
96template<>
97void FbTk::Resource<string>::pushToLua(lua::state &l) const {
98 l.pushstring(*this);
99}
100
101
102template<>
103string FbTk::Resource<bool>::
104getString() const {
105 return string(**this == true ? "true" : "false");
106}
107
108template<>
109void FbTk::Resource<bool>::
110setFromString(char const *strval) {
111 *this = (bool)!strcasecmp(strval, "true");
112}
113
114template<>
115void 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
126template<>
127void FbTk::Resource<bool>::pushToLua(lua::state &l) const {
128 l.pushboolean(*this);
129}
130
131
132namespace {
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
165template<>
166string FbTk::Resource<vector<WinButton::Type> >::
167getString() 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
177template<>
178void FbTk::Resource<vector<WinButton::Type> >::
179setFromString(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
194template<>
195void 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
214template<>
215void 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
227template<>
228string FbTk::Resource<Fluxbox::TabsAttachArea>::
229getString() const {
230 if (m_value == Fluxbox::ATTACH_AREA_TITLEBAR)
231 return "Titlebar";
232 else
233 return "Window";
234}
235
236template<>
237void FbTk::Resource<Fluxbox::TabsAttachArea>::
238setFromString(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
245template<>
246void 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
257template<>
258void FbTk::Resource<Fluxbox::TabsAttachArea>::pushToLua(lua::state &l) const {
259 l.checkstack(1);
260
261 l.pushstring(getString());
262}
263
264
265template<>
266string FbTk::Resource<unsigned int>::
267getString() const {
268 return FbTk::StringUtil::number2String(m_value);
269}
270
271template<>
272void FbTk::Resource<unsigned int>::
273setFromString(const char *strval) {
274 if (!FbTk::StringUtil::extractNumber(strval, m_value))
275 setDefaultValue();
276}
277
278template<>
279void 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
288template<>
289void FbTk::Resource<unsigned int>::pushToLua(lua::state &l) const {
290 l.pushnumber(*this);
291}
292
293
294template<>
295string FbTk::Resource<long long>::
296getString() const {
297 return FbTk::StringUtil::number2String(m_value);
298}
299
300template<>
301void FbTk::Resource<long long>::
302setFromString(const char *strval) {
303 if (!FbTk::StringUtil::extractNumber(strval, m_value))
304 setDefaultValue();
305}
306
307template<>
308void 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
317template<>
318void FbTk::Resource<long long>::pushToLua(lua::state &l) const {
319 l.pushnumber(*this);
320}
321
322
323template<>
324string FbTk::Resource<ResourceLayer>::
325getString() const {
326 return ::ResourceLayer::getString(m_value.getNum());
327}
328
329template<>
330void FbTk::Resource<ResourceLayer>::
331setFromString(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
340template<>
341void 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
358template<>
359void FbTk::Resource<ResourceLayer>::pushToLua(lua::state &l) const {
360 l.pushstring(getString());
361}
362
363
364template<>
365string FbTk::Resource<long>::
366getString() const {
367 return FbTk::StringUtil::number2String(m_value);
368}
369
370template<>
371void FbTk::Resource<long>::
372setFromString(const char *strval) {
373 if (!FbTk::StringUtil::extractNumber(strval, m_value))
374 setDefaultValue();
375}
376
377template<>
378void 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
387template<>
388void FbTk::Resource<long>::pushToLua(lua::state &l) const {
389 l.pushnumber(*this);
390}
391 87
392} // end namespace FbTk 88} // end namespace FbTk