Validate input in generic wxSpinCtrl and wxSpinCtrlDouble

Use respective validator to control what is typed in the text field.

Closes #17882.
This commit is contained in:
Artur Wieczorek
2020-06-11 15:28:38 +02:00
parent 0ca6be3cf4
commit 7c3d540c70
2 changed files with 41 additions and 0 deletions

View File

@@ -147,6 +147,8 @@ protected:
// ensure that the value is in range wrapping it round if necessary
double AdjustToFitInRange(double value) const;
// Assign validator with current parameters
virtual void ResetTextValidator() = 0;
double m_value;
double m_min;
@@ -331,6 +333,7 @@ protected:
virtual bool DoTextToValue(const wxString& text, double *val);
virtual wxString DoValueToText(double val);
virtual void ResetTextValidator() wxOVERRIDE;
private:
// Common part of all ctors.
@@ -411,6 +414,7 @@ protected:
virtual bool DoTextToValue(const wxString& text, double *val) wxOVERRIDE;
virtual wxString DoValueToText(double val) wxOVERRIDE;
virtual void ResetTextValidator() wxOVERRIDE;
void DetermineDigits(double inc);
unsigned m_digits;

View File

@@ -44,6 +44,9 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent);
#if wxUSE_SPINBTN
#include "wx/valnum.h"
#include "wx/valtext.h"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
@@ -250,6 +253,8 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
m_spinButton->SetToolTip(GetToolTipText());
#endif // wxUSE_TOOLTIPS
ResetTextValidator();
m_spin_value = m_spinButton->GetValue();
SetInitialSize(size);
@@ -593,6 +598,8 @@ void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
m_max = max;
if ( m_value > m_max )
DoSetValue(m_max, SendEvent_None);
ResetTextValidator();
}
void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
@@ -636,6 +643,8 @@ bool wxSpinCtrl::SetBase(int base)
m_base = base;
ResetTextValidator();
// ... but DoValueToText() after doing it.
if ( hasValidVal )
m_textCtrl->ChangeValue(DoValueToText(val));
@@ -679,6 +688,24 @@ wxString wxSpinCtrl::DoValueToText(double val)
}
}
void wxSpinCtrl::ResetTextValidator()
{
#if wxUSE_VALIDATORS
if ( GetBase() == 10 )
{
wxIntegerValidator<int> validator;
validator.SetRange(GetMin(), GetMax());
m_textCtrl->SetValidator(validator);
}
else // == 16
{
wxTextValidator validator(wxFILTER_XDIGITS);
m_textCtrl->SetValidator(validator);
}
#endif // wxUSE_VALIDATORS
}
#endif // !wxHAS_NATIVE_SPINCTRL
//-----------------------------------------------------------------------------
@@ -719,11 +746,21 @@ void wxSpinCtrlDouble::SetDigits(unsigned digits)
m_format.Printf(wxT("%%0.%ulf"), digits);
ResetTextValidator();
m_textCtrl->InvalidateBestSize();
DoSetValue(m_value, SendEvent_None);
}
void wxSpinCtrlDouble::ResetTextValidator()
{
#if wxUSE_VALIDATORS
wxFloatingPointValidator<double> validator(m_digits);
validator.SetRange(m_min, m_max);
m_textCtrl->SetValidator(validator);
#endif // wxUSE_VALIDATORS
}
void wxSpinCtrlDouble::DetermineDigits(double inc)
{
inc = fabs(inc);