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 (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 <string_utils.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, KIWAY* aKiway ) 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 kiface;
135}
136
137
142struct APP_TEST : public wxApp
143{
145 {
146 SetPgm( &program );
147 }
148
149 bool OnInit() override
150 {
151
152 try
153 {
154 if( !program.OnPgmInit() )
155 {
157 return false;
158 }
159
160
162 auto app = wxApp::GetInstance();
163 auto ret = c_util.HandleCommandLine( app->argc, app->argv );
164
165 if( ret != KI_TEST::RET_CODES::OK)
166 return false;
167
168 return true;
169 }
170 catch( const std::exception& e )
171 {
172 wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
173 From_UTF8( typeid(e).name() ), From_UTF8( e.what() ) );
174 }
175 catch( const IO_ERROR& ioe )
176 {
177 wxLogError( ioe.What() );
178 }
179 catch(...)
180 {
181 wxLogError( wxT( "Unhandled exception of unknown type" ) );
182 }
183
185
186 return false;
187 }
188
189 int OnExit() override
190 {
192 return wxApp::OnExit();
193 }
194
195 int OnRun() override
196 {
197 int ret = -1;
198
199 try
200 {
201 ret = wxApp::OnRun();
202 }
203 catch( const std::exception& e )
204 {
205 wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
206 From_UTF8( typeid(e).name() ),
207 From_UTF8( e.what() ) );
208 }
209 catch( const IO_ERROR& ioe )
210 {
211 wxLogError( ioe.What() );
212 }
213 catch(...)
214 {
215 wxLogError( wxT( "Unhandled exception of unknown type" ) );
216 }
217
219 return ret;
220 }
221};
222
223
225{
226 return true;
227}
228
229
230void SetTopFrame ( wxFrame* aFrame )
231{
232 Pgm().App().SetTopWindow( aFrame ); // wxApp gets a face.
233 aFrame->Show();
234}
235
236
238
239
240#ifndef TEST_APP_NO_MAIN
241
242int main( int argc, char** argv )
243{
244 wxInitialize( argc, argv );
245
246#ifdef TEST_APP_GUI
247 Pgm().InitPgm( false, true );
248#else
249 Pgm().InitPgm( true, true );
250#endif
251
252 auto ret = wxEntry( argc, argv );
253
254 // This causes some glib warnings on GTK3 (http://trac.wxwidgets.org/ticket/18274)
255 // but without it, Valgrind notices a lot of leaks from WX
256 wxUninitialize();
257
258 return ret;
259}
260
261#endif
const char * name
Definition: DXF_plotter.cpp:57
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
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:65
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:113
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:284
FACE_T
Known KIFACE implementations.
Definition: kiway.h:290
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:292
void OnKiwayEnd()
Definition: kiway.cpp:740
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:102
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:182
void Destroy()
Definition: pgm_base.cpp:170
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:457
This file is part of the common library.
main()
#define KFCTL_STANDALONE
Running as a standalone Top.
Definition: kiway.h:158
@ OK
Tool exited OK.
void SetPgm(PGM_BASE *pgm)
Definition: pgm_base.cpp:1072
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
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.