From 742890efa5a845858b795a14c988054e157fe214 Mon Sep 17 00:00:00 2001 From: codereader Date: Sat, 2 Jan 2021 19:50:30 +0100 Subject: [PATCH] #5127: Add option to prepend custom menu items without having to derive from ResourceTreeView right away. --- libs/wxutil/dataview/ResourceTreeView.cpp | 18 ++++++++++++++++++ libs/wxutil/dataview/ResourceTreeView.h | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/libs/wxutil/dataview/ResourceTreeView.cpp b/libs/wxutil/dataview/ResourceTreeView.cpp index 11b9f54cdb..2c1bd20151 100644 --- a/libs/wxutil/dataview/ResourceTreeView.cpp +++ b/libs/wxutil/dataview/ResourceTreeView.cpp @@ -134,6 +134,19 @@ void ResourceTreeView::SetTreeMode(ResourceTreeView::TreeMode mode) void ResourceTreeView::PopulateContextMenu(wxutil::PopupMenu& popupMenu) { + // Add the pre-registered items first (with an auto-separator) + if (popupMenu.GetMenuItemCount() > 0) + { + popupMenu.addSeparator(); + } + + for (const auto& customItem : _customMenuItems) + { + popupMenu.addItem(customItem); + } + + _customMenuItems.clear(); + if (popupMenu.GetMenuItemCount() > 0) { popupMenu.addSeparator(); @@ -239,6 +252,11 @@ void ResourceTreeView::SetExpandTopLevelItemsAfterPopulation(bool expand) _expandTopLevelItemsAfterPopulation = expand; } +void ResourceTreeView::AddCustomMenuItem(const ui::IMenuItemPtr& item) +{ + _customMenuItems.push_back(item); +} + void ResourceTreeView::_onTreeStorePopulationFinished(TreeModel::PopulationFinishedEvent& ev) { UnselectAll(); diff --git a/libs/wxutil/dataview/ResourceTreeView.h b/libs/wxutil/dataview/ResourceTreeView.h index 9b6967c075..e72a9f8eb0 100644 --- a/libs/wxutil/dataview/ResourceTreeView.h +++ b/libs/wxutil/dataview/ResourceTreeView.h @@ -70,6 +70,8 @@ class ResourceTreeView : bool _expandTopLevelItemsAfterPopulation; std::string _fullNameToSelectAfterPopulation; + std::vector _customMenuItems; + public: ResourceTreeView(wxWindow* parent, const Columns& columns, long style = wxDV_SINGLE); ResourceTreeView(wxWindow* parent, const TreeModel::Ptr& model, const Columns& columns, long style = wxDV_SINGLE); @@ -97,6 +99,11 @@ class ResourceTreeView : void SetExpandTopLevelItemsAfterPopulation(bool expand); + // Add a custom menu item to this control's popup menu (will be added at the top) + // Client code that derive from this class can use the protected PopulateContextMenu + // hook as an alternative to this method. + void AddCustomMenuItem(const ui::IMenuItemPtr& item); + protected: virtual void PopulateContextMenu(wxutil::PopupMenu& popupMenu);