Added wxNumericPropertyValidator, which is a custom wxTextValidator with more accurate filtering of inappropriate input for wxIntProperty, wxFloatProperty and wxUIntProperty (fixes #12563).

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65806 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Jaakko Salli 2010-10-15 15:15:27 +00:00
parent 5ad9ae3a29
commit 026767c6ae
2 changed files with 130 additions and 4 deletions

View File

@ -143,6 +143,29 @@ enum wxPGNumericValidationConstants
// -----------------------------------------------------------------------
#if wxUSE_VALIDATORS
/**
A more comprehensive numeric validator class.
*/
class wxNumericPropertyValidator : public wxTextValidator
{
public:
enum NumericType
{
Signed = 0,
Unsigned,
Float
};
wxNumericPropertyValidator( NumericType numericType, int base = 10 );
virtual ~wxNumericPropertyValidator() { }
virtual bool Validate(wxWindow* parent);
};
#endif // wxUSE_VALIDATORS
/** @class wxIntProperty
@ingroup classes
Basic property with integer value.
@ -257,6 +280,7 @@ public:
virtual bool DoSetAttribute( const wxString& name, wxVariant& value );
virtual bool ValidateValue( wxVariant& value,
wxPGValidationInfo& validationInfo ) const;
virtual wxValidator* DoGetValidator () const;
virtual bool IntToValue( wxVariant& variant,
int number,
int argFlags = 0 ) const;
@ -302,6 +326,7 @@ public:
wxPGValidationInfo* pValidationInfo,
int mode =
wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE );
static wxValidator* GetClassValidator();
virtual wxValidator* DoGetValidator () const;
protected:

View File

@ -142,6 +142,78 @@ bool wxStringProperty::DoSetAttribute( const wxString& name, wxVariant& value )
return true;
}
// -----------------------------------------------------------------------
// wxNumericPropertyValidator
// -----------------------------------------------------------------------
#if wxUSE_VALIDATORS
wxNumericPropertyValidator::
wxNumericPropertyValidator( NumericType numericType, int base )
: wxTextValidator(wxFILTER_INCLUDE_CHAR_LIST)
{
wxArrayString arr;
arr.Add(wxS("0"));
arr.Add(wxS("1"));
arr.Add(wxS("2"));
arr.Add(wxS("3"));
arr.Add(wxS("4"));
arr.Add(wxS("5"));
arr.Add(wxS("6"));
arr.Add(wxS("7"));
if ( base >= 10 )
{
arr.Add(wxS("8"));
arr.Add(wxS("9"));
if ( base >= 16 )
{
arr.Add(wxS("a")); arr.Add(wxS("A"));
arr.Add(wxS("b")); arr.Add(wxS("B"));
arr.Add(wxS("c")); arr.Add(wxS("C"));
arr.Add(wxS("d")); arr.Add(wxS("D"));
arr.Add(wxS("e")); arr.Add(wxS("E"));
arr.Add(wxS("f")); arr.Add(wxS("F"));
}
}
if ( numericType == Signed )
{
arr.Add(wxS("+"));
arr.Add(wxS("-"));
}
else if ( numericType == Float )
{
arr.Add(wxS("+"));
arr.Add(wxS("-"));
arr.Add(wxS("."));
arr.Add(wxS("e"));
}
SetIncludes(arr);
}
bool wxNumericPropertyValidator::Validate(wxWindow* parent)
{
if ( !wxTextValidator::Validate(parent) )
return false;
wxWindow* wnd = GetWindow();
if ( !wnd->IsKindOf(CLASSINFO(wxTextCtrl)) )
return true;
// Do not allow zero-length string
wxTextCtrl* tc = static_cast<wxTextCtrl*>(wnd);
wxString text = tc->GetValue();
if ( !text.length() )
return false;
return true;
}
#endif // wxUSE_VALIDATORS
// -----------------------------------------------------------------------
// wxIntProperty
// -----------------------------------------------------------------------
@ -376,9 +448,8 @@ wxValidator* wxIntProperty::GetClassValidator()
#if wxUSE_VALIDATORS
WX_PG_DOGETVALIDATOR_ENTRY()
// Atleast wxPython 2.6.2.1 required that the string argument is given
static wxString v;
wxTextValidator* validator = new wxTextValidator(wxFILTER_NUMERIC,&v);
wxValidator* validator = new wxNumericPropertyValidator(
wxNumericPropertyValidator::Signed);
WX_PG_DOGETVALIDATOR_EXIT(validator)
#else
@ -531,6 +602,21 @@ bool wxUIntProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& valida
wxS("%llu"));
}
wxValidator* wxUIntProperty::DoGetValidator() const
{
#if wxUSE_VALIDATORS
WX_PG_DOGETVALIDATOR_ENTRY()
wxValidator* validator = new wxNumericPropertyValidator(
wxNumericPropertyValidator::Unsigned,
m_realBase);
WX_PG_DOGETVALIDATOR_EXIT(validator)
#else
return NULL;
#endif
}
bool wxUIntProperty::DoSetAttribute( const wxString& name, wxVariant& value )
{
if ( name == wxPG_UINT_BASE )
@ -701,9 +787,24 @@ bool wxFloatProperty::DoSetAttribute( const wxString& name, wxVariant& value )
return false;
}
wxValidator*
wxFloatProperty::GetClassValidator()
{
#if wxUSE_VALIDATORS
WX_PG_DOGETVALIDATOR_ENTRY()
wxValidator* validator = new wxNumericPropertyValidator(
wxNumericPropertyValidator::Float);
WX_PG_DOGETVALIDATOR_EXIT(validator)
#else
return NULL;
#endif
}
wxValidator* wxFloatProperty::DoGetValidator() const
{
return wxIntProperty::GetClassValidator();
return GetClassValidator();
}
// -----------------------------------------------------------------------