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.

257 lines
7.3 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 ID_LOAD_DEFAULT_PAGE_LAYOUT:
  71. case wxID_NEW:
  72. case wxID_OPEN:
  73. if( GetScreen()->IsModify() && !IsOK( this,
  74. _( "The current page layout has been modified.\n"
  75. "Do you wish to discard the changes?" ) ) )
  76. return;
  77. break;
  78. default:
  79. break;
  80. }
  81. switch( id )
  82. {
  83. case ID_LOAD_DEFAULT_PAGE_LAYOUT:
  84. pglayout.SetPageLayout();
  85. OnNewPageLayout();
  86. break;
  87. case wxID_NEW:
  88. pglayout.AllowVoidList( true );
  89. SetCurrFileName( wxEmptyString );
  90. pglayout.ClearList();
  91. OnNewPageLayout();
  92. break;
  93. case ID_APPEND_DESCR_FILE:
  94. {
  95. wxFileDialog openFileDialog(this, _("Append Page Layout Descr File"),
  96. wxEmptyString,
  97. wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
  98. if (openFileDialog.ShowModal() == wxID_CANCEL)
  99. return;
  100. filename = openFileDialog.GetPath();
  101. if( ! InsertPageLayoutDescrFile( filename ) )
  102. {
  103. msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
  104. wxMessageBox( msg );
  105. }
  106. else
  107. {
  108. GetScreen()->SetModify();
  109. RebuildDesignTree();
  110. m_canvas->Refresh();
  111. msg.Printf( _("File <%s> inserted"), GetChars( filename ) );
  112. SetStatusText( msg );
  113. }
  114. }
  115. break;
  116. case wxID_OPEN:
  117. {
  118. wxFileDialog openFileDialog(this, _("Open file"), wxEmptyString,
  119. wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
  120. if (openFileDialog.ShowModal() == wxID_CANCEL)
  121. return;
  122. filename = openFileDialog.GetPath();
  123. if( ! LoadPageLayoutDescrFile( filename ) )
  124. {
  125. msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
  126. wxMessageBox( msg );
  127. }
  128. else
  129. {
  130. OnNewPageLayout();
  131. msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
  132. SetStatusText( msg );
  133. }
  134. }
  135. break;
  136. case wxID_SAVE:
  137. if( !SavePageLayoutDescrFile( filename ) )
  138. {
  139. msg.Printf( _("Unable to write <%s>"), GetChars( filename ) );
  140. wxMessageBox( msg );
  141. }
  142. else
  143. {
  144. msg.Printf( _("File <%s> written"), GetChars( filename ) );
  145. SetStatusText( msg );
  146. }
  147. break;
  148. case wxID_SAVEAS:
  149. {
  150. wxFileDialog openFileDialog(this, _("Create file"), wxEmptyString,
  151. wxEmptyString, PageLayoutDescrFileWildcard, wxFD_SAVE);
  152. if (openFileDialog.ShowModal() == wxID_CANCEL)
  153. return;
  154. filename = openFileDialog.GetPath();
  155. // Ensure the file has the right extension:
  156. // because a name like name.subname.subsubname is legal,
  157. // add the right extension without replacing the wxFileName
  158. // extension
  159. wxFileName fn(filename);
  160. if( fn.GetExt() != PageLayoutDescrFileExtension )
  161. filename << wxT(".") << PageLayoutDescrFileExtension;
  162. if( !SavePageLayoutDescrFile( filename ) )
  163. {
  164. msg.Printf( _("Unable to create <%s>"), GetChars( filename ) );
  165. wxMessageBox( msg );
  166. }
  167. else
  168. {
  169. msg.Printf( _("File <%s> written"), GetChars( filename ) );
  170. SetStatusText( msg );
  171. if( GetCurrFileName().IsEmpty() )
  172. SetCurrFileName( filename );
  173. }
  174. }
  175. break;
  176. default:
  177. wxMessageBox( wxT( "File_io: unexpected command id" ) );
  178. break;
  179. }
  180. }
  181. /* Loads a .kicad_wks page layout descr file
  182. */
  183. bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
  184. {
  185. if( wxFileExists( aFullFileName ) )
  186. {
  187. WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName );
  188. SetCurrFileName( aFullFileName );
  189. UpdateFileHistory( aFullFileName );
  190. GetScreen()->ClrModify();
  191. return true;
  192. }
  193. return false;
  194. }
  195. /* Inserts a .kicad_wks page layout descr file
  196. * same as LoadPageLayoutDescrFile, but the new data is added
  197. * to the previous data.
  198. */
  199. bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
  200. {
  201. if( wxFileExists( aFullFileName ) )
  202. {
  203. const bool append = true;
  204. SaveCopyInUndoList();
  205. WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
  206. return true;
  207. }
  208. return false;
  209. }
  210. /* Save the current layout in a .kicad_wks page layout descr file
  211. */
  212. bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
  213. {
  214. if( ! aFullFileName.IsEmpty() )
  215. {
  216. WORKSHEET_LAYOUT::GetTheInstance().Save( aFullFileName );
  217. GetScreen()->ClrModify();
  218. return true;
  219. }
  220. return false;
  221. }