KiCad PCB EDA Suite
kicad.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) 2004-2018 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 
31 #include <wx/filename.h>
32 #include <wx/log.h>
33 #include <wx/stdpaths.h>
34 #include <wx/string.h>
35 
36 #include <common.h>
37 #include <hotkeys_basic.h>
38 #include <kiway.h>
39 #include <richio.h>
41 #include <systemdirsappend.h>
42 
43 #include <stdexcept>
44 
45 #include "pgm_kicad.h"
46 #include "kicad_manager_frame.h"
47 
48 
49 // a dummy to quiet linking with EDA_BASE_FRAME::config();
50 #include <kiface_i.h>
52 {
53  // This function should never be called. It is only referenced from
54  // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
55  // not to be actually called.
56  wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
57 
58  throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
59 }
60 
61 
63 
64 
66 {
67  return program;
68 }
69 
70 
72 {
73  return program;
74 }
75 
76 
78 {
79 #if defined(DEBUG)
80  wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
81 
82  if( !wxIsAbsolutePath( absoluteArgv0 ) )
83  {
84  wxLogError( wxT( "No meaningful argv[0]" ) );
85  return false;
86  }
87 #endif
88 
89  if( !InitPgm() )
90  return false;
91 
92  m_bm.Init();
93 
94  // Add search paths to feed the PGM_KICAD::SysSearch() function,
95  // currenly limited in support to only look for project templates
96  {
97  SEARCH_STACK bases;
98 
99  SystemDirsAppend( &bases );
100 
101  for( unsigned i = 0; i < bases.GetCount(); ++i )
102  {
103  wxFileName fn( bases[i], wxEmptyString );
104 
105  // Add KiCad template file path to search path list.
106  fn.AppendDir( wxT( "template" ) );
107 
108  // Only add path if exists and can be read by the user.
109  if( fn.DirExists() && fn.IsDirReadable() )
110  m_bm.m_search.AddPaths( fn.GetPath() );
111  }
112 
113  // The KICAD_TEMPLATE_DIR takes precedence over the search stack template path.
114  ENV_VAR_MAP_CITER it = GetLocalEnvVariables().find( "KICAD_TEMPLATE_DIR" );
115 
116  if( it != GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString )
117  m_bm.m_search.Insert( it->second.GetValue(), 0 );
118 
119  // The KICAD_USER_TEMPLATE_DIR takes precedence over KICAD_TEMPLATE_DIR and the search
120  // stack template path.
121  it = GetLocalEnvVariables().find( "KICAD_USER_TEMPLATE_DIR" );
122 
123  if( it != GetLocalEnvVariables().end() && it->second.GetValue() != wxEmptyString )
124  m_bm.m_search.Insert( it->second.GetValue(), 0 );
125  }
126 
127  KICAD_MANAGER_FRAME* frame = new KICAD_MANAGER_FRAME( NULL, wxT( "KiCad" ),
128  wxDefaultPosition, wxSize( 775, -1 ) );
129  App().SetTopWindow( frame );
130 
131  Kiway.SetTop( frame );
132 
133  wxString projToLoad;
134 
135  if( App().argc > 1 )
136  {
137  projToLoad = App().argv[1];
138  }
139  else if( GetFileHistory().GetCount() )
140  {
141  wxString last_pro = GetFileHistory().GetHistoryFile( 0 );
142 
143  if( !wxFileExists( last_pro ) )
144  {
145  GetFileHistory().RemoveFileFromHistory( 0 );
146  }
147  else
148  {
149  // Try to open the last opened project,
150  // if a project name is not given when starting Kicad
151  projToLoad = last_pro;
152  }
153  }
154 
155  // Do not attempt to load a non-existent project file.
156  if( !projToLoad.empty() && wxFileExists( projToLoad ) )
157  {
158  frame->LoadProject( projToLoad );
159  }
160 
161  frame->Show( true );
162  frame->Raise();
163 
164  return true;
165 }
166 
167 
169 {
170  Kiway.OnKiwayEnd();
171 
173 
174  // write common settings to disk, and destroy everything in PGM_KICAD,
175  // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
176  // than static destruction would.
177  Destroy();
178 }
179 
180 
181 void PGM_KICAD::MacOpenFile( const wxString& aFileName )
182 {
183 #if defined(__WXMAC__)
184 
185  KICAD_MANAGER_FRAME* frame = (KICAD_MANAGER_FRAME*) App().GetTopWindow();
186 
187  if( !aFileName.empty() && wxFileExists( aFileName ) )
188  frame->LoadProject( wxFileName( aFileName ) );
189 
190 #endif
191 }
192 
193 
195 {
196  // unlike a normal destructor, this is designed to be called more
197  // than once safely:
198 
199  m_bm.End();
200 
202 }
203 
204 
206 
207 
212 struct APP_KICAD : public wxApp
213 {
214 #if defined (__LINUX__)
215  APP_KICAD(): wxApp()
216  {
217  // Disable proxy menu in Unity window manager. Only usual menubar works with
218  // wxWidgets (at least <= 3.1). When the proxy menu menubar is enable, some
219  // important things for us do not work: menuitems UI events and shortcuts.
220  wxString wm;
221 
222  if( wxGetEnv( wxT( "XDG_CURRENT_DESKTOP" ), &wm ) && wm.CmpNoCase( wxT( "Unity" ) ) == 0 )
223  {
224  wxSetEnv ( wxT("UBUNTU_MENUPROXY" ), wxT( "0" ) );
225  }
226 
227  // Force the use of X11 backend (or wayland-x11 compatibilty layer). This is required until wxWidgets
228  // supports the Wayland compositors
229  wxSetEnv( wxT( "GDK_BACKEND" ), wxT( "x11" ) );
230 
231  // Disable overlay scrollbars as they mess up wxWidgets window sizing and cause excessive redraw requests
232  wxSetEnv( wxT( "GTK_OVERLAY_SCROLLING" ), wxT( "0" ) );
233 
234  // Set GTK2-style input instead of xinput2. This disables touchscreen and smooth scrolling
235  // Needed to ensure that we are not getting multiple mouse scroll events
236  wxSetEnv( wxT( "GDK_CORE_DEVICE_EVENTS" ), wxT( "1" ) );
237  }
238 #endif
239 
240  bool OnInit() override
241  {
242  return program.OnPgmInit();
243  }
244 
245  int OnExit() override
246  {
247  program.OnPgmExit();
248 
249 #if defined(__FreeBSD__)
250  /* Avoid wxLog crashing when used in destructors. */
251  wxLog::EnableLogging( false );
252 #endif
253 
254  return wxApp::OnExit();
255  }
256 
257  int OnRun() override
258  {
259  try
260  {
261  return wxApp::OnRun();
262  }
263  catch( const std::exception& e )
264  {
265  wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
266  GetChars( FROM_UTF8( typeid(e).name() )),
267  GetChars( FROM_UTF8( e.what() ) ) );
268  }
269  catch( const IO_ERROR& ioe )
270  {
271  wxLogError( GetChars( ioe.What() ) );
272  }
273  catch(...)
274  {
275  wxLogError( wxT( "Unhandled exception of unknown type" ) );
276  }
277 
278  return -1;
279  }
280 
286  void MacOpenFile( const wxString& aFileName )
287  {
288  Pgm().MacOpenFile( aFileName );
289  }
290 };
291 
292 IMPLEMENT_APP( APP_KICAD )
293 
294 
295 // The C++ project manager supports one open PROJECT, so Prj() calls within
296 // this link image need this function.
298 {
299  return Kiway.Prj();
300 }
301 
#define KFCTL_CPP_PROJECT_SUITE
Am running under C++ project mgr, possibly with others.
Definition: kiway.h:160
Class KIFACE_I is a KIFACE (I)mplementation, with some features useful for DSOs which implement a KIF...
Definition: kiface_i.h:37
FILE_HISTORY & GetFileHistory()
Definition: pgm_kicad.h:57
void MacOpenFile(const wxString &aFileName)
Set MacOS file associations.
Definition: kicad.cpp:286
Class PROJECT holds project specific data.
Definition: project.h:58
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:62
int OnExit() override
Definition: kicad.cpp:245
void Init()
Definition: bin_mod.cpp:38
void SaveCommonSettings()
Function saveCommonSettings saves the program (process) settings subset which are stored ....
Definition: pgm_base.cpp:622
Class PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:166
Class PGM_KICAD extends PGM_BASE to bring in FileHistory() and PdfBrowser() which were moved from EDA...
Definition: pgm_kicad.h:40
PROJECT & Prj()
Definition: kicad.cpp:297
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:163
void MacOpenFile(const wxString &aFileName) override
Function MacOpenFile is specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:181
VTBL_ENTRY wxApp & App()
Function App returns a bare naked wxApp, which may come from wxPython, SINGLE_TOP,...
Definition: pgm_base.cpp:164
void Destroy()
Definition: pgm_base.cpp:151
System directories search utilities.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
bool OnPgmInit()
Definition: kicad.cpp:77
void OnKiwayEnd()
Definition: kiway.cpp:517
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:51
bool OnInit() override
Definition: kicad.cpp:240
static PGM_KICAD program
Definition: kicad.cpp:62
void Destroy()
Definition: kicad.cpp:194
VTBL_ENTRY void MacOpenFile(const wxString &aFileName)=0
Function MacOpenFile is specific to MacOSX (not used under Linux or Windows).
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
std::map< wxString, ENV_VAR_ITEM >::const_iterator ENV_VAR_MAP_CITER
Definition: pgm_base.h:148
Definition of file extensions used in Kicad.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Function SystemDirsAppend appends system places to aSearchStack in a platform specific way,...
VTBL_ENTRY const ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.h:325
void SetTop(wxFrame *aTop)
Function SetTop tells this KIWAY about the top most frame in the program and optionally allows it to ...
Definition: kiway.cpp:77
PGM_KICAD & PgmTop()
Definition: kicad.cpp:71
SEARCH_STACK m_search
Definition: bin_mod.h:63
int OnRun() override
Definition: kicad.cpp:257
BIN_MOD m_bm
Definition: pgm_kicad.h:72
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:101
const char * name
Definition: DXF_plotter.cpp:61
size_t i
Definition: json11.cpp:597
bool InitPgm()
Function initPgm initializes this program (process) in a KiCad standard way, using some generalized t...
Definition: pgm_base.cpp:239
The common library.
void LoadProject(const wxFileName &aProjectFileName)
void OnPgmExit()
Definition: kicad.cpp:168
Struct APP_KICAD is not publicly visible because most of the action is in PGM_KICAD these days.
Definition: kicad.cpp:212
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
The main KiCad project manager frame.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Function AddPaths insert or append path(s)
KIWAY Kiway
void End()
Definition: bin_mod.cpp:63