From 8fc2d4400421232c5520cda92c79f832d2673d62 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 23 Apr 2021 22:30:59 +0100 Subject: [PATCH] Don't generate events from wxMSW wxSpinCtrl::SetValue(wxString) The function was documented to not generate the events, but actually did generate wxEVT_TEXT ones, even if it didn't generate wxEVT_SPINCTRL. This was inconsistent with wxGTK and generic wxSpinCtrlDouble used under MSW, so change this to avoid the unwanted events. --- docs/changes.txt | 5 ++++- src/msw/spinctrl.cpp | 9 +++++---- tests/controls/spinctrldbltest.cpp | 6 ++++++ tests/controls/spinctrltest.cpp | 6 ++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index b5242e5681..e8ad00371c 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -115,7 +115,10 @@ Changes in behaviour not resulting in compilation errors - wxChoice::GetString() now consistently asserts when passed an invalid index. - wxSpinCtrlDouble now always resets its value to GetMin() if an invalid text - string is passed to its SetValue() after its creation. + string is passed to its SetValue(wxString) overload after its creation. + +- wxSpinCtrl::SetValue(wxString) overload doesn't generate any events with + wxMSW, which was already the documented behaviour. Changes in behaviour which may result in build errors diff --git a/src/msw/spinctrl.cpp b/src/msw/spinctrl.cpp index 9cad282bcc..e41003091f 100644 --- a/src/msw/spinctrl.cpp +++ b/src/msw/spinctrl.cpp @@ -359,12 +359,9 @@ bool wxSpinCtrl::Create(wxWindow *parent, SetRange(min, max); SetValue(initial); - // Also set the text part of the control if it was specified independently - // but don't generate an event for this, it would be unexpected. - m_blockEvent = true; + // Also set the text part of the control if it was specified independently. if ( !value.empty() ) SetValue(value); - m_blockEvent = false; // Finally deal with the size: notice that this can only be done now both // windows are created and the text one is set up as buddy because @@ -449,10 +446,14 @@ wxString wxSpinCtrl::GetTextValue() const void wxSpinCtrl::SetValue(const wxString& text) { + m_blockEvent = true; + if ( !::SetWindowText(GetBuddyHwnd(), text.c_str()) ) { wxLogLastError(wxT("SetWindowText(buddy)")); } + + m_blockEvent = false; } void wxSpinCtrl::SetValue(int val) diff --git a/tests/controls/spinctrldbltest.cpp b/tests/controls/spinctrldbltest.cpp index a028dbe977..1e834b1915 100644 --- a/tests/controls/spinctrldbltest.cpp +++ b/tests/controls/spinctrldbltest.cpp @@ -177,9 +177,15 @@ TEST_CASE_METHOD(SpinCtrlDoubleTestCase, CHECK( m_spin->GetTextValue() == "57.30" ); CHECK( m_spin->GetValue() == 57.3 ); + CHECK( updatedSpin.GetCount() == 0 ); + CHECK( updatedText.GetCount() == 0 ); + m_spin->SetValue(""); CHECK( m_spin->GetTextValue() == "" ); CHECK( m_spin->GetValue() == 0 ); + + CHECK( updatedSpin.GetCount() == 0 ); + CHECK( updatedText.GetCount() == 0 ); } #if wxUSE_UIACTIONSIMULATOR diff --git a/tests/controls/spinctrltest.cpp b/tests/controls/spinctrltest.cpp index a6624066e2..fa2662deee 100644 --- a/tests/controls/spinctrltest.cpp +++ b/tests/controls/spinctrltest.cpp @@ -276,9 +276,15 @@ TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Value", "[spinctrl]") CHECK( m_spin->GetTextValue() == "57" ); CHECK( m_spin->GetValue() == 57 ); + CHECK(updatedSpin.GetCount() == 0); + CHECK(updatedText.GetCount() == 0); + m_spin->SetValue(""); CHECK( m_spin->GetTextValue() == "" ); CHECK( m_spin->GetValue() == 0 ); + + CHECK(updatedSpin.GetCount() == 0); + CHECK(updatedText.GetCount() == 0); } TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Base", "[spinctrl]")