2011-09-30 14:15:37 -04:00
|
|
|
/**
|
|
|
|
* @file class_treeproject_item.cpp
|
2010-02-20 13:20:55 +00:00
|
|
|
*
|
2012-12-31 09:12:29 +01:00
|
|
|
* @brief Class TREEPROJECT_ITEM is a derived class from wxTreeItemData and
|
2011-09-30 14:15:37 -04:00
|
|
|
* store info about a file or directory shown in the KiCad tree project files
|
2010-02-20 13:20:55 +00:00
|
|
|
*/
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-01-22 22:33:36 -06:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <gestfich.h>
|
2012-04-16 14:56:01 +02:00
|
|
|
#include <macros.h>
|
2010-02-20 13:20:55 +00:00
|
|
|
|
2012-01-22 22:33:36 -06:00
|
|
|
#include <kicad.h>
|
|
|
|
#include <tree_project_frame.h>
|
|
|
|
#include <class_treeprojectfiles.h>
|
|
|
|
#include <class_treeproject_item.h>
|
|
|
|
#include <wx/imaglist.h>
|
2010-02-20 13:20:55 +00:00
|
|
|
|
2012-01-22 22:33:36 -06:00
|
|
|
#include <wx/regex.h>
|
|
|
|
#include <wx/dir.h>
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
TREEPROJECT_ITEM::TREEPROJECT_ITEM( enum TreeFileType type, const wxString& data,
|
2012-12-31 09:12:29 +01:00
|
|
|
wxTreeCtrl* parent ) :
|
2010-02-20 13:20:55 +00:00
|
|
|
wxTreeItemData()
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
m_Type = type;
|
|
|
|
m_parent = parent;
|
|
|
|
m_FileName = data;
|
|
|
|
m_IsRootFile = false; // true only for the root item of the tree (the project name)
|
|
|
|
m_WasPopulated = false;
|
2010-02-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
// Set the state used in the icon list
|
|
|
|
void TREEPROJECT_ITEM::SetState( int state )
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
wxImageList* imglist = m_parent->GetImageList();
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
if( !imglist || state < 0 || state >= imglist->GetImageCount() / ( TREE_MAX - 2 ) )
|
|
|
|
return;
|
2012-12-31 09:12:29 +01:00
|
|
|
|
|
|
|
m_state = state;
|
2010-02-20 13:20:55 +00:00
|
|
|
int imgid = m_Type - 1 + state * ( TREE_MAX - 1 );
|
2012-12-31 09:12:29 +01:00
|
|
|
m_parent->SetItemImage( GetId(), imgid );
|
|
|
|
m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
|
2010-02-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get the directory containing the file */
|
|
|
|
wxString TREEPROJECT_ITEM::GetDir() const
|
|
|
|
{
|
|
|
|
if( TREE_DIRECTORY == m_Type )
|
|
|
|
return m_FileName;
|
|
|
|
|
|
|
|
wxFileName filename = wxFileName( m_FileName );
|
|
|
|
|
|
|
|
filename.MakeRelativeTo( wxGetCwd() );
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
wxArrayString dirs = filename.GetDirs();
|
|
|
|
|
|
|
|
wxString dir;
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
for( unsigned int i = 0; i < dirs.Count(); i++ )
|
|
|
|
{
|
|
|
|
dir += dirs[i] + filename.GetPathSeparator();
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Move the object to dest
|
|
|
|
void TREEPROJECT_ITEM::Move( TREEPROJECT_ITEM* dest )
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
// function not safe.
|
2010-02-20 13:20:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
const wxString sep = wxFileName().GetPathSeparator();
|
|
|
|
|
|
|
|
if( m_Type == TREE_DIRECTORY )
|
|
|
|
return;
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
if( !dest )
|
|
|
|
return;
|
2012-12-31 09:12:29 +01:00
|
|
|
|
|
|
|
if( m_parent != dest->m_parent )
|
2010-02-20 13:20:55 +00:00
|
|
|
return; // Can not cross move!
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
if( dest == this )
|
|
|
|
return; // Can not move to ourself...
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
wxTreeItemId parent = m_parent->GetItemParent( GetId() );
|
|
|
|
|
|
|
|
if( dest == dynamic_cast<TREEPROJECT_ITEM*>( m_parent->GetItemData( parent ) ) )
|
2010-02-20 13:20:55 +00:00
|
|
|
return; // same parent ?
|
|
|
|
|
|
|
|
// We need to create a new item from us, and move
|
|
|
|
// data to there ...
|
|
|
|
|
|
|
|
// First move file on the disk
|
2012-12-31 09:12:29 +01:00
|
|
|
wxFileName fname( m_FileName );
|
|
|
|
|
|
|
|
wxString destName;
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
if( !dest->GetDir().IsEmpty() )
|
|
|
|
destName = dest->GetDir() + sep;
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
destName += fname.GetFullName();
|
|
|
|
|
|
|
|
if( destName == GetFileName() )
|
|
|
|
return; // Same place ??
|
|
|
|
|
|
|
|
// Move the file on the disk:
|
|
|
|
if( !wxRenameFile( GetFileName(), destName, false ) )
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
wxMessageDialog( m_parent, _( "Unable to move file ... " ),
|
2010-02-20 13:20:55 +00:00
|
|
|
_( "Permission error ?" ), wxICON_ERROR | wxOK );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFileName( destName );
|
|
|
|
|
|
|
|
if( TREE_DIRECTORY != GetType() )
|
|
|
|
{
|
|
|
|
// Move the tree item itself now:
|
2012-12-31 09:12:29 +01:00
|
|
|
wxTreeItemId oldId = GetId();
|
|
|
|
int i = m_parent->GetItemImage( oldId );
|
|
|
|
wxString text = m_parent->GetItemText( oldId );
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
// Bye bye old Id :'(
|
2012-12-31 09:12:29 +01:00
|
|
|
wxTreeItemId newId = m_parent->AppendItem( dest->GetId(), text, i );
|
|
|
|
m_parent->SetItemData( newId, this );
|
|
|
|
m_parent->SetItemData( oldId, NULL );
|
|
|
|
m_parent->Delete( oldId );
|
2010-02-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We should move recursively all files, but that's quite boring
|
|
|
|
// let's just refresh that's all ... TODO (change this to a better code ...)
|
|
|
|
wxCommandEvent dummy;
|
2012-12-31 09:12:29 +01:00
|
|
|
dynamic_cast<TREEPROJECTFILES*>( m_parent )->GetParent()->m_Parent->OnRefresh( dummy );
|
2010-02-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rename the file checking if extension change occurs */
|
|
|
|
bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check )
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
// this is broken & unsafe to use on linux.
|
2010-02-20 13:20:55 +00:00
|
|
|
if( m_Type == TREE_DIRECTORY )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( name.IsEmpty() )
|
|
|
|
return false;
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
const wxString sep = wxFileName().GetPathSeparator();
|
|
|
|
wxString newFile;
|
|
|
|
wxString dirs = GetDir();
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
if( !dirs.IsEmpty() && GetType() != TREE_DIRECTORY )
|
|
|
|
newFile = dirs + sep + name;
|
|
|
|
else
|
|
|
|
newFile = name;
|
|
|
|
|
|
|
|
if( newFile == m_FileName )
|
|
|
|
return false;
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
wxString ext = TREE_PROJECT_FRAME::GetFileExt( GetType() );
|
2010-02-20 13:20:55 +00:00
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
wxRegEx reg( wxT( "^.*\\" ) + ext + wxT( "$" ), wxRE_ICASE );
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
wxMessageDialog dialog( m_parent,
|
|
|
|
_(
|
|
|
|
"Changing file extension will change file \
|
|
|
|
type.\n Do you want to continue ?" ),
|
2010-02-20 13:20:55 +00:00
|
|
|
_( "Rename File" ),
|
|
|
|
wxYES_NO | wxICON_QUESTION );
|
|
|
|
|
|
|
|
if( wxID_YES != dialog.ShowModal() )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ( ( wxMAJOR_VERSION < 2 ) || ( ( wxMAJOR_VERSION == 2 ) \
|
2012-12-31 09:12:29 +01:00
|
|
|
&& ( wxMINOR_VERSION < 7 ) ) )
|
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
if( !wxRenameFile( m_FileName, newFile ) )
|
|
|
|
#else
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
if( !wxRenameFile( m_FileName, newFile, false ) )
|
|
|
|
#endif
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
wxMessageDialog( m_parent, _( "Unable to rename file ... " ),
|
2010-02-20 13:20:55 +00:00
|
|
|
_( "Permission error ?" ), wxICON_ERROR | wxOK );
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
SetFileName( newFile );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************/
|
|
|
|
bool TREEPROJECT_ITEM::Delete( bool check )
|
|
|
|
/*******************************************/
|
|
|
|
/* delete a file */
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
msg.Printf( _( "Do you really want to delete '%s'" ), GetChars( GetFileName() ) );
|
|
|
|
wxMessageDialog dialog( m_parent, msg,
|
2010-02-20 13:20:55 +00:00
|
|
|
_( "Delete File" ), wxYES_NO | wxICON_QUESTION );
|
|
|
|
|
|
|
|
if( !check || wxID_YES == dialog.ShowModal() )
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
bool success;
|
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
if( !wxDirExists( m_FileName ) )
|
2012-12-31 09:12:29 +01:00
|
|
|
success = wxRemoveFile( m_FileName );
|
2010-02-20 13:20:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
wxArrayString filelist;
|
|
|
|
|
|
|
|
wxDir::GetAllFiles( m_FileName, &filelist );
|
|
|
|
|
|
|
|
for( unsigned int i = 0; i < filelist.Count(); i++ )
|
|
|
|
wxRemoveFile( filelist[i] );
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
success = wxRmdir( m_FileName );
|
2010-02-20 13:20:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 09:12:29 +01:00
|
|
|
if( success )
|
|
|
|
m_parent->Delete( GetId() );
|
|
|
|
|
|
|
|
return success;
|
2010-02-20 13:20:55 +00:00
|
|
|
}
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Called under item activation */
|
|
|
|
void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* prjframe )
|
|
|
|
{
|
2012-12-31 09:12:29 +01:00
|
|
|
wxString sep = wxFileName().GetPathSeparator();
|
|
|
|
wxString FullFileName = GetFileName();
|
|
|
|
wxTreeItemId id = GetId();
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
AddDelimiterString( FullFileName );
|
2012-12-31 09:12:29 +01:00
|
|
|
|
2010-02-20 13:20:55 +00:00
|
|
|
switch( GetType() )
|
|
|
|
{
|
|
|
|
case TREE_PROJECT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TREE_DIRECTORY:
|
2012-12-31 09:12:29 +01:00
|
|
|
m_parent->Toggle( id );
|
2010-02-20 13:20:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TREE_SCHEMA:
|
2012-12-31 09:12:29 +01:00
|
|
|
ExecuteFile( m_parent, EESCHEMA_EXE, FullFileName );
|
2010-02-20 13:20:55 +00:00
|
|
|
break;
|
|
|
|
|
2012-04-16 14:56:01 +02:00
|
|
|
case TREE_LEGACY_PCB:
|
|
|
|
case TREE_SEXP_PCB:
|
2012-12-31 09:12:29 +01:00
|
|
|
ExecuteFile( m_parent, PCBNEW_EXE, FullFileName );
|
2010-02-20 13:20:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TREE_GERBER:
|
2012-12-31 09:12:29 +01:00
|
|
|
ExecuteFile( m_parent, GERBVIEW_EXE, FullFileName );
|
2010-02-20 13:20:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TREE_PDF:
|
|
|
|
OpenPDF( FullFileName );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TREE_NET:
|
2012-12-31 09:12:29 +01:00
|
|
|
ExecuteFile( m_parent, CVPCB_EXE, FullFileName );
|
2010-02-20 13:20:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TREE_TXT:
|
2012-12-31 09:12:29 +01:00
|
|
|
{
|
|
|
|
wxString editorname = wxGetApp().GetEditorName();
|
|
|
|
|
|
|
|
if( !editorname.IsEmpty() )
|
|
|
|
ExecuteFile( m_parent, editorname, FullFileName );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2010-02-20 13:20:55 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
OpenFile( FullFileName );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|