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-2020 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 <wx/html/htmlwin.h>
34 #include <wx/fs_zip.h>
35 #include <wx/dir.h>
36 #include <wx/filename.h>
37 #include <wx/snglinst.h>
38 #include <wx/stdpaths.h>
39 #include <wx/sysopt.h>
40 #include <wx/richmsgdlg.h>
41 #include <wx/filedlg.h>
42 #include <wx/tooltip.h>
43 
44 #include <build_version.h>
45 #include <config_params.h>
46 #include <confirm.h>
47 #include <core/arraydim.h>
49 #include <eda_base_frame.h>
50 #include <eda_draw_frame.h>
52 #include <gestfich.h>
53 #include <hotkeys_basic.h>
54 #include <id.h>
55 #include <kiplatform/environment.h>
56 #include <lockfile.h>
57 #include <macros.h>
58 #include <menus_helpers.h>
59 #include <paths.h>
60 #include <pgm_base.h>
63 #include <systemdirsappend.h>
64 #include <trace_helpers.h>
65 
66 
74 #undef _
75 #define _(s) s
77 {
78  { wxLANGUAGE_DEFAULT, ID_LANGUAGE_DEFAULT, _( "Default" ), false },
79  { wxLANGUAGE_CATALAN, ID_LANGUAGE_CATALAN, wxT( "Català" ), true },
80  { wxLANGUAGE_CZECH, ID_LANGUAGE_CZECH, wxT( "Čeština" ), true },
81  { wxLANGUAGE_DANISH, ID_LANGUAGE_DANISH, wxT( "Dansk" ), true },
82  { wxLANGUAGE_GERMAN, ID_LANGUAGE_GERMAN, wxT( "Deutsch" ), true },
83  { wxLANGUAGE_GREEK, ID_LANGUAGE_GREEK, wxT( "Ελληνικά" ), true },
84  { wxLANGUAGE_ENGLISH, ID_LANGUAGE_ENGLISH, wxT( "English" ), true },
85  { wxLANGUAGE_SPANISH, ID_LANGUAGE_SPANISH, wxT( "Español" ), true },
86  { wxLANGUAGE_FRENCH, ID_LANGUAGE_FRENCH, wxT( "Français" ), true },
87  { wxLANGUAGE_INDONESIAN, ID_LANGUAGE_INDONESIAN, wxT( "Indonesia" ), true },
88  { wxLANGUAGE_ITALIAN, ID_LANGUAGE_ITALIAN, wxT( "Italiano" ), true },
89  { wxLANGUAGE_LITHUANIAN, ID_LANGUAGE_LITHUANIAN, wxT( "Lietuvių" ), true },
90  { wxLANGUAGE_HUNGARIAN, ID_LANGUAGE_HUNGARIAN, wxT( "Magyar" ), true },
91  { wxLANGUAGE_JAPANESE, ID_LANGUAGE_JAPANESE, wxT( "日本語" ), true },
92  { wxLANGUAGE_POLISH, ID_LANGUAGE_POLISH, wxT( "Polski" ), true },
93  { wxLANGUAGE_PORTUGUESE, ID_LANGUAGE_PORTUGUESE, wxT( "Português" ),true },
94  { wxLANGUAGE_RUSSIAN, ID_LANGUAGE_RUSSIAN, wxT( "Русский" ), true },
95  { wxLANGUAGE_SERBIAN, ID_LANGUAGE_SERBIAN, wxT( "Српски"), true },
96  { wxLANGUAGE_FINNISH, ID_LANGUAGE_FINNISH, wxT( "Suomalainen" ), true },
97  { wxLANGUAGE_VIETNAMESE, ID_LANGUAGE_VIETNAMESE, wxT( "Tiếng việt" ), true },
98  { wxLANGUAGE_TURKISH, ID_LANGUAGE_TURKISH, wxT( "Türk" ), true },
99  { wxLANGUAGE_CHINESE_SIMPLIFIED, ID_LANGUAGE_CHINESE_SIMPLIFIED,
100  wxT( "简体中文" ), true },
101  { wxLANGUAGE_CHINESE_TRADITIONAL, ID_LANGUAGE_CHINESE_TRADITIONAL,
102  wxT( "繁體中文" ), false },
103  { 0, 0, "", false } // Sentinel
104 };
105 #undef _
106 #define _(s) wxGetTranslation((s))
107 
108 
110 {
112  m_locale = NULL;
113  m_Printing = false;
114  m_ModalDialogCount = 0;
115 
116  m_show_env_var_dialog = true;
117 
118  setLanguageId( wxLANGUAGE_DEFAULT );
119 
120  ForceSystemPdfBrowser( false );
121 }
122 
123 
125 {
126  Destroy();
127 }
128 
129 
131 {
132  // unlike a normal destructor, this is designed to be called more than once safely:
133  delete m_pgm_checker;
134  m_pgm_checker = 0;
135 
136  delete m_locale;
137  m_locale = 0;
138 }
139 
140 
142 {
143  wxASSERT( wxTheApp );
144  return *wxTheApp;
145 }
146 
147 
148 void PGM_BASE::SetEditorName( const wxString& aFileName )
149 {
150  m_editor_name = aFileName;
151  wxASSERT( GetCommonSettings() );
152  GetCommonSettings()->m_System.editor_name = aFileName;
153 }
154 
155 
156 const wxString& PGM_BASE::GetEditorName( bool aCanShowFileChooser )
157 {
158  wxString editorname = m_editor_name;
159 
160  if( !editorname )
161  {
162  if( !wxGetEnv( "EDITOR", &editorname ) )
163  {
164  // If there is no EDITOR variable set, try the desktop default
165 #ifdef __WXMAC__
166  editorname = "/usr/bin/open";
167 #elif __WXX11__
168  editorname = "/usr/bin/xdg-open";
169 #endif
170  }
171  }
172 
173  // If we still don't have an editor name show a dialog asking the user to select one
174  if( !editorname && aCanShowFileChooser )
175  {
177  _( "No default editor found, you must choose it" ) );
178 
179  editorname = AskUserForPreferredEditor();
180  }
181 
182  // If we finally have a new editor name request it to be copied to m_editor_name and
183  // saved to the preferences file.
184  if( !editorname.IsEmpty() )
185  SetEditorName( editorname );
186 
187  // m_editor_name already has the same value that editorname, or empty if no editor was
188  // found/chosen.
189  return m_editor_name;
190 }
191 
192 
193 const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEditor )
194 {
195  // Create a mask representing the executable files in the current platform
196 #ifdef __WINDOWS__
197  wxString mask( _( "Executable file (*.exe)|*.exe" ) );
198 #else
199  wxString mask( _( "Executable file (*)|*" ) );
200 #endif
201 
202  // Extract the path, name and extension from the default editor (even if the editor's
203  // name was empty, this method will succeed and return empty strings).
204  wxString path, name, ext;
205  wxFileName::SplitPath( aDefaultEditor, &path, &name, &ext );
206 
207  // Show the modal editor and return the file chosen (may be empty if the user cancels
208  // the dialog).
209  return EDA_FILE_SELECTOR( _( "Select Preferred Editor" ), path,
210  name, ext, mask,
211  NULL, wxFD_OPEN | wxFD_FILE_MUST_EXIST,
212  true );
213 }
214 
215 
217 {
218  wxFileName pgm_name( App().argv[0] );
219 
220  wxInitAllImageHandlers();
221 
222 #ifndef __WINDOWS__
223  if( wxString( wxGetenv( "HOME" ) ).IsEmpty() )
224  {
225  DisplayErrorMessage( nullptr, _( "Environmental variable HOME is empty. Unable to continue." ) );
226  return false;
227  }
228 #endif
229 
230  m_pgm_checker = new wxSingleInstanceChecker( pgm_name.GetName().Lower() + wxT( "-" ) +
231  wxGetUserId(), GetKicadLockFilePath() );
232 
233  if( m_pgm_checker->IsAnotherRunning() )
234  {
235  wxString quiz =
236  wxString::Format( _( "%s is already running. Continue?" ), pgm_name.GetName() );
237 
238  if( !IsOK( NULL, quiz ) )
239  return false;
240  }
241 
242  // Init KiCad environment
243  // the environment variable KICAD (if exists) gives the kicad path:
244  // something like set KICAD=d:\kicad
245  bool isDefined = wxGetEnv( "KICAD", &m_kicad_env );
246 
247  if( isDefined ) // ensure m_kicad_env ends by "/"
248  {
250 
251  if( !m_kicad_env.IsEmpty() && m_kicad_env.Last() != '/' )
253  }
254 
255  // Init parameters for configuration
256  App().SetVendorName( "KiCad" );
257  App().SetAppName( pgm_name.GetName().Lower() );
258 
259  // Install some image handlers, mainly for help
260  if( wxImage::FindHandler( wxBITMAP_TYPE_PNG ) == NULL )
261  wxImage::AddHandler( new wxPNGHandler );
262 
263  if( wxImage::FindHandler( wxBITMAP_TYPE_GIF ) == NULL )
264  wxImage::AddHandler( new wxGIFHandler );
265 
266  if( wxImage::FindHandler( wxBITMAP_TYPE_JPEG ) == NULL )
267  wxImage::AddHandler( new wxJPEGHandler );
268 
269  wxFileSystem::AddHandler( new wxZipFSHandler );
270 
271  // Analyze the command line & initialize the binary path
272  wxString tmp;
274  SetLanguagePath();
275  SetDefaultLanguage( tmp );
276 
277  m_settings_manager = std::make_unique<SETTINGS_MANAGER>();
278 
279  // Something got in the way of settings load: can't continue
280  if( !m_settings_manager->IsOK() )
281  return false;
282 
283  wxFileName baseSharePath;
284 #if defined( __WXMSW__ )
285  // Make the paths relative to the executable dir as KiCad might be installed anywhere
286  // It follows the Windows installer paths scheme, where binaries are installed in
287  // PATH/bin and extra files in PATH/share/kicad
288  baseSharePath.AssignDir( m_bin_dir + "\\.." );
289  baseSharePath.Normalize();
290 #else
291  baseSharePath.AssignDir( wxString( wxT( DEFAULT_INSTALL_PATH ) ) );
292 #endif
293 
294 #if !defined( __WXMAC__ )
295  baseSharePath.AppendDir( "share" );
296  baseSharePath.AppendDir( "kicad" );
297 #endif
298 
299  // KICAD6_FOOTPRINT_DIR
300  wxString envVarName = wxT( "KICAD6_FOOTPRINT_DIR" );
301  ENV_VAR_ITEM envVarItem;
302  wxString envValue;
303  wxFileName tmpFileName;
304 
305  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
306  {
307  tmpFileName.AssignDir( envValue );
308  envVarItem.SetDefinedExternally( true );
309  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Found entry %s externally", envVarName );
310  }
311  else
312  {
313  tmpFileName = baseSharePath;
314  tmpFileName.AppendDir( "modules" );
315  envVarItem.SetDefinedExternally( false );
316  }
317 
318  envVarItem.SetValue( tmpFileName.GetPath() );
319  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Setting entry %s = %s",
320  envVarName, envVarItem.GetValue() );
321  m_local_env_vars[ envVarName ] = envVarItem;
322 
323  // KICAD6_3DMODEL_DIR
324  envVarName = wxT( "KICAD6_3DMODEL_DIR" );
325 
326  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
327  {
328  tmpFileName.AssignDir( envValue );
329  envVarItem.SetDefinedExternally( true );
330  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Found entry %s externally", envVarName );
331  }
332  else
333  {
334  tmpFileName = baseSharePath;
335  tmpFileName.AppendDir( "3dmodels" );
336  envVarItem.SetDefinedExternally( false );
337  }
338 
339  envVarItem.SetValue( tmpFileName.GetFullPath() );
340  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Setting entry %s = %s",
341  envVarName, envVarItem.GetValue() );
342  m_local_env_vars[ envVarName ] = envVarItem;
343 
344  // KICAD6_TEMPLATE_DIR
345  envVarName = "KICAD6_TEMPLATE_DIR";
346 
347  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
348  {
349  tmpFileName.AssignDir( envValue );
350  envVarItem.SetDefinedExternally( true );
351  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Found entry %s externally", envVarName );
352  }
353  else
354  {
355  // Attempt to find the best default template path.
356  SEARCH_STACK bases;
357  SEARCH_STACK templatePaths;
358 
359  SystemDirsAppend( &bases );
360 
361  for( unsigned i = 0; i < bases.GetCount(); ++i )
362  {
363  wxFileName fn( bases[i], wxEmptyString );
364 
365  // Add KiCad template file path to search path list.
366  fn.AppendDir( "template" );
367 
368  // Only add path if exists and can be read by the user.
369  if( fn.DirExists() && fn.IsDirReadable() )
370  {
371  wxLogTrace( tracePathsAndFiles, "Checking template path '%s' exists",
372  fn.GetPath() );
373  templatePaths.AddPaths( fn.GetPath() );
374  }
375  }
376 
377  if( templatePaths.IsEmpty() )
378  {
379  tmpFileName = baseSharePath;
380  tmpFileName.AppendDir( "template" );
381  }
382  else
383  {
384  // Take the first one. There may be more but this will likely be the best option.
385  tmpFileName.AssignDir( templatePaths[0] );
386  }
387 
388  envVarItem.SetDefinedExternally( false );
389  }
390 
391  envVarItem.SetValue( tmpFileName.GetPath() );
392  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Setting entry %s = %s", envVarName,
393  envVarItem.GetValue() );
394  m_local_env_vars[ envVarName ] = envVarItem;
395 
396  // KICAD_USER_TEMPLATE_DIR
397  envVarName = "KICAD_USER_TEMPLATE_DIR";
398 
399  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
400  {
401  tmpFileName.AssignDir( envValue );
402  envVarItem.SetDefinedExternally( true );
403  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Found entry %s externally", envVarName );
404  }
405  else
406  {
407  // Default user template path.
408  tmpFileName.AssignDir( PATHS::GetUserTemplatesPath() );
409  envVarItem.SetDefinedExternally( false );
410  }
411 
412  envVarItem.SetValue( tmpFileName.GetPath() );
413  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Setting entry %s = %s",
414  envVarName, envVarItem.GetValue() );
415  m_local_env_vars[ envVarName ] = envVarItem;
416 
417  // KICAD_SYMBOLS
418  envVarName = wxT( "KICAD6_SYMBOL_DIR" );
419 
420  if( wxGetEnv( envVarName, &envValue ) == true && !envValue.IsEmpty() )
421  {
422  tmpFileName.AssignDir( envValue );
423  envVarItem.SetDefinedExternally( true );
424  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Found entry %s externally", envVarName );
425  }
426  else
427  {
428  tmpFileName = baseSharePath;
429  tmpFileName.AppendDir( "library" );
430  envVarItem.SetDefinedExternally( false );
431  }
432 
433  envVarItem.SetValue( tmpFileName.GetPath() );
434  wxLogTrace( traceEnvVars, "PGM_BASE::InitPgm: Setting entry %s = %s",
435  envVarName, envVarItem.GetValue() );
436  m_local_env_vars[ envVarName ] = envVarItem;
437 
439 
440  // Init user language *before* calling loadCommonSettings, because
441  // env vars could be incorrectly initialized on Linux
442  // (if the value contains some non ASCII7 chars, the env var is not initialized)
443  SetLanguage( tmp, true );
444 
446 
447  ReadPdfBrowserInfos(); // needs GetCommonSettings()
448 
449 #ifdef __WXMAC__
450  // Always show filters on Open dialog to be able to choose plugin
451  wxSystemOptions::SetOption( wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES, 1 );
452 #endif
453 
454  // TODO(JE): Remove this if apps are refactored to not assume Prj() always works
455  // Need to create a project early for now (it can have an empty path for the moment)
457 
458  // TODO: Move tooltips into KIPLATFORM
459  // This sets the maximum tooltip display duration to 10s (up from 5) but only affects
460  // Windows as other platforms display tooltips while the mouse is not moving
461  wxToolTip::SetAutoPop( 10000 );
462 
463  return true;
464 }
465 
466 
468 {
469  m_bin_dir = wxStandardPaths::Get().GetExecutablePath();
470 
471 #ifdef __WXMAC__
472  // On OSX Pgm().GetExecutablePath() will always point to main
473  // bundle directory, e.g., /Applications/kicad.app/
474 
475  wxFileName fn( m_bin_dir );
476 
477  if( fn.GetName() == wxT( "kicad" ) )
478  {
479  // kicad launcher, so just remove the Contents/MacOS part
480  fn.RemoveLastDir();
481  fn.RemoveLastDir();
482  }
483  else
484  {
485  // standalone binaries live in Contents/Applications/<standalone>.app/Contents/MacOS
486  fn.RemoveLastDir();
487  fn.RemoveLastDir();
488  fn.RemoveLastDir();
489  fn.RemoveLastDir();
490  fn.RemoveLastDir();
491  }
492 
493  m_bin_dir = fn.GetPath() + wxT( "/" );
494 #else
495  // Use unix notation for paths. I am not sure this is a good idea,
496  // but it simplifies compatibility between Windows and Unices.
497  // However it is a potential problem in path handling under Windows.
499 
500  // Remove file name form command line:
501  while( m_bin_dir.Last() != '/' && !m_bin_dir.IsEmpty() )
502  m_bin_dir.RemoveLast();
503 #endif
504 
505  return true;
506 }
507 
508 
510 {
511  m_help_size.x = 500;
512  m_help_size.y = 400;
513 
516 
517  for( const auto& it : GetCommonSettings()->m_Env.vars )
518  {
519  wxString key( it.first.c_str(), wxConvUTF8 );
520  wxLogTrace( traceEnvVars, "PGM_BASE::loadCommonSettings: Found entry %s = %s",
521  key, it.second );
522 
523  // Do not store the env var PROJECT_VAR_NAME ("KIPRJMOD") definition if for some reason
524  // it is found in config. (It is reserved and defined as project path)
525  if( key == PROJECT_VAR_NAME )
526  continue;
527 
528  if( m_local_env_vars[ key ].GetDefinedExternally() )
529  continue;
530 
531  wxLogTrace( traceEnvVars, "PGM_BASE::loadCommonSettings: Updating entry %s = %s",
532  key, it.second );
533 
534  m_local_env_vars[ key ] = ENV_VAR_ITEM( it.second, wxGetEnv( it.first, nullptr ) );
535  }
536 
537  for( auto& m_local_env_var : m_local_env_vars )
538  SetLocalEnvVariable( m_local_env_var.first, m_local_env_var.second.GetValue() );
539 }
540 
541 
543 {
544  // GetCommonSettings() is not initialized until fairly late in the
545  // process startup: InitPgm(), so test before using:
546  if( GetCommonSettings() )
547  {
548  GetCommonSettings()->m_System.working_dir = wxGetCwd();
550 
551  // remove only the old env vars that do not exist in list.
552  // We do not clear the full list because some are defined externally,
553  // and we cannot modify or delete them
554  std::map<std::string, wxString>& curr_vars = GetCommonSettings()->m_Env.vars;
555 
556  for( auto it = curr_vars.begin(); it != curr_vars.end(); )
557  {
558  const std::string& key = it->first;
559 
560  if( m_local_env_vars.find( key ) == m_local_env_vars.end() )
561  it = curr_vars.erase( it ); // This entry no longer exists in new list
562  else
563  it++;
564  }
565 
566  // Save the local environment variables.
567  for( auto& m_local_env_var : m_local_env_vars )
568  {
569  if( m_local_env_var.second.GetDefinedExternally() )
570  continue;
571 
572  wxLogTrace( traceEnvVars,
573  "PGM_BASE::SaveCommonSettings: Saving environment variable config "
574  "entry %s as %s",
575  m_local_env_var.first, m_local_env_var.second.GetValue() );
576 
577  std::string key( m_local_env_var.first.ToUTF8() );
578  GetCommonSettings()->m_Env.vars[ key ] = m_local_env_var.second.GetValue();
579  }
580  }
581 }
582 
583 
585 {
587 }
588 
589 
590 bool PGM_BASE::SetLanguage( wxString& aErrMsg, bool first_time )
591 {
592  if( first_time )
593  {
594  setLanguageId( wxLANGUAGE_DEFAULT );
595  // First time SetLanguage is called, the user selected language id is set
596  // from common user config settings
597  wxString languageSel = GetCommonSettings()->m_System.language;
598 
599  // Search for the current selection
600  for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
601  {
602  if( LanguagesList[ii].m_Lang_Label == languageSel )
603  {
604  setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
605  break;
606  }
607  }
608  }
609 
610  // dictionary file name without extend (full name is kicad.mo)
611  wxString dictionaryName( "kicad" );
612 
613  delete m_locale;
614  m_locale = new wxLocale;
615 
616  if( !m_locale->Init( m_language_id ) )
617  {
618  wxLogTrace( traceLocale, "This language is not supported by the system." );
619 
620  setLanguageId( wxLANGUAGE_DEFAULT );
621  delete m_locale;
622 
623  m_locale = new wxLocale;
624  m_locale->Init();
625 
626  aErrMsg = _( "This language is not supported by the operating system." );
627  return false;
628  }
629  else if( !first_time )
630  {
631  wxLogTrace( traceLocale, "Search for dictionary %s.mo in %s",
632  dictionaryName, m_locale->GetName() );
633  }
634 
635  if( !first_time )
636  {
637  // If we are here, the user has selected another language.
638  // Therefore the new preferred language name is stored in common config.
639  // Do NOT store the wxWidgets language Id, it can change between wxWidgets
640  // versions, for a given language
641  wxString languageSel;
642 
643  // Search for the current selection language name
644  for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
645  {
646  if( LanguagesList[ii].m_WX_Lang_Identifier == m_language_id )
647  {
648  languageSel = LanguagesList[ii].m_Lang_Label;
649  break;
650  }
651  }
652 
654  cfg->m_System.language = languageSel;
655  cfg->SaveToFile( GetSettingsManager().GetPathForSettingsFile( cfg ) );
656  }
657 
658  // Try adding the dictionary if it is not currently loaded
659  if( !m_locale->IsLoaded( dictionaryName ) )
660  m_locale->AddCatalog( dictionaryName );
661 
662  // Verify the Kicad dictionary was loaded properly
663  // However, for the English language, the dictionary is not mandatory, as
664  // all messages are already in English, just restricted to ASCII7 chars,
665  // the verification is skipped.
666  if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
667  {
668  wxLogTrace( traceLocale, "Unable to load dictionary %s.mo in %s",
669  dictionaryName, m_locale->GetName() );
670 
671  setLanguageId( wxLANGUAGE_DEFAULT );
672  delete m_locale;
673 
674  m_locale = new wxLocale;
675  m_locale->Init();
676 
677  aErrMsg = _( "The KiCad language file for this language is not installed." );
678  return false;
679  }
680 
681  return true;
682 }
683 
684 
685 bool PGM_BASE::SetDefaultLanguage( wxString& aErrMsg )
686 {
687  setLanguageId( wxLANGUAGE_DEFAULT );
688 
689  // dictionary file name without extend (full name is kicad.mo)
690  wxString dictionaryName( "kicad" );
691 
692  delete m_locale;
693  m_locale = new wxLocale;
694  m_locale->Init();
695 
696  // Try adding the dictionary if it is not currently loaded
697  if( !m_locale->IsLoaded( dictionaryName ) )
698  m_locale->AddCatalog( dictionaryName );
699 
700  // Verify the Kicad dictionary was loaded properly
701  // However, for the English language, the dictionary is not mandatory, as
702  // all messages are already in English, just restricted to ASCII7 chars,
703  // the verification is skipped.
704  if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
705  {
706  wxLogTrace( traceLocale, "Unable to load dictionary %s.mo in %s",
707  dictionaryName, m_locale->GetName() );
708 
709  setLanguageId( wxLANGUAGE_DEFAULT );
710  delete m_locale;
711 
712  m_locale = new wxLocale;
713  m_locale->Init();
714 
715  aErrMsg = _( "The KiCad language file for this language is not installed." );
716  return false;
717  }
718 
719  return true;
720 }
721 
722 
724 {
725  wxLogTrace( traceLocale, "Select language ID %d from %d possible languages.",
726  menu_id, (int)arrayDim( LanguagesList )-1 );
727 
728  for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
729  {
730  if( menu_id == LanguagesList[ii].m_KI_Lang_Identifier )
731  {
732  setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
733  break;
734  }
735  }
736 }
737 
738 
740 {
741  SEARCH_STACK guesses;
742 
743  SystemDirsAppend( &guesses );
744 
745  // Add our internat dir to the wxLocale catalog of paths
746  for( unsigned i = 0; i < guesses.GetCount(); i++ )
747  {
748  wxFileName fn( guesses[i], wxEmptyString );
749 
750  // Append path for Windows and unix KiCad package install
751  fn.AppendDir( "share" );
752  fn.AppendDir( "internat" );
753 
754  if( fn.IsDirReadable() )
755  {
756  wxLogTrace( traceLocale, "Adding locale lookup path: " + fn.GetPath() );
757  wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
758  }
759 
760  // Append path for unix standard install
761  fn.RemoveLastDir();
762  fn.AppendDir( "kicad" );
763  fn.AppendDir( "internat" );
764 
765  if( fn.IsDirReadable() )
766  {
767  wxLogTrace( traceLocale, "Adding locale lookup path: " + fn.GetPath() );
768  wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
769  }
770 
771  // Append path for macOS install
772  fn.RemoveLastDir();
773  fn.RemoveLastDir();
774  fn.RemoveLastDir();
775  fn.AppendDir( "internat" );
776 
777  if( fn.IsDirReadable() )
778  {
779  wxLogTrace( traceLocale, "Adding locale lookup path: " + fn.GetPath() );
780  wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
781  }
782  }
783 
784  if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
785  {
786  wxFileName fn( Pgm().GetExecutablePath() );
787  fn.RemoveLastDir();
788  fn.AppendDir( "translation" );
789  wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
790  }
791 }
792 
793 
794 bool PGM_BASE::SetLocalEnvVariable( const wxString& aName, const wxString& aValue )
795 {
796  wxString env;
797 
798  // Check to see if the environment variable is already set.
799  if( wxGetEnv( aName, &env ) )
800  {
801  wxLogTrace( traceEnvVars,
802  "PGM_BASE::SetLocalEnvVariable: Environment variable %s already set to %s",
803  aName, env );
804  return env == aValue;
805  }
806 
807  wxLogTrace( traceEnvVars,
808  "PGM_BASE::SetLocalEnvVariable: Setting local environment variable %s to %s",
809  aName, aValue );
810 
811  return wxSetEnv( aName, aValue );
812 }
813 
814 
816 {
817  m_local_env_vars.clear();
818  m_local_env_vars = aEnvVarMap;
819 
821 
822  // Overwrites externally defined environment variable until the next time the application
823  // is run.
824  for( auto& m_local_env_var : m_local_env_vars )
825  {
826  wxLogTrace( traceEnvVars,
827  "PGM_BASE::SetLocalEnvVariables: Setting local environment variable %s to %s",
828  m_local_env_var.first,
829  m_local_env_var.second.GetValue() );
830  wxSetEnv( m_local_env_var.first, m_local_env_var.second.GetValue() );
831  }
832 }
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:584
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:167
void loadCommonSettings()
Loads internal settings from COMMON_SETTINGS.
Definition: pgm_base.cpp:509
LANGUAGE_DESCR LanguagesList[]
An array containing all the languages that KiCad supports.
Definition: pgm_base.cpp:76
wxString GetKicadLockFilePath()
Definition: lockfile.cpp:60
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:44
virtual void SetEditorName(const wxString &aFileName)
Definition: pgm_base.cpp:148
virtual ~PGM_BASE()
Definition: pgm_base.cpp:124
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:265
This file is part of the common library TODO brief description.
ENV_VAR_MAP m_local_env_vars
Local environment variable expansion settings such as KICAD6_FOOTPRINT_DIR, and KICAD6_3DMODEL_DIR.
Definition: pgm_base.h:375
This file is part of the common library.
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition: pgm_base.cpp:542
int m_ModalDialogCount
Definition: pgm_base.h:333
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:43
wxLocale * m_locale
The current locale.
Definition: pgm_base.h:361
A simple helper class to store environment variable values and the status of whether or not they were...
Definition: pgm_base.h:84
const wxString & GetValue() const
Definition: pgm_base.h:101
int m_KI_Lang_Identifier
KiCad identifier used in menu selection (See id.h)
Definition: pgm_base.h:63
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:38
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:141
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
bool m_show_env_var_dialog
Flag to indicate if the environment variable overwrite warning dialog should be shown.
Definition: pgm_base.h:378
void Destroy()
Definition: pgm_base.cpp:130
bool setExecutablePath()
Find the path to the executable and stores it in PGM_BASE::m_bin_dir.
Definition: pgm_base.cpp:467
System directories search utilities.
A small class to handle the list of existing translations.
Definition: pgm_base.h:57
wxSingleInstanceChecker * m_pgm_checker
prevents multiple instances of a program from being run at the same time.
Definition: pgm_base.h:352
Look for files in a number of paths.
Definition: search_stack.h:41
virtual void ForceSystemPdfBrowser(bool aFlg)
Force the use of system PDF browser, even if a preferred PDF browser is set.
Definition: pgm_base.h:219
virtual const wxString AskUserForPreferredEditor(const wxString &aDefaultEditor=wxEmptyString)
Shows a dialog that instructs the user to select a new preferred editor.
Definition: pgm_base.cpp:193
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
int m_language_id
The current language setting.
Definition: pgm_base.h:364
This file contains miscellaneous commonly used macros and functions.
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition: pgm_base.h:349
wxString m_kicad_env
The KICAD system environment variable.
Definition: pgm_base.h:358
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
virtual bool SetLocalEnvVariable(const wxString &aName, const wxString &aValue)
Sets the environment variable aName to aValue.
Definition: pgm_base.cpp:794
#define NULL
virtual void SetLanguageIdentifier(int menu_id)
Set in .m_language_id member the wxWidgets language identifier ID fromthe KiCad menu id (internal men...
Definition: pgm_base.cpp:723
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Definition: pgm_base.h:113
std::map< std::string, wxString > vars
void SetDefinedExternally(bool aIsDefinedExternally)
Definition: pgm_base.h:96
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)
A helper function that wraps a call to wxFileSelector.
Definition: gestfich.cpp:52
COMMON_SETTINGS * GetCommonSettings() const
Retrieves the common settings shared by all applications.
Base window classes and related definitions.
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:85
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Append system places to aSearchStack in a platform specific way and pertinent to KiCad programs.
wxLogTrace helper definitions.
void setLanguageId(int aId)
Trap all changes in here, simplifies debugging.
Definition: pgm_base.h:340
const wxChar *const traceLocale
Flag to enable locale debug output.
ENVIRONMENT m_Env
virtual bool SetLanguage(wxString &aErrMsg, bool first_time=false)
Set the dictionary file name for internationalization.
Definition: pgm_base.cpp:590
const wxChar *const traceEnvVars
Flag to enable debug output of environment variable operations.
wxString m_Lang_Label
Labels used in menus.
Definition: pgm_base.h:66
virtual void SetLanguagePath()
Definition: pgm_base.cpp:739
wxString m_bin_dir
full path to this program
Definition: pgm_base.h:355
virtual void SetLocalEnvVariables(const ENV_VAR_MAP &aEnvVarMap)
Set the internal local environment variable map to aEnvVarMap, updates the entries in the ....
Definition: pgm_base.cpp:815
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:42
bool m_Printing
wxWidgets on MSW tends to crash if you spool up more than one print job at a time.
Definition: pgm_base.h:331
see class PGM_BASE
virtual const wxString & GetExecutablePath() const
Definition: pgm_base.h:198
const char * name
Definition: DXF_plotter.cpp:59
void SetValue(const wxString &aValue)
Definition: pgm_base.h:102
#define _(s)
Current list of languages supported by KiCad.
Definition: pgm_base.cpp:106
wxSize m_help_size
Definition: pgm_base.h:372
bool InitPgm()
Initialize this program.
Definition: pgm_base.cpp:216
wxString m_editor_name
Definition: pgm_base.h:371
virtual const wxString & GetEditorName(bool aCanShowFileChooser=true)
Return the preferred editor name.
Definition: pgm_base.cpp:156
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:280
bool SetDefaultLanguage(wxString &aErrMsg)
Set the default language without reference to any preferences.
Definition: pgm_base.cpp:685
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:296
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).
File locking utilities.