Fix tool manager handling in ACTION_MENU event processing

The event handler may not have access to a tool manager, so we must
always check for it.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15107
This commit is contained in:
Ian McInerney 2023-07-04 21:33:39 +01:00
parent 2c983155a4
commit 2baf4db2b6
1 changed files with 13 additions and 12 deletions

View File

@ -333,7 +333,6 @@ ACTION_MENU* ACTION_MENU::create() const
TOOL_MANAGER* ACTION_MENU::getToolManager() const TOOL_MANAGER* ACTION_MENU::getToolManager() const
{ {
wxASSERT( m_tool );
return m_tool ? m_tool->GetManager() : nullptr; return m_tool ? m_tool->GetManager() : nullptr;
} }
@ -342,6 +341,8 @@ void ACTION_MENU::updateHotKeys()
{ {
TOOL_MANAGER* toolMgr = getToolManager(); TOOL_MANAGER* toolMgr = getToolManager();
wxASSERT( toolMgr );
for( std::pair<const int, const TOOL_ACTION*>& ii : m_toolActions ) for( std::pair<const int, const TOOL_ACTION*>& ii : m_toolActions )
{ {
int id = ii.first; int id = ii.first;
@ -401,17 +402,18 @@ void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
wxString menuText; wxString menuText;
wxEventType type = aEvent.GetEventType(); wxEventType type = aEvent.GetEventType();
wxWindow* focus = wxWindow::FindFocus(); wxWindow* focus = wxWindow::FindFocus();
TOOL_MANAGER* toolMgr = getToolManager();
if( type == wxEVT_MENU_OPEN ) if( type == wxEVT_MENU_OPEN )
{ {
if( m_dirty && m_tool ) if( m_dirty && toolMgr )
getToolManager()->RunAction<ACTION_MENU*>( ACTIONS::updateMenu, this ); toolMgr->RunAction<ACTION_MENU*>( ACTIONS::updateMenu, this );
wxMenu* parent = dynamic_cast<wxMenu*>( GetParent() ); wxMenu* parent = dynamic_cast<wxMenu*>( GetParent() );
// Don't update the position if this menu has a parent // Don't update the position if this menu has a parent
if( !parent && m_tool ) if( !parent && toolMgr )
g_menu_open_position = getToolManager()->GetMousePosition(); g_menu_open_position = toolMgr->GetMousePosition();
g_last_menu_highlighted_id = 0; g_last_menu_highlighted_id = 0;
} }
@ -477,7 +479,7 @@ void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
} }
// Check if there is a TOOL_ACTION for the given UI ID // Check if there is a TOOL_ACTION for the given UI ID
if( getToolManager()->GetActionManager()->IsActionUIId( m_selected ) ) if( toolMgr && toolMgr->GetActionManager()->IsActionUIId( m_selected ) )
evt = findToolAction( m_selected ); evt = findToolAction( m_selected );
if( !evt ) if( !evt )
@ -535,7 +537,7 @@ void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
// forward the action/update event to the TOOL_MANAGER // forward the action/update event to the TOOL_MANAGER
// clients that don't supply a tool will have to check GetSelected() themselves // clients that don't supply a tool will have to check GetSelected() themselves
if( evt && m_tool ) if( evt && toolMgr )
{ {
wxLogTrace( kicadTraceToolStack, wxS( "ACTION_MENU::OnMenuEvent %s" ), evt->Format() ); wxLogTrace( kicadTraceToolStack, wxS( "ACTION_MENU::OnMenuEvent %s" ), evt->Format() );
@ -559,11 +561,10 @@ void ACTION_MENU::OnMenuEvent( wxMenuEvent& aEvent )
// manager so that immediate actions work. // manager so that immediate actions work.
else else
{ {
evt->SetMousePosition( getToolManager()->GetMousePosition() ); evt->SetMousePosition( toolMgr->GetMousePosition() );
} }
if( m_tool->GetManager() ) toolMgr->ProcessEvent( *evt );
m_tool->GetManager()->ProcessEvent( *evt );
} }
else else
{ {