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-2017 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 
47 #include "kicad.h"
48 
49 
50 // a dummy to quiet linking with EDA_BASE_FRAME::config();
51 #include <kiface_i.h>
53 {
54  // This function should never be called. It is only referenced from
55  // EDA_BASE_FRAME::config() and this is only provided to satisfy the linker,
56  // not to be actually called.
57  wxLogFatalError( wxT( "Unexpected call to Kiface() in kicad/kicad.cpp" ) );
58 
59  throw std::logic_error( "Unexpected call to Kiface() in kicad/kicad.cpp" );
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  m_bm.m_search.AddPaths( fn.GetPath() );
108  }
109  }
110 
111  // Must be called before creating the main frame in order to
112  // display the real hotkeys in menus or tool tips
113  extern struct EDA_HOTKEY_CONFIG kicad_Manager_Hokeys_Descr[];
114  ReadHotkeyConfig( KICAD_MANAGER_FRAME_NAME, kicad_Manager_Hokeys_Descr );
115 
116  KICAD_MANAGER_FRAME* frame = new KICAD_MANAGER_FRAME( NULL, wxT( "KiCad" ),
117  wxDefaultPosition, wxDefaultSize );
118  App().SetTopWindow( frame );
119 
120  Kiway.SetTop( frame );
121 
122  wxString projToLoad;
123 
124  if( App().argc > 1 )
125  {
126  projToLoad = App().argv[1];
127  }
128  else if( GetFileHistory().GetCount() )
129  {
130  wxString last_pro = GetFileHistory().GetHistoryFile( 0 );
131 
132  if( !wxFileExists( last_pro ) )
133  {
134  GetFileHistory().RemoveFileFromHistory( 0 );
135  }
136  else
137  {
138  // Try to open the last opened project,
139  // if a project name is not given when starting Kicad
140  projToLoad = last_pro;
141  }
142  }
143 
144  // Do not attempt to load a non-existent project file.
145  if( !projToLoad.empty() && wxFileExists( projToLoad ) )
146  {
147  frame->LoadProject( projToLoad );
148  }
149 
150  frame->Show( true );
151  frame->Raise();
152 
153  return true;
154 }
155 
156 
158 {
159  Kiway.OnKiwayEnd();
160 
162 
163  // write common settings to disk, and destroy everything in PGM_KICAD,
164  // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
165  // than static destruction would.
166  Destroy();
167 }
168 
169 
170 void PGM_KICAD::MacOpenFile( const wxString& aFileName )
171 {
172 #if defined(__WXMAC__)
173 
174  KICAD_MANAGER_FRAME* frame = (KICAD_MANAGER_FRAME*) App().GetTopWindow();
175 
176  if( !aFileName.empty() && wxFileExists( aFileName ) )
177  frame->LoadProject( wxFileName( aFileName ) );
178 
179 #endif
180 }
181 
182 
184 {
185  // unlike a normal destructor, this is designed to be called more
186  // than once safely:
187 
188  m_bm.End();
189 
191 }
192 
193 
195 
196 
201 struct APP_KICAD : public wxApp
202 {
203 #if defined (__LINUX__)
204  APP_KICAD(): wxApp()
205  {
206  // Disable proxy menu in Unity window manager. Only usual menubar works with wxWidgets (at least <= 3.1)
207  // When the proxy menu menubar is enable, some important things for us do not work: menuitems UI events and shortcuts.
208  wxString wm;
209 
210  if( wxGetEnv( wxT( "XDG_CURRENT_DESKTOP" ), &wm ) && wm.CmpNoCase( wxT( "Unity" ) ) == 0 )
211  {
212  wxSetEnv ( wxT("UBUNTU_MENUPROXY" ), wxT( "0" ) );
213  }
214  }
215 #endif
216 
217  bool OnInit() override
218  {
219  return program.OnPgmInit();
220  }
221 
222  int OnExit() override
223  {
224  program.OnPgmExit();
225 
226  return wxApp::OnExit();
227  }
228 
229  int OnRun() override
230  {
231  try
232  {
233  return wxApp::OnRun();
234  }
235  catch( const std::exception& e )
236  {
237  wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
238  GetChars( FROM_UTF8( typeid(e).name() )),
239  GetChars( FROM_UTF8( e.what() ) ) );
240  }
241  catch( const IO_ERROR& ioe )
242  {
243  wxLogError( GetChars( ioe.What() ) );
244  }
245  catch(...)
246  {
247  wxLogError( wxT( "Unhandled exception of unknown type" ) );
248  }
249 
250  return -1;
251  }
252 
259  void MacOpenFile( const wxString& aFileName )
260  {
261  Pgm().MacOpenFile( aFileName );
262  }
263 };
264 
266 
267 
268 // The C++ project manager supports one open PROJECT, so Prj() calls within
269 // this link image need this function.
271 {
272  return Kiway.Prj();
273 }
274 
void ReadHotkeyConfig(const wxString &Appname, struct EDA_HOTKEY_CONFIG *aDescList)
Function ReadHotkeyConfig Read hotkey configuration for a given app, possibly before the frame for th...
#define KFCTL_CPP_PROJECT_SUITE
Am running under C++ project mgr, possibly with others.
Definition: kiway.h:159
Class KIFACE_I is a KIFACE (I)mplementation, with some features useful for DSOs which implement a KIF...
Definition: kiface_i.h:37
Structure EDA_HOTKEY_CONFIG contains the information required to save hot key information to a config...
Definition: hotkeys_basic.h:87
void MacOpenFile(const wxString &aFileName)
Function MacOpenFile is specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:259
Class PROJECT holds project specific data.
Definition: project.h:56
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:53
int OnExit() override
Definition: kicad.cpp:222
void Init()
Definition: bin_mod.cpp:36
void SaveCommonSettings()
Function saveCommonSettings saves the program (process) settings subset which are stored ...
Definition: pgm_base.cpp:654
Class PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:107
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:270
void MacOpenFile(const wxString &aFileName) override
Function MacOpenFile is specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:170
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:323
void Destroy()
Definition: pgm_base.cpp:308
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:485
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
bool OnInit() override
Definition: kicad.cpp:217
static PGM_KICAD program
Definition: kicad.cpp:62
void Destroy()
Definition: kicad.cpp:183
VTBL_ENTRY void MacOpenFile(const wxString &aFileName)=0
Function MacOpenFile is specific to MacOSX (not used under Linux or Windows).
KICAD_MANAGER_FRAME is the KiCad main frame.
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:144
The common library.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:257
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Function SystemDirsAppend appends system places to aSearchStack in a platform specific way...
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:78
PGM_KICAD & PgmTop()
Definition: kicad.cpp:71
SEARCH_STACK m_search
Definition: bin_mod.h:61
int OnRun() override
Definition: kicad.cpp:229
BIN_MOD m_bm
Definition: pgm_kicad.h:72
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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
IMPLEMENT_APP(APP_KICAD)
bool InitPgm()
Function initPgm initializes this program (process) in a KiCad standard way, using some generalized t...
Definition: pgm_base.cpp:398
The common library.
const char * name
Some functions to handle hotkeys in KiCad.
void LoadProject(const wxFileName &aProjectFileName)
Definition: prjconfig.cpp:61
wxFileHistory & GetFileHistory()
Definition: pgm_kicad.h:57
void OnPgmExit()
Definition: kicad.cpp:157
Struct APP_KICAD is not publicly visible because most of the action is in PGM_KICAD these days...
Definition: kicad.cpp:201
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
The main KiCad project manager frame.
Definition: kicad.h:136
void AddPaths(const wxString &aPaths, int aIndex=-1)
Function AddPaths insert or append path(s)
#define KICAD_MANAGER_FRAME_NAME
Definition: kicad.h:40
KIWAY Kiway
void End()
Definition: bin_mod.cpp:53