KiCad PCB EDA Suite
pgm_base.h
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-2015 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 
31 #ifndef PGM_BASE_H_
32 #define PGM_BASE_H_
33 
34 #include <bitmaps_png/bitmap_def.h>
35 #include <map>
36 #include <memory>
37 #include <search_stack.h>
38 #include <wx/filename.h>
39 #include <wx/gdicmn.h>
40 
41 
42 class wxSingleInstanceChecker;
43 class wxApp;
44 class wxMenu;
45 class wxWindow;
46 
47 class COMMON_SETTINGS;
48 class SETTINGS_MANAGER;
49 
58 {
61 
64 
66  wxString m_Lang_Label;
67 
70 };
71 
72 
77 
78 
85 {
86 public:
87  ENV_VAR_ITEM( const wxString& aValue = wxEmptyString, bool aIsDefinedExternally = false ) :
88  m_value( aValue ),
89  m_isDefinedExternally( aIsDefinedExternally )
90  {
91  }
92 
93  ~ENV_VAR_ITEM() throw() {} // tell SWIG no exception
94 
96  void SetDefinedExternally( bool aIsDefinedExternally )
97  {
98  m_isDefinedExternally = aIsDefinedExternally;
99  }
100 
101  const wxString& GetValue() const { return m_value; }
102  void SetValue( const wxString& aValue ) { m_value = aValue; }
103 
104 private:
106  wxString m_value;
107 
110 };
111 
112 
113 typedef std::map<wxString, ENV_VAR_ITEM> ENV_VAR_MAP;
114 typedef std::map<wxString, ENV_VAR_ITEM>::iterator ENV_VAR_MAP_ITER;
115 typedef std::map<wxString, ENV_VAR_ITEM>::const_iterator ENV_VAR_MAP_CITER;
116 
117 
131 class PGM_BASE
132 {
133 public:
134  PGM_BASE();
135  virtual ~PGM_BASE();
136 
137 #if 0
138  /*
139 
140  Derived classes must implement these two functions: OnPgmInit() and
141  OnPgmExit(), and since they are only called from same source file as their
142  implementation, these need not be virtual here. In fact, in the case of
143  python project manager's class PGM_PYTHON, these functions are actually
144  written in python. In total there are three implementations, corresponding
145  to the three defines given by kiface.h's KFCTL_* #defines.
146 
147  */
148 
154  virtual bool OnPgmInit() = 0; // call this from wxApp::OnInit()
155 
156  virtual void OnPgmExit() = 0; // call this from wxApp::OnExit()
157 #endif
158 
165  virtual void MacOpenFile( const wxString& aFileName ) = 0;
166 
168 
169  virtual COMMON_SETTINGS* GetCommonSettings() const;
170 
171  virtual void SetEditorName( const wxString& aFileName );
172 
182  virtual const wxString& GetEditorName( bool aCanShowFileChooser = true );
183 
191  virtual const wxString AskUserForPreferredEditor(
192  const wxString& aDefaultEditor = wxEmptyString );
193 
194  virtual bool IsKicadEnvVariableDefined() const { return !m_kicad_env.IsEmpty(); }
195 
196  virtual const wxString& GetKicadEnvVariable() const { return m_kicad_env; }
197 
198  virtual const wxString& GetExecutablePath() const { return m_bin_dir; }
199 
200  virtual wxLocale* GetLocale() { return m_locale; }
201 
202  virtual const wxString& GetPdfBrowserName() const { return m_pdf_browser; }
203 
204  virtual void SetPdfBrowserName( const wxString& aFileName ) { m_pdf_browser = aFileName; }
205 
211  virtual bool UseSystemPdfBrowser() const
212  {
213  return m_use_system_pdf_browser || m_pdf_browser.IsEmpty();
214  }
215 
219  virtual void ForceSystemPdfBrowser( bool aFlg ) { m_use_system_pdf_browser = aFlg; }
220 
231  virtual bool SetLanguage( wxString& aErrMsg, bool first_time = false );
232 
239  bool SetDefaultLanguage( wxString& aErrMsg );
240 
248  virtual void SetLanguageIdentifier( int menu_id );
249 
253  virtual int GetSelectedLanguageIdentifier() const { return m_language_id; }
254 
255  virtual void SetLanguagePath();
256 
260  virtual void ReadPdfBrowserInfos();
261 
265  virtual void WritePdfBrowserInfos();
266 
279  virtual bool SetLocalEnvVariable( const wxString& aName, const wxString& aValue );
280 
288  virtual void SetLocalEnvVariables( const ENV_VAR_MAP& aEnvVarMap );
289 
290  virtual const ENV_VAR_MAP& GetLocalEnvVariables() const
291  {
292  return m_local_env_vars;
293  }
294 
300  virtual wxApp& App();
301 
302  static const wxChar workingDirKey[];
303 
316  bool InitPgm();
317 
318  // The PGM_* classes can have difficulties at termination if they
319  // are not destroyed soon enough. Relying on a static destructor can be
320  // too late for contained objects like wxSingleInstanceChecker.
321  void Destroy();
322 
326  void SaveCommonSettings();
327 
332 
334 
335 protected:
337  void loadCommonSettings();
338 
340  void setLanguageId( int aId ) { m_language_id = aId; }
341 
347  bool setExecutablePath();
348 
349  std::unique_ptr<SETTINGS_MANAGER> m_settings_manager;
350 
352  wxSingleInstanceChecker* m_pgm_checker;
353 
355  wxString m_bin_dir;
356 
358  wxString m_kicad_env;
359 
361  wxLocale* m_locale;
362 
365 
368 
370  wxString m_pdf_browser;
371  wxString m_editor_name;
372  wxSize m_help_size;
373 
376 
379 };
380 
381 
384 extern PGM_BASE& Pgm();
385 
388 extern PGM_BASE* PgmOrNull();
389 
390 
391 #endif // PGM_BASE_H_
virtual bool IsKicadEnvVariableDefined() const
Definition: pgm_base.h:194
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:583
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:167
void loadCommonSettings()
Loads internal settings from COMMON_SETTINGS.
Definition: pgm_base.cpp:508
virtual void SetEditorName(const wxString &aFileName)
Definition: pgm_base.cpp:147
virtual ~PGM_BASE()
Definition: pgm_base.cpp:123
bool m_isDefinedExternally
Flag to indicate if the environment variable was defined externally to the process.
Definition: pgm_base.h:109
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
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition: pgm_base.cpp:541
Container for data for KiCad programs.
Definition: pgm_base.h:131
int m_ModalDialogCount
Definition: pgm_base.h:333
virtual void MacOpenFile(const wxString &aFileName)=0
Specific to MacOSX (not used under Linux or Windows).
wxString m_value
The environment variable string value.
Definition: pgm_base.h:106
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
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:140
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:129
bool setExecutablePath()
Find the path to the executable and stores it in PGM_BASE::m_bin_dir.
Definition: pgm_base.cpp:466
virtual const ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.h:290
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
bool m_DoNotTranslate
Set to true if the m_Lang_Label must not be translated.
Definition: pgm_base.h:69
virtual void WritePdfBrowserInfos()
Save the PDF browser choice to the common configuration.
Definition: eda_doc.cpp:49
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:192
bool m_use_system_pdf_browser
true to use the selected PDF browser, if exists, or false to use the default
Definition: pgm_base.h:367
int m_language_id
The current language setting.
Definition: pgm_base.h:364
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition: pgm_base.h:349
virtual bool UseSystemPdfBrowser() const
Definition: pgm_base.h:211
wxString m_kicad_env
The KICAD system environment variable.
Definition: pgm_base.h:358
static const wxChar workingDirKey[]
Definition: pgm_base.h:302
bool GetDefinedExternally() const
Definition: pgm_base.h:95
virtual bool SetLocalEnvVariable(const wxString &aName, const wxString &aValue)
Sets the environment variable aName to aValue.
Definition: pgm_base.cpp:793
virtual const wxString & GetKicadEnvVariable() const
Definition: pgm_base.h:196
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:722
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
Definition: pgm_base.h:113
void SetDefinedExternally(bool aIsDefinedExternally)
Definition: pgm_base.h:96
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: cvpcb.cpp:112
virtual int GetSelectedLanguageIdentifier() const
Definition: pgm_base.h:253
virtual void SetPdfBrowserName(const wxString &aFileName)
Definition: pgm_base.h:204
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: single_top.cpp:106
std::map< wxString, ENV_VAR_ITEM >::const_iterator ENV_VAR_MAP_CITER
Definition: pgm_base.h:115
~ENV_VAR_ITEM()
Definition: pgm_base.h:93
void setLanguageId(int aId)
Trap all changes in here, simplifies debugging.
Definition: pgm_base.h:340
int m_WX_Lang_Identifier
wxWidgets locale identifier (See wxWidgets doc)
Definition: pgm_base.h:60
virtual bool SetLanguage(wxString &aErrMsg, bool first_time=false)
Set the dictionary file name for internationalization.
Definition: pgm_base.cpp:589
wxString m_Lang_Label
Labels used in menus.
Definition: pgm_base.h:66
virtual void SetLanguagePath()
Definition: pgm_base.cpp:738
wxString m_bin_dir
full path to this program
Definition: pgm_base.h:355
virtual const wxString & GetPdfBrowserName() const
Definition: pgm_base.h:202
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:814
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
virtual const wxString & GetExecutablePath() const
Definition: pgm_base.h:198
void SetValue(const wxString &aValue)
Definition: pgm_base.h:102
wxSize m_help_size
Definition: pgm_base.h:372
bool InitPgm()
Initialize this program.
Definition: pgm_base.cpp:215
virtual wxLocale * GetLocale()
Definition: pgm_base.h:200
wxString m_pdf_browser
The file name of the the program selected for browsing pdf files.
Definition: pgm_base.h:370
wxString m_editor_name
Definition: pgm_base.h:371
std::map< wxString, ENV_VAR_ITEM >::iterator ENV_VAR_MAP_ITER
Definition: pgm_base.h:114
virtual const wxString & GetEditorName(bool aCanShowFileChooser=true)
Return the preferred editor name.
Definition: pgm_base.cpp:155
ENV_VAR_ITEM(const wxString &aValue=wxEmptyString, bool aIsDefinedExternally=false)
Definition: pgm_base.h:87
LANGUAGE_DESCR LanguagesList[]
An array containing all the languages that KiCad supports.
Definition: pgm_base.cpp:76
bool SetDefaultLanguage(wxString &aErrMsg)
Set the default language without reference to any preferences.
Definition: pgm_base.cpp:684