Skip to content

Commit

Permalink
Cleanup sweep throughout the menu classes
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 5, 2017
1 parent 72ab9f2 commit 29d5d39
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 340 deletions.
12 changes: 3 additions & 9 deletions include/iuimanager.h
Expand Up @@ -5,7 +5,6 @@

// Forward declarations
class wxWindow;
class wxObject;
class wxToolBar;
class wxMenuBar;

Expand Down Expand Up @@ -45,14 +44,6 @@ class IMenuManager
*/
virtual wxMenuBar* getMenuBar(const std::string& name) = 0;

/** greebo: Retrieves the menuitem widget specified by the path.
*
* Example: get("main/file/open") delivers the widget for the "Open..." command.
*
* @returns: the widget, or NULL, if no the path hasn't been found.
*/
virtual wxObject* get(const std::string& path) = 0;

/** greebo: Shows/hides the menuitem under the given path.
*
* @path: the path to the item (e.g. "main/view/cameraview")
Expand Down Expand Up @@ -91,6 +82,9 @@ class IMenuManager
const std::string& icon,
const std::string& eventName) = 0;

// Returns true if the given path exists
virtual bool exists(const std::string& path) = 0;

/**
* Removes an entire path from the menus.
*/
Expand Down
6 changes: 4 additions & 2 deletions plugins/uimanager/menu/MenuBar.cpp
Expand Up @@ -14,7 +14,7 @@ MenuBar::MenuBar() :
_menuBar(nullptr)
{}

wxMenuBar* MenuBar::getWidget()
wxMenuBar* MenuBar::getMenuBar()
{
if (_menuBar == nullptr)
{
Expand Down Expand Up @@ -64,7 +64,9 @@ MenuElementPtr MenuBar::findMenu(wxMenu* menu)
{
for (const MenuElementPtr& candidate : _children)
{
if (candidate->getWidget() == menu)
if (!std::dynamic_pointer_cast<MenuFolder>(candidate)) continue;

if (std::static_pointer_cast<MenuFolder>(candidate)->getMenu() == menu)
{
return candidate;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/uimanager/menu/MenuBar.h
Expand Up @@ -15,7 +15,7 @@ class MenuBar :
public:
MenuBar();

virtual wxMenuBar* getWidget() override;
virtual wxMenuBar* getMenuBar();

protected:
virtual void construct() override;
Expand Down
247 changes: 2 additions & 245 deletions plugins/uimanager/menu/MenuElement.cpp
Expand Up @@ -2,8 +2,8 @@

#include "i18n.h"
#include "itextstream.h"
#include "iradiant.h"
#include "ieventmanager.h"

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
Expand All @@ -20,7 +20,6 @@
#include "MenuFolder.h"
#include "MenuItem.h"
#include "MenuSeparator.h"
#include "../LocalBitmapArtProvider.h"

namespace ui
{
Expand All @@ -29,17 +28,13 @@ int MenuElement::_nextMenuItemId = 100;

MenuElement::MenuElement(const MenuElementPtr& parent) :
_parent(parent ? MenuElementWeakPtr(parent) : MenuElementWeakPtr()),
_widget(nullptr),
_type(menuNothing),
_constructed(false),
_isVisible(true),
_needsRefresh(false)
{}

MenuElement::~MenuElement()
{
disconnectEvent();
}
{}

std::string MenuElement::getName() const
{
Expand Down Expand Up @@ -171,84 +166,8 @@ int MenuElement::getMenuPosition(const MenuElementPtr& child)
{
return static_cast<int>(std::distance(_children.begin(),
std::find(_children.begin(), _children.end(), child)));

#if 0
for (std::size_t pos = 0; pos < _children.size(); ++i)
{

}

if (!_constructed)
{
construct();
}

// Check if this is the right item type for this operation
if (_type == menuFolder)
{
// A menufolder is a MenuElement with a contained submenu, retrieve it
wxMenu* container = dynamic_cast<wxMenu*>(_widget);

// Get the list of child widgets
wxMenuItemList& children = container->GetMenuItems();

// The child Widget for comparison
wxObject* childWidget = child->getWidget();

int position = 0;
for (wxMenuItemList::const_iterator i = children.begin(); i != children.end(); ++i, ++position)
{
// Get the widget pointer from the current list item
wxMenuItem* item = *i;

if (item == childWidget ||
(child->getType() == menuFolder && item->GetSubMenu() == childWidget))
{
return position;
}
}
}
else if (_type == menuBar)
{
wxMenuBar* container = dynamic_cast<wxMenuBar*>(_widget);

if (container == NULL)
{
rWarning() << "Cannot find menu position, cannot cast to wxMenuBar." << std::endl;
return -1;
}

// The child Widget for comparison
wxObject* childWidget = child->getWidget();

// Iterate over all registered menus
for (int position = 0; position < container->GetMenuCount(); ++position)
{
// Get the widget pointer from the current list item
if (container->GetMenu(position) == childWidget)
{
return position;
}
}
}

return -1; // not found or wrong type
#endif
}

#if 0
wxObject* MenuElement::getWidget()
{
// Check for toggle, allocate the Gtk::Widget*
if (!_constructed)
{
construct();
}

return _widget;
}
#endif

MenuElementPtr MenuElement::find(const std::string& menuPath)
{
// Split the path and analyse it
Expand Down Expand Up @@ -284,159 +203,6 @@ MenuElementPtr MenuElement::find(const std::string& menuPath)
return MenuElementPtr();
}

#if 0
void MenuElement::construct()
{
if (_type == menuBar)
{
wxMenuBar* menuBar = new wxMenuBar;
_widget = menuBar;

for (std::size_t i = 0; i < _children.size(); i++)
{
// Cast each children onto wxMenu and append it to the menu
wxMenu* menu = dynamic_cast<wxMenu*>(_children[i]->getWidget());

if (menu != NULL)
{
menuBar->Append(menu, _children[i]->getCaption());
}
else
{
rError() << "MenuElement::construct: Cannot cast child to wxMenu" << std::endl;
}
}
}
else if (_type == menuSeparator)
{
_widget = NULL; // separator is handled when adding to the parent menu itself
}
else if (_type == menuFolder)
{
// Create the MenuElement, don't pass a title to the constructor,
// otherwise the caption gets immediately added as first child
wxMenu* menu = new wxMenu;
_widget = menu;

for (std::size_t i = 0; i < _children.size(); i++)
{
if (_children[i]->getType() == menuSeparator)
{
_children[i]->_widget = menu->AppendSeparator();
continue;
}

wxMenuItem* menuItem = dynamic_cast<wxMenuItem*>(_children[i]->getWidget());

if (menuItem != NULL)
{
menu->Append(menuItem);

if (_children[i]->getEvent().empty())
{
// No event attached to this menu item, disable it
menu->Enable(menuItem->GetId(), false);
continue;
}

// Now is the time to connect the event, the item has a valid menu parent at this point
IEventPtr event = GlobalEventManager().findEvent(_children[i]->getEvent());

if (event != NULL)
{
event->connectMenuItem(menuItem);
}

continue;
}

wxMenu* subMenu = dynamic_cast<wxMenu*>(_children[i]->getWidget());

if (subMenu != NULL)
{
menu->AppendSubMenu(subMenu, _children[i]->getCaption());
continue;
}
}
}
else if (_type == menuItem)
{
if (!_event.empty())
{
// Try to lookup the event name
IEventPtr event = GlobalEventManager().findEvent(_event);

if (!event->empty())
{
// Retrieve an accelerator string formatted for a menu
const std::string accelText =
GlobalEventManager().getAcceleratorStr(event, true);

// Create a new MenuElement
// greebo: Accelerators seem to globally catch the key events, add a space to fool wxWidgets
wxMenuItem* item = new wxMenuItem(NULL, _nextMenuItemId++, _caption + "\t " + accelText);
_widget = item;

if (!_icon.empty())
{
item->SetBitmap(wxArtProvider::GetBitmap(LocalBitmapArtProvider::ArtIdPrefix() + _icon));
}

item->SetCheckable(event->isToggle());
}
else
{
rWarning() << "MenuElement: Cannot find associated event: " << _event << std::endl;
}
}
else
{
// Create an empty, desensitised MenuElement, will be disabled once attached to the parent menu
wxMenuItem* item = new wxMenuItem(NULL, _nextMenuItemId++, _caption.empty() ? "-" : _caption);

_widget = item;
}
}
else if (_type == menuRoot)
{
// Cannot instantiate root MenuElement, ignore
}

_constructed = true;
}
#endif

void MenuElement::connectEvent()
{
if (!_event.empty() && _type == menuItem)
{
// Try to lookup the event name
IEventPtr event = GlobalEventManager().findEvent(_event);
wxMenuItem* MenuElement = dynamic_cast<wxMenuItem*>(_widget);

if (event != NULL && MenuElement != NULL)
{
event->connectMenuItem(MenuElement);
}
}
}

void MenuElement::disconnectEvent()
{
if (!_event.empty() && _type == menuItem)
{
IEventPtr ev = GlobalEventManager().findEvent(_event);
wxMenuItem* item = dynamic_cast<wxMenuItem*>(_widget);

// Tell the eventmanager to disconnect the widget in any case
// even if has been destroyed already.
if (ev != NULL && item != NULL)
{
ev->disconnectMenuItem(item);
}
}
}

bool MenuElement::needsRefresh()
{
return _needsRefresh;
Expand All @@ -447,15 +213,6 @@ void MenuElement::setNeedsRefresh(bool needsRefresh)
_needsRefresh = needsRefresh;
}

void MenuElement::setWidget(wxObject* object)
{
// Disconnect the old widget before setting a new one
disconnectEvent();

_widget = object;
_constructed = true;
}

MenuElementPtr MenuElement::CreateFromNode(const xml::Node& node)
{
MenuElementPtr item;
Expand Down
15 changes: 1 addition & 14 deletions plugins/uimanager/menu/MenuElement.h
Expand Up @@ -36,20 +36,15 @@ class MenuElement :
// The icon name
std::string _icon;

// The associated event
// The associated event name
std::string _event;

wxObject* _widget;

// The children of this MenuElement
typedef std::vector<MenuElementPtr> MenuElementList;
MenuElementList _children;

eMenuItemType _type;

// Stays false until the widgets are actually created.
bool _constructed;

bool _isVisible;

// Checked if anything in this item or below has changed
Expand Down Expand Up @@ -122,17 +117,9 @@ class MenuElement :
std::string getEvent() const;
void setEvent(const std::string& eventName);

void connectEvent();
void disconnectEvent();

bool needsRefresh();
void setNeedsRefresh(bool needsRefresh);

// Use this to get the corresponding wx menu widget out of this item.
virtual wxObject* getWidget() = 0;

void setWidget(wxObject* object);

// Tries to (recursively) locate the MenuElement by looking up the path
MenuElementPtr find(const std::string& menuPath);

Expand Down

0 comments on commit 29d5d39

Please sign in to comment.