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.

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