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.

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