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.

216 lines
6.7 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-2019 KiCad Developers, see change_log.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 MODULE_NAME_CHAR_VALIDATOR : public wxTextValidator
  55. {
  56. public:
  57. MODULE_NAME_CHAR_VALIDATOR( wxString* aValue = NULL );
  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 = NULL );
  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 = NULL );
  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 NULL) - 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 = NULL )
  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 = NULL )
  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. protected:
  129. ///> Compiles and stores a regular expression
  130. void compileRegEx( const wxString& aRegEx, int aFlags );
  131. ///> Original regular expression (for copy constructor)
  132. wxString m_regExString;
  133. ///> Original compilation flags (for copy constructor)
  134. int m_regExFlags;
  135. ///> Compiled regex
  136. wxRegEx m_regEx;
  137. };
  138. /**
  139. * Custom validator that verifies that a string defines a valid #LIB_ID.
  140. *
  141. * The default validation allows empty #LIB_ID strings to allow the #LIB_ID to be cleared.
  142. * Use SetStyle( wxFILTER_EMPTY ) to force a valid #LIB_ID string.
  143. */
  144. class LIB_ID_VALIDATOR : public wxTextValidator
  145. {
  146. public:
  147. /**
  148. * @param aLibIdType is the type of #LIB_ID object to validate.
  149. * @param aValue is a pointer to a wxString containing the value to validate.
  150. */
  151. LIB_ID_VALIDATOR( LIB_ID::LIB_ID_TYPE aLibIdType, wxString* aValue = NULL ) :
  152. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue ),
  153. m_idType( aLibIdType )
  154. {
  155. SetCharExcludes( "\r\n\t" );
  156. }
  157. virtual wxObject* Clone() const override
  158. {
  159. return new LIB_ID_VALIDATOR( *this );
  160. }
  161. bool Validate( wxWindow* aParent ) override;
  162. protected:
  163. LIB_ID::LIB_ID_TYPE m_idType;
  164. };
  165. namespace KIUI
  166. {
  167. /**
  168. * Call a text validator's TransferDataToWindow method without firing
  169. * a text change event.
  170. *
  171. * This is useful when you want to keep a validator in sync with other data,
  172. * but the act of changing it should not trigger other updates. It is the
  173. * validator equivalent of ChangeValue() compared to SetValue().
  174. *
  175. * This function blocks all events, but the same technique can be used to
  176. * selectively block events.
  177. *
  178. * @param aValidator the validator to update the control of
  179. */
  180. void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
  181. } // namespace KIUI
  182. #endif // #ifndef VALIDATORS_H