Merge branch 'mac-handle-menu-item'

Fix a possible crash in Mac menu item event handling.

See #23040, #23100.
This commit is contained in:
Vadim Zeitlin 2022-12-31 17:00:50 +01:00
commit d868c3b3e2
3 changed files with 20 additions and 13 deletions

View file

@ -86,9 +86,11 @@ SEL wxOSXGetSelectorFromID(int menuId )
wxUnusedVar(sender);
if ( impl )
{
wxMenuItem* menuitem = impl->GetWXPeer();
if ( menuitem->GetMenu()->HandleCommandProcess(menuitem) == false )
if ( wxMenuItem* menuitem = impl->GetWXPeer() )
{
// Ignore the return value as there doesn't seem anything to do
// with it here.
menuitem->GetMenu()->HandleCommandProcess(menuitem);
}
}
}

View file

@ -367,8 +367,8 @@ extern int wxOSXGetIdFromSelector(SEL action );
wxMenuItemImpl* impl = [nsMenuItem implementation];
if ( impl )
{
wxMenuItem* menuitem = impl->GetWXPeer();
return menuitem->GetMenu()->HandleCommandProcess(menuitem);
if ( wxMenuItem* menuitem = impl->GetWXPeer() )
return menuitem->GetMenu()->HandleCommandProcess(menuitem);
}
}
// otherwise feed back command into common menubar

View file

@ -359,25 +359,30 @@ bool wxMenu::HandleCommandUpdateStatus( wxMenuItem* item )
bool wxMenu::HandleCommandProcess( wxMenuItem* item )
{
int menuid = item ? item->GetId() : 0;
wxCHECK_MSG( item, false, "must have a valid item" );
int menuid = item->GetId();
bool processed = false;
if (item->IsCheckable())
item->Check( !item->IsChecked() ) ;
if ( SendEvent( menuid , item->IsCheckable() ? item->IsChecked() : -1 ) )
processed = true ;
if(!processed && item)
{
processed = item->GetPeer()->DoDefault();
}
// A bit counterintuitively, we call OSXAfterMenuEvent() _before_ calling
// the user defined handler. This is done to account for the case when this
// handler deletes the window, as it can possibly do.
if (wxWindow* const w = GetInvokingWindow())
{
// Let the invoking window update itself if necessary.
w->OSXAfterMenuEvent();
}
if ( SendEvent( menuid , item->IsCheckable() ? item->IsChecked() : -1 ) )
processed = true ;
if(!processed)
{
processed = item->GetPeer()->DoDefault();
}
return processed;
}