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.

251 lines
7.1 KiB

  1. /**
  2. * @file pagelayout_editor/files.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 2013 CERN
  8. * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include <fctsys.h>
  28. #include <common.h>
  29. #include <class_drawpanel.h>
  30. #include <confirm.h>
  31. #include <gestfich.h>
  32. #include <macros.h>
  33. #include <worksheet_shape_builder.h>
  34. #include <pl_editor_frame.h>
  35. #include <properties_frame.h>
  36. #include <pl_editor_id.h>
  37. #include <wildcards_and_files_ext.h>
  38. void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
  39. {
  40. wxString filename;
  41. filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );
  42. if( filename != wxEmptyString )
  43. {
  44. if( GetScreen()->IsModify() && !IsOK( this,
  45. _( "The current page layout has been modified.\n"
  46. "Do you wish to discard the changes?" ) ) )
  47. return;
  48. m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
  49. ::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
  50. if( LoadPageLayoutDescrFile( filename ) )
  51. {
  52. wxString msg;
  53. msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
  54. SetStatusText( msg );
  55. }
  56. OnNewPageLayout();
  57. }
  58. }
  59. /* File commands. */
  60. void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
  61. {
  62. wxString msg;
  63. int id = event.GetId();
  64. wxString filename = GetCurrFileName();
  65. WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
  66. if( filename.IsEmpty() && id == wxID_SAVE )
  67. id = wxID_SAVEAS;
  68. switch( id )
  69. {
  70. case wxID_NEW:
  71. case wxID_OPEN:
  72. if( GetScreen()->IsModify() && !IsOK( this,
  73. _( "The current page layout has been modified.\n"
  74. "Do you wish to discard the changes?" ) ) )
  75. return;
  76. break;
  77. default:
  78. break;
  79. }
  80. switch( id )
  81. {
  82. case wxID_NEW:
  83. pglayout.AllowVoidList( true );
  84. SetCurrFileName( wxEmptyString );
  85. pglayout.ClearList();
  86. OnNewPageLayout();
  87. break;
  88. case ID_APPEND_DESCR_FILE:
  89. {
  90. wxFileDialog openFileDialog(this, _("Append Existing Page Layout File"),
  91. wxEmptyString,
  92. wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
  93. if (openFileDialog.ShowModal() == wxID_CANCEL)
  94. return;
  95. filename = openFileDialog.GetPath();
  96. if( ! InsertPageLayoutDescrFile( filename ) )
  97. {
  98. msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
  99. wxMessageBox( msg );
  100. }
  101. else
  102. {
  103. GetScreen()->SetModify();
  104. RebuildDesignTree();
  105. m_canvas->Refresh();
  106. msg.Printf( _("File <%s> inserted"), GetChars( filename ) );
  107. SetStatusText( msg );
  108. }
  109. }
  110. break;
  111. case wxID_OPEN:
  112. {
  113. wxFileDialog openFileDialog(this, _("Open"), wxEmptyString,
  114. wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
  115. if (openFileDialog.ShowModal() == wxID_CANCEL)
  116. return;
  117. filename = openFileDialog.GetPath();
  118. if( ! LoadPageLayoutDescrFile( filename ) )
  119. {
  120. msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
  121. wxMessageBox( msg );
  122. }
  123. else
  124. {
  125. OnNewPageLayout();
  126. msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
  127. SetStatusText( msg );
  128. }
  129. }
  130. break;
  131. case wxID_SAVE:
  132. if( !SavePageLayoutDescrFile( filename ) )
  133. {
  134. msg.Printf( _("Unable to write <%s>"), GetChars( filename ) );
  135. wxMessageBox( msg );
  136. }
  137. else
  138. {
  139. msg.Printf( _("File <%s> written"), GetChars( filename ) );
  140. SetStatusText( msg );
  141. }
  142. break;
  143. case wxID_SAVEAS:
  144. {
  145. wxFileDialog openFileDialog(this, _("Save As"), wxEmptyString,
  146. wxEmptyString, PageLayoutDescrFileWildcard, wxFD_SAVE);
  147. if (openFileDialog.ShowModal() == wxID_CANCEL)
  148. return;
  149. filename = openFileDialog.GetPath();
  150. // Ensure the file has the right extension:
  151. // because a name like name.subname.subsubname is legal,
  152. // add the right extension without replacing the wxFileName
  153. // extension
  154. wxFileName fn(filename);
  155. if( fn.GetExt() != PageLayoutDescrFileExtension )
  156. filename << wxT(".") << PageLayoutDescrFileExtension;
  157. if( !SavePageLayoutDescrFile( filename ) )
  158. {
  159. msg.Printf( _("Unable to create <%s>"), GetChars( filename ) );
  160. wxMessageBox( msg );
  161. }
  162. else
  163. {
  164. msg.Printf( _("File <%s> written"), GetChars( filename ) );
  165. SetStatusText( msg );
  166. if( GetCurrFileName().IsEmpty() )
  167. SetCurrFileName( filename );
  168. }
  169. }
  170. break;
  171. default:
  172. wxMessageBox( wxT( "File_io: unexpected command id" ) );
  173. break;
  174. }
  175. }
  176. /* Loads a .kicad_wks page layout descr file
  177. */
  178. bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
  179. {
  180. if( wxFileExists( aFullFileName ) )
  181. {
  182. WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName );
  183. SetCurrFileName( aFullFileName );
  184. UpdateFileHistory( aFullFileName );
  185. GetScreen()->ClrModify();
  186. return true;
  187. }
  188. return false;
  189. }
  190. /* Inserts a .kicad_wks page layout descr file
  191. * same as LoadPageLayoutDescrFile, but the new data is added
  192. * to the previous data.
  193. */
  194. bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
  195. {
  196. if( wxFileExists( aFullFileName ) )
  197. {
  198. const bool append = true;
  199. SaveCopyInUndoList();
  200. WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
  201. return true;
  202. }
  203. return false;
  204. }
  205. /* Save the current layout in a .kicad_wks page layout descr file
  206. */
  207. bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
  208. {
  209. if( ! aFullFileName.IsEmpty() )
  210. {
  211. WORKSHEET_LAYOUT::GetTheInstance().Save( aFullFileName );
  212. GetScreen()->ClrModify();
  213. return true;
  214. }
  215. return false;
  216. }