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.

166 lines
5.1 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 The 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. #include <template_fieldnames.h>
  37. /**
  38. * Provide a custom wxValidator object for limiting the allowable characters when
  39. * defining footprint names.
  40. *
  41. * Since the introduction of the PRETTY footprint library format, footprint names cannot have
  42. * any characters that would prevent file creation on any platform. The characters \/:*?|"<>
  43. * are illegal and filtered by the validator.
  44. */
  45. class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
  46. {
  47. public:
  48. FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
  49. };
  50. /**
  51. * Provide a custom wxValidator object for limiting the allowable characters when defining an
  52. * environment variable name in a text edit control.
  53. *
  54. * Only uppercase, numbers, and underscore (_) characters are valid and the first character of
  55. * the name cannot start with a number. This is according to IEEE Std 1003.1-2001. Even though
  56. * most systems support other characters, these characters guarantee compatibility for
  57. * all shells.
  58. */
  59. class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
  60. {
  61. public:
  62. ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
  63. ENV_VAR_NAME_VALIDATOR( const ENV_VAR_NAME_VALIDATOR& val );
  64. virtual ~ENV_VAR_NAME_VALIDATOR();
  65. // Make a clone of this validator (or return nullptr) - currently necessary
  66. // if you're passing a reference to a validator.
  67. virtual wxObject *Clone() const override
  68. {
  69. return new ENV_VAR_NAME_VALIDATOR( *this );
  70. }
  71. void OnChar( wxKeyEvent& event );
  72. void OnTextChanged( wxCommandEvent& event );
  73. };
  74. class NETNAME_VALIDATOR : public wxTextValidator
  75. {
  76. public:
  77. NETNAME_VALIDATOR( wxString* aVal = nullptr );
  78. NETNAME_VALIDATOR( bool aAllowSpaces );
  79. NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );
  80. virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }
  81. virtual bool TransferToWindow() override { return true; }
  82. virtual bool TransferFromWindow() override { return true; }
  83. virtual bool Validate( wxWindow *aParent ) override;
  84. /// @return the error message if the contents of @a aVal are invalid.
  85. wxString IsValid( const wxString& aVal ) const override;
  86. private:
  87. bool m_allowSpaces;
  88. };
  89. namespace KIUI
  90. {
  91. /**
  92. * Call a text validator's TransferDataToWindow method without firing
  93. * a text change event.
  94. *
  95. * This is useful when you want to keep a validator in sync with other data,
  96. * but the act of changing it should not trigger other updates. It is the
  97. * validator equivalent of ChangeValue() compared to SetValue().
  98. *
  99. * This function blocks all events, but the same technique can be used to
  100. * selectively block events.
  101. *
  102. * @param aValidator the validator to update the control of
  103. */
  104. void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
  105. } // namespace KIUI
  106. /**
  107. * A text control validator used for validating the text allowed in fields.
  108. *
  109. * - The reference field does not accept spaces.
  110. * - The value field does not accept spaces in the symbol library editor because in symbol
  111. * libraries, the value field is the symbol name in the library.
  112. */
  113. class FIELD_VALIDATOR : public wxTextValidator
  114. {
  115. public:
  116. FIELD_VALIDATOR( FIELD_T aFieldId, wxString* aValue = nullptr );
  117. FIELD_VALIDATOR( const FIELD_VALIDATOR& aValidator );
  118. virtual wxObject* Clone() const override { return new FIELD_VALIDATOR( *this ); }
  119. /**
  120. * Override the default Validate() function provided by wxTextValidator to provide
  121. * better error messages.
  122. *
  123. * @param aParent is the parent window of the error message dialog.
  124. * @return true if the text in the control is valid otherwise false.
  125. */
  126. virtual bool Validate( wxWindow* aParent ) override;
  127. bool DoValidate( const wxString& aValue, wxWindow* aParent );
  128. private:
  129. FIELD_T m_fieldId;
  130. };
  131. #endif // #ifndef VALIDATORS_H