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.

246 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 <memory>
  32. #include <wx/valtext.h>
  33. #include <wx/grid.h>
  34. #include <wx/regex.h>
  35. #include <lib_id.h>
  36. /**
  37. * This class works around a bug in wxGrid where the first keystroke doesn't get sent through
  38. * the validator if the editor wasn't already open.
  39. */
  40. class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
  41. {
  42. public:
  43. GRID_CELL_TEXT_EDITOR();
  44. virtual void SetValidator( const wxValidator& validator ) override;
  45. virtual void StartingKey( wxKeyEvent& event ) override;
  46. protected:
  47. std::unique_ptr<wxValidator> m_validator;
  48. };
  49. /**
  50. * This class provides a custom wxValidator object for limiting the allowable characters when
  51. * defining footprint names. Since the introduction of the PRETTY footprint library format,
  52. * footprint names cannot have any characters that would prevent file creation on any platform.
  53. * The characters \/:*?|"<> are illegal and filtered by the validator.
  54. */
  55. class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
  56. {
  57. public:
  58. FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
  59. };
  60. /**
  61. * This class provides a custom wxValidator object for limiting the allowable characters when
  62. * defining file names with path, for instance in schematic sheet file names.
  63. * The characters *?|"<> are illegal and filtered by the validator,
  64. * but /\: are valid (\ and : only on Windows.)
  65. */
  66. class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
  67. {
  68. public:
  69. FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = nullptr );
  70. };
  71. /**
  72. * This class provides a custom wxValidator object for limiting the allowable characters
  73. * when defining an environment variable name in a text edit control. Only uppercase,
  74. * numbers, and underscore (_) characters are valid and the first character of the name
  75. * cannot start with a number. This is according to IEEE Std 1003.1-2001. Even though
  76. * most systems support other characters, these characters guarantee compatibility for
  77. * all shells.
  78. */
  79. class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
  80. {
  81. public:
  82. ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
  83. ENV_VAR_NAME_VALIDATOR( const ENV_VAR_NAME_VALIDATOR& val );
  84. virtual ~ENV_VAR_NAME_VALIDATOR();
  85. // Make a clone of this validator (or return nullptr) - currently necessary
  86. // if you're passing a reference to a validator.
  87. virtual wxObject *Clone() const override
  88. {
  89. return new ENV_VAR_NAME_VALIDATOR( *this );
  90. }
  91. void OnChar( wxKeyEvent& event );
  92. void OnTextChanged( wxCommandEvent& event );
  93. };
  94. /**
  95. * Custom validator that checks verifies that a string *exactly* matches a
  96. * regular expression.
  97. */
  98. class REGEX_VALIDATOR : public wxTextValidator
  99. {
  100. public:
  101. /**
  102. * @param aRegEx is a regular expression to validate strings.
  103. * @param aValue is a pointer to a wxString containing the value to validate.
  104. */
  105. REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = nullptr )
  106. : wxTextValidator( wxFILTER_NONE, aValue )
  107. {
  108. compileRegEx( aRegEx, wxRE_DEFAULT );
  109. }
  110. /**
  111. * @param aRegEx is a regular expression to validate strings.
  112. * @param aFlags are compilation flags (normally wxRE_DEFAULT).
  113. * @param aValue is a pointer to a wxString containing the value to validate.
  114. */
  115. REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = nullptr )
  116. : wxTextValidator( wxFILTER_NONE, aValue )
  117. {
  118. compileRegEx( aRegEx, aFlags );
  119. }
  120. REGEX_VALIDATOR( const REGEX_VALIDATOR& aOther ) : wxTextValidator( aOther )
  121. {
  122. compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
  123. }
  124. virtual wxObject* Clone() const override
  125. {
  126. return new REGEX_VALIDATOR( *this );
  127. }
  128. bool Validate( wxWindow* aParent ) override;
  129. const wxString& GetRegEx() const
  130. {
  131. return m_regExString;
  132. }
  133. protected:
  134. ///< Compiles and stores a regular expression
  135. void compileRegEx( const wxString& aRegEx, int aFlags );
  136. ///< Original regular expression (for copy constructor)
  137. wxString m_regExString;
  138. ///< Original compilation flags (for copy constructor)
  139. int m_regExFlags;
  140. ///< Compiled regex
  141. wxRegEx m_regEx;
  142. };
  143. /**
  144. * Custom validator that verifies that a string defines a valid #LIB_ID.
  145. *
  146. * The default validation allows empty #LIB_ID strings to allow the #LIB_ID to be cleared.
  147. * Use SetStyle( wxFILTER_EMPTY ) to force a valid #LIB_ID string.
  148. */
  149. class LIB_ID_VALIDATOR : public wxTextValidator
  150. {
  151. public:
  152. /**
  153. * @param aLibIdType is the type of #LIB_ID object to validate.
  154. * @param aValue is a pointer to a wxString containing the value to validate.
  155. */
  156. LIB_ID_VALIDATOR( wxString* aValue = nullptr ) :
  157. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
  158. {
  159. SetCharExcludes( wxT( "\r\n\t" ) );
  160. }
  161. virtual wxObject* Clone() const override
  162. {
  163. return new LIB_ID_VALIDATOR( *this );
  164. }
  165. bool Validate( wxWindow* aParent ) override;
  166. };
  167. class NETNAME_VALIDATOR : public wxTextValidator
  168. {
  169. public:
  170. NETNAME_VALIDATOR( wxString* aVal = nullptr );
  171. NETNAME_VALIDATOR( bool aAllowSpaces );
  172. NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );
  173. virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }
  174. virtual bool TransferToWindow() override { return true; }
  175. virtual bool TransferFromWindow() override { return true; }
  176. virtual bool Validate( wxWindow *aParent ) override;
  177. protected:
  178. // returns the error message if the contents of 'val' are invalid
  179. wxString IsValid( const wxString& aVal ) const override;
  180. private:
  181. bool m_allowSpaces;
  182. };
  183. namespace KIUI
  184. {
  185. /**
  186. * Call a text validator's TransferDataToWindow method without firing
  187. * a text change event.
  188. *
  189. * This is useful when you want to keep a validator in sync with other data,
  190. * but the act of changing it should not trigger other updates. It is the
  191. * validator equivalent of ChangeValue() compared to SetValue().
  192. *
  193. * This function blocks all events, but the same technique can be used to
  194. * selectively block events.
  195. *
  196. * @param aValidator the validator to update the control of
  197. */
  198. void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
  199. } // namespace KIUI
  200. #endif // #ifndef VALIDATORS_H