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
|
2019-05-25 11:05:39 +00:00
|
|
|
* Copyright (C) 2017-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-11-12 00:31:38 +00:00
|
|
|
*
|
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 <confirm.h>
|
|
|
|
#include <gestfich.h>
|
2020-10-14 00:23:36 +00:00
|
|
|
#include <page_layout/ws_data_model.h>
|
2020-05-20 01:31:47 +00:00
|
|
|
#include <widgets/infobar.h>
|
2013-07-19 18:27:22 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
|
|
|
|
2020-10-13 05:04:31 +00:00
|
|
|
#include "pl_editor_frame.h"
|
|
|
|
#include "pl_editor_id.h"
|
|
|
|
#include "properties_frame.h"
|
2018-08-11 20:46:03 +00:00
|
|
|
|
|
|
|
bool PL_EDITOR_FRAME::saveCurrentPageLayout()
|
|
|
|
{
|
|
|
|
wxCommandEvent saveEvent;
|
|
|
|
saveEvent.SetId( wxID_SAVE );
|
|
|
|
Files_io( saveEvent );
|
|
|
|
|
2019-12-19 15:34:01 +00:00
|
|
|
return !IsContentModified();
|
2018-08-11 20:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
2019-12-19 15:34:01 +00:00
|
|
|
if( IsContentModified() )
|
2018-08-11 20:46:03 +00:00
|
|
|
{
|
2019-06-17 15:59:39 +00:00
|
|
|
if( !HandleUnsavedChanges( this, _( "The current page layout has been modified. "
|
|
|
|
"Save changes?" ),
|
2018-10-19 10:02:38 +00:00
|
|
|
[&]()->bool { return saveCurrentPageLayout(); } ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-08-11 20:46:03 +00:00
|
|
|
}
|
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;
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "File \"%s\" loaded"), filename );
|
2018-08-11 20:46:03 +00:00
|
|
|
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
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void PL_EDITOR_FRAME::OnClearFileHistory( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
ClearFileHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
/* File commands. */
|
|
|
|
void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
|
|
|
|
{
|
2019-05-25 11:05:39 +00:00
|
|
|
wxString msg;
|
|
|
|
int id = event.GetId();
|
2020-05-16 23:08:01 +00:00
|
|
|
wxString filename = GetCurrentFileName();
|
2019-05-25 11:05:39 +00:00
|
|
|
WS_DATA_MODEL& pglayout = WS_DATA_MODEL::GetTheInstance();
|
2013-07-19 18:27:22 +00:00
|
|
|
|
|
|
|
if( filename.IsEmpty() && id == wxID_SAVE )
|
|
|
|
id = wxID_SAVEAS;
|
|
|
|
|
2019-12-19 15:34:01 +00:00
|
|
|
if( ( id == wxID_NEW || id == wxID_OPEN ) && IsContentModified() )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2019-06-17 15:59:39 +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 );
|
2020-05-16 23:08:01 +00:00
|
|
|
SetCurrentFileName( wxEmptyString );
|
2013-07-19 18:27:22 +00:00
|
|
|
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 ) )
|
|
|
|
{
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "Unable to load %s file" ), filename );
|
2020-12-09 00:28:48 +00:00
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetScreen()->SetModify();
|
2019-06-13 13:51:06 +00:00
|
|
|
HardRedraw();
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "File \"%s\" inserted" ), 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 ) )
|
|
|
|
{
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "Unable to load %s file" ), filename );
|
2020-12-09 00:28:48 +00:00
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-20 19:36:19 +00:00
|
|
|
OnNewPageLayout();
|
2020-12-08 23:58:07 +00:00
|
|
|
msg.Printf( _( "File \"%s\" saved." ), filename );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxID_SAVE:
|
|
|
|
if( !SavePageLayoutDescrFile( filename ) )
|
|
|
|
{
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "Unable to write \"%s\"" ), filename );
|
2020-12-09 00:28:48 +00:00
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-08 23:58:07 +00:00
|
|
|
msg.Printf( _("File \"%s\" saved."), 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 ) )
|
|
|
|
{
|
2020-12-09 00:28:48 +00:00
|
|
|
msg.Printf( _( "Unable to create \"%s\"" ), filename );
|
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-12-08 23:58:07 +00:00
|
|
|
msg.Printf( _("File \"%s\" saved."), filename );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2020-05-16 23:08:01 +00:00
|
|
|
if( GetCurrentFileName().IsEmpty() )
|
|
|
|
SetCurrentFileName( filename );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
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 ) )
|
|
|
|
{
|
2019-05-25 11:05:39 +00:00
|
|
|
WS_DATA_MODEL::GetTheInstance().SetPageLayout( aFullFileName );
|
2020-05-16 23:08:01 +00:00
|
|
|
SetCurrentFileName( aFullFileName );
|
2013-07-19 18:27:22 +00:00
|
|
|
UpdateFileHistory( aFullFileName );
|
|
|
|
GetScreen()->ClrModify();
|
2020-05-18 23:44:58 +00:00
|
|
|
|
|
|
|
wxFileName fn = aFullFileName;
|
2020-05-20 01:31:47 +00:00
|
|
|
m_infoBar->Dismiss();
|
2020-05-18 23:44:58 +00:00
|
|
|
|
|
|
|
if( fn.FileExists() && !fn.IsFileWritable() )
|
|
|
|
{
|
|
|
|
m_infoBar->RemoveAllButtons();
|
|
|
|
m_infoBar->AddCloseButton();
|
2021-01-12 13:26:18 +00:00
|
|
|
m_infoBar->ShowMessage( _( "Layout file is read only." ), wxICON_WARNING );
|
2020-05-18 23:44:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
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();
|
2019-05-25 11:05:39 +00:00
|
|
|
WS_DATA_MODEL::GetTheInstance().SetPageLayout( aFullFileName, append );
|
2013-07-19 18:27:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
|
|
|
|
{
|
2020-06-19 23:31:26 +00:00
|
|
|
if( !aFullFileName.IsEmpty() )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2020-06-19 23:31:26 +00:00
|
|
|
wxFileName tempFile( aFullFileName );
|
|
|
|
tempFile.SetName( wxT( "." ) + tempFile.GetName() );
|
|
|
|
tempFile.SetExt( tempFile.GetExt() + wxT( "$" ) );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
WS_DATA_MODEL::GetTheInstance().Save( tempFile.GetFullPath() );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
// In case we started a file but didn't fully write it, clean up
|
|
|
|
wxRemoveFile( tempFile.GetFullPath() );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !wxRenameFile( tempFile.GetFullPath(), aFullFileName ) )
|
|
|
|
return false;
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
GetScreen()->ClrModify();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|