1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22  */
23 
24 #include <pgm_base.h>
25 #include <kiface_base.h>
26 #include <kiway.h>
27 #include <kiway_player.h>
28 
29 #include <wx/app.h>
30 
31 
32 static struct IFACE : public KIFACE_BASE
33 {
34     // Of course all are overloads, implementations of the KIFACE.
35 
IFACEIFACE36     IFACE( const char* aName, KIWAY::FACE_T aType ) :
37             KIFACE_BASE( aName, aType )
38     {}
39 
OnKifaceStartIFACE40     bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override
41     {
42         return true;
43     }
44 
OnKifaceEndIFACE45     void OnKifaceEnd() override {}
46 
CreateWindowIFACE47     wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
48     {
49         assert( false );
50         return nullptr;
51     }
52 
53     /**
54      * Function IfaceOrAddress
55      * return a pointer to the requested object.  The safest way to use this
56      * is to retrieve a pointer to a static instance of an interface, similar to
57      * how the KIFACE interface is exported.  But if you know what you are doing
58      * use it to retrieve anything you want.
59      *
60      * @param aDataId identifies which object you want the address of.
61      *
62      * @return void* - and must be cast into the know type.
63      */
IfaceOrAddressIFACE64     void* IfaceOrAddress( int aDataId ) override
65     {
66         return NULL;
67     }
68 }
69         kiface( "pcb_test_frame", KIWAY::FACE_PCB );
70 
71 static struct PGM_TEST_FRAME : public PGM_BASE
72 {
73     bool OnPgmInit();
74 
OnPgmExitPGM_TEST_FRAME75     void OnPgmExit()
76     {
77         Kiway.OnKiwayEnd();
78 
79         // Destroy everything in PGM_BASE, especially wxSingleInstanceCheckerImpl
80         // earlier than wxApp and earlier than static destruction would.
81         PGM_BASE::Destroy();
82     }
83 
MacOpenFilePGM_TEST_FRAME84     void MacOpenFile( const wxString& aFileName )   override
85     {
86         wxFileName filename( aFileName );
87 
88         if( filename.FileExists() )
89         {
90 #if 0
91             // this pulls in EDA_DRAW_FRAME type info, which we don't want in
92             // the single_top link image.
93             KIWAY_PLAYER* frame = dynamic_cast<KIWAY_PLAYER*>( App().GetTopWindow() );
94 #else
95             KIWAY_PLAYER* frame = (KIWAY_PLAYER*) App().GetTopWindow();
96 #endif
97 
98             if( frame )
99                 frame->OpenProjectFiles( std::vector<wxString>( 1, aFileName ) );
100         }
101     }
102 }
103         program;
104 
Pgm()105 PGM_BASE& Pgm()
106 {
107     return program;
108 }
109 
110 
111 // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face
112 // is run from a python script, mot from a Kicad application
PgmOrNull()113 PGM_BASE* PgmOrNull()
114 {
115     return &program;
116 }
117 
118 
Kiface()119 KIFACE_BASE& Kiface()
120 {
121     return kiface;
122 }
123 
124 
125 class MOCK_PLAYER : public KIWAY_PLAYER
126 {
127 public:
MOCK_PLAYER(KIWAY * aKiway)128     MOCK_PLAYER( KIWAY *aKiway ) :
129             KIWAY_PLAYER( aKiway, nullptr, FRAME_SCH, wxT( "Mock" ), wxDefaultPosition,
130             wxDefaultSize, wxDEFAULT_FRAME_STYLE )
131     {}
132 
133     ~MOCK_PLAYER();
134 };