wxTextValidator improvements

Improve char inclusion/exclusion support; update the sample to show more
features of this class and add a unit test for it.

Closes https://github.com/wxWidgets/wxWidgets/pull/1093
This commit is contained in:
ali kettab
2019-01-01 00:55:14 +01:00
committed by Vadim Zeitlin
parent 697125dc03
commit 36f6f8ad49
18 changed files with 1000 additions and 247 deletions

View File

@@ -9,8 +9,11 @@
/**
Styles used by wxTextValidator.
Note that when you specify more styles in wxTextValidator the validation checks
are performed in the order in which the styles of this enumeration are defined.
Notice that wxFILTER_EXCLUDE[_CHAR]_LIST pair can be used to document the
purpose of the validator only and are not enforced in the implementation of
the wxTextValidator. Therefore, calling the corresponding member functions:
wxTextValidator::{SetExcludes,SetCharExcludes}(), is enough to create the
desired validator.
*/
enum wxTextValidatorStyle
{
@@ -33,9 +36,11 @@ enum wxTextValidatorStyle
/// Non-alphanumeric characters are filtered out.
/// Uses the wxWidgets wrapper for the standard CRT function @c isalnum
/// (which is locale-dependent) on all characters of the string.
/// Equivalent to wxFILTER_ALPHA combined with wxFILTER_DIGITS or
/// wxFILTER_XDIGITS, or with both of them.
wxFILTER_ALPHANUMERIC,
/// Non-numeric characters are filtered out.
/// Non-digit characters are filtered out.
/// Uses the wxWidgets wrapper for the standard CRT function @c isdigit
/// (which is locale-dependent) on all characters of the string.
wxFILTER_DIGITS,
@@ -50,19 +55,36 @@ enum wxTextValidatorStyle
/// the list, complaining if not. See wxTextValidator::SetIncludes().
wxFILTER_INCLUDE_LIST,
/// Use an include list. The validator checks if each input character is
/// in the list (one character per list element), complaining if not.
/// See wxTextValidator::SetCharIncludes().
/// Use an include char list.
/// Characters in the include char list will be allowed to be in the
/// user input. See wxTextValidator::SetCharIncludes().
/// If this style is set with one or more of the following styles:
/// wxFILTER_ASCII, wxFILTER_ALPHA, wxFILTER_ALPHANUMERIC, wxFILTER_DIGITS,
/// wxFILTER_XDIGITS, wxFILTER_NUMERIC it just extends the character class
/// denoted by the aforementioned styles with those specified in the include
/// char list. If set alone, then the charactes allowed to be in the user input
/// are restricted to those, and only those, present in the include char list.
wxFILTER_INCLUDE_CHAR_LIST,
/// Use an exclude list. The validator checks if the user input is on
/// the list, complaining if it is. See wxTextValidator::SetExcludes().
wxFILTER_EXCLUDE_LIST,
/// Use an exclude list. The validator checks if each input character is
/// in the list (one character per list element), complaining if it is.
/// See wxTextValidator::SetCharExcludes().
wxFILTER_EXCLUDE_CHAR_LIST
/// Use an exclude char list.
/// Characters in the exclude char list won't be allowed to be in the
/// user input. See wxTextValidator::SetCharExcludes().
wxFILTER_EXCLUDE_CHAR_LIST,
/// Non-hexadecimal characters are filtered out.
/// Uses the wxWidgets wrapper for the standard CRT function @c isxdigit
/// (which is locale-dependent) on all characters of the string.
wxFILTER_XDIGITS,
/// A convenience flag for use with the other flags.
/// The space character is more often used with alphanumeric characters
/// which makes setting a flag more easier than calling SetCharIncludes(" ")
/// for that matter.
wxFILTER_SPACE
};
/**
@@ -83,7 +105,7 @@ class wxTextValidator : public wxValidator
{
public:
/**
Default constructor.
Copy constructor.
*/
wxTextValidator(const wxTextValidator& validator);
@@ -106,14 +128,28 @@ public:
virtual wxObject* Clone() const;
/**
Returns a reference to the exclude list (the list of invalid values).
Returns a copy of the exclude char list (the list of invalid characters).
@since 3.1.3
*/
wxArrayString& GetExcludes();
wxString GetCharExcludes() const;
/**
Returns a reference to the include list (the list of valid values).
Returns a copy of the include char list (the list of additional valid characters).
@since 3.1.3
*/
wxArrayString& GetIncludes();
wxString GetCharIncludes() const;
/**
Returns a const reference to the exclude list (the list of invalid values).
*/
const wxArrayString& GetExcludes() const;
/**
Returns a const reference to the include list (the list of valid values).
*/
const wxArrayString& GetIncludes() const;
/**
Returns the validator style.
@@ -135,41 +171,81 @@ public:
/**
Sets the exclude list (invalid values for the user input).
@note Beware that exclusion takes priority over inclusion.
*/
void SetExcludes(const wxArrayString& stringList);
/**
Breaks the given @a chars strings in single characters and sets the
internal wxArrayString used to store the "excluded" characters
(see SetExcludes()).
Sets the exclude char list (invalid characters for the user input).
This function is mostly useful when @c wxFILTER_EXCLUDE_CHAR_LIST was used.
@note Beware that exclusion takes priority over inclusion.
@note This function may cancel the effect of @c wxFILTER_SPACE if the passed
in string @a chars contains the @b space character.
*/
void SetCharExcludes(const wxString& chars);
/**
Sets the include list (valid values for the user input).
@see IsIncluded()
*/
void SetIncludes(const wxArrayString& stringList);
/**
Breaks the given @a chars strings in single characters and sets the
internal wxArrayString used to store the "included" characters
(see SetIncludes()).
Sets the include char list (additional valid values for the user input).
This function is mostly useful when @c wxFILTER_INCLUDE_CHAR_LIST was used.
@note Any explicitly excluded characters will still be excluded even if
they're part of @a chars.
*/
void SetCharIncludes(const wxString& chars);
/**
Adds @a exclude to the list of excluded values.
@note Beware that exclusion takes priority over inclusion.
@since 3.1.3
*/
void AddExclude(const wxString& exclude);
/**
Adds @a include to the list of included values.
@note Any explicitly excluded characters will still be excluded.
@since 3.1.3
*/
void AddInclude(const wxString& include);
/**
Adds @a chars to the list of excluded characters.
@note Beware that exclusion takes priority over inclusion.
@since 3.1.3
*/
void AddCharExcludes(const wxString& chars);
/**
Adds @a chars to the list of included characters.
@note Any explicitly excluded characters will still be excluded even if
they're part of @a chars.
@since 3.1.3
*/
void AddCharIncludes(const wxString& chars);
/**
Sets the validator style which must be a combination of one or more
of the ::wxTextValidatorStyle values.
Note that not all possible combinations make sense!
Also note that the order in which the checks are performed is important,
in case you specify more than a single style.
wxTextValidator will perform the checks in the same definition order
used in the ::wxTextValidatorStyle enumeration.
Note that not all possible combinations make sense! Also, some
combinations have shorter and more idiomatic alternative, e.g.
@c wxFILTER_ALPHANUMERIC can be used instead of
@c wxFILTER_ALPHA|wxFILTER_DIGITS.
*/
void SetStyle(long style);
@@ -189,24 +265,52 @@ public:
*/
virtual bool Validate(wxWindow* parent);
protected:
/**
Returns @true if all the characters of the given @a val string
are present in the include list (set by SetIncludes() or SetCharIncludes()).
*/
bool ContainsOnlyIncludedCharacters(const wxString& val) const;
/**
Returns true if at least one character of the given @a val string
is present in the exclude list (set by SetExcludes() or SetCharExcludes()).
*/
bool ContainsExcludedCharacters(const wxString& val) const;
/**
Returns the error message if the contents of @a val are invalid
or the empty string if @a val is valid.
*/
virtual wxString IsValid(const wxString& val) const;
protected:
/**
Returns @true if the char @a c is allowed to be in the user input string.
Additional characters, set by SetCharIncludes() or AddCharIncludes() are
also considered.
@since 3.1.3
*/
bool IsCharIncluded(const wxUniChar& c) const;
/**
Returns @true if the char @a c is not allowed to be in the user input string.
(characters set by SetCharExcludes() or AddCharExcludes()).
@since 3.1.3
*/
bool IsCharExcluded(const wxUniChar& c) const;
/**
Returns @true if the string @a str is one of the includes strings set by
SetIncludes() or AddInclude().
Notice that unless wxFILTER_INCLUDE_LIST is specified (in which case the
validator will complain if the user input is not on the list), the list
will be ignored and won't participate in the validation process.
@since 3.1.3
*/
bool IsIncluded(const wxString& str) const;
/**
Returns @true if the string @a str is one of the excludes strings set by
SetExcludes() or AddExclude().
@since 3.1.3
*/
bool IsExcluded(const wxString& str) const;
/// Returns false if the character @a c is invalid.
bool IsValidChar(const wxUniChar& c) const;
};