From 60cd98604ae0ee62d64e3bc852898f69dae221d4 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 5 Jun 2011 14:39:40 +0200 Subject: Specializations of FbTk::Resource::{setFromLua,pushToLua} for various values of X. --- src/FocusControl.cc | 32 +++++++ src/IconbarTool.cc | 16 ++++ src/Resources.cc | 246 +++++++++++++++++++++++++++++++++++++++++-------- src/Screen.cc | 14 +++ src/ScreenPlacement.cc | 46 +++++++++ src/Slit.cc | 16 ++++ src/Toolbar.cc | 16 ++++ 7 files changed, 346 insertions(+), 40 deletions(-) diff --git a/src/FocusControl.cc b/src/FocusControl.cc index 5bf113f..abedcf2 100644 --- a/src/FocusControl.cc +++ b/src/FocusControl.cc @@ -31,6 +31,7 @@ #include "Debug.hh" #include "FbTk/EventManager.hh" +#include "FbTk/Luamm.hh" #include #include @@ -625,6 +626,22 @@ setFromString(char const *strval) { } template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + + +template<> std::string FbTk::Resource::getString() const { switch (m_value) { case FocusControl::MOUSETABFOCUS: @@ -648,5 +665,20 @@ setFromString(char const *strval) { setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + } // end namespace FbTk diff --git a/src/IconbarTool.cc b/src/IconbarTool.cc index 45e3a45..0f3aeb7 100644 --- a/src/IconbarTool.cc +++ b/src/IconbarTool.cc @@ -51,6 +51,7 @@ #include "FbTk/STLUtil.hh" #include "FbTk/Select2nd.hh" #include "FbTk/Compose.hh" +#include "FbTk/Luamm.hh" #include #include @@ -92,6 +93,21 @@ void FbTk::Resource::setFromString(const char *str) setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + } // end namespace FbTk namespace { 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 @@ #include "FbTk/StringUtil.hh" #include "FbTk/Resource.hh" +#include "FbTk/Luamm.hh" #include "WinButton.hh" #include "fluxbox.hh" @@ -41,8 +42,6 @@ using std::string; using std::vector; -using namespace FbTk; - //----------------------------------------------------------------- //---- accessors for int, bool, and some enums with Resource ------ //----------------------------------------------------------------- @@ -61,6 +60,22 @@ setFromString(const char* strval) { } template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isnumber(-1)) + *this = l.tonumber(-1); + else if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushnumber(*this); +} + + +template<> string FbTk::Resource:: getString() const { return **this; } @@ -70,6 +85,19 @@ setFromString(const char *strval) { *this = strval; } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + *this = l.tostring(-1); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(*this); +} + template<> string FbTk::Resource:: @@ -83,42 +111,67 @@ setFromString(char const *strval) { *this = (bool)!strcasecmp(strval, "true"); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else if(l.isnumber(-1)) + *this = l.tointeger(-1) != 0; + else + *this = l.toboolean(-1); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushboolean(*this); +} + + +namespace { + struct ButtonPair { + WinButton::Type type; + const char *name; + }; + const ButtonPair button_map[] = { + { WinButton::SHADE, "Shade" }, + { WinButton::MINIMIZE, "Minimize" }, + { WinButton::MAXIMIZE, "Maximize" }, + { WinButton::CLOSE, "Close" }, + { WinButton::STICK, "Stick" }, + { WinButton::MENUICON, "MenuIcon" } + }; + + const char *buttonToStr(WinButton::Type t) { + for(size_t i = 0; i < sizeof(button_map)/sizeof(button_map[0]); ++i) { + if(button_map[i].type == t) + return button_map[i].name; + } + assert(0); + } + + WinButton::Type strToButton(const char *v) { + for(size_t i = 0; i < sizeof(button_map)/sizeof(button_map[0]); ++i) { + if(strcasecmp(v, button_map[i].name) == 0) + return button_map[i].type; + } + throw std::runtime_error("bad button"); + } +} template<> string FbTk::Resource >:: getString() const { string retval; for (size_t i = 0; i < m_value.size(); i++) { - switch (m_value[i]) { - case WinButton::SHADE: - retval.append("Shade"); - break; - case WinButton::MINIMIZE: - retval.append("Minimize"); - break; - case WinButton::MAXIMIZE: - retval.append("Maximize"); - break; - case WinButton::CLOSE: - retval.append("Close"); - break; - case WinButton::STICK: - retval.append("Stick"); - break; - case WinButton::MENUICON: - retval.append("MenuIcon"); - break; - default: - break; - } + retval.append(buttonToStr(m_value[i])); retval.append(" "); } return retval; } - - template<> void FbTk::Resource >:: setFromString(char const *strval) { @@ -127,25 +180,49 @@ setFromString(char const *strval) { //clear old values m_value.clear(); - std::string v; for (size_t i = 0; i < val.size(); i++) { - v = FbTk::StringUtil::toLower(val[i]); - if (v == "maximize") - m_value.push_back(WinButton::MAXIMIZE); - else if (v == "minimize") - m_value.push_back(WinButton::MINIMIZE); - else if (v == "shade") - m_value.push_back(WinButton::SHADE); - else if (v == "stick") - m_value.push_back(WinButton::STICK); - else if (v == "menuicon") - m_value.push_back(WinButton::MENUICON); - else if (v == "close") - m_value.push_back(WinButton::CLOSE); + try { + m_value.push_back(strToButton(val[i].c_str())); + } + catch(std::runtime_error &) { + } } } template<> +void FbTk::Resource >::setFromLua(lua::state &l) { + l.checkstack(1); + lua::stack_sentry s(l, -1); + + if(l.type(-1) == lua::TTABLE) { + for(size_t i = 0; l.rawgeti(-1, i), !l.isnil(-1); l.pop(), ++i) { + if(l.isstring(-1)) { + try { + m_value.push_back(strToButton(l.tostring(-1).c_str())); + } + catch(std::runtime_error &) { + } + } + } + l.pop(); + } + l.pop(); +} + +template<> +void FbTk::Resource >::pushToLua(lua::state &l) const { + l.checkstack(2); + l.newtable(); + lua::stack_sentry s(l); + + for (size_t i = 0; i < m_value.size(); ++i) { + l.pushstring(buttonToStr(m_value[i])); + l.rawseti(-2, i); + } +} + + +template<> string FbTk::Resource:: getString() const { if (m_value == Fluxbox::ATTACH_AREA_TITLEBAR) @@ -164,6 +241,26 @@ setFromString(char const *strval) { } template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + + if(l.isstring(-1) && strcasecmp(l.tostring(-1).c_str(), "Titlebar") == 0) + m_value = Fluxbox::ATTACH_AREA_TITLEBAR; + else + m_value = Fluxbox::ATTACH_AREA_WINDOW; + + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.checkstack(1); + + l.pushstring(getString()); +} + + +template<> string FbTk::Resource:: getString() const { return FbTk::StringUtil::number2String(m_value); @@ -176,6 +273,21 @@ setFromString(const char *strval) { setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isnumber(-1)) + *this = l.tonumber(-1); + else if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushnumber(*this); +} + template<> string FbTk::Resource:: @@ -190,6 +302,21 @@ setFromString(const char *strval) { setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isnumber(-1)) + *this = l.tonumber(-1); + else if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushnumber(*this); +} + template<> string FbTk::Resource:: @@ -209,6 +336,30 @@ setFromString(const char *strval) { } template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + + int tempnum = -1; + if(l.isnumber(-1)) + tempnum = l.tonumber(-1); + else if(l.isstring(-1)) + tempnum = ::ResourceLayer::getNumFromString(l.tostring(-1)); + + if (tempnum >= 0 && tempnum < ::ResourceLayer::NUM_LAYERS) + m_value = tempnum; + else + setDefaultValue(); + + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + + +template<> string FbTk::Resource:: getString() const { return FbTk::StringUtil::number2String(m_value); @@ -221,4 +372,19 @@ setFromString(const char *strval) { setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isnumber(-1)) + *this = l.tonumber(-1); + else if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushnumber(*this); +} + } // end namespace FbTk diff --git a/src/Screen.cc b/src/Screen.cc index 86799da..3435843 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -74,6 +74,7 @@ #include "FbTk/STLUtil.hh" #include "FbTk/KeyUtil.hh" #include "FbTk/Util.hh" +#include "FbTk/Luamm.hh" //use GNU extensions #ifndef _GNU_SOURCE @@ -260,6 +261,19 @@ setFromString(const char *strval) { setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + + setFromString(l.isstring(-1) ? l.tostring(-1).c_str() : ""); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + } // end namespace FbTk diff --git a/src/ScreenPlacement.cc b/src/ScreenPlacement.cc index 73ddfa2..cd7ce15 100644 --- a/src/ScreenPlacement.cc +++ b/src/ScreenPlacement.cc @@ -31,6 +31,7 @@ #include "Window.hh" #include "FbTk/Menu.hh" +#include "FbTk/Luamm.hh" #include #include @@ -218,6 +219,21 @@ void FbTk::Resource::setFromString(const char setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + template <> std::string FbTk::Resource::getString() const { @@ -242,6 +258,21 @@ void FbTk::Resource::setFromString(const char *st setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + template <> std::string FbTk::Resource::getString() const { switch (*(*this)) { @@ -265,4 +296,19 @@ void FbTk::Resource::setFromString(const char setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + } // end namespace FbTk diff --git a/src/Slit.cc b/src/Slit.cc index ccd60ab..d950ce9 100644 --- a/src/Slit.cc +++ b/src/Slit.cc @@ -43,6 +43,7 @@ #include "FbTk/Transparent.hh" #include "FbTk/MacroCommand.hh" #include "FbTk/MemFun.hh" +#include "FbTk/Luamm.hh" #include "FbCommands.hh" #include "Layer.hh" @@ -164,6 +165,21 @@ void FbTk::Resource::setFromString(const char *strval) { setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + } // end namespace FbTk namespace { diff --git a/src/Toolbar.cc b/src/Toolbar.cc index a8b2c5b..fe4c591 100644 --- a/src/Toolbar.cc +++ b/src/Toolbar.cc @@ -54,6 +54,7 @@ #include "FbTk/MemFun.hh" #include "FbTk/STLUtil.hh" #include "FbTk/Util.hh" +#include "FbTk/Luamm.hh" // use GNU extensions #ifndef _GNU_SOURCE @@ -132,6 +133,21 @@ setFromString(const char *strval) { setDefaultValue(); } +template<> +void FbTk::Resource::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) + setFromString(l.tostring(-1).c_str()); + else + setDefaultValue(); + l.pop(); +} + +template<> +void FbTk::Resource::pushToLua(lua::state &l) const { + l.pushstring(getString()); +} + } // end namespace FbTk namespace { -- cgit v0.11.2