]> git.saurik.com Git - wxWidgets.git/commitdiff
Keep the item being updated selected in wxMSW wxChoice::SetString().
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 16 Dec 2011 19:47:58 +0000 (19:47 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 16 Dec 2011 19:47:58 +0000 (19:47 +0000)
Changing the text of the selected wxChoice (or wxComboBox, as it derives from
it in wxMSW) item made it unselected. Fix this by explicitly restoring the
selection to the item if needed.

Closes #13769.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70018 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/msw/choice.cpp
tests/controls/itemcontainertest.cpp

index 3fd0fefc5a338c79101d8e712674737adb03e1ed..34dd9b817da0c8deb77c1d565a5dfb85bd532954 100644 (file)
@@ -358,6 +358,10 @@ void wxChoice::SetString(unsigned int n, const wxString& s)
     else if ( HasClientObjectData() )
         oldObjData = GetClientObject(n);
 
+    // and also the selection if we're going to delete the item that was
+    // selected
+    const bool wasSelected = static_cast<int>(n) == GetSelection();
+
     ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
     ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.wx_str() );
 
@@ -367,6 +371,11 @@ void wxChoice::SetString(unsigned int n, const wxString& s)
     else if ( oldObjData )
         SetClientObject(n, oldObjData);
 
+    // and the selection
+    if ( wasSelected )
+        SetSelection(n);
+
+    // the width could have changed so the best size needs to be recomputed
     InvalidateBestSize();
 }
 
index ab54a497021593b62f7ad751b68a60bb2a3495d4..48944ad0a87c55ce1c9d50e9a874ff0e5f85c29b 100644 (file)
@@ -244,13 +244,16 @@ void ItemContainerTestCase::SetString()
 
     container->Append(testitems);
 
+    container->SetSelection(0);
     container->SetString(0, "new item 0");
-#ifndef __WXOSX__
-    container->SetString(2, "");
-#endif
-
     CPPUNIT_ASSERT_EQUAL("new item 0", container->GetString(0));
+
+    // Modifying the item shouldn't deselect it.
+    CPPUNIT_ASSERT_EQUAL(0, container->GetSelection());
+
+    // wxOSX doesn't support having empty items in some containers.
 #ifndef __WXOSX__
+    container->SetString(2, "");
     CPPUNIT_ASSERT_EQUAL("", container->GetString(2));
 #endif
 }