]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix selection corner cases in wxOSX wxComboBox.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 22 Aug 2010 22:15:32 +0000 (22:15 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 22 Aug 2010 22:15:32 +0000 (22:15 +0000)
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
src/osx/combobox_osx.cpp

index 1666c33b83e687475b56dcee0521f3a6125fc618..d98f0a331e526f52a279d3c0b81d0e8f47da66d4 100644 (file)
@@ -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);
 }
 
index 214bdbdff4611cb5d23a4247635fc17ca8a1fd0a..8ab1cf9c1e0bc0fe641a1af1ef701e858e8cadb5 100644 (file)
@@ -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)