KiCad PCB EDA Suite
kiway.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 <dick@softplc.com>
5  * Copyright (C) 2014-2017 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 #include <string.h>
26 
27 #include <macros.h>
28 #include <kiway.h>
29 #include <kiway_player.h>
30 #include <kiway_express.h>
31 #include <pgm_base.h>
32 #include <config.h>
33 #include <id.h>
34 
35 #include <wx/stdpaths.h>
36 #include <wx/debug.h>
37 
38 
39 KIFACE* KIWAY::m_kiface[KIWAY_FACE_COUNT];
40 int KIWAY::m_kiface_version[KIWAY_FACE_COUNT];
41 
42 
43 
44 KIWAY::KIWAY( PGM_BASE* aProgram, int aCtlBits, wxFrame* aTop ):
45  m_program( aProgram ),
46  m_ctl( aCtlBits ),
47  m_top( 0 )
48 {
49  SetTop( aTop ); // hook player_destroy_handler() into aTop.
50 
51 
52  // Prepare the room to store the frame names, once they will be created
53  // with FRAME_T type as index in this table.
54  // (note this is a list of frame names, but a non empty entry
55  // does not mean the frame still exists. It means only the frame was created
56  // at least once. It can be destroyed after. These entries are not cleared.
57  // the purpose is just to allow a call to wxWindow::FindWindowByName(), from
58  // a FRAME_T frame type
59  m_playerFrameName.Add( wxEmptyString, KIWAY_PLAYER_COUNT );
60 }
61 
62 
63 #if 0
64 // Any event types derived from wxCommandEvt, like wxWindowDestroyEvent, are
65 // propagated upwards to parent windows if not handled below. Therefore the
66 // m_top window should receive all wxWindowDestroyEvents originating from
67 // KIWAY_PLAYERs. It does anyways, but now player_destroy_handler eavesdrops
68 // on that event stream looking for KIWAY_PLAYERs being closed.
69 
70 void KIWAY::player_destroy_handler( wxWindowDestroyEvent& event )
71 {
72  // Currently : do nothing
73  event.Skip(); // skip to who, the wxApp? I'm the top window.
74 }
75 #endif
76 
77 
78 void KIWAY::SetTop( wxFrame* aTop )
79 {
80 #if 0
81  if( m_top )
82  {
83  m_top->Disconnect( wxEVT_DESTROY, wxWindowDestroyEventHandler( KIWAY::player_destroy_handler ), NULL, this );
84  }
85 
86  if( aTop )
87  {
88  aTop->Connect( wxEVT_DESTROY, wxWindowDestroyEventHandler( KIWAY::player_destroy_handler ), NULL, this );
89  }
90 #endif
91 
92  m_top = aTop;
93 }
94 
95 
96 const wxString KIWAY::dso_search_path( FACE_T aFaceId )
97 {
98  const char* name;
99 
100  switch( aFaceId )
101  {
102  case FACE_SCH: name = KIFACE_PREFIX "eeschema"; break;
103  case FACE_PCB: name = KIFACE_PREFIX "pcbnew"; break;
104  case FACE_CVPCB: name = KIFACE_PREFIX "cvpcb"; break;
105  case FACE_GERBVIEW: name = KIFACE_PREFIX "gerbview"; break;
106  case FACE_PL_EDITOR: name = KIFACE_PREFIX "pl_editor"; break;
107  case FACE_PCB_CALCULATOR: name = KIFACE_PREFIX "pcb_calculator"; break;
108  case FACE_BMP2CMP: name = KIFACE_PREFIX "bitmap2component"; break;
109 
110  default:
111  wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFaceId" ) );
112  return wxEmptyString;
113  }
114 
115 #ifndef __WXMAC__
116  wxString path;
117 
119  {
120  // The 2 *.cpp program launchers: single_top.cpp and kicad.cpp expect
121  // the *.kiface's to reside in same diretory as their binaries do.
122  // Not so for python launcher, identified by KFCTL_PY_PROJECT_SUITE
123  path = wxStandardPaths::Get().GetExecutablePath();
124  }
125 
126  wxFileName fn = path;
127 #else
128  // we have the dso's in main OSX bundle kicad.app/Contents/PlugIns
129  wxFileName fn = Pgm().GetExecutablePath();
130  fn.AppendDir( wxT( "Contents" ) );
131  fn.AppendDir( wxT( "PlugIns" ) );
132 #endif
133 
134  fn.SetName( name );
135 
136  // Here a "suffix" == an extension with a preceding '.',
137  // so skip the preceding '.' to get an extension
138  fn.SetExt( KIFACE_SUFFIX + 1 ); // + 1 => &KIFACE_SUFFIX[1]
139 
140  return fn.GetFullPath();
141 }
142 
143 
145 {
146  return *(PROJECT*) &m_project; // strip const-ness, function really is const.
147 }
148 
149 
150 KIFACE* KIWAY::KiFACE( FACE_T aFaceId, bool doLoad )
151 {
152  // Since this will be called from python, cannot assume that code will
153  // not pass a bad aFaceId.
154  if( unsigned( aFaceId ) >= DIM( m_kiface ) )
155  {
156  // @todo : throw an exception here for python's benefit, at least that
157  // way it gets some explanatory text.
158 
159  wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFaceId" ) );
160  return NULL;
161  }
162 
163  // return the previously loaded KIFACE, if it was.
164  if( m_kiface[aFaceId] )
165  return m_kiface[aFaceId];
166 
167  wxString msg;
168 
169  // DSO with KIFACE has not been loaded yet, does caller want to load it?
170  if( doLoad )
171  {
172  wxString dname = dso_search_path( aFaceId );
173 
174  wxDynamicLibrary dso;
175 
176  void* addr = NULL;
177 
178  if( !dso.Load( dname, wxDL_VERBATIM | wxDL_NOW | wxDL_GLOBAL ) )
179  {
180  // Failure: error reporting UI was done via wxLogSysError().
181  // No further reporting required here. Apparently this is not true on all
182  // platforms and/or wxWidgets builds and KiCad will crash. Throwing the exception
183  // here and catching it in the KiCad launcher resolves the crash issue. See bug
184  // report https://bugs.launchpad.net/kicad/+bug/1577786.
185 
186  msg.Printf( _( "Failed to load kiface library '%s'." ), GetChars( dname ) );
187  THROW_IO_ERROR( msg );
188  }
189  else if( ( addr = dso.GetSymbol( wxT( KIFACE_INSTANCE_NAME_AND_VERSION ) ) ) == NULL )
190  {
191  // Failure: error reporting UI was done via wxLogSysError().
192  // No further reporting required here. Assume the same thing applies here as
193  // above with the Load() call. This has not been tested.
194  msg.Printf(
195  _( "Could not read instance name and version symbol form kiface library '%s'." ),
196  GetChars( dname ) );
197  THROW_IO_ERROR( msg );
198  }
199  else
200  {
201  KIFACE_GETTER_FUNC* getter = (KIFACE_GETTER_FUNC*) addr;
202 
203  KIFACE* kiface = getter( &m_kiface_version[aFaceId], KIFACE_VERSION, m_program );
204 
205  // KIFACE_GETTER_FUNC function comment (API) says the non-NULL is unconditional.
206  wxASSERT_MSG( kiface,
207  wxT( "attempted DSO has a bug, failed to return a KIFACE*" ) );
208 
209  // Give the DSO a single chance to do its "process level" initialization.
210  // "Process level" specifically means stay away from any projects in there.
211  if( kiface->OnKifaceStart( m_program, m_ctl ) )
212  {
213  // Tell dso's wxDynamicLibrary destructor not to Unload() the program image.
214  (void) dso.Detach();
215 
216  return m_kiface[aFaceId] = kiface;
217  }
218  }
219 
220  // In any of the failure cases above, dso.Unload() should be called here
221  // by dso destructor.
222  // However:
223 
224  // There is a file installation bug. We only look for KIFACE_I's which we know
225  // to exist, and we did not find one. If we do not find one, this is an
226  // installation bug.
227 
228  msg = wxString::Format( _(
229  "Fatal Installation Bug. File:\n"
230  "'%s'\ncould not be loaded\n" ), GetChars( dname ) );
231 
232  if( ! wxFileExists( dname ) )
233  msg << _( "It is missing.\n" );
234  else
235  msg << _( "Perhaps a shared library (.dll or .so) file is missing.\n" );
236 
237  msg << _( "From command line: argv[0]:\n'" );
238  msg << wxStandardPaths::Get().GetExecutablePath() << wxT( "'\n" );
239 
240  // This is a fatal error, one from which we cannot recover, nor do we want
241  // to protect against in client code which would require numerous noisy
242  // tests in numerous places. So we inform the user that the installation
243  // is bad. This exception will likely not get caught until way up in the
244  // wxApp derivative, at which point the process will exit gracefully.
245  THROW_IO_ERROR( msg );
246  }
247 
248  return NULL;
249 }
250 
251 
253 {
254  switch( aFrameType )
255  {
256  case FRAME_SCH:
258  case FRAME_SCH_VIEWER:
260  case FRAME_SIMULATOR:
261  return FACE_SCH;
262 
263  case FRAME_PCB:
268  case FRAME_PCB_DISPLAY3D:
269  return FACE_PCB;
270 
271  case FRAME_CVPCB:
272  case FRAME_CVPCB_DISPLAY:
273  return FACE_CVPCB;
274 
275  case FRAME_GERBER:
276  return FACE_GERBVIEW;
277 
278  case FRAME_PL_EDITOR:
279  return FACE_PL_EDITOR;
280 
281  case FRAME_CALC:
282  return FACE_PCB_CALCULATOR;
283 
284  case FRAME_BM2CMP:
285  return FACE_BMP2CMP;
286 
287  default:
288  return FACE_T( -1 );
289  }
290 }
291 
292 
294 {
295  if( m_playerFrameName[aFrameType].IsEmpty() )
296  return NULL;
297 
298  return static_cast<KIWAY_PLAYER*>( wxWindow::FindWindowByName( m_playerFrameName[aFrameType] ) );
299 }
300 
301 
302 KIWAY_PLAYER* KIWAY::Player( FRAME_T aFrameType, bool doCreate, KIWAY_PLAYER* aParent )
303 {
304  // Since this will be called from python, cannot assume that code will
305  // not pass a bad aFrameType.
306  if( unsigned( aFrameType ) >= KIWAY_PLAYER_COUNT )
307  {
308  // @todo : throw an exception here for python's benefit, at least that
309  // way it gets some explanatory text.
310 
311  wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFrameType" ) );
312  return NULL;
313  }
314 
315  // return the previously opened window
316  KIWAY_PLAYER* frame = GetPlayerFrame( aFrameType );
317 
318  if( frame )
319  return frame;
320 
321  if( doCreate )
322  {
323  FACE_T face_type = KifaceType( aFrameType );
324  wxASSERT( face_type != FACE_T(-1) );
325 
326  KIFACE* kiface = KiFACE( face_type );
327  wxASSERT( kiface );
328 
329  if( kiface )
330  {
331  frame = (KIWAY_PLAYER*) kiface->CreateWindow(
332  aParent, // Parent window of frame, NULL in non modal mode
333  aFrameType,
334  this,
335  m_ctl // questionable need, these same flags where passed to the KIFACE::OnKifaceStart()
336  );
337  wxASSERT( frame );
338 
339  m_playerFrameName[aFrameType] = frame->GetName();
340 
341  return frame;
342  }
343  }
344 
345  return NULL;
346 }
347 
348 
349 bool KIWAY::PlayerClose( FRAME_T aFrameType, bool doForce )
350 {
351  // Since this will be called from python, cannot assume that code will
352  // not pass a bad aFrameType.
353  if( unsigned( aFrameType ) >= KIWAY_PLAYER_COUNT )
354  {
355  // @todo : throw an exception here for python's benefit, at least that
356  // way it gets some explanatory text.
357 
358  wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFrameType" ) );
359  return false;
360  }
361 
362  KIWAY_PLAYER* frame = GetPlayerFrame( aFrameType );
363 
364  if( frame == NULL ) // Already closed
365  return true;
366 
367  if( frame->Close( doForce ) )
368  return true;
369 
370  return false;
371 }
372 
373 
374 bool KIWAY::PlayersClose( bool doForce )
375 {
376  bool ret = true;
377 
378  for( unsigned i=0; i < KIWAY_PLAYER_COUNT; ++i )
379  {
380  ret = ret && PlayerClose( FRAME_T( i ), doForce );
381  }
382 
383  return ret;
384 }
385 
386 
387 void KIWAY::ExpressMail( FRAME_T aDestination,
388  MAIL_T aCommand, const std::string& aPayload, wxWindow* aSource )
389 {
390  KIWAY_EXPRESS mail( aDestination, aCommand, aPayload, aSource );
391 
392  ProcessEvent( mail );
393 }
394 
395 
396 void KIWAY::SetLanguage( int aLanguage )
397 {
398  Pgm().SetLanguageIdentifier( aLanguage );
399  Pgm().SetLanguage();
400 
401 #if 1
402  // This is a risky hack that goes away if we allow the language to be
403  // set only from the top most frame if !Kiface.IsSingle()
404 
405  // Only for the C++ project manager, and not for the python one and not for
406  // single_top do we look for the EDA_BASE_FRAME as the top level window.
407  // For single_top this is not needed because that window is registered in
408  // the array below.
410  {
411  // A dynamic_cast could be better, but creates link issues
412  // (some basic_frame functions not found) on some platforms,
413  // so a static_cast is used.
414  EDA_BASE_FRAME* top = static_cast<EDA_BASE_FRAME*>( m_top );
415 
416  if( top )
417  top->ShowChangedLanguage();
418  }
419 #endif
420 
421  for( unsigned i=0; i < KIWAY_PLAYER_COUNT; ++i )
422  {
423  KIWAY_PLAYER* frame = GetPlayerFrame( ( FRAME_T )i );
424 
425  if( frame )
426  {
427  frame->ShowChangedLanguage();
428  }
429  }
430 }
431 
433 {
434 #if 1
436  {
437  // A dynamic_cast could be better, but creates link issues
438  // (some basic_frame functions not found) on some platforms,
439  // so a static_cast is used.
440  EDA_BASE_FRAME* top = static_cast<EDA_BASE_FRAME*>( m_top );
441 
442  if( top )
443  top->ShowChangedIcons();
444  }
445 #endif
446 
447  for( unsigned i=0; i < KIWAY_PLAYER_COUNT; ++i )
448  {
449  KIWAY_PLAYER* frame = GetPlayerFrame( ( FRAME_T )i );
450 
451  if( frame )
452  {
453  frame->ShowChangedIcons();
454  }
455  }
456 }
457 
458 
459 bool KIWAY::ProcessEvent( wxEvent& aEvent )
460 {
461  KIWAY_EXPRESS* mail = dynamic_cast<KIWAY_EXPRESS*>( &aEvent );
462 
463  if( mail )
464  {
465  FRAME_T dest = mail->Dest();
466 
467  // see if recipient is alive
468  KIWAY_PLAYER* alive = Player( dest, false );
469 
470  if( alive )
471  {
472 #if 1
473  return alive->ProcessEvent( aEvent );
474 #else
475  alive->KiwayMailIn( *mail );
476  return true;
477 #endif
478  }
479  }
480 
481  return false;
482 }
483 
484 
486 {
487  for( unsigned i=0; i < DIM( m_kiface ); ++i )
488  {
489  if( m_kiface[i] )
490  m_kiface[i]->OnKifaceEnd();
491  }
492 }
#define KFCTL_CPP_PROJECT_SUITE
Am running under C++ project mgr, possibly with others.
Definition: kiway.h:159
#define DIM(x)
of elements in an array
Definition: macros.h:98
VTBL_ENTRY bool PlayersClose(bool doForce)
Function PlayersClose calls the KIWAY_PLAYER::Close( bool force ) function on all the windows and if ...
Definition: kiway.cpp:374
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
Definition: kiway_player.h:111
Class KIWAY_EXPRESS carries a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:39
Class PROJECT holds project specific data.
Definition: project.h:52
static FACE_T KifaceType(FRAME_T aFrameType)
Function KifaceType is a simple mapping function which returns the FACE_T which is known to implement...
Definition: kiway.cpp:252
KIWAY_PLAYER * GetPlayerFrame(FRAME_T aFrameType)
Definition: kiway.cpp:293
VTBL_ENTRY const wxString & GetExecutablePath() const
Definition: pgm_base.h:173
virtual void ShowChangedIcons()
Function ShowChangedIcons redraws items menus after a icon was changed option.
Definition: basicframe.cpp:249
Class PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:107
#define KIFACE_VERSION
Definition: kiway.h:111
FRAME_T
Enum FRAME_T is the set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:34
KIWAY(PGM_BASE *aProgram, int aCtlBits, wxFrame *aTop=NULL)
Definition: kiway.cpp:44
VTBL_ENTRY void OnKifaceEnd()=0
Function OnKifaceEnd is called just once just before the DSO is to be unloaded.
PGM_BASE * m_program
Definition: kiway.h:413
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
VTBL_ENTRY void SetLanguage(int aLanguage)
Function SetLanguage changes the language and then calls ShowChangedLanguage() on all KIWAY_PLAYERs...
Definition: kiway.cpp:396
void OnKiwayEnd()
Definition: kiway.cpp:485
#define KIFACE_INSTANCE_NAME_AND_VERSION
Definition: kiway.h:116
VTBL_ENTRY void ShowChangedIcons()
Function ShowChangedIcons Calls ShowChangedIcons() on all KIWAY_PLAYERs.
Definition: kiway.cpp:432
This file contains miscellaneous commonly used macros and functions.
MAIL_T
Enum MAIL_T is the set of mail types sendable via KIWAY::ExpressMail() and supplied as the aCommand p...
Definition: mail_type.h:37
VTBL_ENTRY wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKIWAY, int aCtlBits=0)=0
Function CreateWindow creates a wxWindow for the current project.
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, KIWAY_PLAYER *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:302
VTBL_ENTRY void SetLanguageIdentifier(int menu_id)
Function SetLanguageIdentifier sets in .m_language_id member the wxWidgets language identifier Id fro...
Definition: pgm_base.cpp:765
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:144
VTBL_ENTRY bool PlayerClose(FRAME_T aFrameType, bool doForce)
Function PlayerClose calls the KIWAY_PLAYER::Close( bool force ) function on the window and if not ve...
Definition: kiway.cpp:349
bool ProcessEvent(wxEvent &aEvent) override
Definition: kiway.cpp:459
KIFACE * KIFACE_GETTER_FUNC(int *aKIFACEversion, int aKIWAYversion, PGM_BASE *aProgram)
Function Pointer KIFACE_GETTER_FUNC points to the one and only KIFACE export.
Definition: kiway.h:450
PROJECT m_project
Definition: kiway.h:426
int m_ctl
Definition: kiway.h:414
virtual void KiwayMailIn(KIWAY_EXPRESS &aEvent)
Function KiwayMailIn receives KIWAY_EXPRESS messages from other players.
pcbnew DSO
Definition: kiway.h:266
#define THROW_IO_ERROR(x)
Definition: utf8.cpp:60
const wxString dso_search_path(FACE_T aFaceId)
Get the [path &] name of the DSO holding the requested FACE_T.
Definition: kiway.cpp:96
VTBL_ENTRY KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Function KiFACE returns the KIFACE* given a FACE_T.
Definition: kiway.cpp:150
void SetTop(wxFrame *aTop)
Function SetTop tells this KIWAY about the top most frame in the program and optionally allows it to ...
Definition: kiway.cpp:78
VTBL_ENTRY bool SetLanguage(bool first_time=false)
Function SetLanguage sets the dictionary file name for internationalization.
Definition: pgm_base.cpp:673
FACE_T
Known KIFACE implementations.
Definition: kiway.h:263
eeschema DSO
Definition: kiway.h:265
FRAME_T Dest()
Function Dest returns the destination player id of the message.
Definition: kiway_express.h:46
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, const std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:387
Class EDA_BASE_FRAME is the base frame for deriving all KiCad main window classes.
Definition: wxstruct.h:113
Class KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:150
static int m_kiface_version[KIWAY_FACE_COUNT]
Definition: kiway.h:411
const char * name
VTBL_ENTRY bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits)=0
Function OnKifaceStart is called just once shortly after the DSO is loaded.
wxArrayString m_playerFrameName
Definition: kiway.h:424
return & kiface
Definition: pcbnew.cpp:207
wxFrame * m_top
Definition: kiway.h:416
virtual void ShowChangedLanguage()
Function ShowChangedLanguage redraws the menus and what not in current language.
Definition: basicframe.cpp:242
bool ProcessEvent(wxEvent &aEvent) override
Function ProcessEvent overrides the default process event handler to implement the auto save feature...
Definition: basicframe.cpp:166
int PGM_BASE * aProgram
Definition: pcbnew.cpp:205
static KIFACE * m_kiface[KIWAY_FACE_COUNT]
Definition: kiway.h:410
#define KFCTL_STANDALONE
Am running as a standalone Top.
Definition: kiway.h:158