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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <wx/file.h>
27 #include <wx/snglinst.h>
28 
29 #include <kiface_base.h>
30 #include <confirm.h>
31 #include <gestfich.h>
32 #include <pgm_base.h>
34 
35 #include "pl_editor_frame.h"
36 #include "pl_editor_settings.h"
37 
38 
39 namespace PGE {
40 
41 static struct IFACE : public KIFACE_BASE
42 {
43  // Of course all are virtual overloads, implementations of the KIFACE.
44 
45  IFACE( const char* aName, KIWAY::FACE_T aType ) :
46  KIFACE_BASE( aName, aType )
47  {}
48 
49  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
50 
51  void OnKifaceEnd() override;
52 
53  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
54  int aCtlBits = 0 ) override
55  {
56  switch( aClassId )
57  {
58  case FRAME_PL_EDITOR:
59  {
60  PL_EDITOR_FRAME* frame = new PL_EDITOR_FRAME( aKiway, aParent );
61  return frame;
62  break;
63  }
64 
65  default:
66  ;
67  }
68 
69  return nullptr;
70  }
71 
82  void* IfaceOrAddress( int aDataId ) override
83  {
84  return nullptr;
85  }
86 
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 
101 using namespace PGE;
102 
103 
105 
106 
107 KIFACE_BASE& Kiface() { return kiface; }
108 
109 
110 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
111 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
112 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
113 {
115  return &kiface;
116 }
117 
118 
120 {
121  wxASSERT( process ); // KIFACE_GETTER has already been called.
122  return *process;
123 }
124 
125 
126 // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
128 {
129  return process;
130 }
131 
132 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
133 {
136  start_common( aCtlBits );
137  return true;
138 }
139 
140 
142 {
143  end_common();
144 }
145 
146 
147 void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
148  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
149  const wxString& aSrcFilePath, wxString& aErrors )
150 {
151  wxFileName destFile( aSrcFilePath );
152  wxString destPath = destFile.GetPathWithSep();
153  wxUniChar pathSep = wxFileName::GetPathSeparator();
154  wxString ext = destFile.GetExt();
155 
156  if( destPath.StartsWith( aProjectBasePath + pathSep ) )
157  {
158  destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
159  destFile.SetPath( destPath );
160  }
161 
162  if( ext == "kicad_wks" )
163  {
164  if( destFile.GetName() == aSrcProjectName )
165  destFile.SetName( aNewProjectName );
166 
167  KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
168  }
169  else
170  {
171  wxFAIL_MSG( "Unexpected filetype for Pcbnew::SaveFileAs()" );
172  }
173 }
174 
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:129
A KIFACE implementation.
Definition: kiface_base.h:37
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: pl_editor.cpp:45
This file is part of the common library.
Container for data for KiCad programs.
Definition: pgm_base.h:93
The main window used in the drawing sheet editor.
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:214
PGE::IFACE KIFACE_BASE kiface("pl_editor", KIWAY::FACE_PL_EDITOR)
int PGM_BASE * aProgram
Definition: pl_editor.cpp:113
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition: pl_editor.cpp:82
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:127
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition: kiface_base.h:94
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: pl_editor.cpp:107
void SaveFileAs(const wxString &aProjectBasePath, const wxString &aSrcProjectName, const wxString &aNewProjectBasePath, const wxString &aNewProjectName, const wxString &aSrcFilePath, wxString &aErrors) override
Saving a file under a different name is delegated to the various KIFACEs because the project doesn't ...
Definition: pl_editor.cpp:147
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
Definition: kiface_base.cpp:97
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: pl_editor.cpp:141
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:110
static PGM_BASE * process
Definition: pl_editor.cpp:104
FACE_T
Known KIFACE implementations.
Definition: kiway.h:266
wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition: pl_editor.cpp:53
see class PGM_BASE
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:148
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:92
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pl_editor.cpp:119
int aKiwayVersion
Definition: pl_editor.cpp:112
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Typically start_common() is called from here.
Definition: pl_editor.cpp:132