KiCad PCB EDA Suite
validators.h
Go to the documentation of this file.
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 
31 #ifndef VALIDATORS_H
32 #define VALIDATORS_H
33 
34 #include <wx/valtext.h>
35 #include <wx/grid.h>
36 #include <wx/regex.h>
37 
38 #include <lib_id.h>
39 
44 class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
45 {
46 public:
48 
49  virtual void SetValidator(const wxValidator& validator) override;
50  virtual void StartingKey(wxKeyEvent& event) override;
51 
52 protected:
53  wxScopedPtr<wxValidator> m_validator;
54 };
55 
56 
63 class FILE_NAME_CHAR_VALIDATOR : public wxTextValidator
64 {
65 public:
66  FILE_NAME_CHAR_VALIDATOR( wxString* aValue = NULL );
67 };
68 
69 
76 class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
77 {
78 public:
79  FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = NULL );
80 };
81 
82 
91 class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
92 {
93 public:
94  ENV_VAR_NAME_VALIDATOR( wxString* aValue = NULL );
96 
97  virtual ~ENV_VAR_NAME_VALIDATOR();
98 
99  // Make a clone of this validator (or return NULL) - currently necessary
100  // if you're passing a reference to a validator.
101  virtual wxObject *Clone() const override
102  {
103  return new ENV_VAR_NAME_VALIDATOR( *this );
104  }
105 
106  void OnChar( wxKeyEvent& event );
107 
108  void OnTextChanged( wxCommandEvent& event );
109 };
110 
111 
116 class REGEX_VALIDATOR : public wxTextValidator
117 {
118 public:
123  REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = NULL )
124  : wxTextValidator( wxFILTER_NONE, aValue )
125  {
126  compileRegEx( aRegEx, wxRE_DEFAULT );
127  }
128 
134  REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = NULL )
135  : wxTextValidator( wxFILTER_NONE, aValue )
136  {
137  compileRegEx( aRegEx, aFlags );
138  }
139 
141  {
142  wxValidator::Copy( aOther );
143  compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
144  }
145 
146  virtual wxObject* Clone() const override
147  {
148  return new REGEX_VALIDATOR( *this );
149  }
150 
151  bool Validate( wxWindow* aParent ) override;
152 
153 protected:
155  void compileRegEx( const wxString& aRegEx, int aFlags );
156 
158  wxString m_regExString;
159 
162 
164  wxRegEx m_regEx;
165 };
166 
173 class LIB_ID_VALIDATOR : public wxTextValidator
174 {
175 public:
180  LIB_ID_VALIDATOR( LIB_ID::LIB_ID_TYPE aLibIdType, wxString* aValue = NULL ) :
181  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue ),
182  m_idType( aLibIdType )
183  {
184  SetCharExcludes( "\r\n\t" );
185  }
186 
187  virtual wxObject* Clone() const override
188  {
189  return new LIB_ID_VALIDATOR( *this );
190  }
191 
192  bool Validate( wxWindow* aParent ) override;
193 
194 protected:
196 };
197 
198 namespace KIUI
199 {
213 void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
214 
215 } // namespace KIUI
216 
217 #endif // #ifndef VALIDATORS_H
ENV_VAR_NAME_VALIDATOR(wxString *aValue=NULL)
Definition: validators.cpp:114
wxString m_regExString
Original regular expression (for copy constructor)
Definition: validators.h:158
wxRegEx m_regEx
Compiled regex
Definition: validators.h:164
void compileRegEx(const wxString &aRegEx, int aFlags)
Compiles and stores a regular expression
Definition: validators.cpp:264
void ValidatorTransferToWindowWithoutEvents(wxValidator &aValidator)
Call a text validator's TransferDataToWindow method without firing a text change event.
Definition: validators.cpp:325
wxScopedPtr< wxValidator > m_validator
Definition: validators.h:53
bool Validate(wxWindow *aParent) override
Definition: validators.cpp:226
virtual wxObject * Clone() const override
Definition: validators.h:187
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:76
virtual void StartingKey(wxKeyEvent &event) override
Definition: validators.cpp:55
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Definition: validators.h:44
LIB_ID::LIB_ID_TYPE m_idType
Definition: validators.h:195
void OnTextChanged(wxCommandEvent &event)
Definition: validators.cpp:207
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:63
virtual wxObject * Clone() const override
Definition: validators.h:146
REGEX_VALIDATOR(const wxString &aRegEx, int aFlags, wxString *aValue=NULL)
Definition: validators.h:134
Custom validator that verifies that a string defines a valid LIB_ID.
Definition: validators.h:173
virtual void SetValidator(const wxValidator &validator) override
Definition: validators.cpp:46
REGEX_VALIDATOR(const wxString &aRegEx, wxString *aValue=NULL)
Definition: validators.h:123
virtual wxObject * Clone() const override
Definition: validators.h:101
This class provides a custom wxValidator object for limiting the allowable characters when defining a...
Definition: validators.h:91
LIB_ID_VALIDATOR(LIB_ID::LIB_ID_TYPE aLibIdType, wxString *aValue=NULL)
Definition: validators.h:180
LIB_ID_TYPE
Types of library identifiers
Definition: lib_id.h:56
FILE_NAME_WITH_PATH_CHAR_VALIDATOR(wxString *aValue=NULL)
Definition: validators.cpp:88
REGEX_VALIDATOR(const REGEX_VALIDATOR &aOther)
Definition: validators.h:140
int m_regExFlags
Original compilation flags (for copy constructor)
Definition: validators.h:161
bool Validate(wxWindow *aParent) override
Definition: validators.cpp:277
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:136
Custom validator that checks verifies that a string exactly matches a regular expression.
Definition: validators.h:116
FILE_NAME_CHAR_VALIDATOR(wxString *aValue=NULL)
Definition: validators.cpp:71
virtual ~ENV_VAR_NAME_VALIDATOR()
Definition: validators.cpp:130