valtext.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: valtext.h
  3. // Purpose: interface of wxTextValidator
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Styles used by wxTextValidator.
  9. Note that when you specify more styles in wxTextValidator the validation checks
  10. are performed in the order in which the styles of this enumeration are defined.
  11. */
  12. enum wxTextValidatorStyle
  13. {
  14. /// No filtering takes place.
  15. wxFILTER_NONE,
  16. /// Empty strings are filtered out.
  17. /// If this style is not specified then empty strings are accepted
  18. /// only if they pass the other checks (if you use more than one wxTextValidatorStyle).
  19. wxFILTER_EMPTY,
  20. /// Non-ASCII characters are filtered out. See wxString::IsAscii.
  21. wxFILTER_ASCII,
  22. /// Non-alpha characters are filtered out.
  23. /// Uses the wxWidgets wrapper for the standard CRT function @c isalpha
  24. /// (which is locale-dependent) on all characters of the string.
  25. wxFILTER_ALPHA,
  26. /// Non-alphanumeric characters are filtered out.
  27. /// Uses the wxWidgets wrapper for the standard CRT function @c isalnum
  28. /// (which is locale-dependent) on all characters of the string.
  29. wxFILTER_ALPHANUMERIC,
  30. /// Non-numeric characters are filtered out.
  31. /// Uses the wxWidgets wrapper for the standard CRT function @c isdigit
  32. /// (which is locale-dependent) on all characters of the string.
  33. wxFILTER_DIGITS,
  34. /// Non-numeric characters are filtered out.
  35. /// Works like @c wxFILTER_DIGITS but allows also decimal points,
  36. /// minus/plus signs and the 'e' or 'E' character to input exponents.
  37. /// Note that this is not the same behaviour of wxString::IsNumber().
  38. wxFILTER_NUMERIC,
  39. /// Use an include list. The validator checks if the user input is on
  40. /// the list, complaining if not. See wxTextValidator::SetIncludes().
  41. wxFILTER_INCLUDE_LIST,
  42. /// Use an include list. The validator checks if each input character is
  43. /// in the list (one character per list element), complaining if not.
  44. /// See wxTextValidator::SetCharIncludes().
  45. wxFILTER_INCLUDE_CHAR_LIST,
  46. /// Use an exclude list. The validator checks if the user input is on
  47. /// the list, complaining if it is. See wxTextValidator::SetExcludes().
  48. wxFILTER_EXCLUDE_LIST,
  49. /// Use an exclude list. The validator checks if each input character is
  50. /// in the list (one character per list element), complaining if it is.
  51. /// See wxTextValidator::SetCharExcludes().
  52. wxFILTER_EXCLUDE_CHAR_LIST
  53. };
  54. /**
  55. @class wxTextValidator
  56. wxTextValidator validates text controls, providing a variety of filtering
  57. behaviours.
  58. For more information, please see @ref overview_validator.
  59. @library{wxcore}
  60. @category{validator}
  61. @see @ref overview_validator, wxValidator, wxGenericValidator,
  62. wxIntegerValidator, wxFloatingPointValidator
  63. */
  64. class wxTextValidator : public wxValidator
  65. {
  66. public:
  67. /**
  68. Default constructor.
  69. */
  70. wxTextValidator(const wxTextValidator& validator);
  71. /**
  72. Constructor taking a style and optional pointer to a wxString variable.
  73. @param style
  74. One or more of the ::wxTextValidatorStyle styles. See SetStyle().
  75. @param valPtr
  76. A pointer to a wxString variable that contains the value. This
  77. variable should have a lifetime equal to or longer than the
  78. validator lifetime (which is usually determined by the lifetime of
  79. the window).
  80. */
  81. wxTextValidator(long style = wxFILTER_NONE, wxString* valPtr = NULL);
  82. /**
  83. Clones the text validator using the copy constructor.
  84. */
  85. virtual wxObject* Clone() const;
  86. /**
  87. Returns a reference to the exclude list (the list of invalid values).
  88. */
  89. wxArrayString& GetExcludes();
  90. /**
  91. Returns a reference to the include list (the list of valid values).
  92. */
  93. wxArrayString& GetIncludes();
  94. /**
  95. Returns the validator style.
  96. @see HasFlag()
  97. */
  98. long GetStyle() const;
  99. /**
  100. Returns @true if the given @a style bit is set in the current style.
  101. */
  102. bool HasFlag(wxTextValidatorStyle style) const;
  103. /**
  104. Receives character input from the window and filters it according to
  105. the current validator style.
  106. */
  107. void OnChar(wxKeyEvent& event);
  108. /**
  109. Sets the exclude list (invalid values for the user input).
  110. */
  111. void SetExcludes(const wxArrayString& stringList);
  112. /**
  113. Breaks the given @a chars strings in single characters and sets the
  114. internal wxArrayString used to store the "excluded" characters
  115. (see SetExcludes()).
  116. This function is mostly useful when @c wxFILTER_EXCLUDE_CHAR_LIST was used.
  117. */
  118. void SetCharExcludes(const wxString& chars);
  119. /**
  120. Sets the include list (valid values for the user input).
  121. */
  122. void SetIncludes(const wxArrayString& stringList);
  123. /**
  124. Breaks the given @a chars strings in single characters and sets the
  125. internal wxArrayString used to store the "included" characters
  126. (see SetIncludes()).
  127. This function is mostly useful when @c wxFILTER_INCLUDE_CHAR_LIST was used.
  128. */
  129. void SetCharIncludes(const wxString& chars);
  130. /**
  131. Sets the validator style which must be a combination of one or more
  132. of the ::wxTextValidatorStyle values.
  133. Note that not all possible combinations make sense!
  134. Also note that the order in which the checks are performed is important,
  135. in case you specify more than a single style.
  136. wxTextValidator will perform the checks in the same definition order
  137. used in the ::wxTextValidatorStyle enumeration.
  138. */
  139. void SetStyle(long style);
  140. /**
  141. Transfers the value in the text control to the string.
  142. */
  143. virtual bool TransferFromWindow();
  144. /**
  145. Transfers the string value to the text control.
  146. */
  147. virtual bool TransferToWindow();
  148. /**
  149. Validates the window contents against the include or exclude lists,
  150. depending on the validator style.
  151. */
  152. virtual bool Validate(wxWindow* parent);
  153. protected:
  154. /**
  155. Returns @true if all the characters of the given @a val string
  156. are present in the include list (set by SetIncludes() or SetCharIncludes()).
  157. */
  158. bool ContainsOnlyIncludedCharacters(const wxString& val) const;
  159. /**
  160. Returns true if at least one character of the given @a val string
  161. is present in the exclude list (set by SetExcludes() or SetCharExcludes()).
  162. */
  163. bool ContainsExcludedCharacters(const wxString& val) const;
  164. /**
  165. Returns the error message if the contents of @a val are invalid
  166. or the empty string if @a val is valid.
  167. */
  168. virtual wxString IsValid(const wxString& val) const;
  169. };