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.

384 lines
10 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@gmail.com>
  5. * Copyright (C) 2004-2019 KiCad Developers, see change_log.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. * @file validators.cpp
  27. * @brief Custom text control validator implementations.
  28. */
  29. #include <string_utils.h>
  30. #include <confirm.h>
  31. #include <validators.h>
  32. #include <wx/grid.h>
  33. #include <wx/textctrl.h>
  34. #include <wx/textentry.h>
  35. #include <wx/log.h>
  36. #include <wx/combo.h>
  37. GRID_CELL_TEXT_EDITOR::GRID_CELL_TEXT_EDITOR() : wxGridCellTextEditor()
  38. {
  39. }
  40. void GRID_CELL_TEXT_EDITOR::SetValidator( const wxValidator& validator )
  41. {
  42. // keep our own copy because wxGridCellTextEditor's is annoyingly private
  43. m_validator.reset( static_cast<wxValidator*>( validator.Clone() ) );
  44. wxGridCellTextEditor::SetValidator( *m_validator );
  45. }
  46. void GRID_CELL_TEXT_EDITOR::StartingKey( wxKeyEvent& event )
  47. {
  48. if( m_validator )
  49. {
  50. m_validator.get()->SetWindow( Text() );
  51. m_validator.get()->ProcessEvent( event );
  52. }
  53. if( event.GetSkipped() )
  54. {
  55. wxGridCellTextEditor::StartingKey( event );
  56. event.Skip( false );
  57. }
  58. }
  59. FOOTPRINT_NAME_VALIDATOR::FOOTPRINT_NAME_VALIDATOR( wxString* aValue ) :
  60. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
  61. {
  62. // This list of characters follows the string from footprint.cpp which, in turn mimics the
  63. // strings from lib_id.cpp
  64. // TODO: Unify forbidden character lists
  65. wxString illegalChars = "%$<>\t\n\r\"\\/:";
  66. SetCharExcludes( illegalChars );
  67. }
  68. FILE_NAME_WITH_PATH_CHAR_VALIDATOR::FILE_NAME_WITH_PATH_CHAR_VALIDATOR( wxString* aValue ) :
  69. wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST | wxFILTER_EMPTY, aValue )
  70. {
  71. // The Windows (DOS) file system forbidden characters already include the forbidden
  72. // file name characters for both Posix and OSX systems. The characters *?|"<> are
  73. // illegal and filtered by the validator, but /\: are valid (\ and : only on Windows.
  74. wxString illegalChars = wxFileName::GetForbiddenChars( wxPATH_DOS );
  75. wxTextValidator nameValidator( wxFILTER_EXCLUDE_CHAR_LIST );
  76. wxArrayString illegalCharList;
  77. for( unsigned i = 0; i < illegalChars.size(); i++ )
  78. {
  79. if( illegalChars[i] == '/' )
  80. continue;
  81. #if defined (__WINDOWS__)
  82. if( illegalChars[i] == '\\' || illegalChars[i] == ':' )
  83. continue;
  84. #endif
  85. illegalCharList.Add( wxString( illegalChars[i] ) );
  86. }
  87. SetExcludes( illegalCharList );
  88. }
  89. ENV_VAR_NAME_VALIDATOR::ENV_VAR_NAME_VALIDATOR( wxString* aValue ) :
  90. wxTextValidator()
  91. {
  92. Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
  93. }
  94. ENV_VAR_NAME_VALIDATOR::ENV_VAR_NAME_VALIDATOR( const ENV_VAR_NAME_VALIDATOR& val )
  95. : wxTextValidator()
  96. {
  97. wxValidator::Copy( val );
  98. Connect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
  99. }
  100. ENV_VAR_NAME_VALIDATOR::~ENV_VAR_NAME_VALIDATOR()
  101. {
  102. Disconnect( wxEVT_CHAR, wxKeyEventHandler( ENV_VAR_NAME_VALIDATOR::OnChar ) );
  103. }
  104. void ENV_VAR_NAME_VALIDATOR::OnChar( wxKeyEvent& aEvent )
  105. {
  106. if( !m_validatorWindow )
  107. {
  108. aEvent.Skip();
  109. return;
  110. }
  111. int keyCode = aEvent.GetKeyCode();
  112. // we don't filter special keys and delete
  113. if( keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START )
  114. {
  115. aEvent.Skip();
  116. return;
  117. }
  118. wxUniChar c = (wxUChar) keyCode;
  119. if( c == wxT( '_' ) )
  120. {
  121. // OK anywhere
  122. aEvent.Skip();
  123. }
  124. else if( wxIsdigit( c ) )
  125. {
  126. // not as first character
  127. long from, to;
  128. GetTextEntry()->GetSelection( &from, &to );
  129. if( from < 1 )
  130. wxBell();
  131. else
  132. aEvent.Skip();
  133. }
  134. else if( wxIsalpha( c ) )
  135. {
  136. // Capitals only.
  137. if( wxIslower( c ) )
  138. {
  139. // You may wonder why this scope is so twisted, so make yourself comfortable and read:
  140. // 1. Changing the keyCode and/or uniChar in the event and passing it on
  141. // doesn't work. Some platforms look at the original copy as long as the event
  142. // isn't vetoed.
  143. // 2. Inserting characters by hand does not move the cursor, meaning either you insert
  144. // text backwards (lp:#1798869) or always append, no matter where is the cursor.
  145. // wxTextEntry::{Get/Set}InsertionPoint() do not work at all here.
  146. // 3. There is wxTextEntry::ForceUpper(), but it is not yet available in common
  147. // wxWidgets packages.
  148. //
  149. // So here we are, with a command event handler that converts
  150. // the text to upper case upon every change.
  151. wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( GetTextEntry() );
  152. if( textCtrl )
  153. {
  154. textCtrl->Connect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED,
  155. wxCommandEventHandler( ENV_VAR_NAME_VALIDATOR::OnTextChanged ) );
  156. }
  157. }
  158. aEvent.Skip();
  159. }
  160. else
  161. {
  162. wxBell();
  163. }
  164. }
  165. void ENV_VAR_NAME_VALIDATOR::OnTextChanged( wxCommandEvent& event )
  166. {
  167. wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( event.GetEventObject() );
  168. if( textCtrl )
  169. {
  170. if( !textCtrl->IsModified() )
  171. return;
  172. long insertionPoint = textCtrl->GetInsertionPoint();
  173. textCtrl->ChangeValue( textCtrl->GetValue().Upper() );
  174. textCtrl->SetInsertionPoint( insertionPoint );
  175. textCtrl->Disconnect( textCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED );
  176. }
  177. event.Skip();
  178. }
  179. bool REGEX_VALIDATOR::Validate( wxWindow* aParent )
  180. {
  181. // If window is disabled, simply return
  182. if( !m_validatorWindow->IsEnabled() )
  183. return true;
  184. wxTextEntry* const textEntry = GetTextEntry();
  185. if( !textEntry )
  186. return false;
  187. bool valid = true;
  188. const wxString& value = textEntry->GetValue();
  189. if( m_regEx.Matches( value ) )
  190. {
  191. size_t start, len;
  192. m_regEx.GetMatch( &start, &len );
  193. if( start != 0 || len != value.Length() ) // whole string must match
  194. valid = false;
  195. }
  196. else // no match at all
  197. {
  198. valid = false;
  199. }
  200. if( !valid )
  201. {
  202. m_validatorWindow->SetFocus();
  203. DisplayError( aParent, wxString::Format( _( "Incorrect value: %s" ), value ) );
  204. return false;
  205. }
  206. return true;
  207. }
  208. void REGEX_VALIDATOR::compileRegEx( const wxString& aRegEx, int aFlags )
  209. {
  210. if( !m_regEx.Compile( aRegEx, aFlags ) )
  211. {
  212. throw std::runtime_error( "REGEX_VALIDATOR: Invalid regular expression: "
  213. + aRegEx.ToStdString() );
  214. }
  215. m_regExString = aRegEx;
  216. m_regExFlags = aFlags;
  217. }
  218. bool LIB_ID_VALIDATOR::Validate( wxWindow *aParent )
  219. {
  220. LIB_ID dummy;
  221. // If window is disabled, simply return
  222. if( !m_validatorWindow->IsEnabled() )
  223. return true;
  224. wxTextEntry* const text = GetTextEntry();
  225. if( !text )
  226. return false;
  227. wxString msg;
  228. wxString val( text->GetValue() );
  229. wxString tmp = val.Clone(); // For trailing and leading white space tests.
  230. // Allow empty string if empty filter not set to allow clearing the LIB_ID.
  231. if( !(GetStyle() & wxFILTER_EMPTY) && val.IsEmpty() )
  232. return true;
  233. if( tmp.Trim() != val ) // Trailing white space.
  234. {
  235. msg = _( "Entry contains trailing white space." );
  236. }
  237. else if( tmp.Trim( false ) != val ) // Leading white space.
  238. {
  239. msg = _( "Entry contains leading white space." );
  240. }
  241. else if( dummy.Parse( val ) != -1 || !dummy.IsValid() ) // Is valid LIB_ID.
  242. {
  243. msg.Printf( _( "'%s' is not a valid library identifier format." ), val );
  244. }
  245. if( !msg.empty() )
  246. {
  247. m_validatorWindow->SetFocus();
  248. wxMessageBox( msg, _( "Library Identifier Validation Error" ),
  249. wxOK | wxICON_EXCLAMATION, aParent );
  250. return false;
  251. }
  252. return true;
  253. }
  254. NETNAME_VALIDATOR::NETNAME_VALIDATOR( wxString *aVal ) :
  255. wxTextValidator(),
  256. m_allowSpaces( false )
  257. {
  258. }
  259. NETNAME_VALIDATOR::NETNAME_VALIDATOR( const NETNAME_VALIDATOR& aValidator ) :
  260. wxTextValidator( aValidator ),
  261. m_allowSpaces( aValidator.m_allowSpaces )
  262. {
  263. }
  264. NETNAME_VALIDATOR::NETNAME_VALIDATOR( bool aAllowSpaces ) :
  265. wxTextValidator(),
  266. m_allowSpaces( aAllowSpaces )
  267. {
  268. }
  269. bool NETNAME_VALIDATOR::Validate( wxWindow *aParent )
  270. {
  271. // If window is disabled, simply return
  272. if ( !m_validatorWindow->IsEnabled() )
  273. return true;
  274. wxTextEntry * const text = GetTextEntry();
  275. if ( !text )
  276. return false;
  277. const wxString& errormsg = IsValid( text->GetValue() );
  278. if( !errormsg.empty() )
  279. {
  280. m_validatorWindow->SetFocus();
  281. wxMessageBox( errormsg, _( "Invalid signal name" ), wxOK | wxICON_EXCLAMATION, aParent );
  282. return false;
  283. }
  284. return true;
  285. }
  286. wxString NETNAME_VALIDATOR::IsValid( const wxString& str ) const
  287. {
  288. if( str.Contains( '\r' ) || str.Contains( '\n' ) )
  289. return _( "Signal names cannot contain CR or LF characters" );
  290. if( !m_allowSpaces && ( str.Contains( ' ' ) || str.Contains( '\t' ) ) )
  291. return _( "Signal names cannot contain spaces" );
  292. return wxString();
  293. }
  294. void KIUI::ValidatorTransferToWindowWithoutEvents( wxValidator& aValidator )
  295. {
  296. wxWindow* ctrl = aValidator.GetWindow();
  297. wxCHECK_RET( ctrl != nullptr, "Transferring validator data without a control" );
  298. wxEventBlocker orient_update_blocker( ctrl, wxEVT_ANY );
  299. aValidator.TransferToWindow();
  300. }