From 29d5d3911eeeb2c349af216f13d000f580f7b0ba Mon Sep 17 00:00:00 2001 From: codereader Date: Thu, 5 Jan 2017 10:06:02 +0100 Subject: [PATCH] Cleanup sweep throughout the menu classes --- include/iuimanager.h | 12 +- plugins/uimanager/menu/MenuBar.cpp | 6 +- plugins/uimanager/menu/MenuBar.h | 2 +- plugins/uimanager/menu/MenuElement.cpp | 247 +---------------------- plugins/uimanager/menu/MenuElement.h | 15 +- plugins/uimanager/menu/MenuFolder.cpp | 6 +- plugins/uimanager/menu/MenuFolder.h | 2 +- plugins/uimanager/menu/MenuItem.cpp | 4 +- plugins/uimanager/menu/MenuItem.h | 2 +- plugins/uimanager/menu/MenuManager.cpp | 32 +-- plugins/uimanager/menu/MenuManager.h | 40 ++-- plugins/uimanager/menu/MenuRootElement.h | 5 - plugins/uimanager/menu/MenuSeparator.cpp | 4 +- plugins/uimanager/menu/MenuSeparator.h | 2 +- radiant/ui/mainframe/LayoutCommand.h | 24 ++- 15 files changed, 63 insertions(+), 340 deletions(-) diff --git a/include/iuimanager.h b/include/iuimanager.h index 653d1d3b2e..4da60d5254 100644 --- a/include/iuimanager.h +++ b/include/iuimanager.h @@ -5,7 +5,6 @@ // Forward declarations class wxWindow; -class wxObject; class wxToolBar; class wxMenuBar; @@ -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") @@ -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. */ diff --git a/plugins/uimanager/menu/MenuBar.cpp b/plugins/uimanager/menu/MenuBar.cpp index dbc7318eff..ea81ec87ed 100644 --- a/plugins/uimanager/menu/MenuBar.cpp +++ b/plugins/uimanager/menu/MenuBar.cpp @@ -14,7 +14,7 @@ MenuBar::MenuBar() : _menuBar(nullptr) {} -wxMenuBar* MenuBar::getWidget() +wxMenuBar* MenuBar::getMenuBar() { if (_menuBar == nullptr) { @@ -64,7 +64,9 @@ MenuElementPtr MenuBar::findMenu(wxMenu* menu) { for (const MenuElementPtr& candidate : _children) { - if (candidate->getWidget() == menu) + if (!std::dynamic_pointer_cast(candidate)) continue; + + if (std::static_pointer_cast(candidate)->getMenu() == menu) { return candidate; } diff --git a/plugins/uimanager/menu/MenuBar.h b/plugins/uimanager/menu/MenuBar.h index d440a95dfd..672e2ea645 100644 --- a/plugins/uimanager/menu/MenuBar.h +++ b/plugins/uimanager/menu/MenuBar.h @@ -15,7 +15,7 @@ class MenuBar : public: MenuBar(); - virtual wxMenuBar* getWidget() override; + virtual wxMenuBar* getMenuBar(); protected: virtual void construct() override; diff --git a/plugins/uimanager/menu/MenuElement.cpp b/plugins/uimanager/menu/MenuElement.cpp index c31597035b..4fac467302 100644 --- a/plugins/uimanager/menu/MenuElement.cpp +++ b/plugins/uimanager/menu/MenuElement.cpp @@ -2,8 +2,8 @@ #include "i18n.h" #include "itextstream.h" -#include "iradiant.h" #include "ieventmanager.h" + #include #include #include @@ -20,7 +20,6 @@ #include "MenuFolder.h" #include "MenuItem.h" #include "MenuSeparator.h" -#include "../LocalBitmapArtProvider.h" namespace ui { @@ -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 { @@ -171,84 +166,8 @@ int MenuElement::getMenuPosition(const MenuElementPtr& child) { return static_cast(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(_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(_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 @@ -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(_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(_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(_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(_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(_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; @@ -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; diff --git a/plugins/uimanager/menu/MenuElement.h b/plugins/uimanager/menu/MenuElement.h index 76d1d8f79b..bec5dea0fe 100644 --- a/plugins/uimanager/menu/MenuElement.h +++ b/plugins/uimanager/menu/MenuElement.h @@ -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 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 @@ -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); diff --git a/plugins/uimanager/menu/MenuFolder.cpp b/plugins/uimanager/menu/MenuFolder.cpp index d02900e35f..9d4a30823a 100644 --- a/plugins/uimanager/menu/MenuFolder.cpp +++ b/plugins/uimanager/menu/MenuFolder.cpp @@ -15,7 +15,7 @@ MenuFolder::MenuFolder() : _parentItem(nullptr) {} -wxMenu* MenuFolder::getWidget() +wxMenu* MenuFolder::getMenu() { if (_menu == nullptr) { @@ -56,12 +56,12 @@ void MenuFolder::construct() if (std::dynamic_pointer_cast(parent)) { - wxMenuBar* bar = std::static_pointer_cast(parent)->getWidget(); + wxMenuBar* bar = std::static_pointer_cast(parent)->getMenuBar(); bar->Append(_menu, getCaption()); } else if (std::dynamic_pointer_cast(parent)) { - wxMenu* parentMenu = std::static_pointer_cast(parent)->getWidget(); + wxMenu* parentMenu = std::static_pointer_cast(parent)->getMenu(); _parentItem = parentMenu->AppendSubMenu(_menu, getCaption()); } } diff --git a/plugins/uimanager/menu/MenuFolder.h b/plugins/uimanager/menu/MenuFolder.h index 49d631378d..61a06c7da0 100644 --- a/plugins/uimanager/menu/MenuFolder.h +++ b/plugins/uimanager/menu/MenuFolder.h @@ -19,7 +19,7 @@ class MenuFolder : public: MenuFolder(); - virtual wxMenu* getWidget() override; + virtual wxMenu* getMenu(); // Empties this menu and rebuilds the wxWidget objects // Clears the needsRefresh flag on this object and all children diff --git a/plugins/uimanager/menu/MenuItem.cpp b/plugins/uimanager/menu/MenuItem.cpp index f1dae19489..615f5a9bb2 100644 --- a/plugins/uimanager/menu/MenuItem.cpp +++ b/plugins/uimanager/menu/MenuItem.cpp @@ -13,7 +13,7 @@ MenuItem::MenuItem() : _menuItem(nullptr) {} -wxMenuItem* MenuItem::getWidget() +wxMenuItem* MenuItem::getMenuItem() { if (_menuItem == nullptr) { @@ -46,7 +46,7 @@ void MenuItem::construct() return; } - wxMenu* menu = std::static_pointer_cast(parent)->getWidget(); + wxMenu* menu = std::static_pointer_cast(parent)->getMenu(); std::string caption = _caption; diff --git a/plugins/uimanager/menu/MenuItem.h b/plugins/uimanager/menu/MenuItem.h index 736259ffde..230a289ec3 100644 --- a/plugins/uimanager/menu/MenuItem.h +++ b/plugins/uimanager/menu/MenuItem.h @@ -16,7 +16,7 @@ class MenuItem : public: MenuItem(); - virtual wxMenuItem* getWidget() override; + virtual wxMenuItem* getMenuItem(); protected: virtual void construct() override; diff --git a/plugins/uimanager/menu/MenuManager.cpp b/plugins/uimanager/menu/MenuManager.cpp index 4c3ff5463c..3280780d7a 100644 --- a/plugins/uimanager/menu/MenuManager.cpp +++ b/plugins/uimanager/menu/MenuManager.cpp @@ -3,14 +3,6 @@ #include "itextstream.h" #include "iregistry.h" -#include -#include - -#include -#include -#include -#include - #include "MenuBar.h" #include "MenuFolder.h" #include "MenuRootElement.h" @@ -87,28 +79,13 @@ wxMenuBar* MenuManager::getMenuBar(const std::string& name) { assert(std::dynamic_pointer_cast(menuBar)); - return std::static_pointer_cast(menuBar)->getWidget(); + return std::static_pointer_cast(menuBar)->getMenuBar(); } rError() << "MenuManager: Warning: Menubar with name " << name << " not found!" << std::endl; return nullptr; } -wxObject* MenuManager::get(const std::string& path) -{ - if (!_root) return nullptr; // root has already been removed - - MenuElementPtr element = _root->find(path); - - if (element) - { - return element->getWidget(); - } - - rError() << "MenuManager: Warning: Menu " << path << " not found!" << std::endl; - return nullptr; -} - void MenuManager::add(const std::string& insertPath, const std::string& name, eMenuItemType type, @@ -397,6 +374,13 @@ void MenuManager::insert(const std::string& insertPath, #endif } +bool MenuManager::exists(const std::string& path) +{ + if (!_root) return false; // root has already been removed + + return _root->find(path) != nullptr; +} + void MenuManager::remove(const std::string& path) { if (!_root) return; // root has already been removed diff --git a/plugins/uimanager/menu/MenuManager.h b/plugins/uimanager/menu/MenuManager.h index b53c4da75c..f2ab5fcf34 100644 --- a/plugins/uimanager/menu/MenuManager.h +++ b/plugins/uimanager/menu/MenuManager.h @@ -2,10 +2,10 @@ #include #include "iuimanager.h" -#include "MenuElement.h" -/** greebo: The MenuManager takes care of adding and inserting the - * menuitems at the given paths. +/** + * greebo: The MenuManager takes care of adding and inserting the + * menuitems at the given paths. * * A valid menupath is for example: "main/file/exit" and consists of: * @@ -14,11 +14,13 @@ * The first part of the path is the name of the menubar you want to access. * (the MenuManager can of course keep track of several menubars). * - * Use the add() and insert() commands to create menuitems, the GtkWidget* - * pointers are delivered as return value. - * + * Use the add() and insert() commands to create menuitems. */ -namespace ui { +namespace ui +{ + +class MenuElement; +typedef std::shared_ptr MenuElementPtr; class MenuManager : public IMenuManager @@ -33,8 +35,6 @@ class MenuManager : wxMenuBar* getMenuBar(const std::string& name) override; - wxObject* get(const std::string& path) override; - void setVisibility(const std::string& path, bool visible) override; /** greebo: Adds a new item as child under the given path. @@ -47,11 +47,11 @@ class MenuManager : * @eventname: the event name (e.g. "ToggleShowSizeInfo") */ void add(const std::string& insertPath, - const std::string& name, - ui::eMenuItemType type, - const std::string& caption, - const std::string& icon, - const std::string& eventName) override; + const std::string& name, + eMenuItemType type, + const std::string& caption, + const std::string& icon, + const std::string& eventName) override; /** greebo: Inserts a new menuItem as sibling _before_ the given insertPath. * @@ -62,11 +62,13 @@ class MenuManager : * @eventName: the event name this item is associated with (can be empty). */ void insert(const std::string& insertPath, - const std::string& name, - ui::eMenuItemType type, - const std::string& caption, - const std::string& icon, - const std::string& eventName) override; + const std::string& name, + eMenuItemType type, + const std::string& caption, + const std::string& icon, + const std::string& eventName) override; + + bool exists(const std::string& path) override; /** * Removes an entire menu subtree. diff --git a/plugins/uimanager/menu/MenuRootElement.h b/plugins/uimanager/menu/MenuRootElement.h index a07d3d85f7..9399baaec8 100644 --- a/plugins/uimanager/menu/MenuRootElement.h +++ b/plugins/uimanager/menu/MenuRootElement.h @@ -19,11 +19,6 @@ class MenuRootElement : deconstructChildren(); } - wxObject* getWidget() override - { - return nullptr; - } - protected: void construct() override {} diff --git a/plugins/uimanager/menu/MenuSeparator.cpp b/plugins/uimanager/menu/MenuSeparator.cpp index 1fe0fab2a2..d649c1b577 100644 --- a/plugins/uimanager/menu/MenuSeparator.cpp +++ b/plugins/uimanager/menu/MenuSeparator.cpp @@ -12,7 +12,7 @@ MenuSeparator::MenuSeparator() : _separator(nullptr) {} -wxMenuItem* MenuSeparator::getWidget() +wxMenuItem* MenuSeparator::getMenuItem() { if (_separator == nullptr) { @@ -43,7 +43,7 @@ void MenuSeparator::construct() return; } - wxMenu* menu = std::static_pointer_cast(parent)->getWidget(); + wxMenu* menu = std::static_pointer_cast(parent)->getMenu(); _separator = menu->AppendSeparator(); } diff --git a/plugins/uimanager/menu/MenuSeparator.h b/plugins/uimanager/menu/MenuSeparator.h index 8e92e7ec6d..b36c51f52c 100644 --- a/plugins/uimanager/menu/MenuSeparator.h +++ b/plugins/uimanager/menu/MenuSeparator.h @@ -15,7 +15,7 @@ class MenuSeparator : public: MenuSeparator(); - virtual wxMenuItem* getWidget() override; + virtual wxMenuItem* getMenuItem(); protected: virtual void construct() override; diff --git a/radiant/ui/mainframe/LayoutCommand.h b/radiant/ui/mainframe/LayoutCommand.h index 32c4d66a29..7c047528d8 100644 --- a/radiant/ui/mainframe/LayoutCommand.h +++ b/radiant/ui/mainframe/LayoutCommand.h @@ -12,14 +12,16 @@ #include #include -namespace ui { +namespace ui +{ - namespace { - const std::string MENU_LAYOUTS_PARENT = "main/view"; - const std::string MENU_LAYOUTS = "layouts"; - const std::string MENU_LAYOUTS_PATH = MENU_LAYOUTS_PARENT + "/" + MENU_LAYOUTS; - const std::string MENU_LAYOUTS_INSERT_BEFORE = "main/view/camera"; - } +namespace +{ + const std::string MENU_LAYOUTS_PARENT = "main/view"; + const std::string MENU_LAYOUTS = "layouts"; + const std::string MENU_LAYOUTS_PATH = MENU_LAYOUTS_PARENT + "/" + MENU_LAYOUTS; + const std::string MENU_LAYOUTS_INSERT_BEFORE = "main/view/camera"; +} /** * This little class represents a "command target", providing @@ -46,7 +48,7 @@ class LayoutCommand IMenuManager& menuManager = GlobalUIManager().getMenuManager(); // Add a new folder, if not existing yet - if (menuManager.get(MENU_LAYOUTS_PATH) == NULL) + if (!menuManager.exists(MENU_LAYOUTS_PATH)) { menuManager.insert( MENU_LAYOUTS_INSERT_BEFORE, @@ -75,9 +77,9 @@ class LayoutCommand void activateLayout(const cmd::ArgumentList& args) { GlobalMainFrame().setActiveLayoutName(_layoutName); - wxutil::Messagebox::Show("Restart required", - "Restart DarkRadiant to apply changes", - ui::IDialog::MESSAGE_CONFIRM); + wxutil::Messagebox::Show(_("Restart required"), + _("Restart DarkRadiant to apply changes"), + IDialog::MESSAGE_CONFIRM); } }; typedef std::shared_ptr LayoutCommandPtr;