From c369c792a355ec1e422bf6f423131af1444a8809 Mon Sep 17 00:00:00 2001 From: ikamakj Date: Sat, 30 Jun 2018 23:05:57 +0200 Subject: [PATCH] Fix selection event generation in wxMac wxListBox Prevents deselecting the selected item in single-selection listbox. Also generate correct events in the multi-selection case by reusing the existing wxListBoxBase::CalcAndSendEvent() method. Closes #15603. --- include/wx/osx/listbox.h | 6 ++++++ src/osx/cocoa/listbox.mm | 40 +++++++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/wx/osx/listbox.h b/include/wx/osx/listbox.h index e96b186d7a..f716bf1ae8 100644 --- a/include/wx/osx/listbox.h +++ b/include/wx/osx/listbox.h @@ -125,6 +125,12 @@ public: bool MacGetBlockEvents() const { return m_blockEvents; } virtual void HandleLineEvent( unsigned int n, bool doubleClick ); + + // These are called by wxNSTableView + using wxListBoxBase::DoChangeSingleSelection; + using wxListBoxBase::CalcAndSendEvent; + int GetOldSelection() const { return m_oldSelections.empty() ? wxNOT_FOUND : m_oldSelections[0]; } + protected: // callback for derived classes which may have to insert additional data // at a certain line - which cannot be predetermined for sorted list data diff --git a/src/osx/cocoa/listbox.mm b/src/osx/cocoa/listbox.mm index 2078be5035..402de57c04 100644 --- a/src/osx/cocoa/listbox.mm +++ b/src/osx/cocoa/listbox.mm @@ -297,24 +297,34 @@ protected: int row = [self selectedRow]; - if (row == -1) - { - // no row selected - } - else - { - wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); - wxListBox *list = static_cast ( impl->GetWXPeer()); - wxCHECK_RET( list != NULL , wxT("Listbox expected")); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + wxListBox *list = static_cast ( impl->GetWXPeer()); + wxCHECK_RET( list != NULL , wxT("Listbox expected")); - if ((row < 0) || (row > (int) list->GetCount())) // OS X can select an item below the last item - return; - - if ( !list->MacGetBlockEvents() ) - list->HandleLineEvent( row, false ); + // Correct notification events for multiselection list, like in Carbon version + if (list->HasMultipleSelection() && !list->MacGetBlockEvents()) + { + list->CalcAndSendEvent(); + return; } -} + if ( !list->MacGetBlockEvents() ) + { + // OS X can select an item below the last item. In that case keep the old selection because + // in wxWidgets API there is no notification event for removing the selection from a single-selection list box. + // Otherwise call DoChangeSingleSelection so GetOldSelection() will return the correct value if row < 0 later. + if ((row < 0) || (row > (int) list->GetCount())) + { + int oldsel = list->GetOldSelection(); + if (oldsel >= 0) + list->SetSelection(oldsel); + return; + } + if ( !list->DoChangeSingleSelection(row) ) + return ; + list->HandleLineEvent( row, false ); + } +} - (void)setFont:(NSFont *)aFont {