aboutsummaryrefslogtreecommitdiff
path: root/src/Resources.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/Resources.cc')
-rw-r--r--src/Resources.cc246
1 files changed, 206 insertions, 40 deletions
diff --git a/src/Resources.cc b/src/Resources.cc
index 2e07725..cdc142d 100644
--- a/src/Resources.cc
+++ b/src/Resources.cc
@@ -23,6 +23,7 @@
23 23
24#include "FbTk/StringUtil.hh" 24#include "FbTk/StringUtil.hh"
25#include "FbTk/Resource.hh" 25#include "FbTk/Resource.hh"
26#include "FbTk/Luamm.hh"
26#include "WinButton.hh" 27#include "WinButton.hh"
27 28
28#include "fluxbox.hh" 29#include "fluxbox.hh"
@@ -41,8 +42,6 @@
41using std::string; 42using std::string;
42using std::vector; 43using std::vector;
43 44
44using namespace FbTk;
45
46//----------------------------------------------------------------- 45//-----------------------------------------------------------------
47//---- accessors for int, bool, and some enums with Resource ------ 46//---- accessors for int, bool, and some enums with Resource ------
48//----------------------------------------------------------------- 47//-----------------------------------------------------------------
@@ -61,6 +60,22 @@ setFromString(const char* strval) {
61} 60}
62 61
63template<> 62template<>
63void FbTk::Resource<int>::setFromLua(lua::state &l) {
64 lua::stack_sentry s(l, -1);
65 if(l.isnumber(-1))
66 *this = l.tonumber(-1);
67 else if(l.isstring(-1))
68 setFromString(l.tostring(-1).c_str());
69 l.pop();
70}
71
72template<>
73void FbTk::Resource<int>::pushToLua(lua::state &l) const {
74 l.pushnumber(*this);
75}
76
77
78template<>
64string FbTk::Resource<string>:: 79string FbTk::Resource<string>::
65getString() const { return **this; } 80getString() const { return **this; }
66 81
@@ -70,6 +85,19 @@ setFromString(const char *strval) {
70 *this = strval; 85 *this = strval;
71} 86}
72 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
73 101
74template<> 102template<>
75string FbTk::Resource<bool>:: 103string FbTk::Resource<bool>::
@@ -83,42 +111,67 @@ setFromString(char const *strval) {
83 *this = (bool)!strcasecmp(strval, "true"); 111 *this = (bool)!strcasecmp(strval, "true");
84} 112}
85 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}
86 162
87template<> 163template<>
88string FbTk::Resource<vector<WinButton::Type> >:: 164string FbTk::Resource<vector<WinButton::Type> >::
89getString() const { 165getString() const {
90 string retval; 166 string retval;
91 for (size_t i = 0; i < m_value.size(); i++) { 167 for (size_t i = 0; i < m_value.size(); i++) {
92 switch (m_value[i]) { 168 retval.append(buttonToStr(m_value[i]));
93 case WinButton::SHADE:
94 retval.append("Shade");
95 break;
96 case WinButton::MINIMIZE:
97 retval.append("Minimize");
98 break;
99 case WinButton::MAXIMIZE:
100 retval.append("Maximize");
101 break;
102 case WinButton::CLOSE:
103 retval.append("Close");
104 break;
105 case WinButton::STICK:
106 retval.append("Stick");
107 break;
108 case WinButton::MENUICON:
109 retval.append("MenuIcon");
110 break;
111 default:
112 break;
113 }
114 retval.append(" "); 169 retval.append(" ");
115 } 170 }
116 171
117 return retval; 172 return retval;
118} 173}
119 174
120
121
122template<> 175template<>
123void FbTk::Resource<vector<WinButton::Type> >:: 176void FbTk::Resource<vector<WinButton::Type> >::
124setFromString(char const *strval) { 177setFromString(char const *strval) {
@@ -127,25 +180,49 @@ setFromString(char const *strval) {
127 //clear old values 180 //clear old values
128 m_value.clear(); 181 m_value.clear();
129 182
130 std::string v;
131 for (size_t i = 0; i < val.size(); i++) { 183 for (size_t i = 0; i < val.size(); i++) {
132 v = FbTk::StringUtil::toLower(val[i]); 184 try {
133 if (v == "maximize") 185 m_value.push_back(strToButton(val[i].c_str()));
134 m_value.push_back(WinButton::MAXIMIZE); 186 }
135 else if (v == "minimize") 187 catch(std::runtime_error &) {
136 m_value.push_back(WinButton::MINIMIZE); 188 }
137 else if (v == "shade")
138 m_value.push_back(WinButton::SHADE);
139 else if (v == "stick")
140 m_value.push_back(WinButton::STICK);
141 else if (v == "menuicon")
142 m_value.push_back(WinButton::MENUICON);
143 else if (v == "close")
144 m_value.push_back(WinButton::CLOSE);
145 } 189 }
146} 190}
147 191
148template<> 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<>
149string FbTk::Resource<Fluxbox::TabsAttachArea>:: 226string FbTk::Resource<Fluxbox::TabsAttachArea>::
150getString() const { 227getString() const {
151 if (m_value == Fluxbox::ATTACH_AREA_TITLEBAR) 228 if (m_value == Fluxbox::ATTACH_AREA_TITLEBAR)
@@ -164,6 +241,26 @@ setFromString(char const *strval) {
164} 241}
165 242
166template<> 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<>
167string FbTk::Resource<unsigned int>:: 264string FbTk::Resource<unsigned int>::
168getString() const { 265getString() const {
169 return FbTk::StringUtil::number2String(m_value); 266 return FbTk::StringUtil::number2String(m_value);
@@ -176,6 +273,21 @@ setFromString(const char *strval) {
176 setDefaultValue(); 273 setDefaultValue();
177} 274}
178 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
179 291
180template<> 292template<>
181string FbTk::Resource<long long>:: 293string FbTk::Resource<long long>::
@@ -190,6 +302,21 @@ setFromString(const char *strval) {
190 setDefaultValue(); 302 setDefaultValue();
191} 303}
192 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
193 320
194template<> 321template<>
195string FbTk::Resource<ResourceLayer>:: 322string FbTk::Resource<ResourceLayer>::
@@ -209,6 +336,30 @@ setFromString(const char *strval) {
209} 336}
210 337
211template<> 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<>
212string FbTk::Resource<long>:: 363string FbTk::Resource<long>::
213getString() const { 364getString() const {
214 return FbTk::StringUtil::number2String(m_value); 365 return FbTk::StringUtil::number2String(m_value);
@@ -221,4 +372,19 @@ setFromString(const char *strval) {
221 setDefaultValue(); 372 setDefaultValue();
222} 373}
223 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
224} // end namespace FbTk 390} // end namespace FbTk