2013-07-19 18:27:22 +00:00
|
|
|
/**
|
2013-09-17 00:52:08 +00:00
|
|
|
* @file pagelayout_editor/files.cpp
|
2013-07-19 18:27:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2013-08-13 17:51:22 +00:00
|
|
|
* Copyright (C) 2013 CERN
|
2017-11-12 00:31:38 +00:00
|
|
|
* Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2013-08-13 17:51:22 +00:00
|
|
|
* @author Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2013-07-19 18:27:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fctsys.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <class_drawpanel.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <gestfich.h>
|
|
|
|
#include <macros.h>
|
|
|
|
#include <worksheet_shape_builder.h>
|
|
|
|
|
|
|
|
#include <pl_editor_frame.h>
|
2013-07-20 19:36:19 +00:00
|
|
|
#include <properties_frame.h>
|
2013-07-19 18:27:22 +00:00
|
|
|
#include <pl_editor_id.h>
|
|
|
|
#include <wildcards_and_files_ext.h>
|
|
|
|
|
2018-08-11 20:46:03 +00:00
|
|
|
|
|
|
|
bool PL_EDITOR_FRAME::saveCurrentPageLayout()
|
|
|
|
{
|
|
|
|
wxCommandEvent saveEvent;
|
|
|
|
saveEvent.SetId( wxID_SAVE );
|
|
|
|
Files_io( saveEvent );
|
|
|
|
|
|
|
|
return( !GetScreen()->IsModify() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
wxString filename;
|
|
|
|
|
|
|
|
filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );
|
|
|
|
|
|
|
|
if( filename != wxEmptyString )
|
|
|
|
{
|
2018-10-19 10:02:38 +00:00
|
|
|
if( GetScreen()->IsModify() )
|
2018-08-11 20:46:03 +00:00
|
|
|
{
|
2018-10-19 10:02:38 +00:00
|
|
|
if( !HandleUnsavedChanges( this,
|
|
|
|
_( "The current page layout has been modified. Save changes?" ),
|
|
|
|
[&]()->bool { return saveCurrentPageLayout(); } ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-08-11 20:46:03 +00:00
|
|
|
}
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2018-08-11 20:46:03 +00:00
|
|
|
m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
|
2013-07-19 18:27:22 +00:00
|
|
|
::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
|
2013-07-20 19:36:19 +00:00
|
|
|
|
2018-08-11 20:46:03 +00:00
|
|
|
if( LoadPageLayoutDescrFile( filename ) )
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
msg.Printf( _( "File \"%s\" loaded"), GetChars( filename ) );
|
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2018-08-11 20:46:03 +00:00
|
|
|
OnNewPageLayout();
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
/* File commands. */
|
|
|
|
void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
int id = event.GetId();
|
|
|
|
wxString filename = GetCurrFileName();
|
|
|
|
WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
|
|
|
|
|
|
|
|
if( filename.IsEmpty() && id == wxID_SAVE )
|
|
|
|
id = wxID_SAVEAS;
|
|
|
|
|
2018-08-11 20:46:03 +00:00
|
|
|
if( ( id == wxID_NEW || id == wxID_OPEN ) && GetScreen()->IsModify() )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2018-10-19 10:02:38 +00:00
|
|
|
if( !HandleUnsavedChanges( this, _( "The current page layout has been modified. Save changes?" ),
|
2018-08-11 20:46:03 +00:00
|
|
|
[&]()->bool { return saveCurrentPageLayout(); } ) )
|
|
|
|
{
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
2018-08-11 20:46:03 +00:00
|
|
|
}
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch( id )
|
|
|
|
{
|
|
|
|
case wxID_NEW:
|
|
|
|
pglayout.AllowVoidList( true );
|
|
|
|
SetCurrFileName( wxEmptyString );
|
|
|
|
pglayout.ClearList();
|
2013-07-20 19:36:19 +00:00
|
|
|
OnNewPageLayout();
|
2013-07-19 18:27:22 +00:00
|
|
|
break;
|
|
|
|
|
2013-08-13 17:51:22 +00:00
|
|
|
case ID_APPEND_DESCR_FILE:
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2017-11-12 00:31:38 +00:00
|
|
|
wxFileDialog openFileDialog( this, _( "Append Existing Page Layout File" ),
|
|
|
|
wxEmptyString, wxEmptyString,
|
|
|
|
PageLayoutDescrFileWildcard(), wxFD_OPEN );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
if( openFileDialog.ShowModal() == wxID_CANCEL )
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
filename = openFileDialog.GetPath();
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
if( ! InsertPageLayoutDescrFile( filename ) )
|
|
|
|
{
|
2017-11-12 00:31:38 +00:00
|
|
|
msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
wxMessageBox( msg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetScreen()->SetModify();
|
|
|
|
RebuildDesignTree();
|
|
|
|
m_canvas->Refresh();
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _( "File \"%s\" inserted" ), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxID_OPEN:
|
|
|
|
{
|
2017-11-12 00:31:38 +00:00
|
|
|
wxFileDialog openFileDialog( this, _( "Open" ), wxEmptyString, wxEmptyString,
|
|
|
|
PageLayoutDescrFileWildcard(), wxFD_OPEN );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
if( openFileDialog.ShowModal() == wxID_CANCEL )
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
filename = openFileDialog.GetPath();
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
if( ! LoadPageLayoutDescrFile( filename ) )
|
|
|
|
{
|
2017-11-12 00:31:38 +00:00
|
|
|
msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
wxMessageBox( msg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-20 19:36:19 +00:00
|
|
|
OnNewPageLayout();
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _( "File \"%s\" loaded" ), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxID_SAVE:
|
|
|
|
if( !SavePageLayoutDescrFile( filename ) )
|
|
|
|
{
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _( "Unable to write \"%s\"" ), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
wxMessageBox( msg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _("File \"%s\" written"), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxID_SAVEAS:
|
|
|
|
{
|
2017-11-12 00:31:38 +00:00
|
|
|
wxFileDialog openFileDialog( this, _( "Save As" ), wxEmptyString, wxEmptyString,
|
|
|
|
PageLayoutDescrFileWildcard(), wxFD_SAVE );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
if( openFileDialog.ShowModal() == wxID_CANCEL )
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
filename = openFileDialog.GetPath();
|
2013-08-29 07:20:09 +00:00
|
|
|
// Ensure the file has the right extension:
|
2013-08-30 19:28:31 +00:00
|
|
|
// because a name like name.subname.subsubname is legal,
|
|
|
|
// add the right extension without replacing the wxFileName
|
|
|
|
// extension
|
2013-08-29 07:20:09 +00:00
|
|
|
wxFileName fn(filename);
|
2013-08-30 19:28:31 +00:00
|
|
|
|
|
|
|
if( fn.GetExt() != PageLayoutDescrFileExtension )
|
|
|
|
filename << wxT(".") << PageLayoutDescrFileExtension;
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
if( !SavePageLayoutDescrFile( filename ) )
|
|
|
|
{
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _("Unable to create \"%s\""), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
wxMessageBox( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _("File \"%s\" written"), GetChars( filename ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
if( GetCurrFileName().IsEmpty() )
|
|
|
|
SetCurrFileName( filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxMessageBox( wxT( "File_io: unexpected command id" ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
if( wxFileExists( aFullFileName ) )
|
|
|
|
{
|
|
|
|
WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName );
|
|
|
|
SetCurrFileName( aFullFileName );
|
|
|
|
UpdateFileHistory( aFullFileName );
|
|
|
|
GetScreen()->ClrModify();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
if( wxFileExists( aFullFileName ) )
|
|
|
|
{
|
|
|
|
const bool append = true;
|
2013-07-26 12:50:29 +00:00
|
|
|
SaveCopyInUndoList();
|
2013-07-19 18:27:22 +00:00
|
|
|
WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
|
|
|
|
{
|
|
|
|
if( ! aFullFileName.IsEmpty() )
|
|
|
|
{
|
|
|
|
WORKSHEET_LAYOUT::GetTheInstance().Save( aFullFileName );
|
|
|
|
GetScreen()->ClrModify();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|