You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
9.1 KiB

12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
12 years ago
12 years ago
12 years ago
  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 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. #include <string.h>
  25. #include <macros.h>
  26. #include <kiway.h>
  27. #include <kiway_player.h>
  28. #include <kiway_express.h>
  29. #include <config.h>
  30. #include <wx/debug.h>
  31. #include <wx/stdpaths.h>
  32. KIFACE* KIWAY::m_kiface[KIWAY_FACE_COUNT];
  33. int KIWAY::m_kiface_version[KIWAY_FACE_COUNT];
  34. KIWAY::KIWAY( PGM_BASE* aProgram, wxFrame* aTop ):
  35. m_program( aProgram ),
  36. m_top( 0 )
  37. {
  38. SetTop( aTop ); // hook playerDestroyHandler() into aTop.
  39. memset( m_player, 0, sizeof( m_player ) );
  40. }
  41. // Any event types derived from wxCommandEvt, like wxWindowDestroyEvent, are
  42. // propogated upwards to parent windows if not handled below. Therefor the
  43. // m_top window should receive all wxWindowDestroyEvents originating from
  44. // KIWAY_PLAYERs. It does anyways, but now playerDestroyHandler eavesdrops
  45. // on that event stream looking for KIWAY_PLAYERs being closed.
  46. void KIWAY::playerDestroyHandler( wxWindowDestroyEvent& event )
  47. {
  48. wxWindow* w = event.GetWindow();
  49. for( unsigned i=0; i<DIM(m_player); ++i )
  50. {
  51. // if destroying one of our flock, then mark it as deceased.
  52. if( (wxWindow*) m_player[i] == w )
  53. {
  54. // DBG(printf( "%s: marking m_player[%d] as destroyed\n", __func__, i );)
  55. m_player[i] = 0;
  56. }
  57. }
  58. }
  59. void KIWAY::SetTop( wxFrame* aTop )
  60. {
  61. if( m_top )
  62. {
  63. m_top->Disconnect( wxEVT_DESTROY, wxWindowDestroyEventHandler( KIWAY::playerDestroyHandler ), NULL, this );
  64. }
  65. if( aTop )
  66. {
  67. aTop->Connect( wxEVT_DESTROY, wxWindowDestroyEventHandler( KIWAY::playerDestroyHandler ), NULL, this );
  68. }
  69. m_top = aTop;
  70. }
  71. const wxString KIWAY::dso_full_path( FACE_T aFaceId )
  72. {
  73. const wxChar* name;
  74. switch( aFaceId )
  75. {
  76. case FACE_SCH: name = KIFACE_PREFIX wxT( "eeschema" ); break;
  77. case FACE_PCB: name = KIFACE_PREFIX wxT( "pcbnew" ); break;
  78. case FACE_CVPCB: name = KIFACE_PREFIX wxT( "cvpcb" ); break;
  79. case FACE_GERBVIEW: name = KIFACE_PREFIX wxT( "gerbview" ); break;
  80. case FACE_PL_EDITOR: name = KIFACE_PREFIX wxT( "pl_editor" ); break;
  81. // case FACE_PCB_CALCULATOR: who knows.
  82. default:
  83. wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFaceId" ) );
  84. return wxEmptyString;
  85. }
  86. wxFileName fn = wxStandardPaths::Get().GetExecutablePath();
  87. fn.SetName( name );
  88. // Here a "suffix" == an extension with a preceding '.',
  89. // so skip the preceding '.' to get an extension
  90. fn.SetExt( KIFACE_SUFFIX + 1 ); // + 1 => &KIFACE_SUFFIX[1]
  91. return fn.GetFullPath();
  92. }
  93. PROJECT& KIWAY::Prj() const
  94. {
  95. return *(PROJECT*) &m_project; // strip const-ness, function really is const.
  96. }
  97. KIFACE* KIWAY::KiFACE( FACE_T aFaceId, bool doLoad )
  98. {
  99. // Since this will be called from python, cannot assume that code will
  100. // not pass a bad aFaceId.
  101. if( unsigned( aFaceId ) >= DIM( m_kiface ) )
  102. {
  103. // @todo : throw an exception here for python's benefit, at least that
  104. // way it gets some explanatory text.
  105. wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFaceId" ) );
  106. return NULL;
  107. }
  108. // return the previously loaded KIFACE, if it was.
  109. if( m_kiface[aFaceId] )
  110. return m_kiface[aFaceId];
  111. // DSO with KIFACE has not been loaded yet, does caller want to load it?
  112. if( doLoad )
  113. {
  114. wxString dname = dso_full_path( aFaceId );
  115. wxDynamicLibrary dso;
  116. void* addr = NULL;
  117. if( !dso.Load( dname, wxDL_VERBATIM | wxDL_NOW ) )
  118. {
  119. // Failure: error reporting UI was done via wxLogSysError().
  120. // No further reporting required here.
  121. }
  122. else if( ( addr = dso.GetSymbol( wxT( KIFACE_INSTANCE_NAME_AND_VERSION ) ) ) == NULL )
  123. {
  124. // Failure: error reporting UI was done via wxLogSysError().
  125. // No further reporting required here.
  126. }
  127. else
  128. {
  129. KIFACE_GETTER_FUNC* getter = (KIFACE_GETTER_FUNC*) addr;
  130. KIFACE* kiface = getter( &m_kiface_version[aFaceId], KIFACE_VERSION, m_program );
  131. // KIFACE_GETTER_FUNC function comment (API) says the non-NULL is unconditional.
  132. wxASSERT_MSG( kiface,
  133. wxT( "attempted DSO has a bug, failed to return a KIFACE*" ) );
  134. // Give the DSO a single chance to do its "process level" initialization.
  135. // "Process level" specifically means stay away from any projects in there.
  136. if( kiface->OnKifaceStart( m_program, KFCTL_PROJECT_SUITE ) )
  137. {
  138. // Tell dso's wxDynamicLibrary destructor not to Unload() the program image.
  139. (void) dso.Detach();
  140. return m_kiface[aFaceId] = kiface;
  141. }
  142. }
  143. // In any of the failure cases above, dso.Unload() should be called here
  144. // by dso destructor.
  145. }
  146. return NULL;
  147. }
  148. KIWAY::FACE_T KIWAY::KifaceType( FRAME_T aFrameType )
  149. {
  150. switch( aFrameType )
  151. {
  152. case FRAME_SCH:
  153. case FRAME_SCH_LIB_EDITOR:
  154. case FRAME_SCH_VIEWER:
  155. return FACE_SCH;
  156. case FRAME_PCB:
  157. case FRAME_PCB_MODULE_EDITOR:
  158. case FRAME_PCB_MODULE_VIEWER:
  159. case FRAME_PCB_FOOTPRINT_WIZARD:
  160. case FRAME_PCB_DISPLAY3D:
  161. return FACE_PCB;
  162. case FRAME_CVPCB:
  163. case FRAME_CVPCB_DISPLAY:
  164. return FACE_CVPCB;
  165. case FRAME_GERBER:
  166. return FACE_GERBVIEW;
  167. case FRAME_PL_EDITOR:
  168. return FACE_PL_EDITOR;
  169. default:
  170. return FACE_T( -1 );
  171. }
  172. }
  173. KIWAY_PLAYER* KIWAY::Player( FRAME_T aFrameType, bool doCreate )
  174. {
  175. // Since this will be called from python, cannot assume that code will
  176. // not pass a bad aFrameType.
  177. if( unsigned( aFrameType ) >= DIM( m_player ) )
  178. {
  179. // @todo : throw an exception here for python's benefit, at least that
  180. // way it gets some explanatory text.
  181. wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFrameType" ) );
  182. return NULL;
  183. }
  184. // return the previously opened window
  185. if( m_player[aFrameType] )
  186. return m_player[aFrameType];
  187. if( doCreate )
  188. {
  189. FACE_T face_type = KifaceType( aFrameType );
  190. wxASSERT( face_type != FACE_T(-1) );
  191. KIFACE* kiface = KiFACE( face_type );
  192. KIWAY_PLAYER* frame = (KIWAY_PLAYER*) kiface->CreateWindow( m_top, aFrameType, this, KFCTL_PROJECT_SUITE );
  193. return m_player[aFrameType] = frame;
  194. }
  195. return NULL;
  196. }
  197. bool KIWAY::PlayerClose( FRAME_T aFrameType, bool doForce )
  198. {
  199. // Since this will be called from python, cannot assume that code will
  200. // not pass a bad aFrameType.
  201. if( unsigned( aFrameType ) >= DIM( m_player ) )
  202. {
  203. // @todo : throw an exception here for python's benefit, at least that
  204. // way it gets some explanatory text.
  205. wxASSERT_MSG( 0, wxT( "caller has a bug, passed a bad aFrameType" ) );
  206. return false;
  207. }
  208. if( m_player[aFrameType] )
  209. {
  210. if( m_player[aFrameType]->Close( doForce ) )
  211. {
  212. m_player[aFrameType] = 0;
  213. return true;
  214. }
  215. return false;
  216. }
  217. return true; // window is closed already.
  218. }
  219. bool KIWAY::PlayersClose( bool doForce )
  220. {
  221. bool ret = true;
  222. for( unsigned i=0; i < DIM( m_player ); ++i )
  223. {
  224. ret = ret && PlayerClose( FRAME_T( i ), doForce );
  225. }
  226. return ret;
  227. }
  228. void KIWAY::ExpressMail( FRAME_T aDestination,
  229. int aCommand, const std::string& aPayload, wxWindow* aSource )
  230. {
  231. KIWAY_EXPRESS mail( aDestination, aCommand, aPayload, aSource );
  232. ProcessEvent( mail );
  233. }
  234. bool KIWAY::ProcessEvent( wxEvent& aEvent )
  235. {
  236. KIWAY_EXPRESS* mail = dynamic_cast<KIWAY_EXPRESS*>( &aEvent );
  237. if( mail )
  238. {
  239. FRAME_T dest = mail->Dest();
  240. // see if recipient is alive
  241. KIWAY_PLAYER* alive = Player( dest, false );
  242. if( alive )
  243. {
  244. #if 0
  245. // This is still broken, but is the way to go.
  246. return alive->ProcessEvent( aEvent );
  247. #else
  248. alive->KiwayMailIn( *mail );
  249. return true;
  250. #endif
  251. }
  252. }
  253. return false;
  254. }