You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.2 KiB

  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. /**
  25. * @file validators.cpp
  26. * @brief Custom text control validator implementations.
  27. */
  28. #include <kicad_string.h>
  29. #include <validators.h>
  30. #include <wx/grid.h>
  31. #include <wx/textctrl.h>
  32. #include <wx/textentry.h>
  33. #include <wx/log.h>
  34. GRID_CELL_TEXT_EDITOR::GRID_CELL_TEXT_EDITOR() : wxGridCellTextEditor()
  35. {
  36. }
  37. void GRID_CELL_TEXT_EDITOR::SetValidator(const wxValidator& validator)
  38. {
  39. // keep our own copy because wxGridCellTextEditor's is annoyingly private
  40. m_validator.reset( static_cast<wxValidator*>( validator.Clone() ) );
  41. wxGridCellTextEditor::SetValidator( validator );
  42. }
  43. void GRID_CELL_TEXT_EDITOR::StartingKey( wxKeyEvent& event )
  44. {
  45. if( m_validator )
  46. {
  47. m_validator.get()->SetWindow( Text() );
  48. m_validator.get()->ProcessEvent( event );
  49. }
  50. if( !event.WasProcessed() )
  51. wxGridCellTextEditor::StartingKey( event );
  52. }
  53. FILE_NAME_CHAR_VALIDATOR::FILE_NAME_CHAR_VALIDATOR( wxString* aValue ) :
  54. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
  55. {
  56. // The Windows (DOS) file system forbidden characters already include the forbidden
  57. // file name characters for both Posix and OSX systems. The characters \/:*?|"<> are
  58. // illegal and filtered by the validator.
  59. wxString illegalChars = wxFileName::GetForbiddenChars( wxPATH_DOS );
  60. wxTextValidator nameValidator( wxFILTER_EXCLUDE_CHAR_LIST );
  61. wxArrayString illegalCharList;
  62. for( unsigned i = 0; i < illegalChars.size(); i++ )
  63. illegalCharList.Add( wxString( illegalChars[i] ) );
  64. SetExcludes( illegalCharList );
  65. }
  66. FILE_NAME_WITH_PATH_CHAR_VALIDATOR::FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue ) :
  67. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST | wxFILTER_EMPTY, aValue )
  68. {
  69. // The Windows (DOS) file system forbidden characters already include the forbidden
  70. // file name characters for both Posix and OSX systems. The characters *?|"<> are
  71. // illegal and filtered by the validator, but /\: are valid (\ and : only on Windows.
  72. wxString illegalChars = wxFileName::GetForbiddenChars( wxPATH_DOS );
  73. wxTextValidator nameValidator( wxFILTER_EXCLUDE_CHAR_LIST );
  74. wxArrayString illegalCharList;
  75. for( unsigned i = 0; i < illegalChars.size(); i++ )
  76. {
  77. if( illegalChars[i] == '/' )
  78. continue;
  79. #if defined (__WINDOWS__)
  80. if( illegalChars[i] == '\\' || illegalChars[i] == ':' )
  81. continue;
  82. #endif
  83. illegalCharList.Add( wxString( illegalChars[i] ) );
  84. }
  85. SetExcludes( illegalCharList );
  86. }
  87. ENV_VAR_NAME_VALIDATOR::ENV_VAR_NAME_VALIDATOR( wxString* aValue ) :
  88. wxTextValidator()
  89. {
  90. Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
  91. }
  92. ENV_VAR_NAME_VALIDATOR::ENV_VAR_NAME_VALIDATOR( const ENV_VAR_NAME_VALIDATOR& val )
  93. : wxTextValidator()
  94. {
  95. wxValidator::Copy( val );
  96. Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
  97. }
  98. ENV_VAR_NAME_VALIDATOR::~ENV_VAR_NAME_VALIDATOR()
  99. {
  100. Disconnect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
  101. }
  102. void ENV_VAR_NAME_VALIDATOR::OnChar( wxKeyEvent& aEvent )
  103. {
  104. if (!m_validatorWindow)
  105. {
  106. aEvent.Skip();
  107. return;
  108. }
  109. int keyCode = aEvent.GetKeyCode();
  110. // we don't filter special keys and delete
  111. if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START)
  112. {
  113. aEvent.Skip();
  114. return;
  115. }
  116. wxUniChar c = (wxUChar) keyCode;
  117. if( c == wxT( '_' ) )
  118. {
  119. // OK anywhere
  120. aEvent.Skip();
  121. }
  122. else if( wxIsdigit( c ) )
  123. {
  124. // not as first character
  125. long from, to;
  126. GetTextEntry()->GetSelection( &from, &to );
  127. if( from < 1 )
  128. wxBell();
  129. else
  130. aEvent.Skip();
  131. }
  132. else if( wxIsalpha( c ) )
  133. {
  134. // Capitals only.
  135. // Note: changing the keyCode and/or uniChar in the event and passing it on
  136. // doesn't work. Some platforms look at the original copy as long as the event
  137. // isn't vetoed. Insert the capitalized character by hand.
  138. wxString s( c, 1 );
  139. s = s.Capitalize();
  140. GetTextEntry()->WriteText( s );
  141. }
  142. else
  143. {
  144. wxBell();
  145. }
  146. }