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 <[email protected]>
5 * Copyright (C) 2014-2022 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
52static 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* CreateKiWindow( 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}
89kiface( "pcb_test_frame", KIWAY::FACE_PCB );
90
91
93
94
95static struct PGM_TEST_FRAME : public PGM_BASE
96{
97 bool OnPgmInit();
98
99 void OnPgmExit()
100 {
101 printf("Destroy\n");
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}
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
156struct APP_TEST : public wxApp
157{
159 {}
160
161 bool OnInit() override
162 {
163
164 try
165 {
166 if( !program.OnPgmInit() )
167 {
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
197
198 return false;
199 }
200
201 int OnExit() override
202 {
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
231 return ret;
232 }
233};
234
235
237{
238 return true;
239}
240
241
242void 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
254int main( int argc, char** argv )
255{
256 wxInitialize( argc, argv );
257
258#ifdef TEST_APP_GUI
259 Pgm().InitPgm( false, true );
260#else
261 Pgm().InitPgm( true, 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
const char * name
Definition: DXF_plotter.cpp:56
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
A KIFACE implementation.
Definition: kiface_base.h:39
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:66
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 minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:274
FACE_T
Known KIFACE implementations.
Definition: kiway.h:280
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:282
void OnKiwayEnd()
Definition: kiway.cpp:683
Class that handles delegation of command lines to one of a number of "sub-utilities".
int HandleCommandLine(int argc, char **argv) const
Take in a command line and:
Container for data for KiCad programs.
Definition: pgm_base.h:95
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:163
void Destroy()
Definition: pgm_base.cpp:151
This file is part of the common library.
int PGM_BASE * aProgram
Definition: cvpcb.cpp:110
#define OK
KIWAY Kiway
#define KFCTL_STANDALONE
Running as a standalone Top.
Definition: kiway.h:158
This file contains miscellaneous commonly used macros and functions.
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
see class PGM_BASE
Implement a bare naked wxApp (so that we don't become dependent on functionality in a wxApp derivativ...
int OnExit() override
bool OnInit() override
int OnRun() override
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Typically start_common() is called from here.
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
IFACE(const char *aName, KIWAY::FACE_T aType)
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
IMPLEMENT_APP_NO_MAIN(APP_TEST)
int main(int argc, char **argv)
PGM_TEST_FRAME program
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
void SetTopFrame(wxFrame *aFrame)
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...