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>2011-06-15 23:12:25 (GMT)
commitcd9df3b8144c4c41f7b57f0075fbb98ef7561296 (patch)
tree10ffc1692e4afbcb2c74b9db8c8a44b434ef62ca /src/Resources.cc
parent787c7a994d4328a9c90a037dd464f8007351b0c0 (diff)
downloadfluxbox_pavel-cd9df3b8144c4c41f7b57f0075fbb98ef7561296.zip
fluxbox_pavel-cd9df3b8144c4c41f7b57f0075fbb98ef7561296.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.cc372
1 files changed, 34 insertions, 338 deletions
diff --git a/src/Resources.cc b/src/Resources.cc
index cdc142d..cf09543 100644
--- a/src/Resources.cc
+++ b/src/Resources.cc
@@ -48,343 +48,39 @@ 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 { NULL, WinButton::MENUICON }
59 FbTk::StringUtil::extractNumber(strval, get()); 59};
60} 60
61 61template<>
62template<> 62const EnumTraits<Fluxbox::TabsAttachArea>::Pair EnumTraits<Fluxbox::TabsAttachArea>::s_map[] = {
63void FbTk::Resource<int>::setFromLua(lua::state &l) { 63 { "Titlebar", Fluxbox::ATTACH_AREA_TITLEBAR },
64 lua::stack_sentry s(l, -1); 64 { "Window", Fluxbox::ATTACH_AREA_WINDOW },
65 if(l.isnumber(-1)) 65 { NULL, Fluxbox::ATTACH_AREA_WINDOW }
66 *this = l.tonumber(-1); 66};
67 else if(l.isstring(-1)) 67
68 setFromString(l.tostring(-1).c_str()); 68template<>
69 l.pop(); 69const EnumTraits<ResourceLayer::Type>::Pair EnumTraits<ResourceLayer::Type>::s_map[] = {
70} 70 { "Menu", ResourceLayer::MENU },
71 71 { "1", ResourceLayer::LAYER1 },
72template<> 72 { "AboveDock",ResourceLayer::ABOVE_DOCK },
73void FbTk::Resource<int>::pushToLua(lua::state &l) const { 73 { "3", ResourceLayer::LAYER3 },
74 l.pushnumber(*this); 74 { "Dock", ResourceLayer::DOCK },
75} 75 { "5", ResourceLayer::LAYER5 },
76 76 { "Top", ResourceLayer::TOP },
77 77 { "7", ResourceLayer::LAYER7 },
78template<> 78 { "Normal", ResourceLayer::NORMAL },
79string FbTk::Resource<string>:: 79 { "9", ResourceLayer::LAYER9 },
80getString() const { return **this; } 80 { "Bottom", ResourceLayer::BOTTOM },
81 81 { "11", ResourceLayer::LAYER11 },
82template<> 82 { "Desktop", ResourceLayer::DESKTOP },
83void FbTk::Resource<string>:: 83 { NULL, ResourceLayer::DESKTOP }
84setFromString(const char *strval) { 84};
85 *this = strval;
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 };
145
146 const char *buttonToStr(WinButton::Type t) {
147 for(size_t i = 0; i < sizeof(button_map)/sizeof(button_map[0]); ++i) {
148 if(button_map[i].type == t)
149 return button_map[i].name;
150 }
151 assert(0);
152 }
153
154 WinButton::Type strToButton(const char *v) {
155 for(size_t i = 0; i < sizeof(button_map)/sizeof(button_map[0]); ++i) {
156 if(strcasecmp(v, button_map[i].name) == 0)
157 return button_map[i].type;
158 }
159 throw std::runtime_error("bad button");
160 }
161}
162
163template<>
164string FbTk::Resource<vector<WinButton::Type> >::
165getString() const {
166 string retval;
167 for (size_t i = 0; i < m_value.size(); i++) {
168 retval.append(buttonToStr(m_value[i]));
169 retval.append(" ");
170 }
171
172 return retval;
173}
174
175template<>
176void FbTk::Resource<vector<WinButton::Type> >::
177setFromString(char const *strval) {
178 vector<string> val;
179 StringUtil::stringtok(val, strval);
180 //clear old values
181 m_value.clear();
182
183 for (size_t i = 0; i < val.size(); i++) {
184 try {
185 m_value.push_back(strToButton(val[i].c_str()));
186 }
187 catch(std::runtime_error &) {
188 }
189 }
190}
191
192template<>
193void FbTk::Resource<vector<WinButton::Type> >::setFromLua(lua::state &l) {
194 l.checkstack(1);
195 lua::stack_sentry s(l, -1);
196
197 if(l.type(-1) == lua::TTABLE) {
198 for(size_t i = 0; l.rawgeti(-1, i), !l.isnil(-1); l.pop(), ++i) {
199 if(l.isstring(-1)) {
200 try {
201 m_value.push_back(strToButton(l.tostring(-1).c_str()));
202 }
203 catch(std::runtime_error &) {
204 }
205 }
206 }
207 l.pop();
208 }
209 l.pop();
210}
211
212template<>
213void FbTk::Resource<vector<WinButton::Type> >::pushToLua(lua::state &l) const {
214 l.checkstack(2);
215 l.newtable();
216 lua::stack_sentry s(l);
217
218 for (size_t i = 0; i < m_value.size(); ++i) {
219 l.pushstring(buttonToStr(m_value[i]));
220 l.rawseti(-2, i);
221 }
222}
223
224
225template<>
226string FbTk::Resource<Fluxbox::TabsAttachArea>::
227getString() const {
228 if (m_value == Fluxbox::ATTACH_AREA_TITLEBAR)
229 return "Titlebar";
230 else
231 return "Window";
232}
233
234template<>
235void FbTk::Resource<Fluxbox::TabsAttachArea>::
236setFromString(char const *strval) {
237 if (strcasecmp(strval, "Titlebar")==0)
238 m_value= Fluxbox::ATTACH_AREA_TITLEBAR;
239 else
240 m_value= Fluxbox::ATTACH_AREA_WINDOW;
241}
242
243template<>
244void FbTk::Resource<Fluxbox::TabsAttachArea>::setFromLua(lua::state &l) {
245 lua::stack_sentry s(l, -1);
246
247 if(l.isstring(-1) && strcasecmp(l.tostring(-1).c_str(), "Titlebar") == 0)
248 m_value = Fluxbox::ATTACH_AREA_TITLEBAR;
249 else
250 m_value = Fluxbox::ATTACH_AREA_WINDOW;
251
252 l.pop();
253}
254
255template<>
256void FbTk::Resource<Fluxbox::TabsAttachArea>::pushToLua(lua::state &l) const {
257 l.checkstack(1);
258
259 l.pushstring(getString());
260}
261
262
263template<>
264string FbTk::Resource<unsigned int>::
265getString() const {
266 return FbTk::StringUtil::number2String(m_value);
267}
268
269template<>
270void FbTk::Resource<unsigned int>::
271setFromString(const char *strval) {
272 if (!FbTk::StringUtil::extractNumber(strval, m_value))
273 setDefaultValue();
274}
275
276template<>
277void FbTk::Resource<unsigned int>::setFromLua(lua::state &l) {
278 lua::stack_sentry s(l, -1);
279 if(l.isnumber(-1))
280 *this = l.tonumber(-1);
281 else if(l.isstring(-1))
282 setFromString(l.tostring(-1).c_str());
283 l.pop();
284}
285
286template<>
287void FbTk::Resource<unsigned int>::pushToLua(lua::state &l) const {
288 l.pushnumber(*this);
289}
290
291
292template<>
293string FbTk::Resource<long long>::
294getString() const {
295 return FbTk::StringUtil::number2String(m_value);
296}
297
298template<>
299void FbTk::Resource<long long>::
300setFromString(const char *strval) {
301 if (!FbTk::StringUtil::extractNumber(strval, m_value))
302 setDefaultValue();
303}
304
305template<>
306void FbTk::Resource<long long>::setFromLua(lua::state &l) {
307 lua::stack_sentry s(l, -1);
308 if(l.isnumber(-1))
309 *this = l.tonumber(-1);
310 else if(l.isstring(-1))
311 setFromString(l.tostring(-1).c_str());
312 l.pop();
313}
314
315template<>
316void FbTk::Resource<long long>::pushToLua(lua::state &l) const {
317 l.pushnumber(*this);
318}
319
320
321template<>
322string FbTk::Resource<ResourceLayer>::
323getString() const {
324 return ::ResourceLayer::getString(m_value.getNum());
325}
326
327template<>
328void FbTk::Resource<ResourceLayer>::
329setFromString(const char *strval) {
330 string str(strval);
331 int tempnum = ::ResourceLayer::getNumFromString(str);
332 if (tempnum >= 0 && tempnum < ::ResourceLayer::NUM_LAYERS)
333 m_value = tempnum;
334 else
335 setDefaultValue();
336}
337
338template<>
339void FbTk::Resource<ResourceLayer>::setFromLua(lua::state &l) {
340 lua::stack_sentry s(l, -1);
341
342 int tempnum = -1;
343 if(l.isnumber(-1))
344 tempnum = l.tonumber(-1);
345 else if(l.isstring(-1))
346 tempnum = ::ResourceLayer::getNumFromString(l.tostring(-1));
347
348 if (tempnum >= 0 && tempnum < ::ResourceLayer::NUM_LAYERS)
349 m_value = tempnum;
350 else
351 setDefaultValue();
352
353 l.pop();
354}
355
356template<>
357void FbTk::Resource<ResourceLayer>::pushToLua(lua::state &l) const {
358 l.pushstring(getString());
359}
360
361
362template<>
363string FbTk::Resource<long>::
364getString() const {
365 return FbTk::StringUtil::number2String(m_value);
366}
367
368template<>
369void FbTk::Resource<long>::
370setFromString(const char *strval) {
371 if (!FbTk::StringUtil::extractNumber(strval, m_value))
372 setDefaultValue();
373}
374
375template<>
376void FbTk::Resource<long>::setFromLua(lua::state &l) {
377 lua::stack_sentry s(l, -1);
378 if(l.isnumber(-1))
379 *this = l.tonumber(-1);
380 else if(l.isstring(-1))
381 setFromString(l.tostring(-1).c_str());
382 l.pop();
383}
384
385template<>
386void FbTk::Resource<long>::pushToLua(lua::state &l) const {
387 l.pushnumber(*this);
388}
389 85
390} // end namespace FbTk 86} // end namespace FbTk