From 6938d919429b65981b14cabe4e54473aaea73487 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Apr 2021 19:45:34 +0100 Subject: [PATCH] Update number of digits in wxSpinCtrlDouble::SetIncrement() This is consistent with using the value of the increment specified in the ctor for setting the initial number of digits, it was surprising that creating the control with some value of the increment (e.g. 0.1) and calling SetIncrement(0.1) later resulted in very different outcomes, as in the former case the value was shown with a digit after the period while in the latter case only the integer part was shown. This also makes the behaviour compatible with that of the previous versions of the generic wxSpinCtrlDouble, which used "%g" to format the number before the changes of edc553870f (Fix displaying wxSpinCtrlDouble values with default precision, 2020-05-18), as they did show the fractional part even in the latter case. Add a test checking that this works as expected: before this commit, the test failed with "1 == 1.2" and "1 == 1.23" errors. --- include/wx/generic/spinctlg.h | 5 ++++- src/generic/spinctlg.cpp | 17 +++++++++++++++++ tests/controls/spinctrldbltest.cpp | 13 ++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/wx/generic/spinctlg.h b/include/wx/generic/spinctlg.h index 154beb3af9..ced305ef97 100644 --- a/include/wx/generic/spinctlg.h +++ b/include/wx/generic/spinctlg.h @@ -402,7 +402,7 @@ public: { wxSpinCtrlGenericBase::SetValue(value); } void SetValue(double value) { DoSetValue(value, SendEvent_None); } void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); } - void SetIncrement(double inc) { DoSetIncrement(inc); } + void SetIncrement(double inc); void SetDigits(unsigned digits); // We don't implement bases support for floating point numbers, this is not @@ -432,6 +432,9 @@ private: // Set the number of digits and the format unconditionally. void DoSetDigits(unsigned digits); + // Update the appearance after the number of digits has changed. + void UpdateAfterDigitsChange(); + wxString m_format; diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 1342acc478..98feb4b5a3 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -746,6 +746,18 @@ wxString wxSpinCtrlDouble::DoValueToText(double val) return wxString::Format(m_format, val); } +void wxSpinCtrlDouble::SetIncrement(double inc) +{ + if ( inc == m_increment ) + return; + + DoSetIncrement(inc); + + DetermineDigits(inc); + + UpdateAfterDigitsChange(); +} + void wxSpinCtrlDouble::SetDigits(unsigned digits) { wxCHECK_RET( digits <= SPINCTRLDBL_MAX_DIGITS, "too many digits for wxSpinCtrlDouble" ); @@ -755,6 +767,11 @@ void wxSpinCtrlDouble::SetDigits(unsigned digits) DoSetDigits(digits); + UpdateAfterDigitsChange(); +} + +void wxSpinCtrlDouble::UpdateAfterDigitsChange() +{ ResetTextValidator(); m_textCtrl->InvalidateBestSize(); diff --git a/tests/controls/spinctrldbltest.cpp b/tests/controls/spinctrldbltest.cpp index 0308f3e3ea..f4dcf380f2 100644 --- a/tests/controls/spinctrldbltest.cpp +++ b/tests/controls/spinctrldbltest.cpp @@ -217,9 +217,20 @@ TEST_CASE_METHOD(SpinCtrlDoubleTestCase, TEST_CASE_METHOD(SpinCtrlDoubleTestCase, "SpinCtrlDouble::Digits", "[spinctrl][spinctrldouble]") { - m_spin->SetDigits(5); + // Setting increment should adjust the number of digits shown to be big + // enough to show numbers with the corresponding granularity. + m_spin->SetIncrement(0.1); + m_spin->SetValue(1.23456789); + CHECK( m_spin->GetTextValue() == "1.2" ); + m_spin->SetIncrement(0.01); + m_spin->SetValue(1.23456789); + CHECK( m_spin->GetTextValue() == "1.23" ); + + m_spin->SetDigits(5); CHECK( m_spin->GetDigits() == 5 ); + m_spin->SetValue(1.23456789); + CHECK( m_spin->GetTextValue() == "1.23457" ); } static inline unsigned int GetInitialDigits(double inc)