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.

289 lines
8.2 KiB

  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) 2017-2019 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
  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. #include <confirm.h>
  27. #include <gestfich.h>
  28. #include <ws_draw_item.h>
  29. #include <ws_data_model.h>
  30. #include <pl_editor_frame.h>
  31. #include <properties_frame.h>
  32. #include <pl_editor_id.h>
  33. #include <widgets/infobar.h>
  34. #include <wildcards_and_files_ext.h>
  35. bool PL_EDITOR_FRAME::saveCurrentPageLayout()
  36. {
  37. wxCommandEvent saveEvent;
  38. saveEvent.SetId( wxID_SAVE );
  39. Files_io( saveEvent );
  40. return !IsContentModified();
  41. }
  42. void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
  43. {
  44. wxString filename;
  45. filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );
  46. if( filename != wxEmptyString )
  47. {
  48. if( IsContentModified() )
  49. {
  50. if( !HandleUnsavedChanges( this, _( "The current page layout has been modified. "
  51. "Save changes?" ),
  52. [&]()->bool { return saveCurrentPageLayout(); } ) )
  53. {
  54. return;
  55. }
  56. }
  57. ::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
  58. if( LoadPageLayoutDescrFile( filename ) )
  59. {
  60. wxString msg;
  61. msg.Printf( _( "File \"%s\" loaded"), GetChars( filename ) );
  62. SetStatusText( msg );
  63. }
  64. OnNewPageLayout();
  65. }
  66. }
  67. void PL_EDITOR_FRAME::OnClearFileHistory( wxCommandEvent& aEvent )
  68. {
  69. ClearFileHistory();
  70. }
  71. /* File commands. */
  72. void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
  73. {
  74. wxString msg;
  75. int id = event.GetId();
  76. wxString filename = GetCurrentFileName();
  77. WS_DATA_MODEL& pglayout = WS_DATA_MODEL::GetTheInstance();
  78. if( filename.IsEmpty() && id == wxID_SAVE )
  79. id = wxID_SAVEAS;
  80. if( ( id == wxID_NEW || id == wxID_OPEN ) && IsContentModified() )
  81. {
  82. if( !HandleUnsavedChanges( this, _( "The current page layout has been modified. "
  83. "Save changes?" ),
  84. [&]()->bool { return saveCurrentPageLayout(); } ) )
  85. {
  86. return;
  87. }
  88. }
  89. switch( id )
  90. {
  91. case wxID_NEW:
  92. pglayout.AllowVoidList( true );
  93. SetCurrentFileName( wxEmptyString );
  94. pglayout.ClearList();
  95. OnNewPageLayout();
  96. break;
  97. case ID_APPEND_DESCR_FILE:
  98. {
  99. wxFileDialog openFileDialog( this, _( "Append Existing Page Layout File" ),
  100. wxEmptyString, wxEmptyString,
  101. PageLayoutDescrFileWildcard(), wxFD_OPEN );
  102. if( openFileDialog.ShowModal() == wxID_CANCEL )
  103. return;
  104. filename = openFileDialog.GetPath();
  105. if( ! InsertPageLayoutDescrFile( filename ) )
  106. {
  107. msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
  108. wxMessageBox( msg );
  109. }
  110. else
  111. {
  112. GetScreen()->SetModify();
  113. HardRedraw();
  114. msg.Printf( _( "File \"%s\" inserted" ), GetChars( filename ) );
  115. SetStatusText( msg );
  116. }
  117. }
  118. break;
  119. case wxID_OPEN:
  120. {
  121. wxFileDialog openFileDialog( this, _( "Open" ), wxEmptyString, wxEmptyString,
  122. PageLayoutDescrFileWildcard(), wxFD_OPEN );
  123. if( openFileDialog.ShowModal() == wxID_CANCEL )
  124. return;
  125. filename = openFileDialog.GetPath();
  126. if( ! LoadPageLayoutDescrFile( filename ) )
  127. {
  128. msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
  129. wxMessageBox( msg );
  130. }
  131. else
  132. {
  133. OnNewPageLayout();
  134. msg.Printf( _( "File \"%s\" loaded" ), GetChars( filename ) );
  135. SetStatusText( msg );
  136. }
  137. }
  138. break;
  139. case wxID_SAVE:
  140. if( !SavePageLayoutDescrFile( filename ) )
  141. {
  142. msg.Printf( _( "Unable to write \"%s\"" ), GetChars( filename ) );
  143. wxMessageBox( msg );
  144. }
  145. else
  146. {
  147. msg.Printf( _("File \"%s\" written"), GetChars( filename ) );
  148. SetStatusText( msg );
  149. }
  150. break;
  151. case wxID_SAVEAS:
  152. {
  153. wxFileDialog openFileDialog( this, _( "Save As" ), wxEmptyString, wxEmptyString,
  154. PageLayoutDescrFileWildcard(), wxFD_SAVE );
  155. if( openFileDialog.ShowModal() == wxID_CANCEL )
  156. return;
  157. filename = openFileDialog.GetPath();
  158. // Ensure the file has the right extension:
  159. // because a name like name.subname.subsubname is legal,
  160. // add the right extension without replacing the wxFileName
  161. // extension
  162. wxFileName fn(filename);
  163. if( fn.GetExt() != PageLayoutDescrFileExtension )
  164. filename << wxT(".") << PageLayoutDescrFileExtension;
  165. if( !SavePageLayoutDescrFile( filename ) )
  166. {
  167. msg.Printf( _("Unable to create \"%s\""), GetChars( filename ) );
  168. wxMessageBox( msg );
  169. }
  170. else
  171. {
  172. msg.Printf( _("File \"%s\" written"), GetChars( filename ) );
  173. SetStatusText( msg );
  174. if( GetCurrentFileName().IsEmpty() )
  175. SetCurrentFileName( filename );
  176. }
  177. }
  178. break;
  179. default:
  180. wxMessageBox( wxT( "File_io: unexpected command id" ) );
  181. break;
  182. }
  183. }
  184. bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
  185. {
  186. if( wxFileExists( aFullFileName ) )
  187. {
  188. WS_DATA_MODEL::GetTheInstance().SetPageLayout( aFullFileName );
  189. SetCurrentFileName( aFullFileName );
  190. UpdateFileHistory( aFullFileName );
  191. GetScreen()->ClrModify();
  192. wxFileName fn = aFullFileName;
  193. m_infoBar->Dismiss();
  194. if( fn.FileExists() && !fn.IsFileWritable() )
  195. {
  196. m_infoBar->RemoveAllButtons();
  197. m_infoBar->AddCloseButton();
  198. m_infoBar->ShowMessage( "Layout file is read only.", wxICON_WARNING );
  199. }
  200. return true;
  201. }
  202. return false;
  203. }
  204. bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
  205. {
  206. if( wxFileExists( aFullFileName ) )
  207. {
  208. const bool append = true;
  209. SaveCopyInUndoList();
  210. WS_DATA_MODEL::GetTheInstance().SetPageLayout( aFullFileName, append );
  211. return true;
  212. }
  213. return false;
  214. }
  215. bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
  216. {
  217. if( !aFullFileName.IsEmpty() )
  218. {
  219. wxFileName tempFile( aFullFileName );
  220. tempFile.SetName( wxT( "." ) + tempFile.GetName() );
  221. tempFile.SetExt( tempFile.GetExt() + wxT( "$" ) );
  222. try
  223. {
  224. WS_DATA_MODEL::GetTheInstance().Save( tempFile.GetFullPath() );
  225. }
  226. catch( const IO_ERROR& ioe )
  227. {
  228. // In case we started a file but didn't fully write it, clean up
  229. wxRemoveFile( tempFile.GetFullPath() );
  230. return false;
  231. }
  232. if( !wxRenameFile( tempFile.GetFullPath(), aFullFileName ) )
  233. return false;
  234. GetScreen()->ClrModify();
  235. return true;
  236. }
  237. return false;
  238. }