KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <memory>
35
36#include <wx/valtext.h>
37#include <wx/grid.h>
38#include <wx/regex.h>
39
40#include <lib_id.h>
41
42
43#define FIELD_NAME -1
44#define FIELD_VALUE -2
45
46#define SHEETNAME_V 100 // We can't use SHEETNAME and SHEETFILENAME because they
47#define SHEETFILENAME_V 101 // overlap with REFERENCE_FIELD and VALUE_FIELD
48#define SHEETUSERFIELD_V 102
49
50#define LABELUSERFIELD_V 200
51
52
57class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
58{
59public:
61
62 virtual void SetValidator( const wxValidator& validator ) override;
63 virtual void StartingKey( wxKeyEvent& event ) override;
64
65protected:
66 std::unique_ptr<wxValidator> m_validator;
67};
68
69
76class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
77{
78public:
79 FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
80};
81
82
89class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
90{
91public:
92 FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = nullptr );
93};
94
95
104class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
105{
106public:
107 ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
109
110 virtual ~ENV_VAR_NAME_VALIDATOR();
111
112 // Make a clone of this validator (or return nullptr) - currently necessary
113 // if you're passing a reference to a validator.
114 virtual wxObject *Clone() const override
115 {
116 return new ENV_VAR_NAME_VALIDATOR( *this );
117 }
118
119 void OnChar( wxKeyEvent& event );
120
121 void OnTextChanged( wxCommandEvent& event );
122};
123
124
129class REGEX_VALIDATOR : public wxTextValidator
130{
131public:
136 REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = nullptr )
137 : wxTextValidator( wxFILTER_NONE, aValue )
138 {
139 compileRegEx( aRegEx, wxRE_DEFAULT );
140 }
141
147 REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = nullptr )
148 : wxTextValidator( wxFILTER_NONE, aValue )
149 {
150 compileRegEx( aRegEx, aFlags );
151 }
152
153 REGEX_VALIDATOR( const REGEX_VALIDATOR& aOther ) : wxTextValidator( aOther )
154 {
155 compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
156 }
157
158 virtual wxObject* Clone() const override
159 {
160 return new REGEX_VALIDATOR( *this );
161 }
162
163 bool Validate( wxWindow* aParent ) override;
164
165 const wxString& GetRegEx() const
166 {
167 return m_regExString;
168 }
169
170protected:
172 void compileRegEx( const wxString& aRegEx, int aFlags );
173
176
179
181 wxRegEx m_regEx;
182};
183
184class NETNAME_VALIDATOR : public wxTextValidator
185{
186public:
187 NETNAME_VALIDATOR( wxString* aVal = nullptr );
188
189 NETNAME_VALIDATOR( bool aAllowSpaces );
190
191 NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );
192
193 virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }
194
195 virtual bool TransferToWindow() override { return true; }
196
197 virtual bool TransferFromWindow() override { return true; }
198
199 virtual bool Validate( wxWindow *aParent ) override;
200
201protected:
202 // returns the error message if the contents of 'val' are invalid
203 wxString IsValid( const wxString& aVal ) const override;
204
205private:
207};
208
209
210namespace KIUI
211{
225void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
226
227} // namespace KIUI
228
229
237class FIELD_VALIDATOR : public wxTextValidator
238{
239public:
240 FIELD_VALIDATOR( int aFieldId, wxString* aValue = nullptr );
241
242 FIELD_VALIDATOR( const FIELD_VALIDATOR& aValidator );
243
244 virtual wxObject* Clone() const override { return new FIELD_VALIDATOR( *this ); }
245
253 virtual bool Validate( wxWindow* aParent ) override;
254
255private:
257};
258
259
260#endif // #ifndef VALIDATORS_H
This class provides a custom wxValidator object for limiting the allowable characters when defining a...
Definition: validators.h:105
virtual ~ENV_VAR_NAME_VALIDATOR()
Definition: validators.cpp:125
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:131
void OnTextChanged(wxCommandEvent &event)
Definition: validators.cpp:202
virtual wxObject * Clone() const override
Definition: validators.h:114
A text control validator used for validating the text allowed in fields.
Definition: validators.h:238
virtual wxObject * Clone() const override
Definition: validators.h:244
virtual bool Validate(wxWindow *aParent) override
Override the default Validate() function provided by wxTextValidator to provide better error messages...
Definition: validators.cpp:375
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:90
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:77
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Definition: validators.h:58
std::unique_ptr< wxValidator > m_validator
Definition: validators.h:66
virtual void StartingKey(wxKeyEvent &event) override
Definition: validators.cpp:56
virtual void SetValidator(const wxValidator &validator) override
Definition: validators.cpp:47
wxString IsValid(const wxString &aVal) const override
Definition: validators.cpp:317
virtual wxObject * Clone() const override
Definition: validators.h:193
virtual bool Validate(wxWindow *aParent) override
Definition: validators.cpp:293
virtual bool TransferToWindow() override
Definition: validators.h:195
virtual bool TransferFromWindow() override
Definition: validators.h:197
Custom validator that checks verifies that a string exactly matches a regular expression.
Definition: validators.h:130
virtual wxObject * Clone() const override
Definition: validators.h:158
wxRegEx m_regEx
Definition: validators.h:181
REGEX_VALIDATOR(const REGEX_VALIDATOR &aOther)
Definition: validators.h:153
const wxString & GetRegEx() const
Definition: validators.h:165
bool Validate(wxWindow *aParent) override
Definition: validators.cpp:221
wxString m_regExString
Original compilation flags (for copy constructor)
Definition: validators.h:175
REGEX_VALIDATOR(const wxString &aRegEx, wxString *aValue=nullptr)
Definition: validators.h:136
int m_regExFlags
Compiled regex.
Definition: validators.h:178
REGEX_VALIDATOR(const wxString &aRegEx, int aFlags, wxString *aValue=nullptr)
Definition: validators.h:147
void compileRegEx(const wxString &aRegEx, int aFlags)
< Compiles and stores a regular expression
Definition: validators.cpp:259
void ValidatorTransferToWindowWithoutEvents(wxValidator &aValidator)
Call a text validator's TransferDataToWindow method without firing a text change event.
Definition: validators.cpp:329