From 6f07c007a54c53c1561944f139254e8afa1477cc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 22 Aug 2010 22:15:32 +0000 Subject: [PATCH] Fix selection corner cases in wxOSX wxComboBox. Don't crash in wxComboBox::GetString() if it's passed an invalid index. Don't call GetString() with invalid index from GetStringSelection() if there is no selection. Do accept wxNOT_FOUND in SetSelectedItem() as it means, according to the docs, that the existing selection should be reset. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65384 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/osx/cocoa/combobox.mm | 16 ++++++++++++++-- src/osx/combobox_osx.cpp | 5 ++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/osx/cocoa/combobox.mm b/src/osx/cocoa/combobox.mm index 1666c33b83..d98f0a331e 100644 --- a/src/osx/cocoa/combobox.mm +++ b/src/osx/cocoa/combobox.mm @@ -95,9 +95,21 @@ int wxNSComboBoxControl::GetSelectedItem() const void wxNSComboBoxControl::SetSelectedItem(int item) { - wxASSERT_MSG(item >= 0 && item < [m_comboBox numberOfItems], "Inavlid item index."); SendEvents(false); - [m_comboBox selectItemAtIndex: item]; + + if ( item != wxNOT_FOUND ) + { + wxASSERT_MSG( item >= 0 && item < [m_comboBox numberOfItems], + "Inavlid item index." ); + [m_comboBox selectItemAtIndex: item]; + } + else // remove current selection (if we have any) + { + const int sel = GetSelectedItem(); + if ( sel != wxNOT_FOUND ) + [m_comboBox deselectItemAtIndex:sel]; + } + SendEvents(true); } diff --git a/src/osx/combobox_osx.cpp b/src/osx/combobox_osx.cpp index 214bdbdff4..8ab1cf9c1e 100644 --- a/src/osx/combobox_osx.cpp +++ b/src/osx/combobox_osx.cpp @@ -186,12 +186,15 @@ int wxComboBox::FindString(const wxString& s, bool bCase) const wxString wxComboBox::GetString(unsigned int n) const { + wxCHECK_MSG( n < GetCount(), wxString(), "Invalid combobox index" ); + return GetComboPeer()->GetStringAtIndex(n); } wxString wxComboBox::GetStringSelection() const { - return GetString(GetSelection()); + const int sel = GetSelection(); + return sel == wxNOT_FOUND ? wxString() : GetString(sel); } void wxComboBox::SetString(unsigned int n, const wxString& s)