From a3aeda348a94676c619143eb98cff72ecf2e8c8c Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Mon, 14 Sep 2015 17:11:31 +0200 Subject: [PATCH] CLuaInstance: Add return value to menu function "addItem" Function returns an ID of each item handles Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/e0bfbfe9e333e7fc8fb78991fdedffa7252d2d95 Author: Michael Liebmann Date: 2015-09-14 (Mon, 14 Sep 2015) --- src/gui/luainstance.cpp | 24 ++++++++++++++++++------ src/gui/luainstance.h | 5 +++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/gui/luainstance.cpp b/src/gui/luainstance.cpp index b2d52c0a5..cbb48298a 100644 --- a/src/gui/luainstance.cpp +++ b/src/gui/luainstance.cpp @@ -1081,6 +1081,7 @@ CLuaMenu::CLuaMenu() CLuaMenu::~CLuaMenu() { + itemmap.clear(); delete m; delete observ; } @@ -1274,10 +1275,13 @@ int CLuaInstance::MenuAddKey(lua_State *L) int CLuaInstance::MenuAddItem(lua_State *L) { CLuaMenu *m = MenuCheck(L, 1); - if (!m) - return 0; + if (!m) { + lua_pushnil(L); + return 1; + } lua_assert(lua_istable(L, 2)); + CMenuItem *mi = NULL; CLuaMenuItem i; m->items.push_back(i); CLuaMenuItem *b = &m->items.back(); @@ -1339,8 +1343,6 @@ int CLuaInstance::MenuAddItem(lua_State *L) int range_from = 0, range_to = 99; sscanf(tmp.c_str(), "%d,%d", &range_from, &range_to); - CMenuItem *mi = NULL; - if (type == "forwarder") { b->str_val = value; CLuaMenuForwarder *forwarder = new CLuaMenuForwarder(L, action, id); @@ -1360,7 +1362,8 @@ int CLuaInstance::MenuAddItem(lua_State *L) lua_pop(L, 1); if (options_count == 0) { m->m->addItem(new CMenuSeparator(CMenuSeparator::STRING | CMenuSeparator::LINE, "ERROR! (options_count)", NONEXISTANT_LOCALE)); - return 0; + lua_pushnil(L); + return 1; } CMenuOptionChooser::keyval_ext *kext = (CMenuOptionChooser::keyval_ext *)calloc(options_count, sizeof(CMenuOptionChooser::keyval_ext)); @@ -1432,7 +1435,16 @@ int CLuaInstance::MenuAddItem(lua_State *L) m->m->addItem(mi); } } - return 0; + + if (mi) { + lua_Integer id = m->itemmap.size() + 1; + m->itemmap.insert(itemmap_pair_t(id, mi)); + lua_pushinteger(L, id); + } + else + lua_pushnil(L); + + return 1; } int CLuaInstance::MenuHide(lua_State *L) diff --git a/src/gui/luainstance.h b/src/gui/luainstance.h index ee5eff3be..f19b86faf 100644 --- a/src/gui/luainstance.h +++ b/src/gui/luainstance.h @@ -56,6 +56,10 @@ class CLuaMenuChangeObserver : public CChangeObserver bool changeNotify(lua_State *, const std::string &, const std::string &, void *); }; +typedef std::pair itemmap_pair_t; +typedef std::map itemmap_t; +typedef itemmap_t::iterator itemmap_iterator_t; + class CLuaMenu { public: @@ -64,6 +68,7 @@ class CLuaMenu std::list items; std::list targets; std::list tofree; + itemmap_t itemmap; CLuaMenu(); ~CLuaMenu(); };