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.

547 lines
16 KiB

* 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
15 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
* 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
* 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
* 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
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2013 CERN (www.cern.ch)
  7. * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. /**
  27. * @file eeschema/files-io.cpp
  28. */
  29. #include <fctsys.h>
  30. #include <class_drawpanel.h>
  31. #include <confirm.h>
  32. #include <gestfich.h>
  33. #include <schframe.h>
  34. #include <pgm_base.h>
  35. #include <kiface_i.h>
  36. #include <eeschema_id.h>
  37. #include <class_library.h>
  38. #include <libeditframe.h>
  39. #include <sch_sheet.h>
  40. #include <sch_sheet_path.h>
  41. #include <sch_component.h>
  42. #include <wildcards_and_files_ext.h>
  43. #include <project_rescue.h>
  44. #include <eeschema_config.h>
  45. bool SCH_EDIT_FRAME::SaveEEFile( SCH_SCREEN* aScreen, bool aSaveUnderNewName, bool aCreateBackupFile )
  46. {
  47. wxString msg;
  48. wxFileName schematicFileName;
  49. bool success;
  50. if( aScreen == NULL )
  51. aScreen = GetScreen();
  52. // If no name exists in the window yet - save as new.
  53. if( aScreen->GetFileName().IsEmpty() )
  54. aSaveUnderNewName = true;
  55. // Construct the name of the file to be saved
  56. schematicFileName = Prj().AbsolutePath( aScreen->GetFileName() );
  57. if( aSaveUnderNewName )
  58. {
  59. wxFileDialog dlg( this, _( "Schematic Files" ),
  60. wxPathOnly( Prj().GetProjectFullName() ),
  61. schematicFileName.GetFullName(), SchematicFileWildcard,
  62. wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  63. if( dlg.ShowModal() == wxID_CANCEL )
  64. return false;
  65. schematicFileName = dlg.GetPath();
  66. if( schematicFileName.GetExt() != SchematicFileExtension )
  67. schematicFileName.SetExt( SchematicFileExtension );
  68. }
  69. if( !IsWritable( schematicFileName ) )
  70. return false;
  71. // Create backup if requested
  72. if( aCreateBackupFile && schematicFileName.FileExists() )
  73. {
  74. wxFileName backupFileName = schematicFileName;
  75. // Rename the old file to a '.bak' one:
  76. backupFileName.SetExt( SchematicBackupFileExtension );
  77. if( backupFileName.FileExists() )
  78. wxRemoveFile( backupFileName.GetFullPath() );
  79. if( !wxRenameFile( schematicFileName.GetFullPath(), backupFileName.GetFullPath() ) )
  80. {
  81. msg.Printf( _( "Could not save backup of file '%s'" ),
  82. GetChars( schematicFileName.GetFullPath() ) );
  83. DisplayError( this, msg );
  84. }
  85. }
  86. // Save
  87. wxLogTrace( traceAutoSave,
  88. wxT( "Saving file <" ) + schematicFileName.GetFullPath() + wxT( ">" ) );
  89. FILE* f = wxFopen( schematicFileName.GetFullPath(), wxT( "wt" ) );
  90. if( !f )
  91. {
  92. msg.Printf( _( "Failed to create file '%s'" ),
  93. GetChars( schematicFileName.GetFullPath() ) );
  94. DisplayError( this, msg );
  95. return false;
  96. }
  97. success = aScreen->Save( f );
  98. if( success )
  99. {
  100. // Delete auto save file.
  101. wxFileName autoSaveFileName = schematicFileName;
  102. autoSaveFileName.SetName( AUTOSAVE_PREFIX_FILENAME + schematicFileName.GetName() );
  103. if( autoSaveFileName.FileExists() )
  104. {
  105. wxLogTrace( traceAutoSave,
  106. wxT( "Removing auto save file <" ) + autoSaveFileName.GetFullPath() +
  107. wxT( ">" ) );
  108. wxRemoveFile( autoSaveFileName.GetFullPath() );
  109. }
  110. // Update the screen and frame info.
  111. if( aSaveUnderNewName )
  112. aScreen->SetFileName( schematicFileName.GetFullPath() );
  113. aScreen->ClrSave();
  114. aScreen->ClrModify();
  115. msg.Printf( _( "File %s saved" ), GetChars( aScreen->GetFileName() ) );
  116. SetStatusText( msg, 0 );
  117. }
  118. else
  119. {
  120. DisplayError( this, _( "File write operation failed." ) );
  121. }
  122. fclose( f );
  123. return success;
  124. }
  125. void SCH_EDIT_FRAME::Save_File( wxCommandEvent& event )
  126. {
  127. int id = event.GetId();
  128. switch( id )
  129. {
  130. case ID_UPDATE_ONE_SHEET:
  131. SaveEEFile( NULL );
  132. break;
  133. case ID_SAVE_ONE_SHEET_UNDER_NEW_NAME:
  134. if( SaveEEFile( NULL, true ) )
  135. {
  136. CreateArchiveLibraryCacheFile( true );
  137. }
  138. break;
  139. }
  140. UpdateTitle();
  141. }
  142. bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
  143. {
  144. // implement the pseudo code from KIWAY_PLAYER.h:
  145. SCH_SCREENS screenList;
  146. // This is for python:
  147. if( aFileSet.size() != 1 )
  148. {
  149. UTF8 msg = StrPrintf( "Eeschema:%s() takes only a single filename", __func__ );
  150. DisplayError( this, msg );
  151. return false;
  152. }
  153. wxString fullFileName( aFileSet[0] );
  154. // We insist on caller sending us an absolute path, if it does not, we say it's a bug.
  155. wxASSERT_MSG( wxFileName( fullFileName ).IsAbsolute(),
  156. wxT( "bug in single_top.cpp or project manager." ) );
  157. if( !LockFile( fullFileName ) )
  158. {
  159. wxString msg = wxString::Format( _(
  160. "Schematic file '%s' is already open." ),
  161. GetChars( fullFileName )
  162. );
  163. DisplayError( this, msg );
  164. return false;
  165. }
  166. // save any currently open and modified project files.
  167. for( SCH_SCREEN* screen = screenList.GetFirst(); screen; screen = screenList.GetNext() )
  168. {
  169. if( screen->IsModify() )
  170. {
  171. int response = YesNoCancelDialog( this, _(
  172. "The current schematic has been modified. Do you wish to save the changes?" ),
  173. wxEmptyString,
  174. _( "Save and Load" ),
  175. _( "Load Without Saving" )
  176. );
  177. if( response == wxID_CANCEL )
  178. {
  179. return false;
  180. }
  181. else if( response == wxID_YES )
  182. {
  183. wxCommandEvent dummy;
  184. OnSaveProject( dummy );
  185. }
  186. else
  187. {
  188. // response == wxID_NO, fall thru
  189. }
  190. break;
  191. }
  192. }
  193. wxFileName pro = fullFileName;
  194. pro.SetExt( ProjectFileExtension );
  195. bool is_new = !wxFileName::IsFileReadable( fullFileName );
  196. // If its a non-existent schematic and caller thinks it exists
  197. if( is_new && !( aCtl & KICTL_CREATE ) )
  198. {
  199. // notify user that fullFileName does not exist, ask if user wants to create it.
  200. wxString ask = wxString::Format( _(
  201. "Schematic '%s' does not exist. Do you wish to create it?" ),
  202. GetChars( fullFileName )
  203. );
  204. if( !IsOK( this, ask ) )
  205. return false;
  206. }
  207. // unload current project file before loading new
  208. {
  209. delete g_RootSheet;
  210. g_RootSheet = NULL;
  211. CreateScreens();
  212. }
  213. GetScreen()->SetFileName( fullFileName );
  214. g_RootSheet->SetFileName( fullFileName );
  215. SetStatusText( wxEmptyString );
  216. ClearMsgPanel();
  217. wxString msg = wxString::Format( _(
  218. "Ready\nProject dir: '%s'\n" ),
  219. GetChars( wxPathOnly( Prj().GetProjectFullName() ) )
  220. );
  221. SetStatusText( msg );
  222. // PROJECT::SetProjectFullName() is an impactful function. It should only be
  223. // called under carefully considered circumstances.
  224. // The calling code should know not to ask me here to change projects unless
  225. // it knows what consequences that will have on other KIFACEs running and using
  226. // this same PROJECT. It can be very harmful if that calling code is stupid.
  227. Prj().SetProjectFullName( pro.GetFullPath() );
  228. LoadProjectFile();
  229. // load the libraries here, not in SCH_SCREEN::Draw() which is a context
  230. // that will not tolerate DisplayError() dialog since we're already in an
  231. // event handler in there.
  232. // And when a schematic file is loaded, we need these libs to initialize
  233. // some parameters (links to PART LIB, dangling ends ...)
  234. Prj().SetElem( PROJECT::ELEM_SCH_PART_LIBS, NULL );
  235. Prj().SchLibs();
  236. if( is_new )
  237. {
  238. // mark new, unsaved file as modified.
  239. GetScreen()->SetModify();
  240. }
  241. else
  242. {
  243. g_RootSheet->SetScreen( NULL );
  244. DBG( printf( "%s: loading schematic %s\n", __func__, TO_UTF8( fullFileName ) );)
  245. bool diag = g_RootSheet->Load( this );
  246. (void) diag;
  247. SetScreen( m_CurrentSheet->LastScreen() );
  248. GetScreen()->ClrModify();
  249. UpdateFileHistory( fullFileName );
  250. // Check to see whether some old library parts need to be rescued
  251. // Only do this if RescueNeverShow was not set.
  252. wxConfigBase *config = Kiface().KifaceSettings();
  253. bool rescueNeverShow = false;
  254. config->Read( RESCUE_NEVER_SHOW_KEY, &rescueNeverShow, false );
  255. if( !rescueNeverShow )
  256. {
  257. if( RescueProject( false ) )
  258. {
  259. GetScreen()->CheckComponentsToPartsLinks();
  260. GetScreen()->TestDanglingEnds();
  261. }
  262. }
  263. }
  264. GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  265. Zoom_Automatique( false );
  266. SetSheetNumberAndCount();
  267. m_canvas->Refresh( true );
  268. return true;
  269. }
  270. bool SCH_EDIT_FRAME::AppendOneEEProject()
  271. {
  272. wxString fullFileName;
  273. SCH_SCREEN* screen = GetScreen();
  274. if( !screen )
  275. {
  276. wxLogError( wxT("Document not ready, cannot import") );
  277. return false;
  278. }
  279. // open file chooser dialog
  280. wxString path = wxPathOnly( Prj().GetProjectFullName() );
  281. wxFileDialog dlg( this, _( "Import Schematic" ), path,
  282. wxEmptyString, SchematicFileWildcard,
  283. wxFD_OPEN | wxFD_FILE_MUST_EXIST );
  284. if( dlg.ShowModal() == wxID_CANCEL )
  285. return false;
  286. fullFileName = dlg.GetPath();
  287. wxFileName fn = fullFileName;
  288. if( fn.IsRelative() )
  289. {
  290. fn.MakeAbsolute();
  291. fullFileName = fn.GetFullPath();
  292. }
  293. wxString cache_name = PART_LIBS::CacheName( fullFileName );
  294. if( !!cache_name )
  295. {
  296. PART_LIBS* libs = Prj().SchLibs();
  297. if( PART_LIB* lib = libs->AddLibrary( cache_name ) )
  298. lib->SetCache();
  299. }
  300. wxLogDebug( wxT( "Importing schematic " ) + fullFileName );
  301. // Keep trace of the last item in list.
  302. // New items will be loaded after this one.
  303. SCH_ITEM* bs = screen->GetDrawItems();
  304. if( bs )
  305. while( bs->Next() )
  306. bs = bs->Next();
  307. // load the project
  308. bool success = LoadOneEEFile( screen, fullFileName, true );
  309. if( success )
  310. {
  311. // the new loaded items need cleaning to avoid duplicate parameters
  312. // which should be unique (ref and time stamp).
  313. // Clear ref and set a new time stamp for new items
  314. if( bs == NULL )
  315. bs = screen->GetDrawItems();
  316. else
  317. bs = bs->Next();
  318. while( bs )
  319. {
  320. SCH_ITEM* nextbs = bs->Next();
  321. // To avoid issues with the current hieratchy,
  322. // do not load included sheets files and give new filenames
  323. // and new sheet names.
  324. // There are many tricky cases (loops, creation of complex hierarchies
  325. // with duplicate file names, duplicate sheet names...)
  326. // So the included sheets names are renamed if existing,
  327. // and filenames are just renamed to avoid loops and
  328. // creation of complex hierarchies.
  329. // If someone want to change it for a better append function, remember
  330. // these cases need work to avoid issues.
  331. if( bs->Type() == SCH_SHEET_T )
  332. {
  333. SCH_SHEET * sheet = (SCH_SHEET *) bs;
  334. time_t newtimestamp = GetNewTimeStamp();
  335. sheet->SetTimeStamp( newtimestamp );
  336. // Check for existing subsheet name in the current sheet
  337. wxString tmp = sheet->GetName();
  338. sheet->SetName( wxEmptyString );
  339. const SCH_SHEET* subsheet = GetScreen()->GetSheet( tmp );
  340. if( subsheet )
  341. sheet->SetName( wxString::Format( wxT( "Sheet%8.8lX" ), (long) newtimestamp ) );
  342. else
  343. sheet->SetName( tmp );
  344. sheet->SetFileName( wxString::Format( wxT( "file%8.8lX.sch" ), (long) newtimestamp ) );
  345. SCH_SCREEN* screen = new SCH_SCREEN( &Kiway() );
  346. screen->SetMaxUndoItems( m_UndoRedoCountMax );
  347. sheet->SetScreen( screen );
  348. sheet->GetScreen()->SetFileName( sheet->GetFileName() );
  349. }
  350. // clear annotation and init new time stamp for the new components
  351. else if( bs->Type() == SCH_COMPONENT_T )
  352. {
  353. ( (SCH_COMPONENT*) bs )->SetTimeStamp( GetNewTimeStamp() );
  354. ( (SCH_COMPONENT*) bs )->ClearAnnotation( NULL );
  355. // Clear flags, which are set by these previous modifications:
  356. bs->ClearFlags();
  357. }
  358. bs = nextbs;
  359. }
  360. }
  361. OnModify();
  362. // redraw base screen (ROOT) if necessary
  363. GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  364. Zoom_Automatique( false );
  365. SetSheetNumberAndCount();
  366. m_canvas->Refresh( true );
  367. return success;
  368. }
  369. void SCH_EDIT_FRAME::OnAppendProject( wxCommandEvent& event )
  370. {
  371. wxString msg = _( "This operation cannot be undone. "
  372. "Besides, take into account that hierarchical sheets will not be appended.\n\n"
  373. "Do you want to save the current document before proceeding?" );
  374. if( IsOK( this, msg ) )
  375. OnSaveProject( event );
  376. AppendOneEEProject();
  377. }
  378. void SCH_EDIT_FRAME::OnSaveProject( wxCommandEvent& aEvent )
  379. {
  380. SCH_SCREEN* screen;
  381. SCH_SCREENS screenList;
  382. // I want to see it in the debugger, show me the string! Can't do that with wxFileName.
  383. wxString fileName = Prj().AbsolutePath( g_RootSheet->GetFileName() );
  384. wxFileName fn = fileName;
  385. if( !fn.IsDirWritable() )
  386. {
  387. wxString msg = wxString::Format( _(
  388. "Directory '%s' is not writable" ),
  389. GetChars( fn.GetPath() )
  390. );
  391. DisplayError( this, msg );
  392. return;
  393. }
  394. for( screen = screenList.GetFirst(); screen; screen = screenList.GetNext() )
  395. SaveEEFile( screen );
  396. CreateArchiveLibraryCacheFile();
  397. UpdateTitle();
  398. }
  399. bool SCH_EDIT_FRAME::doAutoSave()
  400. {
  401. wxFileName tmpFileName = g_RootSheet->GetFileName();
  402. wxFileName fn = tmpFileName;
  403. wxFileName tmp;
  404. SCH_SCREENS screens;
  405. bool autoSaveOk = true;
  406. tmp.AssignDir( fn.GetPath() );
  407. if( !tmp.IsOk() )
  408. return false;
  409. if( !IsWritable( tmp ) )
  410. return false;
  411. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  412. {
  413. // Only create auto save files for the schematics that have been modified.
  414. if( !screen->IsSave() )
  415. continue;
  416. tmpFileName = fn = screen->GetFileName();
  417. // Auto save file name is the normal file name prefixed with AUTOSAVE_PREFIX_FILENAME.
  418. fn.SetName( AUTOSAVE_PREFIX_FILENAME + fn.GetName() );
  419. screen->SetFileName( fn.GetFullPath() );
  420. if( SaveEEFile( screen, false, NO_BACKUP_FILE ) )
  421. screen->SetModify();
  422. else
  423. autoSaveOk = false;
  424. screen->SetFileName( tmpFileName.GetFullPath() );
  425. }
  426. if( autoSaveOk )
  427. m_autoSaveState = false;
  428. return autoSaveOk;
  429. }