You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
7.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
  5. * Copyright (C) 2004-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. * Copyright (C) 2018 CERN
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file validators.h
  27. * @brief Custom text control validator definitions.
  28. */
  29. #ifndef VALIDATORS_H
  30. #define VALIDATORS_H
  31. #include <wx/valtext.h>
  32. #include <wx/grid.h>
  33. #include <wx/regex.h>
  34. #include <lib_id.h>
  35. /**
  36. * This class works around a bug in wxGrid where the first keystroke doesn't get sent through
  37. * the validator if the editor wasn't already open.
  38. */
  39. class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
  40. {
  41. public:
  42. GRID_CELL_TEXT_EDITOR();
  43. virtual void SetValidator( const wxValidator& validator ) override;
  44. virtual void StartingKey( wxKeyEvent& event ) override;
  45. protected:
  46. wxScopedPtr<wxValidator> m_validator;
  47. };
  48. /**
  49. * This class provides a custom wxValidator object for limiting the allowable characters when
  50. * defining footprint names. Since the introduction of the PRETTY footprint library format,
  51. * footprint names cannot have any characters that would prevent file creation on any platform.
  52. * The characters \/:*?|"<> are illegal and filtered by the validator.
  53. */
  54. class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
  55. {
  56. public:
  57. FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
  58. };
  59. /**
  60. * This class provides a custom wxValidator object for limiting the allowable characters when
  61. * defining file names with path, for instance in schematic sheet file names.
  62. * The characters *?|"<> are illegal and filtered by the validator,
  63. * but /\: are valid (\ and : only on Windows.)
  64. */
  65. class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
  66. {
  67. public:
  68. FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = nullptr );
  69. };
  70. /**
  71. * This class provides a custom wxValidator object for limiting the allowable characters
  72. * when defining an environment variable name in a text edit control. Only uppercase,
  73. * numbers, and underscore (_) characters are valid and the first character of the name
  74. * cannot start with a number. This is according to IEEE Std 1003.1-2001. Even though
  75. * most systems support other characters, these characters guarantee compatibility for
  76. * all shells.
  77. */
  78. class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
  79. {
  80. public:
  81. ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
  82. ENV_VAR_NAME_VALIDATOR( const ENV_VAR_NAME_VALIDATOR& val );
  83. virtual ~ENV_VAR_NAME_VALIDATOR();
  84. // Make a clone of this validator (or return nullptr) - currently necessary
  85. // if you're passing a reference to a validator.
  86. virtual wxObject *Clone() const override
  87. {
  88. return new ENV_VAR_NAME_VALIDATOR( *this );
  89. }
  90. void OnChar( wxKeyEvent& event );
  91. void OnTextChanged( wxCommandEvent& event );
  92. };
  93. /**
  94. * Custom validator that checks verifies that a string *exactly* matches a
  95. * regular expression.
  96. */
  97. class REGEX_VALIDATOR : public wxTextValidator
  98. {
  99. public:
  100. /**
  101. * @param aRegEx is a regular expression to validate strings.
  102. * @param aValue is a pointer to a wxString containing the value to validate.
  103. */
  104. REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = nullptr )
  105. : wxTextValidator( wxFILTER_NONE, aValue )
  106. {
  107. compileRegEx( aRegEx, wxRE_DEFAULT );
  108. }
  109. /**
  110. * @param aRegEx is a regular expression to validate strings.
  111. * @param aFlags are compilation flags (normally wxRE_DEFAULT).
  112. * @param aValue is a pointer to a wxString containing the value to validate.
  113. */
  114. REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = nullptr )
  115. : wxTextValidator( wxFILTER_NONE, aValue )
  116. {
  117. compileRegEx( aRegEx, aFlags );
  118. }
  119. REGEX_VALIDATOR( const REGEX_VALIDATOR& aOther ) : wxTextValidator( aOther )
  120. {
  121. compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
  122. }
  123. virtual wxObject* Clone() const override
  124. {
  125. return new REGEX_VALIDATOR( *this );
  126. }
  127. bool Validate( wxWindow* aParent ) override;
  128. const wxString& GetRegEx() const
  129. {
  130. return m_regExString;
  131. }
  132. protected:
  133. ///< Compiles and stores a regular expression
  134. void compileRegEx( const wxString& aRegEx, int aFlags );
  135. ///< Original regular expression (for copy constructor)
  136. wxString m_regExString;
  137. ///< Original compilation flags (for copy constructor)
  138. int m_regExFlags;
  139. ///< Compiled regex
  140. wxRegEx m_regEx;
  141. };
  142. /**
  143. * Custom validator that verifies that a string defines a valid #LIB_ID.
  144. *
  145. * The default validation allows empty #LIB_ID strings to allow the #LIB_ID to be cleared.
  146. * Use SetStyle( wxFILTER_EMPTY ) to force a valid #LIB_ID string.
  147. */
  148. class LIB_ID_VALIDATOR : public wxTextValidator
  149. {
  150. public:
  151. /**
  152. * @param aLibIdType is the type of #LIB_ID object to validate.
  153. * @param aValue is a pointer to a wxString containing the value to validate.
  154. */
  155. LIB_ID_VALIDATOR( wxString* aValue = nullptr ) :
  156. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
  157. {
  158. SetCharExcludes( wxT( "\r\n\t" ) );
  159. }
  160. virtual wxObject* Clone() const override
  161. {
  162. return new LIB_ID_VALIDATOR( *this );
  163. }
  164. bool Validate( wxWindow* aParent ) override;
  165. };
  166. class NETNAME_VALIDATOR : public wxTextValidator
  167. {
  168. public:
  169. NETNAME_VALIDATOR( wxString* aVal = nullptr );
  170. NETNAME_VALIDATOR( bool aAllowSpaces );
  171. NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );
  172. virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }
  173. virtual bool TransferToWindow() override { return true; }
  174. virtual bool TransferFromWindow() override { return true; }
  175. virtual bool Validate( wxWindow *aParent ) override;
  176. protected:
  177. // returns the error message if the contents of 'val' are invalid
  178. wxString IsValid( const wxString& aVal ) const override;
  179. private:
  180. bool m_allowSpaces;
  181. };
  182. namespace KIUI
  183. {
  184. /**
  185. * Call a text validator's TransferDataToWindow method without firing
  186. * a text change event.
  187. *
  188. * This is useful when you want to keep a validator in sync with other data,
  189. * but the act of changing it should not trigger other updates. It is the
  190. * validator equivalent of ChangeValue() compared to SetValue().
  191. *
  192. * This function blocks all events, but the same technique can be used to
  193. * selectively block events.
  194. *
  195. * @param aValidator the validator to update the control of
  196. */
  197. void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
  198. } // namespace KIUI
  199. #endif // #ifndef VALIDATORS_H