KiCad PCB EDA Suite
test_app_main.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) 2014 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2014 KiCad Developers, see CHANGELOG.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 
25 
26 /*
27 
28  This is a program launcher for a single KIFACE DSO. It only mimics a KIWAY,
29  not actually implements one, since only a single DSO is supported by it.
30 
31  It is compiled multiple times, once for each standalone program and as such
32  gets different compiler command line supplied #defines from CMake.
33 
34 */
35 
36 
37 #include <typeinfo>
38 #include <macros.h>
39 #include <wx/filename.h>
40 #include <wx/stdpaths.h>
41 #include <wx/snglinst.h>
42 #include <wx/html/htmlwin.h>
43 #include <pgm_base.h>
44 #include <kiface_i.h>
45 
46 #include <kiway.h>
47 #include <pgm_base.h>
48 #include <kiway_player.h>
49 #include <confirm.h>
52 
53 
54 static struct IFACE : public KIFACE_I
55 {
56  // Of course all are overloads, implementations of the KIFACE.
57 
58  IFACE( const char* aName, KIWAY::FACE_T aType ) :
59  KIFACE_I( aName, aType )
60  {}
61 
62  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override
63  {
64  return true;
65  }
66 
67  void OnKifaceEnd() override {}
68 
69  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
70  {
71  assert( false );
72  return nullptr;
73  }
74 
86  void* IfaceOrAddress( int aDataId ) override
87  {
88  return NULL;
89  }
90 }
91 kiface( "pcb_test_frame", KIWAY::FACE_PCB );
92 
94 
95 static struct PGM_TEST_FRAME : public PGM_BASE
96 {
97  bool OnPgmInit();
98 
99  void OnPgmExit()
100  {
101  printf("Destroy\n");
102  Kiway.OnKiwayEnd();
103 
104  // Destroy everything in PGM_BASE, especially wxSingleInstanceCheckerImpl
105  // earlier than wxApp and earlier than static destruction would.
107  }
108 
109 
110  void MacOpenFile( const wxString& aFileName ) override
111  {
112  wxFileName filename( aFileName );
113 
114  if( filename.FileExists() )
115  {
116  #if 0
117  // this pulls in EDA_DRAW_FRAME type info, which we don't want in
118  // the single_top link image.
119  KIWAY_PLAYER* frame = dynamic_cast<KIWAY_PLAYER*>( App().GetTopWindow() );
120  #else
121  KIWAY_PLAYER* frame = (KIWAY_PLAYER*) App().GetTopWindow();
122  #endif
123 
124  if( frame )
125  frame->OpenProjectFiles( std::vector<wxString>( 1, aFileName ) );
126  }
127  }
128 }
129 program;
130 
132 {
133  return program;
134 }
135 
136 
137 // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face
138 // is run from a python script, mot from a Kicad application
140 {
141  return &program;
142 }
143 
144 
146 {
147  return kiface;
148 }
149 
155 struct APP_TEST : public wxApp
156 {
158  {}
159 
160  bool OnInit() override
161  {
162 
163  try
164  {
165  if( !program.OnPgmInit() )
166  {
167  program.OnPgmExit();
168  return false;
169  }
170 
171 
173  auto app = wxApp::GetInstance();
174  auto ret = c_util.HandleCommandLine( app->argc, app->argv );
175 
176  if( ret != KI_TEST::RET_CODES::OK)
177  return false;
178 
179  return true;
180  }
181  catch( const std::exception& e )
182  {
183  wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
184  FROM_UTF8( typeid(e).name() ),
185  FROM_UTF8( e.what() ) );
186  }
187  catch( const IO_ERROR& ioe )
188  {
189  wxLogError( ioe.What() );
190  }
191  catch(...)
192  {
193  wxLogError( wxT( "Unhandled exception of unknown type" ) );
194  }
195 
196  program.OnPgmExit();
197 
198 
199 
200  return false;
201  }
202 
203  int OnExit() override
204  {
205  program.OnPgmExit();
206  return wxApp::OnExit();
207  }
208 
209  int OnRun() override
210  {
211  int ret = -1;
212 
213  try
214  {
215  ret = wxApp::OnRun();
216  }
217  catch( const std::exception& e )
218  {
219  wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
220  FROM_UTF8( typeid(e).name() ),
221  FROM_UTF8( e.what() ) );
222  }
223  catch( const IO_ERROR& ioe )
224  {
225  wxLogError( ioe.What() );
226  }
227  catch(...)
228  {
229  wxLogError( wxT( "Unhandled exception of unknown type" ) );
230  }
231 
232  program.OnPgmExit();
233  return ret;
234  }
235 };
236 
237 
239 {
240  if( !InitPgm() )
241  return false;
242 
243  return true;
244 }
245 
246 void SetTopFrame ( wxFrame* aFrame )
247 {
248  Pgm().App().SetTopWindow( aFrame ); // wxApp gets a face.
249  aFrame->Show();
250 }
251 
253 
254 #ifndef TEST_APP_NO_MAIN
255 
256 int main( int argc, char** argv )
257 {
258  wxInitialize( argc, argv );
259 
260  Pgm().InitPgm();
261 
262  auto ret = wxEntry( argc, argv );
263 
264  // This causes some glib warnings on GTK3 (http://trac.wxwidgets.org/ticket/18274)
265  // but without it, Valgrind notices a lot of leaks from WX
266  wxUninitialize();
267 
268  return ret;
269 }
270 
271 #endif
virtual bool OpenProjectFiles(const std::vector< wxString > &aFileList, int aCtl=0)
Open a project or set of files given by aFileList.
Definition: kiway_player.h:115
A KIFACE (I)mplementation.
Definition: kiface_i.h:37
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:61
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
void SetTopFrame(wxFrame *aFrame)
Struct APP_SINGLE_TOP implements a bare naked wxApp (so that we don't become dependent on functionali...
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Typically start_common() is called from here.
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
This file is part of the common library.
Container for data for KiCad programs.
Definition: pgm_base.h:131
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
#define OK
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:141
void Destroy()
Definition: pgm_base.cpp:130
wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
IFACE(const char *aName, KIWAY::FACE_T aType)
void OnKiwayEnd()
Definition: kiway.cpp:602
int main(int argc, char **argv)
Class that handles delegation of command lines to one of a number of "sub-utilities".
This file contains miscellaneous commonly used macros and functions.
PGM_TEST_FRAME program
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
int OnRun() override
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
pcbnew DSO
Definition: kiway.h:269
void * IfaceOrAddress(int aDataId) override
Function IfaceOrAddress return a pointer to the requested object.
FACE_T
Known KIFACE implementations.
Definition: kiway.h:266
see class PGM_BASE
const char * name
Definition: DXF_plotter.cpp:59
int HandleCommandLine(int argc, char **argv) const
Take in a a command line and:
bool InitPgm()
Initialize this program.
Definition: pgm_base.cpp:216
IFACE KIFACE_I kiface("pcb_test_frame", KIWAY::FACE_PCB)
int OnExit() override
IMPLEMENT_APP_NO_MAIN(APP_TEST)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
int PGM_BASE * aProgram
Definition: cvpcb.cpp:97
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
#define KFCTL_STANDALONE
Running as a standalone Top.
Definition: kiway.h:155
KIWAY Kiway
bool OnInit() override