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  // DBG( bases.Show( (std::string(__func__) + " bases").c_str() );)
102 
103  for( unsigned i = 0; i < bases.GetCount(); ++i )
104  {
105  wxFileName fn( bases[i], wxEmptyString );
106 
107  // Add KiCad template file path to search path list.
108  fn.AppendDir( wxT( "template" ) );
109  m_bm.m_search.AddPaths( fn.GetPath() );
110  }
111 
112  //DBG( m_bm.m_search.Show( (std::string( __func__ ) + " SysSearch()").c_str() );)
113  }
114 
115  // Must be called before creating the main frame in order to
116  // display the real hotkeys in menus or tool tips
117  extern struct EDA_HOTKEY_CONFIG kicad_Manager_Hokeys_Descr[];
118  ReadHotkeyConfig( KICAD_MANAGER_FRAME_NAME, kicad_Manager_Hokeys_Descr );
119 
120  KICAD_MANAGER_FRAME* frame = new KICAD_MANAGER_FRAME( NULL, wxT( "KiCad" ),
121  wxDefaultPosition, wxDefaultSize );
122  App().SetTopWindow( frame );
123 
124  Kiway.SetTop( frame );
125 
126  bool prjloaded = false; // true when the project is loaded
127 
128  if( App().argc > 1 )
129  frame->SetProjectFileName( App().argv[1] );
130 
131  else if( GetFileHistory().GetCount() )
132  {
133  wxString last_pro = GetFileHistory().GetHistoryFile( 0 );
134 
135  if( !wxFileExists( last_pro ) )
136  {
137  GetFileHistory().RemoveFileFromHistory( 0 );
138 
139  wxFileName namelessProject( wxStandardPaths::Get().GetDocumentsDir(), NAMELESS_PROJECT,
141 
142  frame->SetProjectFileName( namelessProject.GetFullPath() );
143  }
144  else
145  {
146  // Try to open the last opened project,
147  // if a project name is not given when starting Kicad
148  frame->SetProjectFileName( last_pro );
149 
150  wxCommandEvent cmd( 0, wxID_FILE1 );
151 
152  frame->OnFileHistory( cmd );
153  prjloaded = true; // OnFileHistory() loads the project
154  }
155  }
156  else // there is no history
157  {
158  wxFileName namelessProject( wxStandardPaths::Get().GetDocumentsDir(), NAMELESS_PROJECT,
160 
161  frame->SetProjectFileName( namelessProject.GetFullPath() );
162  }
163 
164  if( !prjloaded )
165  {
166  wxCommandEvent cmd( 0, wxID_ANY );
167 
168  frame->OnLoadProject( cmd );
169  }
170 
171  frame->Show( true );
172  frame->Raise();
173 
174  return true;
175 }
176 
177 
179 {
180  Kiway.OnKiwayEnd();
181 
183 
184  // write common settings to disk, and destroy everything in PGM_KICAD,
185  // especially wxSingleInstanceCheckerImpl earlier than wxApp and earlier
186  // than static destruction would.
187  Destroy();
188 }
189 
190 
191 void PGM_KICAD::MacOpenFile( const wxString& aFileName )
192 {
193 #if defined(__WXMAC__)
194 
195  KICAD_MANAGER_FRAME* frame = (KICAD_MANAGER_FRAME*) App().GetTopWindow();
196 
197  frame->SetProjectFileName( aFileName );
198 
199  wxCommandEvent loadEvent( 0, wxID_ANY );
200 
201  frame->OnLoadProject( loadEvent );
202 #endif
203 }
204 
205 
207 {
208  // unlike a normal destructor, this is designed to be called more
209  // than once safely:
210 
211  m_bm.End();
212 
214 }
215 
216 
218 
219 
224 struct APP_KICAD : public wxApp
225 {
226 #if defined (__LINUX__)
227  APP_KICAD(): wxApp()
228  {
229  // Disable proxy menu in Unity window manager. Only usual menubar works with wxWidgets (at least <= 3.1)
230  // When the proxy menu menubar is enable, some important things for us do not work: menuitems UI events and shortcuts.
231  wxString wm;
232 
233  if( wxGetEnv( wxT( "XDG_CURRENT_DESKTOP" ), &wm ) && wm.CmpNoCase( wxT( "Unity" ) ) == 0 )
234  {
235  wxSetEnv ( wxT("UBUNTU_MENUPROXY" ), wxT( "0" ) );
236  }
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  return wxApp::OnExit();
250  }
251 
252  int OnRun() override
253  {
254  try
255  {
256  return wxApp::OnRun();
257  }
258  catch( const std::exception& e )
259  {
260  wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
261  GetChars( FROM_UTF8( typeid(e).name() )),
262  GetChars( FROM_UTF8( e.what() ) ) );
263  }
264  catch( const IO_ERROR& ioe )
265  {
266  wxLogError( GetChars( ioe.What() ) );
267  }
268  catch(...)
269  {
270  wxLogError( wxT( "Unhandled exception of unknown type" ) );
271  }
272 
273  return -1;
274  }
275 
282  void MacOpenFile( const wxString& aFileName )
283  {
284  Pgm().MacOpenFile( aFileName );
285  }
286 };
287 
289 
290 
291 // The C++ project manager supports one open PROJECT, so Prj() calls within
292 // this link image need this function.
294 {
295  return Kiway.Prj();
296 }
297 
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:282
Class PROJECT holds project specific data.
Definition: project.h:52
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:245
void Init()
Definition: bin_mod.cpp:36
void SaveCommonSettings()
Function saveCommonSettings saves the program (process) settings subset which are stored ...
Definition: pgm_base.cpp:646
const wxString ProjectFileExtension
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:293
void MacOpenFile(const wxString &aFileName) override
Function MacOpenFile is specific to MacOSX (not used under Linux or Windows).
Definition: kicad.cpp:191
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:321
void Destroy()
Definition: pgm_base.cpp:306
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 OnLoadProject(wxCommandEvent &event)
Function OnLoadProject loads an exiting or creates a new project (.pro) file.
Definition: prjconfig.cpp:207
void OnKiwayEnd()
Definition: kiway.cpp:485
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
bool OnInit() override
Definition: kicad.cpp:240
void OnFileHistory(wxCommandEvent &event)
static PGM_KICAD program
Definition: kicad.cpp:62
void Destroy()
Definition: kicad.cpp:206
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:252
void SetProjectFileName(const wxString &aFullProjectProFileName)
Definition: mainframe.cpp:131
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:396
The common library.
const char * name
Some functions to handle hotkeys in KiCad.
wxFileHistory & GetFileHistory()
Definition: pgm_kicad.h:57
#define NAMELESS_PROJECT
default name for nameless projects
Definition: common.h:73
void OnPgmExit()
Definition: kicad.cpp:178
Struct APP_KICAD is not publicly visible because most of the action is in PGM_KICAD these days...
Definition: kicad.cpp:224
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
Class KICAD_MANAGER_FRAME is the main KiCad project manager frame.
Definition: kicad.h:137
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