KiCad PCB EDA Suite
validators.cpp
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@verizon.net>
5  * Copyright (C) 2004-2013 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <kicad_string.h>
31 #include <validators.h>
32 
33 #include <wx/grid.h>
34 #include <wx/textctrl.h>
35 #include <wx/textentry.h>
36 #include <wx/log.h>
37 
38 
40 {
41 }
42 
43 
44 void GRID_CELL_TEXT_EDITOR::SetValidator( const wxValidator& validator )
45 {
46  // keep our own copy because wxGridCellTextEditor's is annoyingly private
47  m_validator.reset( static_cast<wxValidator*>( validator.Clone() ) );
48 
49  wxGridCellTextEditor::SetValidator( *m_validator );
50 }
51 
52 
53 void GRID_CELL_TEXT_EDITOR::StartingKey( wxKeyEvent& event )
54 {
55  if( m_validator )
56  {
57  m_validator.get()->SetWindow( Text() );
58  m_validator.get()->ProcessEvent( event );
59  }
60 
61  if( event.GetSkipped() )
62  {
63  wxGridCellTextEditor::StartingKey( event );
64  event.Skip( false );
65  }
66 }
67 
68 
70  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
71 {
72  // The Windows (DOS) file system forbidden characters already include the forbidden
73  // file name characters for both Posix and OSX systems. The characters \/:*?|"<> are
74  // illegal and filtered by the validator.
75  wxString illegalChars = wxFileName::GetForbiddenChars( wxPATH_DOS );
76  wxTextValidator nameValidator( wxFILTER_EXCLUDE_CHAR_LIST );
77  wxArrayString illegalCharList;
78 
79  for( unsigned i = 0; i < illegalChars.size(); i++ )
80  illegalCharList.Add( wxString( illegalChars[i] ) );
81 
82  SetExcludes( illegalCharList );
83  }
84 
85 
87  wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST | wxFILTER_EMPTY, aValue )
88 {
89  // The Windows (DOS) file system forbidden characters already include the forbidden
90  // file name characters for both Posix and OSX systems. The characters *?|"<> are
91  // illegal and filtered by the validator, but /\: are valid (\ and : only on Windows.
92  wxString illegalChars = wxFileName::GetForbiddenChars( wxPATH_DOS );
93  wxTextValidator nameValidator( wxFILTER_EXCLUDE_CHAR_LIST );
94  wxArrayString illegalCharList;
95 
96  for( unsigned i = 0; i < illegalChars.size(); i++ )
97  {
98  if( illegalChars[i] == '/' )
99  continue;
100 
101 #if defined (__WINDOWS__)
102  if( illegalChars[i] == '\\' || illegalChars[i] == ':' )
103  continue;
104 #endif
105  illegalCharList.Add( wxString( illegalChars[i] ) );
106  }
107 
108  SetExcludes( illegalCharList );
109 }
110 
111 
113  wxTextValidator()
114 {
115  Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
116 }
117 
118 
120  : wxTextValidator()
121 {
122  wxValidator::Copy( val );
123 
124  Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
125 }
126 
127 
129 {
130  Disconnect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
131 }
132 
133 
134 void ENV_VAR_NAME_VALIDATOR::OnChar( wxKeyEvent& aEvent )
135 {
136  if (!m_validatorWindow)
137  {
138  aEvent.Skip();
139  return;
140  }
141 
142  int keyCode = aEvent.GetKeyCode();
143 
144  // we don't filter special keys and delete
145  if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START)
146  {
147  aEvent.Skip();
148  return;
149  }
150 
151  wxUniChar c = (wxUChar) keyCode;
152 
153  if( c == wxT( '_' ) )
154  {
155  // OK anywhere
156  aEvent.Skip();
157  }
158  else if( wxIsdigit( c ) )
159  {
160  // not as first character
161  long from, to;
162  GetTextEntry()->GetSelection( &from, &to );
163  if( from < 1 )
164  wxBell();
165  else
166  aEvent.Skip();
167  }
168  else if( wxIsalpha( c ) )
169  {
170  // Capitals only.
171 
172  if( wxIslower( c ) )
173  {
174  // You may wonder why this scope is so twisted, so make yourself comfortable and read:
175  // 1. Changing the keyCode and/or uniChar in the event and passing it on
176  // doesn't work. Some platforms look at the original copy as long as the event
177  // isn't vetoed.
178  // 2. Inserting characters by hand does not move the cursor, meaning either you insert
179  // text backwards (lp:#1798869) or always append, no matter where is the cursor.
180  // wxTextEntry::{Get/Set}InsertionPoint() do not work at all here.
181  // 3. There is wxTextEntry::ForceUpper(), but it is not yet available in common
182  // wxWidgets packages.
183  //
184  // So here we are, with a command event handler that converts
185  // the text to upper case upon every change.
186  wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( GetTextEntry() );
187 
188  if( textCtrl )
189  {
190  textCtrl->Connect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
191  (wxObjectEventFunction) &ENV_VAR_NAME_VALIDATOR::OnTextChanged );
192  }
193  }
194 
195  aEvent.Skip();
196  }
197  else
198  {
199  wxBell();
200  }
201 }
202 
203 
204 void ENV_VAR_NAME_VALIDATOR::OnTextChanged( wxCommandEvent& event )
205 {
206  wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( event.GetEventObject() );
207 
208  if( textCtrl )
209  {
210  if( !textCtrl->IsModified() )
211  return;
212 
213  long insertionPoint = textCtrl->GetInsertionPoint();
214  textCtrl->ChangeValue( textCtrl->GetValue().Upper() );
215  textCtrl->SetInsertionPoint( insertionPoint );
216  textCtrl->Disconnect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED );
217  }
218 
219  event.Skip();
220 }
ENV_VAR_NAME_VALIDATOR(wxString *aValue=NULL)
Definition: validators.cpp:112
wxScopedPtr< wxValidator > m_validator
Definition: validators.h:51
virtual void StartingKey(wxKeyEvent &event) override
Definition: validators.cpp:53
void OnTextChanged(wxCommandEvent &event)
Definition: validators.cpp:204
virtual void SetValidator(const wxValidator &validator) override
Definition: validators.cpp:44
Class ENV_VAR_NAME_VALIDATOR.
Definition: validators.h:95
FILE_NAME_WITH_PATH_CHAR_VALIDATOR(wxString *aValue=NULL)
Definition: validators.cpp:86
size_t i
Definition: json11.cpp:597
Custom text control validator definitions.
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:134
FILE_NAME_CHAR_VALIDATOR(wxString *aValue=NULL)
Definition: validators.cpp:69
virtual ~ENV_VAR_NAME_VALIDATOR()
Definition: validators.cpp:128