KiCad PCB EDA Suite
pgm_base.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) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
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 
33 #include <fctsys.h>
34 #include <wx/html/htmlwin.h>
35 #include <wx/fs_zip.h>
36 #include <wx/dir.h>
37 #include <wx/filename.h>
38 #include <wx/snglinst.h>
39 #include <wx/stdpaths.h>
40 #include <wx/sysopt.h>
41 #include <wx/richmsgdlg.h>
42 
43 #include <pgm_base.h>
44 #include <draw_frame.h>
45 #include <eda_base_frame.h>
46 #include <macros.h>
47 #include <config_params.h>
48 #include <id.h>
49 #include <build_version.h>
50 #include <hotkeys_basic.h>
51 #include <gestfich.h>
52 #include <menus_helpers.h>
53 #include <confirm.h>
54 #include <dialog_configure_paths.h>
55 #include <lockfile.h>
56 #include <systemdirsappend.h>
57 #include <trace_helpers.h>
59 
60 #define KICAD_COMMON wxT( "kicad_common" )
61 
62 // some key strings used to store parameters in KICAD_COMMON
63 
64 const wxChar PGM_BASE::workingDirKey[] = wxT( "WorkingDir" ); // public
65 
66 static const wxChar languageCfgKey[] = wxT( "LanguageID" );
67 static const wxChar pathEnvVariables[] = wxT( "EnvironmentVariables" );
68 static const wxChar showEnvVarWarningDialog[] = wxT( "ShowEnvVarWarningDialog" );
69 static const wxChar traceEnvVars[] = wxT( "KIENVVARS" );
70 
71 
72 FILE_HISTORY::FILE_HISTORY( size_t aMaxFiles, int aBaseFileId ) :
73  wxFileHistory( std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE ) )
74 {
75  SetBaseId( aBaseFileId );
76 }
77 
78 
79 void FILE_HISTORY::SetMaxFiles( size_t aMaxFiles )
80 {
81  m_fileMaxFiles = std::min( aMaxFiles, (size_t) MAX_FILE_HISTORY_SIZE );
82 
83  size_t numFiles = m_fileHistory.size();
84 
85  while( numFiles > m_fileMaxFiles )
86  RemoveFileFromHistory( --numFiles );
87 }
88 
89 
99 {
102 
105 
108 
110  wxString m_Lang_Label;
111 
114 };
115 
116 
124 {
125  { wxLANGUAGE_DEFAULT, ID_LANGUAGE_DEFAULT, lang_def_xpm, _( "Default" ) },
126  { wxLANGUAGE_ENGLISH, ID_LANGUAGE_ENGLISH, lang_en_xpm, wxT( "English" ), true },
127  { wxLANGUAGE_FRENCH, ID_LANGUAGE_FRENCH, lang_fr_xpm, _( "French" ) },
128  { wxLANGUAGE_FINNISH, ID_LANGUAGE_FINNISH, lang_fi_xpm, _( "Finnish" ) },
129  { wxLANGUAGE_SPANISH, ID_LANGUAGE_SPANISH, lang_es_xpm, _( "Spanish" ) },
130  { wxLANGUAGE_PORTUGUESE, ID_LANGUAGE_PORTUGUESE, lang_pt_xpm, _( "Portuguese" ) },
131  { wxLANGUAGE_ITALIAN, ID_LANGUAGE_ITALIAN, lang_it_xpm, _( "Italian" ) },
132  { wxLANGUAGE_GERMAN, ID_LANGUAGE_GERMAN, lang_de_xpm, _( "German" ) },
133  { wxLANGUAGE_GREEK, ID_LANGUAGE_GREEK, lang_gr_xpm, _( "Greek" ) },
134  { wxLANGUAGE_SLOVENIAN, ID_LANGUAGE_SLOVENIAN, lang_sl_xpm, _( "Slovenian" ) },
135  { wxLANGUAGE_SLOVAK, ID_LANGUAGE_SLOVAK, lang_sk_xpm, _( "Slovak" ) },
136  { wxLANGUAGE_HUNGARIAN, ID_LANGUAGE_HUNGARIAN, lang_hu_xpm, _( "Hungarian" ) },
137  { wxLANGUAGE_POLISH, ID_LANGUAGE_POLISH, lang_pl_xpm, _( "Polish" ) },
138  { wxLANGUAGE_CZECH, ID_LANGUAGE_CZECH, lang_cs_xpm, _( "Czech" ) },
139  { wxLANGUAGE_RUSSIAN, ID_LANGUAGE_RUSSIAN, lang_ru_xpm, _( "Russian" ) },
140  { wxLANGUAGE_KOREAN, ID_LANGUAGE_KOREAN, lang_ko_xpm, _( "Korean" ) },
141  { wxLANGUAGE_CHINESE_SIMPLIFIED, ID_LANGUAGE_CHINESE_SIMPLIFIED, lang_zh_xpm,
142  _( "Chinese simplified" ) },
143  { wxLANGUAGE_CHINESE_TRADITIONAL, ID_LANGUAGE_CHINESE_TRADITIONAL, lang_zh_xpm,
144  _( "Chinese traditional" ) },
145  { wxLANGUAGE_CATALAN, ID_LANGUAGE_CATALAN, lang_ca_xpm, _( "Catalan" ) },
146  { wxLANGUAGE_DUTCH, ID_LANGUAGE_DUTCH, lang_nl_xpm, _( "Dutch" ) },
147  { wxLANGUAGE_JAPANESE, ID_LANGUAGE_JAPANESE, lang_jp_xpm, _( "Japanese" ) },
148  { wxLANGUAGE_BULGARIAN, ID_LANGUAGE_BULGARIAN, lang_bg_xpm, _( "Bulgarian" ) },
149  { wxLANGUAGE_LITHUANIAN, ID_LANGUAGE_LITHUANIAN, lang_lt_xpm, _( "Lithuanian" ) }
150 };
151 
152 
154 {
155  m_pgm_checker = NULL;
156  m_locale = NULL;
157  m_common_settings = NULL;
158 
159  m_show_env_var_dialog = true;
160 
161  setLanguageId( wxLANGUAGE_DEFAULT );
162 
163  ForceSystemPdfBrowser( false );
164 }
165 
166 
168 {
169  Destroy();
170 }
171 
172 
174 {
175  // unlike a normal destructor, this is designed to be called more than once safely:
176 
177  delete m_common_settings;
178  m_common_settings = 0;
179 
180  delete m_pgm_checker;
181  m_pgm_checker = 0;
182 
183  delete m_locale;
184  m_locale = 0;
185 }
186 
187 
189 {
190  wxASSERT( wxTheApp );
191  return *wxTheApp;
192 }
193 
194 
195 void PGM_BASE::SetEditorName( const wxString& aFileName )
196 {
197  m_editor_name = aFileName;
198  wxASSERT( m_common_settings );
199  m_common_settings->Write( "Editor", aFileName );
200 }
201 
202 
203 const wxString& PGM_BASE::GetEditorName( bool aCanShowFileChooser )
204 {
205  wxString editorname = m_editor_name;
206 
207  if( !editorname )
208  {
209  if( !wxGetEnv( "EDITOR", &editorname ) )
210  {
211  // If there is no EDITOR variable set, try the desktop default
212 #ifdef __WXMAC__
213  editorname = "/usr/bin/open";
214 #elif __WXX11__
215  editorname = "/usr/bin/xdg-open";
216 #endif
217  }
218  }
219 
220  // If we still don't have an editor name show a dialog asking the user to select one
221  if( !editorname && aCanShowFileChooser )
222  {
223  DisplayInfoMessage( NULL,
224  _( "No default editor found, you must choose it" ) );
225 
226  editorname = AskUserForPreferredEditor();
227  }
228 
229  // If we finally have a new editor name request it to be copied to m_editor_name and
230  // saved to the preferences file.
231  if( !editorname.IsEmpty() )
232  SetEditorName( editorname );
233 
234  // m_editor_name already has the same value that editorname, or empty if no editor was
235  // found/chosen.
236  return m_editor_name;
237 }
238 
239 
240 const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEditor )
241 {
242  // Create a mask representing the executable files in the current platform
243 #ifdef __WINDOWS__
244  wxString mask( _( "Executable file (*.exe)|*.exe" ) );
245 #else
246  wxString mask( _( "Executable file (*)|*" ) );
247 #endif
248 
249  // Extract the path, name and extension from the default editor (even if the editor's
250  // name was empty, this method will succeed and return empty strings).
251  wxString path, name, ext;
252  wxFileName::SplitPath( aDefaultEditor, &path, &name, &ext );
253 
254  // Show the modal editor and return the file chosen (may be empty if the user cancels
255  // the dialog).
256  return EDA_FILE_SELECTOR( _( "Select Preferred Editor" ), path,
257  name, ext, mask,
258  NULL, wxFD_OPEN | wxFD_FILE_MUST_EXIST,
259  true );
260 }
261 
262 
264 {
265  wxFileName pgm_name( App().argv[0] );
266 
267  wxConfigBase::DontCreateOnDemand();
268 
269  wxInitAllImageHandlers();
270 
271  m_pgm_checker = new wxSingleInstanceChecker( pgm_name.GetName().Lower() + wxT( "-" ) +
272  wxGetUserId(), GetKicadLockFilePath() );
273 
274  if( m_pgm_checker->IsAnotherRunning() )
275  {
276  wxString quiz = wxString::Format(
277  _( "%s is already running. Continue?" ),
278  GetChars( pgm_name.GetName() )
279  );
280 
281  if( !IsOK( NULL, quiz ) )
282  return false;
283  }
284 
285  // Init KiCad environment
286  // the environment variable KICAD (if exists) gives the kicad path:
287  // something like set KICAD=d:\kicad
288  bool isDefined = wxGetEnv( "KICAD", &m_kicad_env );
289 
290  if( isDefined ) // ensure m_kicad_env ends by "/"
291  {
292  m_kicad_env.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
293 
294  if( !m_kicad_env.IsEmpty() && m_kicad_env.Last() != '/' )
295  m_kicad_env += UNIX_STRING_DIR_SEP;
296  }
297 
298  // Init parameters for configuration
299  App().SetVendorName( "KiCad" );
300  App().SetAppName( pgm_name.GetName().Lower() );
301 
302  // Install some image handlers, mainly for help
303  if( wxImage::FindHandler( wxBITMAP_TYPE_PNG ) == NULL )
304  wxImage::AddHandler( new wxPNGHandler );
305 
306  if( wxImage::FindHandler( wxBITMAP_TYPE_GIF ) == NULL )
307  wxImage::AddHandler( new wxGIFHandler );
308 
309  if( wxImage::FindHandler( wxBITMAP_TYPE_JPEG ) == NULL )
310  wxImage::AddHandler( new wxJPEGHandler );
311 
312  wxFileSystem::AddHandler( new wxZipFSHandler );
313 
314  // Analyze the command line & initialize the binary path
315  setExecutablePath();
316 
317  SetLanguagePath();
318 
319  // OS specific instantiation of wxConfigBase derivative:
320  m_common_settings = GetNewConfig( KICAD_COMMON );
321 
322  wxString envVarName = wxT( "KIGITHUB" );
323  ENV_VAR_ITEM envVarItem;
324  wxString envValue;
325  wxFileName tmpFileName;
326 
327  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
328  {
329  tmpFileName.AssignDir( envValue );
330  envVarItem.SetDefinedExternally( true );
331  }
332  else
333  {
334  envVarItem.SetValue( wxString( wxT( "https://github.com/KiCad" ) ) );
335  envVarItem.SetDefinedExternally( false );
336  }
337 
338  m_local_env_vars[ envVarName ] = envVarItem;
339 
340  wxFileName baseSharePath;
341 #if defined( __WXMSW__ )
342  // Make the paths relative to the executable dir as KiCad might be installed anywhere
343  // It follows the Windows installer paths scheme, where binaries are installed in
344  // PATH/bin and extra files in PATH/share/kicad
345  baseSharePath.AssignDir( m_bin_dir + "\\.." );
346  baseSharePath.Normalize();
347 #else
348  baseSharePath.AssignDir( wxString( wxT( DEFAULT_INSTALL_PATH ) ) );
349 #endif
350 
351 #if !defined( __WXMAC__ )
352  baseSharePath.AppendDir( "share" );
353  baseSharePath.AppendDir( "kicad" );
354 #endif
355 
356  // KISYSMOD
357  envVarName = wxT( "KISYSMOD" );
358 
359  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
360  {
361  tmpFileName.AssignDir( envValue );
362  envVarItem.SetDefinedExternally( true );
363  }
364  else
365  {
366  tmpFileName = baseSharePath;
367  tmpFileName.AppendDir( "modules" );
368  envVarItem.SetDefinedExternally( false );
369  }
370 
371  envVarItem.SetValue( tmpFileName.GetPath() );
372  m_local_env_vars[ envVarName ] = envVarItem;
373 
374  // KISYS3DMOD
375  envVarName = wxT( "KISYS3DMOD" );
376 
377  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
378  {
379  tmpFileName.AssignDir( envValue );
380  envVarItem.SetDefinedExternally( true );
381  }
382  else
383  {
384  tmpFileName.AppendDir( "packages3d" );
385  envVarItem.SetDefinedExternally( false );
386  }
387 
388  envVarItem.SetValue( tmpFileName.GetFullPath() );
389  m_local_env_vars[ envVarName ] = envVarItem;
390 
391  // KICAD_TEMPLATE_DIR
392  envVarName = "KICAD_TEMPLATE_DIR";
393 
394  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
395  {
396  tmpFileName.AssignDir( envValue );
397  envVarItem.SetDefinedExternally( true );
398  }
399  else
400  {
401  // Attempt to find the best default template path.
402  SEARCH_STACK bases;
403  SEARCH_STACK templatePaths;
404 
405  SystemDirsAppend( &bases );
406 
407  for( unsigned i = 0; i < bases.GetCount(); ++i )
408  {
409  wxFileName fn( bases[i], wxEmptyString );
410 
411  // Add KiCad template file path to search path list.
412  fn.AppendDir( "template" );
413 
414  // Only add path if exists and can be read by the user.
415  if( fn.DirExists() && fn.IsDirReadable() )
416  {
417  wxLogTrace( tracePathsAndFiles, "Checking template path '%s' exists",
418  fn.GetPath() );
419  templatePaths.AddPaths( fn.GetPath() );
420  }
421  }
422 
423  if( templatePaths.IsEmpty() )
424  {
425  tmpFileName = baseSharePath;
426  tmpFileName.AppendDir( "template" );
427  }
428  else
429  {
430  // Take the first one. There may be more but this will likely be the best option.
431  tmpFileName.AssignDir( templatePaths[0] );
432  }
433 
434  envVarItem.SetDefinedExternally( false );
435  }
436 
437  envVarItem.SetValue( tmpFileName.GetPath() );
438  m_local_env_vars[ envVarName ] = envVarItem;
439 
440  // KICAD_USER_TEMPLATE_DIR
441  envVarName = "KICAD_USER_TEMPLATE_DIR";
442 
443  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
444  {
445  tmpFileName.AssignDir( envValue );
446  envVarItem.SetDefinedExternally( true );
447  }
448  else
449  {
450  // Default user template path.
451  tmpFileName = wxStandardPaths::Get().GetDocumentsDir();
452  tmpFileName.AppendDir( "kicad" );
453  tmpFileName.AppendDir( "template" );
454  envVarItem.SetDefinedExternally( false );
455  }
456 
457  envVarItem.SetValue( tmpFileName.GetPath() );
458  m_local_env_vars[ envVarName ] = envVarItem;
459 
460  // KICAD_SYMBOLS
461  envVarName = wxT( "KICAD_SYMBOL_DIR" );
462 
463  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
464  {
465  tmpFileName.AssignDir( envValue );
466  envVarItem.SetDefinedExternally( true );
467  }
468  else
469  {
470  tmpFileName = baseSharePath;
471  tmpFileName.AppendDir( "library" );
472  envVarItem.SetDefinedExternally( false );
473  }
474 
475  envVarItem.SetValue( tmpFileName.GetPath() );
476  m_local_env_vars[ envVarName ] = envVarItem;
477 
478  ReadPdfBrowserInfos(); // needs m_common_settings
479 
480  // Init user language *before* calling loadCommonSettings, because
481  // env vars could be incorrectly initialized on Linux
482  // (if the value contains some non ASCII7 chars, the env var is not initialized)
483  SetLanguage( true );
484 
485  loadCommonSettings();
486 
487 #ifdef __WXMAC__
488  // Always show filters on Open dialog to be able to choose plugin
489  wxSystemOptions::SetOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES, 1 );
490 #endif
491 
492  return true;
493 }
494 
495 
497 {
498  m_bin_dir = wxStandardPaths::Get().GetExecutablePath();
499 
500 #ifdef __WXMAC__
501  // On OSX Pgm().GetExecutablePath() will always point to main
502  // bundle directory, e.g., /Applications/kicad.app/
503 
504  wxFileName fn( m_bin_dir );
505 
506  if( fn.GetName() == wxT( "kicad" ) )
507  {
508  // kicad launcher, so just remove the Contents/MacOS part
509  fn.RemoveLastDir();
510  fn.RemoveLastDir();
511  }
512  else
513  {
514  // standalone binaries live in Contents/Applications/<standalone>.app/Contents/MacOS
515  fn.RemoveLastDir();
516  fn.RemoveLastDir();
517  fn.RemoveLastDir();
518  fn.RemoveLastDir();
519  fn.RemoveLastDir();
520  }
521 
522  m_bin_dir = fn.GetPath() + wxT( "/" );
523 #else
524  // Use unix notation for paths. I am not sure this is a good idea,
525  // but it simplifies compatibility between Windows and Unices.
526  // However it is a potential problem in path handling under Windows.
527  m_bin_dir.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
528 
529  // Remove file name form command line:
530  while( m_bin_dir.Last() != '/' && !m_bin_dir.IsEmpty() )
531  m_bin_dir.RemoveLast();
532 #endif
533 
534  return true;
535 }
536 
537 
539 {
540  wxASSERT( m_common_settings );
541 
542  m_help_size.x = 500;
543  m_help_size.y = 400;
544 
545  // This only effect the first time KiCad is run. The user's setting will be used for all
546  // subsequent runs. Menu icons are off by default on OSX and on for all other platforms.
547 #if defined( __WXMAC__ )
548  bool defaultUseIconsInMenus = false;
549 #else
550  bool defaultUseIconsInMenus = true;
551 #endif
552 
553  m_common_settings->Read( showEnvVarWarningDialog, &m_show_env_var_dialog );
554 
555  if( !m_common_settings->HasEntry( USE_ICONS_IN_MENUS_KEY ) )
556  m_common_settings->Write( USE_ICONS_IN_MENUS_KEY, defaultUseIconsInMenus );
557 
558  if( !m_common_settings->HasEntry( ICON_SCALE_KEY )
559  || !m_common_settings->HasEntry( GAL_ANTIALIASING_MODE_KEY ) )
560  {
561  // 5.0 and earlier saved common settings in each app, and saved hardware antialiasing
562  // options only in pcbnew (which was the only canvas to support them). Since there's
563  // no single right answer to where to pull the common settings from, we might as well
564  // get them along with the hardware antialiasing option from pcbnew.
565  wxConfigBase* pcbnewConfig = GetNewConfig( wxString::FromUTF8( "pcbnew" ) );
566  wxString pcbFrameKey( PCB_EDIT_FRAME_NAME );
567 
568  if( !m_common_settings->HasEntry( ICON_SCALE_KEY ) )
569  {
570  wxString value;
571  bool option;
572 
573  pcbnewConfig->Read( "PcbIconScale", &value );
574  m_common_settings->Write( ICON_SCALE_KEY, value );
575 
576  pcbnewConfig->Read( ENBL_MOUSEWHEEL_PAN_KEY, &option, false );
577  m_common_settings->Write( ENBL_MOUSEWHEEL_PAN_KEY, option );
578 
579  pcbnewConfig->Read( ENBL_ZOOM_NO_CENTER_KEY, &option, false );
580  m_common_settings->Write( ENBL_ZOOM_NO_CENTER_KEY, option );
581 
582  pcbnewConfig->Read( ENBL_AUTO_PAN_KEY, &option, true );
583  m_common_settings->Write( ENBL_AUTO_PAN_KEY, option );
584  }
585 
586  if( !m_common_settings->HasEntry( GAL_ANTIALIASING_MODE_KEY ) )
587  {
588  int temp;
589  pcbnewConfig->Read( pcbFrameKey + GAL_DISPLAY_OPTIONS_KEY + GAL_ANTIALIASING_MODE_KEY,
591  m_common_settings->Write( GAL_ANTIALIASING_MODE_KEY, temp );
592  }
593  }
594 
595  m_editor_name = m_common_settings->Read( "Editor" );
596 
597  wxString entry, oldPath;
598  wxArrayString entries;
599  long index = 0L;
600 
601  oldPath = m_common_settings->GetPath();
602  m_common_settings->SetPath( pathEnvVariables );
603 
604  while( m_common_settings->GetNextEntry( entry, index ) )
605  {
606  wxLogTrace( traceEnvVars,
607  "Enumerating over entry %s, %ld.", GetChars( entry ), index );
608  entries.Add( entry );
609  }
610 
611  for( unsigned i = 0; i < entries.GetCount(); i++ )
612  {
613  wxString val = m_common_settings->Read( entries[i], wxEmptyString );
614 
615  if( m_local_env_vars[ entries[i] ].GetDefinedExternally() )
616  continue;
617 
618  m_local_env_vars[ entries[i] ] = ENV_VAR_ITEM( val, wxGetEnv( entries[i], NULL ) );
619  }
620 
621  for( ENV_VAR_MAP_ITER it = m_local_env_vars.begin(); it != m_local_env_vars.end(); ++it )
622  SetLocalEnvVariable( it->first, it->second.GetValue() );
623 
624  m_common_settings->SetPath( oldPath );
625 }
626 
627 
629 {
630  // m_common_settings is not initialized until fairly late in the
631  // process startup: InitPgm(), so test before using:
632  if( m_common_settings )
633  {
634  wxString cur_dir = wxGetCwd();
635 
636  m_common_settings->Write( workingDirKey, cur_dir );
637  m_common_settings->Write( showEnvVarWarningDialog, m_show_env_var_dialog );
638 
639  // Save the local environment variables.
640  m_common_settings->SetPath( pathEnvVariables );
641 
642  for( ENV_VAR_MAP_ITER it = m_local_env_vars.begin(); it != m_local_env_vars.end(); ++it )
643  {
644  if( it->second.GetDefinedExternally() )
645  continue;
646 
647  wxLogTrace( traceEnvVars, "Saving environment variable config entry %s as %s",
648  GetChars( it->first ), GetChars( it->second.GetValue() ) );
649  m_common_settings->Write( it->first, it->second.GetValue() );
650  }
651 
652  m_common_settings->SetPath( ".." );
653  }
654 }
655 
656 
657 bool PGM_BASE::SetLanguage( bool first_time )
658 {
659  bool retv = true;
660 
661  if( first_time )
662  {
663  setLanguageId( wxLANGUAGE_DEFAULT );
664  // First time SetLanguage is called, the user selected language id is set
665  // from commun user config settings
666  wxString languageSel;
667 
668  m_common_settings->Read( languageCfgKey, &languageSel );
669 
670  // Search for the current selection
671  for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
672  {
673  if( s_Languages[ii].m_Lang_Label == languageSel )
674  {
675  setLanguageId( s_Languages[ii].m_WX_Lang_Identifier );
676  break;
677  }
678  }
679  }
680 
681  // dictionary file name without extend (full name is kicad.mo)
682  wxString dictionaryName( "kicad" );
683 
684  delete m_locale;
685  m_locale = new wxLocale;
686 
687  if( !m_locale->Init( m_language_id ) )
688  {
689  wxLogTrace( traceLocale, "This language is not supported by the system." );
690 
691  setLanguageId( wxLANGUAGE_DEFAULT );
692  delete m_locale;
693 
694  m_locale = new wxLocale;
695  m_locale->Init();
696  retv = false;
697  }
698  else if( !first_time )
699  {
700  wxLogTrace( traceLocale, "Search for dictionary %s.mo in %s",
701  GetChars( dictionaryName ), GetChars( m_locale->GetName() ) );
702  }
703 
704  if( !first_time )
705  {
706  // If we are here, the user has selected another language.
707  // Therefore the new prefered language name is stored in common config.
708  // Do NOT store the wxWidgets language Id, it can change between wxWidgets
709  // versions, for a given language
710  wxString languageSel;
711 
712  // Search for the current selection language name
713  for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
714  {
715  if( s_Languages[ii].m_WX_Lang_Identifier == m_language_id )
716  {
717  languageSel = s_Languages[ii].m_Lang_Label;
718  break;
719  }
720  }
721 
722  m_common_settings->Write( languageCfgKey, languageSel );
723  }
724 
725  // Test if floating point notation is working (bug encountered in cross compilation)
726  // Make a conversion double <=> string
727  double dtst = 0.5;
728  wxString msg;
729 
730  msg << dtst;
731  double result;
732  msg.ToDouble( &result );
733 
734  if( result != dtst )
735  // string to double encode/decode does not work! Bug detected:
736  // Disable floating point localization:
737  setlocale( LC_NUMERIC, "C" );
738 
739  if( !m_locale->IsLoaded( dictionaryName ) )
740  m_locale->AddCatalog( dictionaryName );
741 
742  if( !retv )
743  return retv;
744 
745  return m_locale->IsOk();
746 }
747 
748 
750 {
751  wxLogTrace( traceLocale, "Select language ID %d from %d possible languages.",
752  menu_id, DIM( s_Languages ) );
753 
754  for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
755  {
756  if( menu_id == s_Languages[ii].m_KI_Lang_Identifier )
757  {
758  setLanguageId( s_Languages[ii].m_WX_Lang_Identifier );
759  break;
760  }
761  }
762 }
763 
764 
766 {
767  SEARCH_STACK guesses;
768 
769  SystemDirsAppend( &guesses );
770 
771  // Add our internat dir to the wxLocale catalog of paths
772  for( unsigned i = 0; i < guesses.GetCount(); i++ )
773  {
774  wxFileName fn( guesses[i], wxEmptyString );
775 
776  // Append path for Windows and unix KiCad package install
777  fn.AppendDir( "share" );
778  fn.AppendDir( "internat" );
779 
780  if( fn.IsDirReadable() )
781  {
782  wxLogTrace( traceLocale, "Adding locale lookup path: " + fn.GetPath() );
783  wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
784  }
785 
786  // Append path for unix standard install
787  fn.RemoveLastDir();
788  fn.AppendDir( "kicad" );
789  fn.AppendDir( "internat" );
790 
791  if( fn.IsDirReadable() )
792  {
793  wxLogTrace( traceLocale, "Adding locale lookup path: " + fn.GetPath() );
794  wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
795  }
796  }
797 }
798 
799 
800 void PGM_BASE::AddMenuLanguageList( wxMenu* MasterMenu )
801 {
802  wxMenu* menu = NULL;
803  wxMenuItem* item = MasterMenu->FindItem( ID_LANGUAGE_CHOICE );
804 
805  if( item ) // This menu exists, do nothing
806  return;
807 
808  menu = new wxMenu;
809 
810  for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
811  {
812  wxString label;
813 
814  if( s_Languages[ii].m_DoNotTranslate )
815  label = s_Languages[ii].m_Lang_Label;
816  else
817  label = wxGetTranslation( s_Languages[ii].m_Lang_Label );
818 
819  AddMenuItem( menu, s_Languages[ii].m_KI_Lang_Identifier,
820  label, KiBitmap(s_Languages[ii].m_Lang_Icon ),
821  wxITEM_CHECK );
822  }
823 
824  AddMenuItem( MasterMenu, menu,
826  _( "Set Language" ),
827  _( "Select application language (only for testing)" ),
828  KiBitmap( language_xpm ) );
829 
830  // Set Check mark on current selected language
831  for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
832  {
833  if( m_language_id == s_Languages[ii].m_WX_Lang_Identifier )
834  menu->Check( s_Languages[ii].m_KI_Lang_Identifier, true );
835  else
836  menu->Check( s_Languages[ii].m_KI_Lang_Identifier, false );
837  }
838 }
839 
840 
841 bool PGM_BASE::SetLocalEnvVariable( const wxString& aName, const wxString& aValue )
842 {
843  wxString env;
844 
845  // Check to see if the environment variable is already set.
846  if( wxGetEnv( aName, &env ) )
847  {
848  wxLogTrace( traceEnvVars, "Environment variable %s already set to %s.",
849  GetChars( aName ), GetChars( env ) );
850  return env == aValue;
851  }
852 
853  wxLogTrace( traceEnvVars, "Setting local environment variable %s to %s.",
854  GetChars( aName ), GetChars( aValue ) );
855 
856  return wxSetEnv( aName, aValue );
857 }
858 
859 
861 {
862  m_local_env_vars.clear();
863  m_local_env_vars = aEnvVarMap;
864 
865  if( m_common_settings )
866  m_common_settings->DeleteGroup( pathEnvVariables );
867 
868  SaveCommonSettings();
869 
870  // Overwrites externally defined environment variable until the next time the application
871  // is run.
872  for( ENV_VAR_MAP_ITER it = m_local_env_vars.begin(); it != m_local_env_vars.end(); ++it )
873  {
874  wxLogTrace( traceEnvVars, "Setting local environment variable %s to %s.",
875  GetChars( it->first ), GetChars( it->second.GetValue() ) );
876  wxSetEnv( it->first, it->second.GetValue() );
877  }
878 }
FILE_HISTORY(size_t aMaxFiles, int aBaseFileId)
Definition: pgm_base.cpp:72
static const wxChar pathEnvVariables[]
Definition: pgm_base.cpp:67
void loadCommonSettings()
Function loadCommonSettings loads the program (process) settings subset which are stored in ...
Definition: pgm_base.cpp:538
#define DIM(x)
of elements in an array
Definition: macros.h:98
wxString GetKicadLockFilePath()
Function GetKicadLockFilePath.
Definition: lockfile.cpp:59
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:44
VTBL_ENTRY void SetEditorName(const wxString &aFileName)
Definition: pgm_base.cpp:195
virtual ~PGM_BASE()
Definition: pgm_base.cpp:167
PNG memory record (file in memory).
Definition: bitmap_types.h:43
This file is part of the common library TODO brief description.
This file is part of the common library.
void SaveCommonSettings()
Function saveCommonSettings saves the program (process) settings subset which are stored ...
Definition: pgm_base.cpp:628
wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmap &aImage, wxItemKind aType=wxITEM_NORMAL)
Function AddMenuItem is an inline helper function to create and insert a menu item with an icon into ...
Definition: bitmap.cpp:223
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:43
Class ENV_VAR_ITEM.
Definition: pgm_base.h:100
static const wxChar workingDirKey[]
Definition: pgm_base.h:326
BITMAP_DEF m_Lang_Icon
The menu language icons.
Definition: pgm_base.cpp:107
static const wxChar showEnvVarWarningDialog[]
Definition: pgm_base.cpp:68
int m_KI_Lang_Identifier
KiCad identifier used in menu selection (See id.h)
Definition: pgm_base.cpp:104
#define MAX_FILE_HISTORY_SIZE
Definition: pgm_base.h:59
wxConfigBase * GetNewConfig(const wxString &aProgName)
Create a new wxConfig so we can put configuration files in a more proper place for each platform...
Definition: common.cpp:251
VTBL_ENTRY wxApp & App()
Function App returns a bare naked wxApp, which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:188
static const wxChar traceEnvVars[]
Definition: pgm_base.cpp:69
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
void Destroy()
Definition: pgm_base.cpp:173
bool setExecutablePath()
Function setExecutablePath finds the path to the executable and stores it in PGM_BASE::m_bin_dir.
Definition: pgm_base.cpp:496
System directories search utilities.
The common library.
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:55
A small class to handle the list of existing translations.
Definition: pgm_base.cpp:98
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
bool m_DoNotTranslate
Set to true if the m_Lang_Label must not be translated.
Definition: pgm_base.cpp:113
VTBL_ENTRY const wxString AskUserForPreferredEditor(const wxString &aDefaultEditor=wxEmptyString)
Shows a dialog that instructs the user to select a new preferred editor.
Definition: pgm_base.cpp:240
#define KICAD_COMMON
Definition: pgm_base.cpp:60
#define ENBL_ZOOM_NO_CENTER_KEY
Definition: pgm_base.h:47
This file contains miscellaneous commonly used macros and functions.
#define PCB_EDIT_FRAME_NAME
Definition: draw_frame.h:56
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
#define GAL_DISPLAY_OPTIONS_KEY
Definition: pgm_base.h:52
#define ENBL_AUTO_PAN_KEY
Definition: pgm_base.h:50
VTBL_ENTRY bool SetLocalEnvVariable(const wxString &aName, const wxString &aValue)
Function SetLocalEnvVariable.
Definition: pgm_base.cpp:841
VTBL_ENTRY void SetLanguageIdentifier(int menu_id)
Function SetLanguageIdentifier sets in .m_language_id member the wxWidgets language identifier Id fro...
Definition: pgm_base.cpp:749
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Definition: pgm_base.h:129
void SetDefinedExternally(bool aIsDefinedExternally)
Definition: pgm_base.h:112
wxString EDA_FILE_SELECTOR(const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition, wxString *aMruPath)
Function EDA_FILE_SELECTOR.
Definition: gestfich.cpp:82
#define ENBL_MOUSEWHEEL_PAN_KEY
Definition: pgm_base.h:48
Base window classes and related definitions.
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Function SystemDirsAppend appends system places to aSearchStack in a platform specific way...
wxLogTrace helper definitions.
int m_WX_Lang_Identifier
wxWidgets locale identifier (See wxWidgets doc)
Definition: pgm_base.cpp:101
const wxChar *const traceLocale
Flag to enable locale debug output.
#define ICON_SCALE_KEY
Definition: pgm_base.h:45
wxString m_Lang_Label
Labels used in menus.
Definition: pgm_base.cpp:110
VTBL_ENTRY void SetLanguagePath()
Definition: pgm_base.cpp:765
VTBL_ENTRY bool SetLanguage(bool first_time=false)
Function SetLanguage sets the dictionary file name for internationalization.
Definition: pgm_base.cpp:657
VTBL_ENTRY void SetLocalEnvVariables(const ENV_VAR_MAP &aEnvVarMap)
Function SetLocalEnvVariables.
Definition: pgm_base.cpp:860
void SetMaxFiles(size_t aMaxFiles)
Definition: pgm_base.cpp:79
#define GAL_ANTIALIASING_MODE_KEY
Definition: pgm_base.h:53
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
see class PGM_BASE
const char * name
Definition: DXF_plotter.cpp:61
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void SetValue(const wxString &aValue)
Definition: pgm_base.h:118
size_t i
Definition: json11.cpp:597
#define USE_ICONS_IN_MENUS_KEY
Definition: pgm_base.h:44
bool InitPgm()
Function initPgm initializes this program (process) in a KiCad standard way, using some generalized t...
Definition: pgm_base.cpp:263
static const wxChar languageCfgKey[]
Definition: pgm_base.cpp:66
static LANGUAGE_DESCR s_Languages[]
Variable s_Languages Note: because this list is not created on the fly, wxTranslation must be called ...
Definition: pgm_base.cpp:123
std::map< wxString, ENV_VAR_ITEM >::iterator ENV_VAR_MAP_ITER
Definition: pgm_base.h:130
VTBL_ENTRY const wxString & GetEditorName(bool aCanShowFileChooser=true)
Return the preferred editor name.
Definition: pgm_base.cpp:203
VTBL_ENTRY void AddMenuLanguageList(wxMenu *MasterMenu)
Function AddMenuLanguageList creates a menu list for language choice, and add it as submenu to Master...
Definition: pgm_base.cpp:800
Some functions to handle hotkeys in KiCad.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:277
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:295
void AddPaths(const wxString &aPaths, int aIndex=-1)
Function AddPaths insert or append path(s)
#define min(a, b)
Definition: auxiliary.h:85
File locking utilities.