KiCad PCB EDA Suite
pl_editor.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) 2013 CERN
5  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
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 <wx/file.h>
26 #include <wx/snglinst.h>
27 
28 #include <kiface_i.h>
29 #include <confirm.h>
30 #include <gestfich.h>
31 #include <pgm_base.h>
33 
34 #include "pl_editor_frame.h"
35 #include "pl_editor_settings.h"
36 
37 
38 namespace PGE {
39 
40 static struct IFACE : public KIFACE_I
41 {
42  // Of course all are virtual overloads, implementations of the KIFACE.
43 
44  IFACE( const char* aName, KIWAY::FACE_T aType ) :
45  KIFACE_I( aName, aType )
46  {}
47 
48  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
49 
50  void OnKifaceEnd() override;
51 
52  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
53  {
54  switch( aClassId )
55  {
56  case FRAME_PL_EDITOR:
57  {
58  PL_EDITOR_FRAME* frame = new PL_EDITOR_FRAME( aKiway, aParent );
59  return frame;
60  }
61  break;
62 
63  default:
64  ;
65  }
66 
67  return NULL;
68  }
69 
81  void* IfaceOrAddress( int aDataId ) override
82  {
83  return NULL;
84  }
85 
92  void SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
93  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
94  const wxString& aSrcFilePath, wxString& aErrors ) override;
95 
96 } kiface( "pl_editor", KIWAY::FACE_PL_EDITOR );
97 
98 } // namespace
99 
100 using namespace PGE;
101 
103 
104 KIFACE_I& Kiface() { return kiface; }
105 
106 
107 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
108 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
109 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
110 {
112  return &kiface;
113 }
114 
115 
117 {
118  wxASSERT( process ); // KIFACE_GETTER has already been called.
119  return *process;
120 }
121 
122 
123 // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
125 {
126  return process;
127 }
128 
129 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
130 {
133  start_common( aCtlBits );
134  return true;
135 }
136 
137 
139 {
140  end_common();
141 }
142 
143 
144 void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
145  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
146  const wxString& aSrcFilePath, wxString& aErrors )
147 {
148  wxFileName destFile( aSrcFilePath );
149  wxString destPath = destFile.GetPathWithSep();
150  wxUniChar pathSep = wxFileName::GetPathSeparator();
151  wxString ext = destFile.GetExt();
152 
153  if( destPath.StartsWith( aProjectBasePath + pathSep ) )
154  {
155  destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
156  destFile.SetPath( destPath );
157  }
158 
159  if( ext == "kicad_wks" )
160  {
161  if( destFile.GetName() == aSrcProjectName )
162  destFile.SetName( aNewProjectName );
163 
164  KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
165  }
166  else
167  {
168  wxFAIL_MSG( "Unexpected filetype for Pcbnew::SaveFileAs()" );
169  }
170 }
171 
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:167
A KIFACE (I)mplementation.
Definition: kiface_i.h:37
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
Definition: kiface_i.cpp:88
This file is part of the common library TODO brief description.
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: pl_editor.cpp:44
This file is part of the common library.
Container for data for KiCad programs.
Definition: pgm_base.h:131
PL_EDITOR_FRAME is the main window used in the drawing sheet editor.
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:363
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_i.h:92
int PGM_BASE * aProgram
Definition: pl_editor.cpp:110
void * IfaceOrAddress(int aDataId) override
Function IfaceOrAddress return a pointer to the requested object.
Definition: pl_editor.cpp:81
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: pl_editor.cpp:124
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: pl_editor.cpp:104
void SaveFileAs(const wxString &aProjectBasePath, const wxString &aSrcProjectName, const wxString &aNewProjectBasePath, const wxString &aNewProjectName, const wxString &aSrcFilePath, wxString &aErrors) override
Function SaveFileAs Saving a file under a different name is delegated to the various KIFACEs because ...
Definition: pl_editor.cpp:144
#define NULL
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
Definition: kiface_i.cpp:98
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: pl_editor.cpp:138
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
#define KIFACE_GETTER
Definition: kiway.h:109
static PGM_BASE * process
Definition: pl_editor.cpp:102
FACE_T
Known KIFACE implementations.
Definition: kiway.h:266
JSON_SETTINGS * RegisterSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition: pl_editor.cpp:52
see class PGM_BASE
PGE::IFACE KIFACE_I kiface("pl_editor", KIWAY::FACE_PL_EDITOR)
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:147
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition: kiface_i.h:94
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pl_editor.cpp:116
int aKiwayVersion
Definition: pl_editor.cpp:109
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Typically start_common() is called from here.
Definition: pl_editor.cpp:129