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-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 
26 /**
27  * @file validators.h
28  * @brief Custom text control validator definitions.
29  */
30 
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 
40 /**
41  * This class works around a bug in wxGrid where the first keystroke doesn't get sent through
42  * the validator if the editor wasn't already open.
43  */
44 class GRID_CELL_TEXT_EDITOR : public wxGridCellTextEditor
45 {
46 public:
47     GRID_CELL_TEXT_EDITOR();
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 
57 /**
58  * This class provides a custom wxValidator object for limiting the allowable characters when
59  * defining footprint names.  Since the introduction of the PRETTY footprint library format,
60  * footprint names cannot have any characters that would prevent file creation on any platform.
61  * The characters \/:*?|"<> are illegal and filtered by the validator.
62  */
63 class FOOTPRINT_NAME_VALIDATOR : public wxTextValidator
64 {
65 public:
66     FOOTPRINT_NAME_VALIDATOR( wxString* aValue = nullptr );
67 };
68 
69 
70 /**
71  * This class provides a custom wxValidator object for limiting the allowable characters when
72  * defining file names with path, for instance in schematic sheet file names.
73  * The characters *?|"<> are illegal and filtered by the validator,
74  * but /\: are valid (\ and : only on Windows.)
75  */
76 class FILE_NAME_WITH_PATH_CHAR_VALIDATOR : public wxTextValidator
77 {
78 public:
79     FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue = nullptr );
80 };
81 
82 
83 /**
84  * This class provides a custom wxValidator object for limiting the allowable characters
85  * when defining an environment variable name in a text edit control.  Only uppercase,
86  * numbers, and underscore (_) characters are valid and the first character of the name
87  * cannot start with a number.  This is according to IEEE Std 1003.1-2001.  Even though
88  * most systems support other characters, these characters guarantee compatibility for
89  * all shells.
90  */
91 class ENV_VAR_NAME_VALIDATOR : public wxTextValidator
92 {
93 public:
94     ENV_VAR_NAME_VALIDATOR( wxString* aValue = nullptr );
95     ENV_VAR_NAME_VALIDATOR( const ENV_VAR_NAME_VALIDATOR& val );
96 
97     virtual ~ENV_VAR_NAME_VALIDATOR();
98 
99     // Make a clone of this validator (or return nullptr) - currently necessary
100     // if you're passing a reference to a validator.
Clone()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 
112 /**
113  * Custom validator that checks verifies that a string *exactly* matches a
114  * regular expression.
115  */
116 class REGEX_VALIDATOR : public wxTextValidator
117 {
118 public:
119     /**
120      * @param aRegEx is a regular expression to validate strings.
121      * @param aValue is a pointer to a wxString containing the value to validate.
122      */
123     REGEX_VALIDATOR( const wxString& aRegEx, wxString* aValue = nullptr )
wxTextValidator(wxFILTER_NONE,aValue)124         : wxTextValidator( wxFILTER_NONE, aValue )
125     {
126         compileRegEx( aRegEx, wxRE_DEFAULT );
127     }
128 
129     /**
130      * @param aRegEx is a regular expression to validate strings.
131      * @param aFlags are compilation flags (normally wxRE_DEFAULT).
132      * @param aValue is a pointer to a wxString containing the value to validate.
133      */
134     REGEX_VALIDATOR( const wxString& aRegEx, int aFlags, wxString* aValue = nullptr )
wxTextValidator(wxFILTER_NONE,aValue)135         : wxTextValidator( wxFILTER_NONE, aValue )
136     {
137         compileRegEx( aRegEx, aFlags );
138     }
139 
REGEX_VALIDATOR(const REGEX_VALIDATOR & aOther)140     REGEX_VALIDATOR( const REGEX_VALIDATOR& aOther ) : wxTextValidator( aOther )
141     {
142         compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
143     }
144 
Clone()145     virtual wxObject* Clone() const override
146     {
147         return new REGEX_VALIDATOR( *this );
148     }
149 
150     bool Validate( wxWindow* aParent ) override;
151 
152 protected:
153     ///< Compiles and stores a regular expression
154     void compileRegEx( const wxString& aRegEx, int aFlags );
155 
156     ///< Original regular expression (for copy constructor)
157     wxString m_regExString;
158 
159     ///< Original compilation flags (for copy constructor)
160     int m_regExFlags;
161 
162     ///< Compiled regex
163     wxRegEx m_regEx;
164 };
165 
166 /**
167  * Custom validator that verifies that a string defines a valid #LIB_ID.
168  *
169  * The default validation allows empty #LIB_ID strings to allow the #LIB_ID to be cleared.
170  * Use SetStyle( wxFILTER_EMPTY ) to force a valid #LIB_ID string.
171  */
172 class LIB_ID_VALIDATOR : public wxTextValidator
173 {
174 public:
175     /**
176      * @param aLibIdType is the type of #LIB_ID object to validate.
177      * @param aValue is a pointer to a wxString containing the value to validate.
178      */
179     LIB_ID_VALIDATOR( wxString* aValue = nullptr ) :
wxTextValidator(wxFILTER_EXCLUDE_CHAR_LIST,aValue)180         wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
181     {
182         SetCharExcludes( "\r\n\t" );
183     }
184 
Clone()185     virtual wxObject* Clone() const override
186     {
187         return new LIB_ID_VALIDATOR( *this );
188     }
189 
190     bool Validate( wxWindow* aParent ) override;
191 };
192 
193 
194 class NETNAME_VALIDATOR : public wxTextValidator
195 {
196 public:
197     NETNAME_VALIDATOR( wxString* aVal = nullptr );
198 
199     NETNAME_VALIDATOR( bool aAllowSpaces );
200 
201     NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator );
202 
Clone()203     virtual wxObject* Clone() const override { return new NETNAME_VALIDATOR( *this ); }
204 
TransferToWindow()205     virtual bool TransferToWindow() override { return true; }
206 
TransferFromWindow()207     virtual bool TransferFromWindow() override { return true; }
208 
209     virtual bool Validate( wxWindow *aParent ) override;
210 
211 protected:
212     // returns the error message if the contents of 'val' are invalid
213     wxString IsValid( const wxString& aVal ) const override;
214 
215 private:
216     bool m_allowSpaces;
217 };
218 
219 
220 namespace KIUI
221 {
222 /**
223  * Call a text validator's TransferDataToWindow method without firing
224  * a text change event.
225  *
226  * This is useful when you want to keep a validator in sync with other data,
227  * but the act of changing it should not trigger other updates. It is the
228  * validator equivalent of ChangeValue() compared to SetValue().
229  *
230  * This function blocks all events, but the same technique can be used to
231  * selectively block events.
232  *
233  * @param aValidator the validator to update the control of
234  */
235 void ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator );
236 
237 } // namespace KIUI
238 
239 #endif  // #ifndef VALIDATORS_H
240