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)