Turn the toolbar context menus into unique_ptr for easier management

This commit is contained in:
Ian McInerney 2020-10-01 22:55:17 +01:00
parent 454c4f1783
commit b69d396afc
3 changed files with 28 additions and 31 deletions

View File

@ -188,11 +188,12 @@ ACTION_TOOLBAR::~ACTION_TOOLBAR()
{ {
delete m_paletteTimer; delete m_paletteTimer;
// Delete all the menus // Clear all the maps keeping track of our items on the toolbar
for( auto it = m_toolMenus.begin(); it != m_toolMenus.end(); it++ )
delete it->second;
m_toolMenus.clear(); m_toolMenus.clear();
m_actionGroups.clear();
m_toolCancellable.clear();
m_toolKinds.clear();
m_toolActions.clear();
} }
@ -242,23 +243,12 @@ void ACTION_TOOLBAR::AddScaledSeparator( wxWindow* aWindow )
} }
void ACTION_TOOLBAR::AddToolContextMenu( const TOOL_ACTION& aAction, ACTION_MENU* aMenu ) void ACTION_TOOLBAR::AddToolContextMenu( const TOOL_ACTION& aAction,
std::unique_ptr<ACTION_MENU> aMenu )
{ {
int toolId = aAction.GetUIId(); int toolId = aAction.GetUIId();
// If this is replacing an existing menu, delete the existing menu before adding the new one m_toolMenus[toolId] = std::move( aMenu );
const auto it = m_toolMenus.find( toolId );
if( it != m_toolMenus.end() )
{
// Don't delete it if it is the same menu, just ignore this call
if( it->second == aMenu )
return;
delete it->second;
}
m_toolMenus[toolId] = aMenu;
} }
@ -333,12 +323,14 @@ void ACTION_TOOLBAR::doSelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aA
void ACTION_TOOLBAR::ClearToolbar() void ACTION_TOOLBAR::ClearToolbar()
{ {
// Delete all the menus // Clear all the maps keeping track of our items on the toolbar
for( auto it = m_toolMenus.begin(); it != m_toolMenus.end(); it++ )
delete it->second;
// Clear the menu items and the actual toolbar
m_toolMenus.clear(); m_toolMenus.clear();
m_actionGroups.clear();
m_toolCancellable.clear();
m_toolKinds.clear();
m_toolActions.clear();
// Remove the actual tools from the toolbar
Clear(); Clear();
} }
@ -436,7 +428,10 @@ void ACTION_TOOLBAR::onToolRightClick( wxAuiToolBarEvent& aEvent )
return; return;
// Update and show the menu // Update and show the menu
ACTION_MENU* menu = menuIt->second; std::unique_ptr<ACTION_MENU>& owningMenu = menuIt->second;
// Get the actual menu pointer to show it
ACTION_MENU* menu = owningMenu.get();
SELECTION dummySel; SELECTION dummySel;
if( CONDITIONAL_MENU* condMenu = dynamic_cast<CONDITIONAL_MENU*>( menu ) ) if( CONDITIONAL_MENU* condMenu = dynamic_cast<CONDITIONAL_MENU*>( menu ) )

View File

@ -25,6 +25,7 @@
#define ACTION_TOOLBAR_H #define ACTION_TOOLBAR_H
#include <map> #include <map>
#include <memory>
#include <vector> #include <vector>
#include <wx/bitmap.h> // Needed for the auibar include #include <wx/bitmap.h> // Needed for the auibar include
#include <wx/aui/auibar.h> #include <wx/aui/auibar.h>
@ -232,7 +233,7 @@ public:
* @param aAction is the action to get the menu * @param aAction is the action to get the menu
* @param aMenu is the context menu * @param aMenu is the context menu
*/ */
void AddToolContextMenu( const TOOL_ACTION& aAction, ACTION_MENU* aMenu ); void AddToolContextMenu( const TOOL_ACTION& aAction, std::unique_ptr<ACTION_MENU> aMenu );
/** /**
* Add a set of actions to a toolbar as a group. One action from the group will be displayed * Add a set of actions to a toolbar as a group. One action from the group will be displayed
@ -321,9 +322,9 @@ protected:
std::map<int, bool> m_toolKinds; std::map<int, bool> m_toolKinds;
std::map<int, bool> m_toolCancellable; std::map<int, bool> m_toolCancellable;
std::map<int, const TOOL_ACTION*> m_toolActions; std::map<int, const TOOL_ACTION*> m_toolActions;
std::map<int, ACTION_MENU*> m_toolMenus;
std::map<int, ACTION_GROUP*> m_actionGroups; std::map<int, ACTION_GROUP*> m_actionGroups;
std::map<int, std::unique_ptr<ACTION_MENU>> m_toolMenus;
}; };
#endif #endif

View File

@ -451,19 +451,20 @@ void PCB_EDIT_FRAME::ReCreateVToolbar()
m_drawToolBar->Add( ACTIONS::measureTool, ACTION_TOOLBAR::TOGGLE ); m_drawToolBar->Add( ACTIONS::measureTool, ACTION_TOOLBAR::TOGGLE );
SELECTION_TOOL* selTool = m_toolManager->GetTool<SELECTION_TOOL>(); SELECTION_TOOL* selTool = m_toolManager->GetTool<SELECTION_TOOL>();
ACTION_MENU* routeMenu = new ACTION_MENU( false, selTool );
std::unique_ptr<ACTION_MENU> routeMenu = std::make_unique<ACTION_MENU>( false, selTool );
routeMenu->Add( PCB_ACTIONS::routerHighlightMode, ACTION_MENU::CHECK ); routeMenu->Add( PCB_ACTIONS::routerHighlightMode, ACTION_MENU::CHECK );
routeMenu->Add( PCB_ACTIONS::routerShoveMode, ACTION_MENU::CHECK ); routeMenu->Add( PCB_ACTIONS::routerShoveMode, ACTION_MENU::CHECK );
routeMenu->Add( PCB_ACTIONS::routerWalkaroundMode, ACTION_MENU::CHECK ); routeMenu->Add( PCB_ACTIONS::routerWalkaroundMode, ACTION_MENU::CHECK );
routeMenu->AppendSeparator(); routeMenu->AppendSeparator();
routeMenu->Add( PCB_ACTIONS::routerSettingsDialog ); routeMenu->Add( PCB_ACTIONS::routerSettingsDialog );
m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::routeSingleTrack, routeMenu ); m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::routeSingleTrack, std::move( routeMenu ) );
ACTION_MENU* zoneMenu = new ACTION_MENU( false, selTool ); std::unique_ptr<ACTION_MENU> zoneMenu = std::make_unique<ACTION_MENU>( false, selTool );
zoneMenu->Add( PCB_ACTIONS::zoneFillAll ); zoneMenu->Add( PCB_ACTIONS::zoneFillAll );
zoneMenu->Add( PCB_ACTIONS::zoneUnfillAll ); zoneMenu->Add( PCB_ACTIONS::zoneUnfillAll );
m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::drawZone, zoneMenu ); m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::drawZone, std::move( zoneMenu ) );
m_drawToolBar->Realize(); m_drawToolBar->Realize();
} }