KiCad PCB EDA Suite
ENV_VAR_NAME_VALIDATOR Class Reference

This class provides a custom wxValidator object for limiting the allowable characters when defining an environment variable name in a text edit control. More...

#include <validators.h>

Inheritance diagram for ENV_VAR_NAME_VALIDATOR:

Public Member Functions

 ENV_VAR_NAME_VALIDATOR (wxString *aValue=NULL)
 
 ENV_VAR_NAME_VALIDATOR (const ENV_VAR_NAME_VALIDATOR &val)
 
virtual ~ENV_VAR_NAME_VALIDATOR ()
 
virtual wxObject * Clone () const override
 
void OnChar (wxKeyEvent &event)
 
void OnTextChanged (wxCommandEvent &event)
 

Detailed Description

This class provides a custom wxValidator object for limiting the allowable characters when defining an environment variable name in a text edit control.

Only uppercase, numbers, and underscore (_) characters are valid and the first character of the name cannot start with a number. This is according to IEEE Std 1003.1-2001. Even though most systems support other characters, these characters guarantee compatibility for all shells.

Definition at line 91 of file validators.h.

Constructor & Destructor Documentation

◆ ENV_VAR_NAME_VALIDATOR() [1/2]

ENV_VAR_NAME_VALIDATOR::ENV_VAR_NAME_VALIDATOR ( wxString *  aValue = NULL)

Definition at line 108 of file validators.cpp.

108  :
109  wxTextValidator()
110 {
111  Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
112 }
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:130

References OnChar().

Referenced by Clone().

◆ ENV_VAR_NAME_VALIDATOR() [2/2]

ENV_VAR_NAME_VALIDATOR::ENV_VAR_NAME_VALIDATOR ( const ENV_VAR_NAME_VALIDATOR val)

Definition at line 115 of file validators.cpp.

116  : wxTextValidator()
117 {
118  wxValidator::Copy( val );
119 
120  Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
121 }
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:130

References OnChar().

◆ ~ENV_VAR_NAME_VALIDATOR()

ENV_VAR_NAME_VALIDATOR::~ENV_VAR_NAME_VALIDATOR ( )
virtual

Definition at line 124 of file validators.cpp.

125 {
126  Disconnect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
127 }
void OnChar(wxKeyEvent &event)
Definition: validators.cpp:130

References OnChar().

Member Function Documentation

◆ Clone()

virtual wxObject* ENV_VAR_NAME_VALIDATOR::Clone ( ) const
inlineoverridevirtual

Definition at line 101 of file validators.h.

102  {
103  return new ENV_VAR_NAME_VALIDATOR( *this );
104  }
ENV_VAR_NAME_VALIDATOR(wxString *aValue=NULL)
Definition: validators.cpp:108

References ENV_VAR_NAME_VALIDATOR().

◆ OnChar()

void ENV_VAR_NAME_VALIDATOR::OnChar ( wxKeyEvent &  event)

Definition at line 130 of file validators.cpp.

131 {
132  if( !m_validatorWindow )
133  {
134  aEvent.Skip();
135  return;
136  }
137 
138  int keyCode = aEvent.GetKeyCode();
139 
140  // we don't filter special keys and delete
141  if( keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START )
142  {
143  aEvent.Skip();
144  return;
145  }
146 
147  wxUniChar c = (wxUChar) keyCode;
148 
149  if( c == wxT( '_' ) )
150  {
151  // OK anywhere
152  aEvent.Skip();
153  }
154  else if( wxIsdigit( c ) )
155  {
156  // not as first character
157  long from, to;
158  GetTextEntry()->GetSelection( &from, &to );
159 
160  if( from < 1 )
161  wxBell();
162  else
163  aEvent.Skip();
164  }
165  else if( wxIsalpha( c ) )
166  {
167  // Capitals only.
168 
169  if( wxIslower( c ) )
170  {
171  // You may wonder why this scope is so twisted, so make yourself comfortable and read:
172  // 1. Changing the keyCode and/or uniChar in the event and passing it on
173  // doesn't work. Some platforms look at the original copy as long as the event
174  // isn't vetoed.
175  // 2. Inserting characters by hand does not move the cursor, meaning either you insert
176  // text backwards (lp:#1798869) or always append, no matter where is the cursor.
177  // wxTextEntry::{Get/Set}InsertionPoint() do not work at all here.
178  // 3. There is wxTextEntry::ForceUpper(), but it is not yet available in common
179  // wxWidgets packages.
180  //
181  // So here we are, with a command event handler that converts
182  // the text to upper case upon every change.
183  wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( GetTextEntry() );
184 
185  if( textCtrl )
186  {
187  textCtrl->Connect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
188  wxCommandEventHandler( ENV_VAR_NAME_VALIDATOR::OnTextChanged ) );
189  }
190  }
191 
192  aEvent.Skip();
193  }
194  else
195  {
196  wxBell();
197  }
198 }
void OnTextChanged(wxCommandEvent &event)
Definition: validators.cpp:201

References OnTextChanged().

Referenced by ENV_VAR_NAME_VALIDATOR(), and ~ENV_VAR_NAME_VALIDATOR().

◆ OnTextChanged()

void ENV_VAR_NAME_VALIDATOR::OnTextChanged ( wxCommandEvent &  event)

Definition at line 201 of file validators.cpp.

202 {
203  wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( event.GetEventObject() );
204 
205  if( textCtrl )
206  {
207  if( !textCtrl->IsModified() )
208  return;
209 
210  long insertionPoint = textCtrl->GetInsertionPoint();
211  textCtrl->ChangeValue( textCtrl->GetValue().Upper() );
212  textCtrl->SetInsertionPoint( insertionPoint );
213  textCtrl->Disconnect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED );
214  }
215 
216  event.Skip();
217 }

Referenced by OnChar().


The documentation for this class was generated from the following files: