diff options
Diffstat (limited to 'src/Resources.cc')
-rw-r--r-- | src/Resources.cc | 372 |
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; | |||
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 | { NULL, WinButton::MENUICON } |
59 | FbTk::StringUtil::extractNumber(strval, get()); | 59 | }; |
60 | } | 60 | |
61 | 61 | template<> | |
62 | template<> | 62 | const EnumTraits<Fluxbox::TabsAttachArea>::Pair EnumTraits<Fluxbox::TabsAttachArea>::s_map[] = { |
63 | void 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()); | 68 | template<> |
69 | l.pop(); | 69 | const EnumTraits<ResourceLayer::Type>::Pair EnumTraits<ResourceLayer::Type>::s_map[] = { |
70 | } | 70 | { "Menu", ResourceLayer::MENU }, |
71 | 71 | { "1", ResourceLayer::LAYER1 }, | |
72 | template<> | 72 | { "AboveDock",ResourceLayer::ABOVE_DOCK }, |
73 | void 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 }, | |
78 | template<> | 78 | { "Normal", ResourceLayer::NORMAL }, |
79 | string FbTk::Resource<string>:: | 79 | { "9", ResourceLayer::LAYER9 }, |
80 | getString() const { return **this; } | 80 | { "Bottom", ResourceLayer::BOTTOM }, |
81 | 81 | { "11", ResourceLayer::LAYER11 }, | |
82 | template<> | 82 | { "Desktop", ResourceLayer::DESKTOP }, |
83 | void FbTk::Resource<string>:: | 83 | { NULL, ResourceLayer::DESKTOP } |
84 | setFromString(const char *strval) { | 84 | }; |
85 | *this = strval; | ||
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 | }; | ||
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 | |||
163 | template<> | ||
164 | string FbTk::Resource<vector<WinButton::Type> >:: | ||
165 | getString() 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 | |||
175 | template<> | ||
176 | void FbTk::Resource<vector<WinButton::Type> >:: | ||
177 | setFromString(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 | |||
192 | template<> | ||
193 | void 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 | |||
212 | template<> | ||
213 | void 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 | |||
225 | template<> | ||
226 | string FbTk::Resource<Fluxbox::TabsAttachArea>:: | ||
227 | getString() const { | ||
228 | if (m_value == Fluxbox::ATTACH_AREA_TITLEBAR) | ||
229 | return "Titlebar"; | ||
230 | else | ||
231 | return "Window"; | ||
232 | } | ||
233 | |||
234 | template<> | ||
235 | void FbTk::Resource<Fluxbox::TabsAttachArea>:: | ||
236 | setFromString(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 | |||
243 | template<> | ||
244 | void 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 | |||
255 | template<> | ||
256 | void FbTk::Resource<Fluxbox::TabsAttachArea>::pushToLua(lua::state &l) const { | ||
257 | l.checkstack(1); | ||
258 | |||
259 | l.pushstring(getString()); | ||
260 | } | ||
261 | |||
262 | |||
263 | template<> | ||
264 | string FbTk::Resource<unsigned int>:: | ||
265 | getString() const { | ||
266 | return FbTk::StringUtil::number2String(m_value); | ||
267 | } | ||
268 | |||
269 | template<> | ||
270 | void FbTk::Resource<unsigned int>:: | ||
271 | setFromString(const char *strval) { | ||
272 | if (!FbTk::StringUtil::extractNumber(strval, m_value)) | ||
273 | setDefaultValue(); | ||
274 | } | ||
275 | |||
276 | template<> | ||
277 | void 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 | |||
286 | template<> | ||
287 | void FbTk::Resource<unsigned int>::pushToLua(lua::state &l) const { | ||
288 | l.pushnumber(*this); | ||
289 | } | ||
290 | |||
291 | |||
292 | template<> | ||
293 | string FbTk::Resource<long long>:: | ||
294 | getString() const { | ||
295 | return FbTk::StringUtil::number2String(m_value); | ||
296 | } | ||
297 | |||
298 | template<> | ||
299 | void FbTk::Resource<long long>:: | ||
300 | setFromString(const char *strval) { | ||
301 | if (!FbTk::StringUtil::extractNumber(strval, m_value)) | ||
302 | setDefaultValue(); | ||
303 | } | ||
304 | |||
305 | template<> | ||
306 | void 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 | |||
315 | template<> | ||
316 | void FbTk::Resource<long long>::pushToLua(lua::state &l) const { | ||
317 | l.pushnumber(*this); | ||
318 | } | ||
319 | |||
320 | |||
321 | template<> | ||
322 | string FbTk::Resource<ResourceLayer>:: | ||
323 | getString() const { | ||
324 | return ::ResourceLayer::getString(m_value.getNum()); | ||
325 | } | ||
326 | |||
327 | template<> | ||
328 | void FbTk::Resource<ResourceLayer>:: | ||
329 | setFromString(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 | |||
338 | template<> | ||
339 | void 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 | |||
356 | template<> | ||
357 | void FbTk::Resource<ResourceLayer>::pushToLua(lua::state &l) const { | ||
358 | l.pushstring(getString()); | ||
359 | } | ||
360 | |||
361 | |||
362 | template<> | ||
363 | string FbTk::Resource<long>:: | ||
364 | getString() const { | ||
365 | return FbTk::StringUtil::number2String(m_value); | ||
366 | } | ||
367 | |||
368 | template<> | ||
369 | void FbTk::Resource<long>:: | ||
370 | setFromString(const char *strval) { | ||
371 | if (!FbTk::StringUtil::extractNumber(strval, m_value)) | ||
372 | setDefaultValue(); | ||
373 | } | ||
374 | |||
375 | template<> | ||
376 | void 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 | |||
385 | template<> | ||
386 | void FbTk::Resource<long>::pushToLua(lua::state &l) const { | ||
387 | l.pushnumber(*this); | ||
388 | } | ||
389 | 85 | ||
390 | } // end namespace FbTk | 86 | } // end namespace FbTk |