KiCad PCB EDA Suite
dialog_env_var_config.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) 2015 Wayne Stambaugh <stambaughw@gmail.com>
5  * Copyright (C) 2015 KiCad Developers, see AUTHORS.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 
29 #include <dialog_env_var_config.h>
30 
31 #include <validators.h>
32 #include <html_messagebox.h>
33 
34 #include <wx/regex.h>
35 
36 
37 DIALOG_ENV_VAR_CONFIG::DIALOG_ENV_VAR_CONFIG( wxWindow* aParent, const ENV_VAR_MAP& aEnvVarMap ) :
39 {
40  m_extDefsChanged = false;
41  m_envVarMap = aEnvVarMap;
42 
43  m_grid->AppendRows( (int) m_envVarMap.size() );
44 
45  for( size_t row = 0; row < m_envVarMap.size(); row++ )
46  {
47  wxGridCellTextEditor* editor = new wxGridCellTextEditor;
49  editor->SetValidator( envVarValidator );
50  m_grid->SetCellEditor( (int) row, 0, editor );
51 
52  editor = new wxGridCellTextEditor;
54  editor->SetValidator( pathValidator );
55  m_grid->SetCellEditor( (int) row, 1, editor );
56  }
57 }
58 
59 
61 {
62  wxLogDebug( wxT( "In DIALOG_ENV_VAR_CONFIG::TransferDataToWindow()." ) );
63 
64  if( !wxDialog::TransferDataToWindow() )
65  return false;
66 
67  long row = 0L;
68 
69  for( ENV_VAR_MAP_ITER it = m_envVarMap.begin(); it != m_envVarMap.end(); ++it )
70  {
71  m_grid->SetCellValue( row, 0, it->first );
72  m_grid->SetCellValue( row, 1, it->second.GetValue() );
73 
74  // Highlight environment variables that are externally defined.
75  if( it->second.GetDefinedExternally() )
76  {
77  wxGridCellAttr* attr = m_grid->GetOrCreateCellAttr( row, 0 );
78  attr->SetBackgroundColour( *wxLIGHT_GREY );
79  m_grid->SetRowAttr( row, attr );
80  }
81 
82  row++;
83  }
84 
85  m_grid->AutoSizeColumns();
86  m_grid->AutoSizeRows();
87  GetSizer()->Layout();
88  GetSizer()->Fit( this );
89  GetSizer()->SetSizeHints( this );
90 
91  return true;
92 }
93 
94 
96 {
97  if( !wxDialog::TransferDataFromWindow() )
98  return false;
99 
100  int row;
101  wxArrayString envVarNames;
102 
103  for( row = 0; row < m_grid->GetNumberRows(); row++ )
104  {
105  wxString caption = _( "Invalid Input" );
106  wxString name = m_grid->GetCellValue( row, 0 );
107  wxString value = m_grid->GetCellValue( row, 1 );
108 
109  // Ignore completely empty rows.
110  if( name.IsEmpty() && value.IsEmpty() )
111  continue;
112 
113  wxLogDebug( wxT( "Row %d, name: %s, value %s." ), row,
114  GetChars( name ), GetChars( value ) );
115 
116  // Name cannot be empty.
117  if( name.IsEmpty() )
118  {
119  wxMessageBox( _( "Environment variable name cannot be empty." ),
120  caption, wxOK | wxICON_ERROR, this );
121  m_grid->GoToCell( row, 0 );
122  m_grid->SetGridCursor( row, 0 );
123  return false;
124  }
125 
126  // Value cannot be empty.
127  if( value.IsEmpty() )
128  {
129  wxMessageBox( _( "Environment variable value cannot be empty." ), caption,
130  wxOK | wxICON_ERROR, this );
131  m_grid->GoToCell( row, 1 );
132  m_grid->SetGridCursor( row, 1 );
133  m_grid->SetFocus();
134  return false;
135  }
136 
137  // First character of the environment variable name cannot be a digit (0-9).
138  if( name.Left( 1 ).IsNumber() )
139  {
140  wxMessageBox( _( "The first character of an environment variable name cannot be "
141  "a digit (0-9)." ), caption, wxOK | wxICON_ERROR, this );
142  m_grid->GoToCell( row, 0 );
143  m_grid->SetGridCursor( row, 0 );
144  m_grid->SelectBlock( row, 0, row, 0 );
145  m_grid->SetFocus();
146  return false;
147  }
148 
149  // Check for duplicate environment variable names.
150  if( envVarNames.Index( name ) != wxNOT_FOUND )
151  {
152  wxMessageBox( _( "Cannot have duplicate environment variable names." ), caption,
153  wxOK | wxICON_ERROR, this );
154  m_grid->GoToCell( row, 0 );
155  m_grid->SetGridCursor( row, 0 );
156  m_grid->SelectRow( row );
157  m_grid->SetFocus();
158  return false;
159  }
160 
161  envVarNames.Add( name );
162  }
163 
164  // Add new entries and update any modified entries.
165  for( row = 0; row < m_grid->GetNumberRows(); row++ )
166  {
167  wxString name = m_grid->GetCellValue( row, 0 );
168  wxString value = m_grid->GetCellValue( row, 1 );
169  ENV_VAR_MAP_ITER it = m_envVarMap.find( name );
170 
171  if( it == m_envVarMap.end() )
172  {
173  ENV_VAR_ITEM item( value, wxGetEnv( name, NULL ) );
174 
175  // Add new environment variable.
176  m_envVarMap[ name ] = item;
177  }
178  else if( it->second.GetValue() != value )
179  {
180  // Environment variable already defined but it's value changed.
181  it->second.SetValue( value );
182 
183  // Externally defined variable has been changed.
184  if( it->second.GetDefinedExternally() )
185  m_extDefsChanged = true;
186  }
187  }
188 
189  std::vector< wxString > removeFromMap;
190 
191  // Remove deleted entries from the map.
192  for( ENV_VAR_MAP_ITER it = m_envVarMap.begin(); it != m_envVarMap.end(); ++it )
193  {
194  bool found = false;
195 
196  for( row = 0; row < m_grid->GetNumberRows(); row++ )
197  {
198  if( m_grid->GetCellValue( row, 0 ) == it->first )
199  {
200  found = true;
201  break;
202  }
203  }
204 
205  if( !found )
206  removeFromMap.push_back( it->first );
207  }
208 
209  for( size_t i = 0; i < removeFromMap.size(); i++ )
210  m_envVarMap.erase( removeFromMap[i] );
211 
212  return true;
213 }
214 
215 
216 void DIALOG_ENV_VAR_CONFIG::OnAddRow( wxCommandEvent& aEvent )
217 {
218  m_grid->AppendRows();
219 
220  int row = m_grid->GetNumberRows() - 1;
221  wxGridCellTextEditor* editor = new wxGridCellTextEditor;
222  ENVIRONMENT_VARIABLE_CHAR_VALIDATOR envVarNameValidator;
223  editor->SetValidator( envVarNameValidator );
224  m_grid->SetCellEditor( row, 0, editor );
225 
226  editor = new wxGridCellTextEditor;
228  editor->SetValidator( pathValidator );
229  m_grid->SetCellEditor( row, 1, editor );
230  m_grid->GoToCell( row, 0 );
231  m_grid->SetGridCursor( row, 0 );
232  m_grid->SetFocus();
233 }
234 
235 
236 void DIALOG_ENV_VAR_CONFIG::OnDeleteSelectedRows( wxCommandEvent& aEvent )
237 {
238  if( !m_grid->IsSelection() )
239  return;
240 
241  wxGridUpdateLocker locker( m_grid );
242 
243  for( int n = 0; n < m_grid->GetNumberRows(); )
244  {
245  if( m_grid->IsInSelection( n , 0 ) )
246  m_grid->DeleteRows( n, 1 );
247  else
248  n++;
249  }
250 }
251 
252 
253 void DIALOG_ENV_VAR_CONFIG::OnHelpRequest( wxCommandEvent& aEvent )
254 {
255  wxString msg = _( "Enter the name and path for each environment variable. Grey entries "
256  "are names that have been defined externally at the system or user "
257  "level. Environment variables defined at the system or user level "
258  "take precedence over the ones defined in this table. This means the "
259  "values in this table are ignored." );
260  msg << wxT( "<br><br><b>" );
261  msg << _( "To ensure environment variable names are valid on all platforms, the name field "
262  "will only accept upper case letters, digits, and the underscore characters." );
263  msg << wxT( "</b><br><br>" );
264  msg << _( "<b>KIGITHUB</b> is used by KiCad to define the URL of the repository "
265  "of the official KiCad libraries." );
266  msg << wxT( "<br><br>" );
267  msg << _( "<b>KISYS3DMOD</b> is the base path of system footprint 3D "
268  "shapes (.3Dshapes folders)." );
269  msg << wxT( "<br><br>" );
270  msg << _( "<b>KISYSMOD</b> is the base path of locally installed system "
271  "footprint libraries (.pretty folders)." );
272  msg << wxT( "<br><br>" );
273  msg << _( "<b>KIPRJMOD</b> is internally defined by KiCad (cannot be edited) and is set "
274  "to the absolute path of the currently loaded project file. This environment "
275  "variable can be used to define files and paths relative to the currently loaded "
276  "project. For instance, ${KIPRJMOD}/libs/footprints.pretty can be defined as a "
277  "folder containing a project specific footprint library named footprints.pretty." );
278  msg << wxT( "<br><br>" );
279  msg << _( "<b>KICAD_PTEMPLATES</b> is optional and can be defined if you want to "
280  "create your own project templates folder." );
281 
282  HTML_MESSAGE_BOX dlg( GetParent(), _( "Environment Variable Help" ) );
283  dlg.AddHTML_Text( msg );
284  dlg.ShowModal();
285 }
bool TransferDataFromWindow() override
Class ENV_VAR_ITEM.
Definition: pgm_base.h:58
Class DIALOG_ENV_VAR_CONFIG_BASE.
Class ENVIRONMENT_VARIABLE_CHAR_VALIDATOR.
Definition: validators.h:75
Class FILE_NAME_WITH_PATH_CHAR_VALIDATOR.
Definition: validators.h:58
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Definition: pgm_base.h:87
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
virtual void OnAddRow(wxCommandEvent &aEvent) override
virtual void OnHelpRequest(wxCommandEvent &aEvent) override
Class HTML_MESSAGE_BOX.
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void SetValue(const wxString &aValue)
Definition: pgm_base.h:76
virtual void OnDeleteSelectedRows(wxCommandEvent &aEvent) override
void AddHTML_Text(const wxString &message)
Function AddHTML_Text adds html text (without any change) to message list.
DIALOG_ENV_VAR_CONFIG(wxWindow *parent, const ENV_VAR_MAP &aEnvVarMap)
std::map< wxString, ENV_VAR_ITEM >::iterator ENV_VAR_MAP_ITER
Definition: pgm_base.h:88
bool TransferDataToWindow() override
Custom text control validator definitions.