From d2e5a703c3ad87b7004b4becf56cb52eaf38e69b Mon Sep 17 00:00:00 2001 From: martii Date: Fri, 10 May 2013 15:25:57 +0200 Subject: [PATCH] lua: update menue callback api to include an id parameter Signed-off-by: M. Liebmann --- src/gui/luainstance.cpp | 57 +++++++++++++++++++---------------------- src/gui/luainstance.h | 9 ++++--- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/gui/luainstance.cpp b/src/gui/luainstance.cpp index 1d232a6d3..d04f9e5c5 100644 --- a/src/gui/luainstance.cpp +++ b/src/gui/luainstance.cpp @@ -631,9 +631,9 @@ local m = menue.new{name="mytitle", icon="myicon", hide_parent=true} m:addItem{type="back"} m:addItem{type="separator"} -function talk(a) +function talk(a, b) print(">talk") - print(a) + print(a .. " => " b) print("luaAction = luaAction; - i->luaState = L; i->exec(NULL, ""); delete i; if (!luaAction.empty()){ lua_pushglobaltable(L); lua_getfield(L, -1, luaAction.c_str()); lua_remove(L, -2); + lua_pushstring(L, luaId.c_str()); lua_pushstring(L, value); - lua_pcall(L, 1 /* one arg */, 1 /* one result */, 0); + lua_pcall(L, 2 /* two args */, 1 /* one result */, 0); //double res = lua_isnumber(L, -1) ? lua_tonumber(L, -1) : 0; - lua_pop(L, 1); + lua_pop(L, 2); } return menu_return::RETURN_REPAINT; } @@ -896,6 +897,7 @@ int CLuaInstance::MenueAddItem(lua_State *L) std::string right_icon; tableLookup(L, "right_icon", right_icon); std::string action; tableLookup(L, "action", action); std::string value; tableLookup(L, "value", value); + std::string id; tableLookup(L, "id", id); std::string tmp; int directkey = CRCInput::RC_nokey; tableLookup(L, "directkey", directkey); int pulldown = false; tableLookup(L, "pulldown", pulldown); @@ -908,11 +910,10 @@ int CLuaInstance::MenueAddItem(lua_State *L) if (type == "forwarder") { strncpy(b->s, value.c_str(), sizeof(b->s)); - CLuaMenueForwarder *forwarder = new CLuaMenueForwarder(L, action); + CLuaMenueForwarder *forwarder = new CLuaMenueForwarder(L, action, id); CMenuItem *mi = new CMenuForwarderNonLocalized( b->name.c_str(), enabled, b->s, forwarder, NULL/*ActionKey*/, directkey, icon.c_str(), right_icon.c_str()); - mi->luaAction = action; - mi->luaState = L; + mi->setLua(L, action, id); m->m->addItem(mi); m->targets.push_back(forwarder); } else if (type == "chooser") { @@ -947,41 +948,36 @@ int CLuaInstance::MenueAddItem(lua_State *L) } lua_pop(L, 1); CMenuItem *mi = new CMenuOptionChooser(b->name.c_str(), &b->i, kext, options_count, enabled, m->observ, directkey, icon.c_str(), pulldown); - mi->luaAction = action; - mi->luaState = L; + mi->setLua(L, action, id); m->m->addItem(mi); } else if (type == "numeric") { b->i = range_from; sscanf(value.c_str(), "%d", &b->i); CMenuItem *mi = new CMenuOptionNumberChooser(NONEXISTANT_LOCALE, &b->i, enabled, range_from, range_to, m->observ, 0, 0, NONEXISTANT_LOCALE, b->name.c_str(), pulldown); - mi->luaAction = action; - mi->luaState = L; + mi->setLua(L, action, id); m->m->addItem(mi); } else if (type == "string") { strncpy(b->s, value.c_str(), sizeof(b->s)); CMenuItem *mi = new CMenuOptionStringChooser(b->name.c_str(), b->s, enabled, m->observ, directkey, icon.c_str(), pulldown); - mi->luaAction = action; - mi->luaState = L; + mi->setLua(L, action, id); m->m->addItem(mi); - } else if (type == "stringinput") { strncpy(b->s, value.c_str(), sizeof(b->s)); std::string valid_chars = "abcdefghijklmnopqrstuvwxyz0123456789!\"ยง$%&/()=?-. "; tableLookup(L, "valid_chars", valid_chars); int sms = 0; tableLookup(L, "sms", sms); int size = 30; tableLookup(L, "size", size); - CLuaMenueStringinput *stringinput = new CLuaMenueStringinput(L, action, b->name.c_str(), b->s, size, valid_chars, m->observ, icon.c_str(), sms); + CLuaMenueStringinput *stringinput = new CLuaMenueStringinput(L, action, id, b->name.c_str(), b->s, size, valid_chars, m->observ, icon.c_str(), sms); CMenuItem *mi = new CMenuForwarderNonLocalized( b->name.c_str(), enabled, b->s, stringinput, NULL/*ActionKey*/, directkey, icon.c_str(), right_icon.c_str()); - mi->luaAction = action; - mi->luaState = L; + mi->setLua(L, action, id); m->m->addItem(mi); m->targets.push_back(stringinput); } else if (type == "filebrowser") { strncpy(b->s, value.c_str(), sizeof(b->s)); int dirMode = 0; tableLookup(L, "dir_mode", dirMode); - CLuaMenueFilebrowser *filebrowser = new CLuaMenueFilebrowser(L, action, b->s, dirMode); + CLuaMenueFilebrowser *filebrowser = new CLuaMenueFilebrowser(L, action, id, b->s, dirMode); lua_pushstring(L, "filter"); lua_gettable(L, -2); if (lua_istable(L, -1)) @@ -994,8 +990,7 @@ int CLuaInstance::MenueAddItem(lua_State *L) CMenuItem *mi = new CMenuForwarderNonLocalized( b->name.c_str(), enabled, b->s, filebrowser, NULL/*ActionKey*/, directkey, icon.c_str(), right_icon.c_str()); - mi->luaAction = action; - mi->luaState = L; + mi->setLua(L, action, id); m->m->addItem(mi); m->targets.push_back(filebrowser); } diff --git a/src/gui/luainstance.h b/src/gui/luainstance.h index 0005824e2..daf2428ea 100644 --- a/src/gui/luainstance.h +++ b/src/gui/luainstance.h @@ -52,7 +52,7 @@ struct CLuaMenueItem class CLuaMenueChangeObserver : public CChangeObserver { public: - bool changeNotify(lua_State *, const std::string &, void *); + bool changeNotify(lua_State *, const std::string &, const std::string &, void *); }; class CLuaMenue @@ -72,7 +72,8 @@ class CLuaMenueForwarder : public CMenuTarget public: lua_State *L; std::string luaAction; - CLuaMenueForwarder(lua_State *L, std::string _luaAction); + std::string luaId; + CLuaMenueForwarder(lua_State *L, std::string _luaAction, std::string _luaId); ~CLuaMenueForwarder(); int exec(CMenuTarget* parent, const std::string & actionKey); }; @@ -84,7 +85,7 @@ class CLuaMenueFilebrowser : public CLuaMenueForwarder bool dirMode; std::vector filter; public: - CLuaMenueFilebrowser(lua_State *_L, std::string _luaAction, char *_value, bool _dirMode); + CLuaMenueFilebrowser(lua_State *_L, std::string _luaAction, std::string _luaId, char *_value, bool _dirMode); int exec(CMenuTarget* parent, const std::string & actionKey); void addFilter(std::string s) { filter.push_back(s); }; }; @@ -100,7 +101,7 @@ class CLuaMenueStringinput : public CLuaMenueForwarder int size; CChangeObserver *observ; public: - CLuaMenueStringinput(lua_State *_L, std::string _luaAction, const char *_name, char *_value, int _size, std::string _valid_chars, CChangeObserver *_observ, const char *_icon, bool _sms); + CLuaMenueStringinput(lua_State *_L, std::string _luaAction, std::string _luaId, const char *_name, char *_value, int _size, std::string _valid_chars, CChangeObserver *_observ, const char *_icon, bool _sms); int exec(CMenuTarget* parent, const std::string & actionKey); };