diff --git a/common/tool/context_menu.cpp b/common/tool/context_menu.cpp index c7fb43f3ea..9211ea8ae8 100644 --- a/common/tool/context_menu.cpp +++ b/common/tool/context_menu.cpp @@ -286,15 +286,14 @@ void CONTEXT_MENU::onMenuEvent( wxMenuEvent& aEvent ) // One of menu entries was selected.. else if( type == wxEVT_COMMAND_MENU_SELECTED ) { - // Store the selected position + // Store the selected position, so it can be checked by the tools m_selected = aEvent.GetId(); // Check if there is a TOOL_ACTION for the given ID - if( m_toolActions.count( aEvent.GetId() ) == 1 ) - { - evt = m_toolActions[aEvent.GetId()]->MakeEvent(); - } - else + if( m_selected >= ACTION_ID ) + evt = findToolAction( m_selected ); + + if( !evt ) { #ifdef __WINDOWS__ if( !evt ) @@ -344,7 +343,10 @@ void CONTEXT_MENU::runOnSubmenus( std::function aFunction ) { try { - std::for_each( m_submenus.begin(), m_submenus.end(), aFunction ); + std::for_each( m_submenus.begin(), m_submenus.end(), [&]( CONTEXT_MENU* m ) { + aFunction( m ); + m->runOnSubmenus( aFunction ); + } ); } catch( std::exception& e ) { @@ -353,6 +355,29 @@ void CONTEXT_MENU::runOnSubmenus( std::function aFunction ) } +OPT_TOOL_EVENT CONTEXT_MENU::findToolAction( int aId ) +{ + OPT_TOOL_EVENT evt; + + auto findFunc = [&]( CONTEXT_MENU* m ) { + if( evt ) + return; + + const auto it = m->m_toolActions.find( aId ); + + if( it != m->m_toolActions.end() ) + evt = it->second->MakeEvent(); + }; + + findFunc( this ); + + if( !evt ) + runOnSubmenus( findFunc ); + + return evt; +} + + void CONTEXT_MENU::copyFrom( const CONTEXT_MENU& aMenu ) { SetTitle( aMenu.GetTitle() ); diff --git a/include/tool/context_menu.h b/include/tool/context_menu.h index dc68c5160f..ab9051f725 100644 --- a/include/tool/context_menu.h +++ b/include/tool/context_menu.h @@ -199,6 +199,9 @@ private: ///> Runs a function on the menu and all its submenus. void runOnSubmenus( std::function aFunction ); + ///> Checks if any of submenus contains a TOOL_ACTION with a specific ID. + OPT_TOOL_EVENT findToolAction( int aId ); + ///> Flag indicating that the menu title was set up. bool m_titleDisplayed;