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-2021 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 
25 
26 /*
27  * This is a program launcher for a single KIFACE DSO. It only mimics a KIWAY,
28  * not actually implements one, since only a single DSO is supported by it.
29  *
30  * It is compiled multiple times, once for each standalone program and as such
31  * gets different compiler command line supplied #defines from CMake.
32  */
33 
34 
35 #include <typeinfo>
36 #include <macros.h>
37 #include <wx/filename.h>
38 #include <wx/stdpaths.h>
39 #include <wx/snglinst.h>
40 #include <wx/html/htmlwin.h>
41 #include <pgm_base.h>
42 #include <kiface_base.h>
43 
44 #include <kiway.h>
45 #include <pgm_base.h>
46 #include <kiway_player.h>
47 #include <confirm.h>
50 
51 
52 static struct IFACE : public KIFACE_BASE
53 {
54  // Of course all are overloads, implementations of the KIFACE.
55 
56  IFACE( const char* aName, KIWAY::FACE_T aType ) :
57  KIFACE_BASE( aName, aType )
58  {}
59 
60  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override
61  {
62  return true;
63  }
64 
65  void OnKifaceEnd() override {}
66 
67  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
68  int aCtlBits = 0 ) override
69  {
70  assert( false );
71  return nullptr;
72  }
73 
84  void* IfaceOrAddress( int aDataId ) override
85  {
86  return nullptr;
87  }
88 }
89 kiface( "pcb_test_frame", KIWAY::FACE_PCB );
90 
91 
93 
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 
131 
133 {
134  return program;
135 }
136 
137 
138 // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face
139 // is run from a python script, mot from a Kicad application
141 {
142  return &program;
143 }
144 
145 
147 {
148  return kiface;
149 }
150 
151 
156 struct APP_TEST : public wxApp
157 {
159  {}
160 
161  bool OnInit() override
162  {
163 
164  try
165  {
166  if( !program.OnPgmInit() )
167  {
168  program.OnPgmExit();
169  return false;
170  }
171 
172 
174  auto app = wxApp::GetInstance();
175  auto ret = c_util.HandleCommandLine( app->argc, app->argv );
176 
177  if( ret != KI_TEST::RET_CODES::OK)
178  return false;
179 
180  return true;
181  }
182  catch( const std::exception& e )
183  {
184  wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
185  FROM_UTF8( typeid(e).name() ), 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  return false;
199  }
200 
201  int OnExit() override
202  {
203  program.OnPgmExit();
204  return wxApp::OnExit();
205  }
206 
207  int OnRun() override
208  {
209  int ret = -1;
210 
211  try
212  {
213  ret = wxApp::OnRun();
214  }
215  catch( const std::exception& e )
216  {
217  wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
218  FROM_UTF8( typeid(e).name() ),
219  FROM_UTF8( e.what() ) );
220  }
221  catch( const IO_ERROR& ioe )
222  {
223  wxLogError( ioe.What() );
224  }
225  catch(...)
226  {
227  wxLogError( wxT( "Unhandled exception of unknown type" ) );
228  }
229 
230  program.OnPgmExit();
231  return ret;
232  }
233 };
234 
235 
237 {
238  return true;
239 }
240 
241 
242 void SetTopFrame ( wxFrame* aFrame )
243 {
244  Pgm().App().SetTopWindow( aFrame ); // wxApp gets a face.
245  aFrame->Show();
246 }
247 
248 
250 
251 
252 #ifndef TEST_APP_NO_MAIN
253 
254 int main( int argc, char** argv )
255 {
256  wxInitialize( argc, argv );
257 
258 #ifdef TEST_APP_GUI
259  Pgm().InitPgm( false );
260 #else
261  Pgm().InitPgm( true );
262 #endif
263 
264  auto ret = wxEntry( argc, argv );
265 
266  // This causes some glib warnings on GTK3 (http://trac.wxwidgets.org/ticket/18274)
267  // but without it, Valgrind notices a lot of leaks from WX
268  wxUninitialize();
269 
270  return ret;
271 }
272 
273 #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:118
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:64
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
void SetTopFrame(wxFrame *aFrame)
Implement a bare naked wxApp (so that we don't become dependent on functionality in a wxApp derivativ...
A KIFACE implementation.
Definition: kiface_base.h:37
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:93
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:131
void Destroy()
Definition: pgm_base.cpp:123
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:640
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
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
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
Return a pointer to the requested object.
FACE_T
Known KIFACE implementations.
Definition: kiway.h:266
see class PGM_BASE
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
const char * name
Definition: DXF_plotter.cpp:56
int HandleCommandLine(int argc, char **argv) const
Take in a command line and:
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:110
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
#define KFCTL_STANDALONE
Running as a standalone Top.
Definition: kiway.h:156
KIWAY Kiway
bool OnInit() override