From 4390a092f6e9cd5a3e1d51d7abb954ebd1eeee47 Mon Sep 17 00:00:00 2001 From: Andreas Falkenhahn Date: Sat, 5 Dec 2020 22:45:13 +0100 Subject: [PATCH] Send wxEVT_COMBOBOX immediately in wxOSX To fix the problem with GetValue() not returning the updated value from the event handlers, just set the new value forcefully ourselves before generating the event rather than postponing sending the event. This makes the event order under Mac consistent with those elsewhere, i.e. wxEVT_COMBOBOX_CLOSEUP is now received after wxEVT_COMBOBOX there too, and not before, as without this change. See #18973. --- src/osx/cocoa/combobox.mm | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/osx/cocoa/combobox.mm b/src/osx/cocoa/combobox.mm index 7fc69b1276..9277d08d50 100644 --- a/src/osx/cocoa/combobox.mm +++ b/src/osx/cocoa/combobox.mm @@ -139,22 +139,25 @@ - (void)comboBoxSelectionDidChange:(NSNotification *)notification { wxUnusedVar(notification); - wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + wxNSComboBoxControl* const + impl = (wxNSComboBoxControl* ) wxWidgetImpl::FindFromWXWidget( self ); if ( impl && impl->ShouldSendEvents()) { wxComboBox* wxpeer = static_cast(impl->GetWXPeer()); if ( wxpeer ) { const int sel = wxpeer->GetSelection(); + const wxString& val = wxpeer->GetString(sel); + + // We need to manually set the new value because at this time it + // still contains the old value, but we want GetValue() to return + // the new one if it's called from an event handler invoked below. + impl->SetStringValue(val); wxCommandEvent event(wxEVT_COMBOBOX, wxpeer->GetId()); event.SetEventObject( wxpeer ); event.SetInt( sel ); - event.SetString( wxpeer->GetString(sel) ); - // For some reason, wxComboBox::GetValue will not return the newly selected item - // while we're inside this callback, so use AddPendingEvent to make sure - // GetValue() returns the right value. - - wxpeer->GetEventHandler()->AddPendingEvent( event ); + event.SetString( val ); + wxpeer->HandleWindowEvent( event ); } }