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 <[email protected]>
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
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
44class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
45{
46public:
48
49 virtual void SetValidator( const wxValidator& validator ) override;
50 virtual void StartingKey( wxKeyEvent& event ) override;
51
52protected:
53 wxScopedPtr<wxValidator> m_validator;
54};
55
56
63class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
64{
65public:
66 FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
67};
68
69
76class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
77{
78public:
79 FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = nullptr );
80};
81
82
91class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
92{
93public:
94 ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
96
98
99 // Make a clone of this validator (or return nullptr) - 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
116class REGEX_VALIDATOR : public wxTextValidator
117{
118public:
123 REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = nullptr )
124 : wxTextValidator( wxFILTER_NONE, aValue )
125 {
126 compileRegEx( aRegEx, wxRE_DEFAULT );
127 }
128
134 REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = nullptr )
135 : wxTextValidator( wxFILTER_NONE, aValue )
136 {
137 compileRegEx( aRegEx, aFlags );
138 }
139
140 REGEX_VALIDATOR( const REGEX_VALIDATOR& aOther ) : wxTextValidator( aOther )
141 {
142 compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
143 }
144
145 virtual wxObject* Clone() const override
146 {
147 return new REGEX_VALIDATOR( *this );
148 }
149
150 bool Validate( wxWindow* aParent ) override;
151
152 const wxString& GetRegEx() const
153 {
154 return m_regExString;
155 }
156
157protected:
159 void compileRegEx( const wxString& aRegEx, int aFlags );
160
163
166
168 wxRegEx m_regEx;
169};
170
177class LIB_ID_VALIDATOR : public wxTextValidator
178{
179public:
184 LIB_ID_VALIDATOR( wxString* aValue = nullptr ) :
185 wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
186 {
187 SetCharExcludes( wxT( "\r\n\t" ) );
188 }
189
190 virtual wxObject* Clone() const override
191 {
192 return new LIB_ID_VALIDATOR( *this );
193 }
194
195 bool Validate( wxWindow* aParent ) override;
196};
197
198
199class NETNAME_VALIDATOR : public wxTextValidator
200{
201public:
202 NETNAME_VALIDATOR( wxString* aVal = nullptr );
203
204 NETNAME_VALIDATOR( bool aAllowSpaces );
205
206 NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );
207
208 virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }
209
210 virtual bool TransferToWindow() override { return true; }
211
212 virtual bool TransferFromWindow() override { return true; }
213
214 virtual bool Validate( wxWindow *aParent ) override;
215
216protected:
217 // returns the error message if the contents of 'val' are invalid
218 wxString IsValid( const wxString& aVal ) const override;
219
220private:
222};
223
224
225namespace KIUI
226{
240void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
241
242} // namespace KIUI
243
244#endif // #ifndef VALIDATORS_H
This class provides a custom wxValidator object for limiting the allowable characters when defining a...
Definition: validators.h:92
virtual ~ENV_VAR_NAME_VALIDATOR()
Definition: validators.cpp:124
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:130
ENV_VAR_NAME_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.cpp:108
void OnTextChanged(wxCommandEvent &event)
Definition: validators.cpp:201
virtual wxObject * Clone() const override
Definition: validators.h:101
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:77
FILE_NAME_WITH_PATH_CHAR_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.cpp:82
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:64
FOOTPRINT_NAME_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.cpp:71
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Definition: validators.h:45
wxScopedPtr< wxValidator > m_validator
Definition: validators.h:53
virtual void StartingKey(wxKeyEvent &event) override
Definition: validators.cpp:55
virtual void SetValidator(const wxValidator &validator) override
Definition: validators.cpp:46
Custom validator that verifies that a string defines a valid LIB_ID.
Definition: validators.h:178
bool Validate(wxWindow *aParent) override
Definition: validators.cpp:271
virtual wxObject * Clone() const override
Definition: validators.h:190
LIB_ID_VALIDATOR(wxString *aValue=nullptr)
Definition: validators.h:184
wxString IsValid(const wxString &aVal) const override
Definition: validators.cpp:364
virtual wxObject * Clone() const override
Definition: validators.h:208
virtual bool Validate(wxWindow *aParent) override
Definition: validators.cpp:340
virtual bool TransferToWindow() override
Definition: validators.h:210
NETNAME_VALIDATOR(wxString *aVal=nullptr)
Definition: validators.cpp:319
virtual bool TransferFromWindow() override
Definition: validators.h:212
Custom validator that checks verifies that a string exactly matches a regular expression.
Definition: validators.h:117
virtual wxObject * Clone() const override
Definition: validators.h:145
wxRegEx m_regEx
Definition: validators.h:168
REGEX_VALIDATOR(const REGEX_VALIDATOR &aOther)
Definition: validators.h:140
const wxString & GetRegEx() const
Definition: validators.h:152
bool Validate(wxWindow *aParent) override
Definition: validators.cpp:220
wxString m_regExString
Original compilation flags (for copy constructor)
Definition: validators.h:162
REGEX_VALIDATOR(const wxString &aRegEx, wxString *aValue=nullptr)
Definition: validators.h:123
int m_regExFlags
Compiled regex.
Definition: validators.h:165
REGEX_VALIDATOR(const wxString &aRegEx, int aFlags, wxString *aValue=nullptr)
Definition: validators.h:134
void compileRegEx(const wxString &aRegEx, int aFlags)
< Compiles and stores a regular expression
Definition: validators.cpp:258
void ValidatorTransferToWindowWithoutEvents(wxValidator &aValidator)
Call a text validator's TransferDataToWindow method without firing a text change event.
Definition: validators.cpp:376