files.cpp 7.35 KB
Newer Older
1
/**
2
 * @file pagelayout_editor/files.cpp
3 4 5 6 7
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
8 9
 * Copyright (C) 2013 CERN
 * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * 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>
38
#include <properties_frame.h>
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <pl_editor_id.h>
#include <wildcards_and_files_ext.h>

void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
{
    wxString filename;

    filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );

    if( filename != wxEmptyString )
    {
        if( GetScreen()->IsModify() && !IsOK( this,
                   _( "The current page layout has been modified.\n"
                      "Do you wish to discard the changes?" ) ) )
            return;

         m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
        ::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
        if( LoadPageLayoutDescrFile( filename ) )
        {
            wxString msg;
            msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
            SetStatusText( msg );
        }
63 64

        OnNewPageLayout();
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    }
}

/* 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;

    switch( id )
    {
    case ID_LOAD_DEFAULT_PAGE_LAYOUT:
    case wxID_NEW:
    case wxID_OPEN:
        if( GetScreen()->IsModify() && !IsOK( this,
                   _( "The current page layout has been modified.\n"
                      "Do you wish to discard the changes?" ) ) )
            return;
        break;

    default:
        break;
    }


    switch( id )
    {
    case ID_LOAD_DEFAULT_PAGE_LAYOUT:
        pglayout.SetPageLayout();
99
        OnNewPageLayout();
100 101 102 103 104 105
        break;

    case wxID_NEW:
        pglayout.AllowVoidList( true );
        SetCurrFileName( wxEmptyString );
        pglayout.ClearList();
106
        OnNewPageLayout();
107 108
        break;

109
    case ID_APPEND_DESCR_FILE:
110
    {
111
         wxFileDialog openFileDialog(this, _("Append Page Layout Descr File"),
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                wxEmptyString,
                wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);

        if (openFileDialog.ShowModal() == wxID_CANCEL)
            return;

        filename = openFileDialog.GetPath();
        if( ! InsertPageLayoutDescrFile( filename ) )
        {
            wxString msg;
            msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
            wxMessageBox( msg );
        }
        else
        {
            GetScreen()->SetModify();
            RebuildDesignTree();
            m_canvas->Refresh();
            msg.Printf( _("File <%s> inserted"), GetChars( filename ) );
            SetStatusText( msg );
        }
    }
        break;

    case wxID_OPEN:
    {
         wxFileDialog openFileDialog(this, _("Open file"), wxEmptyString,
                wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);

        if (openFileDialog.ShowModal() == wxID_CANCEL)
            return;

        filename = openFileDialog.GetPath();
        if( ! LoadPageLayoutDescrFile( filename ) )
        {
            wxString msg;
            msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
            wxMessageBox( msg );
        }
        else
        {
153
            OnNewPageLayout();
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
            SetStatusText( msg );
        }
    }
        break;

    case wxID_SAVE:
        if( !SavePageLayoutDescrFile( filename ) )
        {
            msg.Printf( _("Unable to write <%s>"), GetChars( filename ) );
            wxMessageBox( msg );
        }
        else
        {
            msg.Printf( _("File <%s> written"), GetChars( filename ) );
            SetStatusText( msg );
        }
        break;

    case wxID_SAVEAS:
    {
         wxFileDialog openFileDialog(this, _("Create file"), wxEmptyString,
                wxEmptyString, PageLayoutDescrFileWildcard, wxFD_SAVE);

        if (openFileDialog.ShowModal() == wxID_CANCEL)
            return;

        filename = openFileDialog.GetPath();
182
        // Ensure the file has the right extension:
183 184 185
        // because a name like name.subname.subsubname is legal,
        // add the right extension without replacing the wxFileName
        // extension
186
        wxFileName fn(filename);
187 188 189 190

        if( fn.GetExt() != PageLayoutDescrFileExtension )
            filename << wxT(".") << PageLayoutDescrFileExtension;

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        if( !SavePageLayoutDescrFile( filename ) )
        {
            wxString msg;
            msg.Printf( _("Unable to create <%s>"), GetChars( filename ) );
            wxMessageBox( msg );
        }

        else
        {
            msg.Printf( _("File <%s> written"), GetChars( filename ) );
            SetStatusText( msg );
            if( GetCurrFileName().IsEmpty() )
                SetCurrFileName( filename );
        }
    }
        break;

    default:
        wxMessageBox( wxT( "File_io: unexpected command id" ) );
        break;
    }
}

/* Loads a .kicad_wks page layout descr file
 */
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;
}

/* Inserts a .kicad_wks page layout descr file
 * same as LoadPageLayoutDescrFile, but the new data is added
 * to the previous data.
 */
bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
{
    if( wxFileExists( aFullFileName ) )
    {
        const bool append = true;
239
        SaveCopyInUndoList();
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
        return true;
    }

    return false;
}


/* Save the current layout in a .kicad_wks page layout descr file
 */
bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
{
    if( ! aFullFileName.IsEmpty() )
    {
        WORKSHEET_LAYOUT::GetTheInstance().Save( aFullFileName );
        GetScreen()->ClrModify();
        return true;
    }

    return false;
}