KiCad PCB EDA Suite
kiway.h
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) 2016-2020 KiCad Developers, see CHANGELOG.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 #ifndef KIWAY_H_
26 #define KIWAY_H_
27 
98 #include <wx/event.h>
99 #include <wx/dynlib.h>
100 #include <import_export.h>
101 #include <search_stack.h>
102 #include <project.h>
103 #include <frame_type.h>
104 #include <mail_type.h>
105 #include <ki_exception.h>
106 
107 
108 #define KIFACE_VERSION 1
109 #define KIFACE_GETTER KIFACE_1
110 
111 // The KIFACE acquisition function is declared extern "C" so its name should not
112 // be mangled.
113 #define KIFACE_INSTANCE_NAME_AND_VERSION "KIFACE_1"
114 
115 #ifndef SWIG
116 #if defined(__linux__) || defined(__FreeBSD__)
117  #define LIB_ENV_VAR wxT( "LD_LIBRARY_PATH" )
118 #elif defined(__WXMAC__)
119  #define LIB_ENV_VAR wxT( "DYLD_LIBRARY_PATH" )
120 #elif defined(_WIN32)
121  #define LIB_ENV_VAR wxT( "PATH" )
122 #else
123  #error Platform support missing
124 #endif
125 #endif // SWIG
126 
127 class wxConfigBase;
128 class wxWindow;
129 class PGM_BASE;
130 class KIWAY;
131 class KIWAY_PLAYER;
132 class wxTopLevelWindow;
133 
134 
147 struct KIFACE
148 {
149  // The order of functions establishes the vtable sequence, do not change the
150  // order of functions in this listing unless you recompile all clients of
151  // this interface.
152 
153  virtual ~KIFACE() throw() {}
154 
155 #define KFCTL_STANDALONE (1<<0)
156 #define KFCTL_CPP_PROJECT_SUITE (1<<1)
157 #define KFCTL_PY_PROJECT_SUITE (1<<2)
158 
159 
174  virtual bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) = 0;
175 
182  virtual void OnKifaceEnd() = 0;
183 
200  virtual wxWindow* CreateWindow( wxWindow* aParent, int aClassId,
201  KIWAY* aKIWAY, int aCtlBits = 0 ) = 0;
202 
208  virtual void SaveFileAs( const wxString& srcProjectBasePath,
209  const wxString& srcProjectName,
210  const wxString& newProjectBasePath,
211  const wxString& newProjectName,
212  const wxString& srcFilePath,
213  wxString& aErrors )
214  {
215  // If a KIFACE owns files then it needs to implement this....
216  }
217 
229  virtual void* IfaceOrAddress( int aDataId ) = 0;
230 };
231 
232 
260 class KIWAY : public wxEvtHandler
261 {
262  friend struct PGM_SINGLE_TOP; // can use set_kiface()
263 
264 public:
266  enum FACE_T
267  {
275 
277  };
278 
279  ~KIWAY() throw () {}
280 
287  static FACE_T KifaceType( FRAME_T aFrameType );
288 
289  // If you change the vtable, recompile all of KiCad.
290 
297  virtual KIFACE* KiFACE( FACE_T aFaceId, bool doLoad = true );
298 
318  virtual KIWAY_PLAYER* Player( FRAME_T aFrameType, bool doCreate = true,
319  wxTopLevelWindow* aParent = nullptr );
320 
329  virtual bool PlayerClose( FRAME_T aFrameType, bool doForce );
330 
340  virtual bool PlayersClose( bool doForce );
341 
348  virtual void ExpressMail( FRAME_T aDestination, MAIL_T aCommand, std::string& aPayload,
349  wxWindow* aSource = nullptr );
350 
357  virtual PROJECT& Prj() const;
358 
362  virtual void SetLanguage( int aLanguage );
363 
369  virtual void CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged );
370 
375  virtual void ProjectChanged();
376 
377  KIWAY( PGM_BASE* aProgram, int aCtlBits, wxFrame* aTop = nullptr );
378 
385  void SetTop( wxFrame* aTop );
386 
387  void OnKiCadExit();
388 
389  void OnKiwayEnd();
390 
391  bool ProcessEvent( wxEvent& aEvent ) override;
392 
393 private:
395  const wxString dso_search_path( FACE_T aFaceId );
396 
397 #if 0
398  void player_destroy_handler( wxWindowDestroyEvent& event );
400 #endif
401 
402  bool set_kiface( FACE_T aFaceType, KIFACE* aKiface )
403  {
404  if( (unsigned) aFaceType < (unsigned) KIWAY_FACE_COUNT )
405  {
406  m_kiface[aFaceType] = aKiface;
407  return true;
408  }
409 
410  return false;
411  }
412 
417  KIWAY_PLAYER* GetPlayerFrame( FRAME_T aFrameType );
418 
421 
423  int m_ctl;
424 
425  wxFrame* m_top; // Usually m_top is the Project manager
426 
427  // a string array ( size KIWAY_PLAYER_COUNT ) to Store the frame name
428  // of PLAYER frames which were run.
429  // A non empty name means only a PLAYER was run at least one time.
430  // It can be closed. Call :
431  // wxWindow::FindWindowByName( m_playerFrameName[aFrameType] )
432  // to know if still exists (or GetPlayerFrame( FRAME_T aFrameType )
433  wxArrayString m_playerFrameName;
434 };
435 
436 
437 #ifndef SWIG
438 // provided by single_top.cpp and kicad.cpp;
439 extern KIWAY Kiway;
440 // whereas python launchers: single_top.py and project manager instantiate as a python object
441 #endif
442 
443 
456 typedef KIFACE* KIFACE_GETTER_FUNC( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
457 
458 
459 #ifndef SWIG
460 
462 extern "C" {
463 #if defined(BUILD_KIWAY_DLL)
464  MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
465 #else
466  KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKIWAYversion, PGM_BASE* aProgram );
467 #endif
468 }
469 
470 #endif // SWIG
471 
472 #endif // KIWAY_H_
virtual wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKIWAY, int aCtlBits=0)=0
Create a wxWindow for the current project.
virtual bool PlayersClose(bool doForce)
Call the KIWAY_PLAYER::Close( bool force ) function on all the windows and if none are vetoed,...
Definition: kiway.cpp:438
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:61
void OnKiCadExit()
Definition: kiway.cpp:599
Struct PGM_SINGLE_TOP implements PGM_BASE with its own OnPgmInit() and OnPgmExit().
Definition: single_top.cpp:66
Container for project specific data.
Definition: project.h:62
static FACE_T KifaceType(FRAME_T aFrameType)
A simple mapping function which returns the FACE_T which is known to implement aFrameType.
Definition: kiway.cpp:307
KIWAY_PLAYER * GetPlayerFrame(FRAME_T aFrameType)
Definition: kiway.cpp:348
virtual ~KIFACE()
Definition: kiway.h:153
Container for data for KiCad programs.
Definition: pgm_base.h:130
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:32
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:173
PGM_BASE * m_program
Definition: kiway.h:422
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:357
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:451
virtual void SetLanguage(int aLanguage)
Change the language and then calls ShowChangedLanguage() on all #KIWAY_PLAYERs.
Definition: kiway.cpp:459
~KIWAY()
Definition: kiway.h:279
void OnKiwayEnd()
Definition: kiway.cpp:614
bool set_kiface(FACE_T aFaceType, KIFACE *aKiface)
Definition: kiway.h:402
MAIL_T
The set of mail types sendable via KIWAY::ExpressMail() and supplied as the aCommand parameter to tha...
Definition: mail_type.h:37
#define MY_API(rettype)
Definition: import_export.h:55
virtual bool PlayerClose(FRAME_T aFrameType, bool doForce)
Call the KIWAY_PLAYER::Close( bool force ) function on the window and if not vetoed,...
Definition: kiway.cpp:413
bool ProcessEvent(wxEvent &aEvent) override
Definition: kiway.cpp:573
KIFACE * KIFACE_GETTER_FUNC(int *aKIFACEversion, int aKIWAYversion, PGM_BASE *aProgram)
Point to the one and only KIFACE export.
Definition: kiway.h:456
virtual void ProjectChanged()
Calls ProjectChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:550
int m_ctl
Definition: kiway.h:423
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
#define KIFACE_GETTER
Definition: kiway.h:109
const wxString dso_search_path(FACE_T aFaceId)
Get the [path &] name of the DSO holding the requested FACE_T.
Definition: kiway.cpp:99
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:179
void SetTop(wxFrame *aTop)
Tell this KIWAY about the top most frame in the program and optionally allows it to play the role of ...
Definition: kiway.cpp:81
virtual void SaveFileAs(const wxString &srcProjectBasePath, const wxString &srcProjectName, const wxString &newProjectBasePath, const wxString &newProjectName, const wxString &srcFilePath, wxString &aErrors)
Saving a file under a different name is delegated to the various KIFACEs because the project doesn't ...
Definition: kiway.h:208
FACE_T
Known KIFACE implementations.
Definition: kiway.h:266
eeschema DSO
Definition: kiway.h:268
int aKIWAYversion
Definition: cvpcb.cpp:96
virtual void * IfaceOrAddress(int aDataId)=0
Return pointer to the requested object.
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:147
KIWAY(PGM_BASE *aProgram, int aCtlBits, wxFrame *aTop=nullptr)
Definition: kiway.cpp:49
static int m_kiface_version[KIWAY_FACE_COUNT]
Definition: kiway.h:420
virtual void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged)
Call CommonSettingsChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:527
wxArrayString m_playerFrameName
Definition: kiway.h:433
wxFrame * m_top
Definition: kiway.h:425
virtual void OnKifaceEnd()=0
Called just once just before the DSO is to be unloaded.
int PGM_BASE * aProgram
Definition: cvpcb.cpp:97
static KIFACE * m_kiface[KIWAY_FACE_COUNT]
Definition: kiway.h:419
KIWAY Kiway
virtual bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits)=0
Called just once shortly after the DSO is loaded.