Skip to content

Commit

Permalink
Validate input in generic wxSpinCtrl and wxSpinCtrlDouble
Browse files Browse the repository at this point in the history
Use respective validator to control what is typed in the text field.

Closes #17882.
  • Loading branch information
a-wi committed Jun 11, 2020
1 parent 0ca6be3 commit 7c3d540
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/wx/generic/spinctlg.h
Expand Up @@ -147,6 +147,8 @@ class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
// 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;
Expand Down Expand Up @@ -331,6 +333,7 @@ class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase

virtual bool DoTextToValue(const wxString& text, double *val);
virtual wxString DoValueToText(double val);
virtual void ResetTextValidator() wxOVERRIDE;

private:
// Common part of all ctors.
Expand Down Expand Up @@ -411,6 +414,7 @@ class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase

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;
Expand Down
37 changes: 37 additions & 0 deletions src/generic/spinctlg.cpp
Expand Up @@ -44,6 +44,9 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent);

#if wxUSE_SPINBTN

#include "wx/valnum.h"
#include "wx/valtext.h"

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7c3d540

Please sign in to comment.