From 35fa1f93bc3d01defe9892cb852e5d51b0259259 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 23 Apr 2021 16:25:39 +0100 Subject: [PATCH] Reset wxSpinCtrl value to GetMin() if text string is invalid Previously, wxSpinCtrl (using native control) and wxSpinCtrlDouble (using the generic implementation) behaved differently in this case, with the former changing its value but the latter keeping the last valid value instead. Make them behave the same by resetting the value in both cases and document this behaviour. --- docs/changes.txt | 3 +++ interface/wx/spinctrl.h | 8 ++++---- src/generic/spinctlg.cpp | 2 ++ tests/controls/spinctrldbltest.cpp | 2 +- tests/controls/spinctrltest.cpp | 1 + 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 5d2d9d12cd..b5242e5681 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -114,6 +114,9 @@ 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. + Changes in behaviour which may result in build errors ----------------------------------------------------- diff --git a/interface/wx/spinctrl.h b/interface/wx/spinctrl.h index 55353ef86d..6371aeec7c 100644 --- a/interface/wx/spinctrl.h +++ b/interface/wx/spinctrl.h @@ -197,10 +197,10 @@ public: Sets the value of the spin control. It is recommended to use the overload taking an integer value instead. - The behaviour of this function when @a text doesn't represent a valid - number currently differs between the platforms, however passing an - empty string does clear the text part contents, without affecting the - value returned by GetValue(), under all of them. + If @a text doesn't represent a valid number, it may not be shown in the + text part of the control at all (only empty string is guaranteed to be + supported under all platforms) and the numeric value will be changed to + GetMin(). Notice that, unlike wxTextCtrl::SetValue(), but like most of the other setter methods in wxWidgets, calling this method does not generate any diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index aa99410c35..7255045e24 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -522,6 +522,8 @@ void wxSpinCtrlGenericBase::SetValue(const wxString& text) } else // not a number at all or out of range { + m_value = m_min; + m_textCtrl->ChangeValue(text); m_textCtrl->SelectAll(); } diff --git a/tests/controls/spinctrldbltest.cpp b/tests/controls/spinctrldbltest.cpp index a4fe967942..a028dbe977 100644 --- a/tests/controls/spinctrldbltest.cpp +++ b/tests/controls/spinctrldbltest.cpp @@ -179,7 +179,7 @@ TEST_CASE_METHOD(SpinCtrlDoubleTestCase, m_spin->SetValue(""); CHECK( m_spin->GetTextValue() == "" ); - CHECK( m_spin->GetValue() == 57.3 ); + CHECK( m_spin->GetValue() == 0 ); } #if wxUSE_UIACTIONSIMULATOR diff --git a/tests/controls/spinctrltest.cpp b/tests/controls/spinctrltest.cpp index ec46aada54..a6624066e2 100644 --- a/tests/controls/spinctrltest.cpp +++ b/tests/controls/spinctrltest.cpp @@ -278,6 +278,7 @@ TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Value", "[spinctrl]") m_spin->SetValue(""); CHECK( m_spin->GetTextValue() == "" ); + CHECK( m_spin->GetValue() == 0 ); } TEST_CASE_METHOD(SpinCtrlTestCase2, "SpinCtrl::Base", "[spinctrl]")