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.
This commit is contained in:
Vadim Zeitlin
2021-04-25 19:45:34 +01:00
parent eecc62ba65
commit 6938d91942
3 changed files with 33 additions and 2 deletions

View File

@@ -402,7 +402,7 @@ public:
{ wxSpinCtrlGenericBase::SetValue(value); } { wxSpinCtrlGenericBase::SetValue(value); }
void SetValue(double value) { DoSetValue(value, SendEvent_None); } void SetValue(double value) { DoSetValue(value, SendEvent_None); }
void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); } void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
void SetIncrement(double inc) { DoSetIncrement(inc); } void SetIncrement(double inc);
void SetDigits(unsigned digits); void SetDigits(unsigned digits);
// We don't implement bases support for floating point numbers, this is not // 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. // Set the number of digits and the format unconditionally.
void DoSetDigits(unsigned digits); void DoSetDigits(unsigned digits);
// Update the appearance after the number of digits has changed.
void UpdateAfterDigitsChange();
wxString m_format; wxString m_format;

View File

@@ -746,6 +746,18 @@ wxString wxSpinCtrlDouble::DoValueToText(double val)
return wxString::Format(m_format, 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) void wxSpinCtrlDouble::SetDigits(unsigned digits)
{ {
wxCHECK_RET( digits <= SPINCTRLDBL_MAX_DIGITS, "too many digits for wxSpinCtrlDouble" ); wxCHECK_RET( digits <= SPINCTRLDBL_MAX_DIGITS, "too many digits for wxSpinCtrlDouble" );
@@ -755,6 +767,11 @@ void wxSpinCtrlDouble::SetDigits(unsigned digits)
DoSetDigits(digits); DoSetDigits(digits);
UpdateAfterDigitsChange();
}
void wxSpinCtrlDouble::UpdateAfterDigitsChange()
{
ResetTextValidator(); ResetTextValidator();
m_textCtrl->InvalidateBestSize(); m_textCtrl->InvalidateBestSize();

View File

@@ -217,9 +217,20 @@ TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
TEST_CASE_METHOD(SpinCtrlDoubleTestCase, TEST_CASE_METHOD(SpinCtrlDoubleTestCase,
"SpinCtrlDouble::Digits", "[spinctrl][spinctrldouble]") "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 ); CHECK( m_spin->GetDigits() == 5 );
m_spin->SetValue(1.23456789);
CHECK( m_spin->GetTextValue() == "1.23457" );
} }
static inline unsigned int GetInitialDigits(double inc) static inline unsigned int GetInitialDigits(double inc)