Skip to content

Commit

Permalink
Implement MenuManager::remove() method
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 5, 2017
1 parent 610a245 commit 145530c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion plugins/uimanager/menu/MenuElement.cpp
Expand Up @@ -136,9 +136,13 @@ void MenuElement::removeChild(const MenuElementPtr& child)
{
if (*i == child)
{
// Deconstruct the child before removal
child->deconstruct();

// Release from parent and remove from the list
child->setParent(MenuElementPtr());
_children.erase(i);
return;
break;
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions plugins/uimanager/menu/MenuItem.cpp
Expand Up @@ -96,6 +96,17 @@ void MenuItem::deconstruct()

if (_menuItem != nullptr)
{
// Try to lookup the event name
if (!_event.empty())
{
IEventPtr event = GlobalEventManager().findEvent(_event);

if (event)
{
event->disconnectMenuItem(_menuItem);
}
}

if (_menuItem->GetMenu() != nullptr)
{
_menuItem->GetMenu()->Remove(_menuItem);
Expand Down
33 changes: 33 additions & 0 deletions plugins/uimanager/menu/MenuManager.cpp
Expand Up @@ -79,6 +79,8 @@ void MenuManager::setVisibility(const std::string& path, bool visible)

wxMenuBar* MenuManager::getMenuBar(const std::string& name)
{
if (!_root) return nullptr; // root has already been removed

MenuElementPtr menuBar = _root->find(name);

if (menuBar)
Expand All @@ -94,6 +96,8 @@ wxMenuBar* MenuManager::getMenuBar(const std::string& name)

wxObject* MenuManager::get(const std::string& path)
{
if (!_root) return nullptr; // root has already been removed

MenuElementPtr element = _root->find(path);

if (element)
Expand All @@ -112,6 +116,8 @@ void MenuManager::add(const std::string& insertPath,
const std::string& icon,
const std::string& eventName)
{
if (!_root) return; // root has already been removed

MenuElementPtr parent = _root->find(insertPath);

if (!parent)
Expand Down Expand Up @@ -243,6 +249,8 @@ void MenuManager::insert(const std::string& insertPath,
const std::string& icon,
const std::string& eventName)
{
if (!_root) return; // root has already been removed

MenuElementPtr insertBefore = _root->find(insertPath);

if (!insertBefore || !insertBefore->getParent())
Expand Down Expand Up @@ -391,6 +399,31 @@ void MenuManager::insert(const std::string& insertPath,

void MenuManager::remove(const std::string& path)
{
if (!_root) return; // root has already been removed

MenuElementPtr element = _root->find(path);

if (!element)
{
return; // no warning, some code just tries to remove stuff unconditionally
}

if (!element->getParent())
{
rWarning() << "Cannot remove item without a parent " << path << std::endl;
return;
}

element->getParent()->removeChild(element);

// The corresponding top level menu needs reconstruction
MenuElementPtr parentMenu = findTopLevelMenu(element);

if (parentMenu)
{
parentMenu->setNeedsRefresh(true);
}

// TODO
#if 0
// Sanity check for empty menu
Expand Down

0 comments on commit 145530c

Please sign in to comment.