KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21
22/*
23 * This is a program launcher for a single KIFACE DSO. It only mimics a KIWAY,
24 * not actually implements one, since only a single DSO is supported by it.
25 *
26 * It is compiled multiple times, once for each standalone program and as such
27 * gets different compiler command line supplied #defines from CMake.
28 */
29
31#include <typeinfo>
32#include <string_utils.h>
33#include <wx/filename.h>
34#include <wx/stdpaths.h>
35#include <wx/snglinst.h>
36#include <wx/html/htmlwin.h>
37#include <pgm_base.h>
38#include <kiface_base.h>
39
40#include <kiway.h>
41#include <pgm_base.h>
42#include <kiway_player.h>
43#include <confirm.h>
46
47
48static struct IFACE : public KIFACE_BASE
49{
50 // Of course all are overloads, implementations of the KIFACE.
51
52 IFACE( const char* aName, KIWAY::FACE_T aType ) :
53 KIFACE_BASE( aName, aType )
54 {}
55
56 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override
57 {
58 return true;
59 }
60
61 void OnKifaceEnd() override {}
62
63 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
64 int aCtlBits = 0 ) override
65 {
66 assert( false );
67 return nullptr;
68 }
69
80 void* IfaceOrAddress( int aDataId ) override
81 {
82 return nullptr;
83 }
84}
85kiface( "pcb_test_frame", KIWAY::FACE_PCB );
86
87
89
90
91static struct PGM_TEST_FRAME : public PGM_BASE
92{
93 bool OnPgmInit();
94
95 void OnPgmExit()
96 {
97 printf("Destroy\n");
98 Kiway.OnKiwayEnd();
99
100 // Destroy everything in PGM_BASE, especially wxSingleInstanceCheckerImpl
101 // earlier than wxApp and earlier than static destruction would.
103 }
104
105
106 void MacOpenFile( const wxString& aFileName ) override
107 {
108 wxFileName filename( aFileName );
109
110 if( filename.FileExists() )
111 {
112 #if 0
113 // this pulls in EDA_DRAW_FRAME type info, which we don't want in
114 // the single_top link image.
115 KIWAY_PLAYER* frame = dynamic_cast<KIWAY_PLAYER*>( App().GetTopWindow() );
116 #else
117 KIWAY_PLAYER* frame = (KIWAY_PLAYER*) App().GetTopWindow();
118 #endif
119
120 if( frame )
121 frame->OpenProjectFiles( std::vector<wxString>( 1, aFileName ) );
122 }
123 }
124}
126
127
129{
130 return kiface;
131}
132
133
138struct APP_TEST : public wxApp
139{
141 {
142 SetPgm( &program );
143 }
144
145 bool OnInit() override
146 {
147 try
148 {
149 if( !program.OnPgmInit() )
150 {
151 program.OnPgmExit();
152 return false;
153 }
154
155
157 auto app = wxApp::GetInstance();
158 auto ret = c_util.HandleCommandLine( app->argc, app->argv );
159
160 if( ret != KI_TEST::RET_CODES::OK)
161 return false;
162
163 return true;
164 }
165 catch( const std::exception& e )
166 {
167 wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
168 From_UTF8( typeid(e).name() ), From_UTF8( e.what() ) );
169 }
170 catch(...)
171 {
172 wxLogError( wxT( "Unhandled exception of unknown type" ) );
173 }
174
175 return false;
176 }
177
178 int OnExit() override
179 {
180 // Drain wxPendingDelete (frames deferred via Destroy()) before tearing down
181 // PGM_BASE singletons. Kept consistent with the GUI apps; see
182 // https://gitlab.com/kicad/code/kicad/-/issues/23373
183 int ret = wxApp::OnExit();
184 program.OnPgmExit();
185 return ret;
186 }
187
188 int OnRun() override
189 {
190 int ret = -1;
191
192 try
193 {
194 ret = wxApp::OnRun();
195 }
196 catch( const std::exception& e )
197 {
198 wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
199 From_UTF8( typeid(e).name() ),
200 From_UTF8( e.what() ) );
201 }
202 catch(...)
203 {
204 wxLogError( wxT( "Unhandled exception of unknown type" ) );
205 }
206
207 program.OnPgmExit();
208 return ret;
209 }
210};
211
212
214{
215 return true;
216}
217
218
219void SetTopFrame ( wxFrame* aFrame )
220{
221 Pgm().App().SetTopWindow( aFrame ); // wxApp gets a face.
222 aFrame->Show();
223}
224
225
227
228
229#ifndef TEST_APP_NO_MAIN
230
231int main( int argc, char** argv )
232{
233 wxInitialize( argc, argv );
234
235#ifdef TEST_APP_GUI
236 Pgm().InitPgm( false );
237#else
238 Pgm().InitPgm( true );
239#endif
240
241 auto ret = wxEntry( argc, argv );
242
243 // This causes some glib warnings on GTK3 (http://trac.wxwidgets.org/ticket/18274)
244 // but without it, Valgrind notices a lot of leaks from WX
245 wxUninitialize();
246
247 return ret;
248}
249
250#endif
const char * name
A KIFACE implementation.
Definition kiface_base.h:35
KIFACE_BASE(const char *aKifaceName, KIWAY::FACE_T aId)
Definition kiface_base.h:63
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
virtual bool OpenProjectFiles(const std::vector< wxString > &aFileList, int aCtl=0)
Open a project or set of files given by aFileList.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:311
FACE_T
Known KIFACE implementations.
Definition kiway.h:317
@ FACE_PCB
pcbnew DSO
Definition kiway.h:319
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:104
virtual wxApp & App()
Return a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition pgm_base.cpp:207
void Destroy()
Definition pgm_base.cpp:186
bool InitPgm(bool aHeadless=false, bool aIsUnitTest=false)
Initialize this program.
Definition pgm_base.cpp:323
This file is part of the common library.
#define KFCTL_STANDALONE
Running as a standalone Top.
Definition kiway.h:159
@ OK
Tool exited OK.
void SetPgm(PGM_BASE *pgm)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
PGM_SINGLE_TOP program
wxString From_UTF8(const char *cstring)
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, KIWAY *aKiway) 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)
PGM_TEST_FRAME program
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
KIWAY Kiway(KFCTL_STANDALONE)
void SetTopFrame(wxFrame *aFrame)
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.