CLuaInstance: Add return value to menu function "addItem"

Function returns an ID of each item handles


Origin commit data
------------------
Commit: e0bfbfe9e3
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2015-09-14 (Mon, 14 Sep 2015)
This commit is contained in:
Michael Liebmann
2015-09-14 17:11:31 +02:00
parent 684a164507
commit a3aeda348a
2 changed files with 23 additions and 6 deletions

View File

@@ -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)

View File

@@ -56,6 +56,10 @@ class CLuaMenuChangeObserver : public CChangeObserver
bool changeNotify(lua_State *, const std::string &, const std::string &, void *);
};
typedef std::pair<lua_Integer, CMenuItem*> itemmap_pair_t;
typedef std::map<lua_Integer, CMenuItem*> itemmap_t;
typedef itemmap_t::iterator itemmap_iterator_t;
class CLuaMenu
{
public:
@@ -64,6 +68,7 @@ class CLuaMenu
std::list<CLuaMenuItem> items;
std::list<CMenuTarget *> targets;
std::list<void *> tofree;
itemmap_t itemmap;
CLuaMenu();
~CLuaMenu();
};