basicframe.cpp 21.7 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
Lorenzo Marcantonio's avatar
Lorenzo Marcantonio committed
5
 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
6
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
 */

26 27
/**
 * @file basicframe.cpp
28
 * @brief EDA_BASE_FRAME class implementation.
29
 */
plyatov's avatar
plyatov committed
30

31
#include <wx/aboutdlg.h>
32
#include <wx/fontdlg.h>
33 34 35
#include <wx/clipbrd.h>
#include <wx/statline.h>
#include <wx/platinfo.h>
36

37 38 39 40 41 42 43 44
#include <build_version.h>
#include <fctsys.h>
#include <appl_wxstruct.h>
#include <online_help.h>
#include <id.h>
#include <eda_doc.h>
#include <wxstruct.h>
#include <macros.h>
45
#include <menus_helpers.h>
46

47 48
#include <boost/version.hpp>

49

50 51 52 53
/// The default auto save interval is 10 minutes.
#define DEFAULT_AUTO_SAVE_INTERVAL 600


54
const wxChar traceAutoSave[] = wxT( "KicadAutoSave" );
55 56

/// Configuration file entry name for auto save interval.
57
static const wxChar entryAutoSaveInterval[] = wxT( "AutoSaveInterval" );
58

59
/// Configuration file entry for wxAuiManger perspective.
60
static const wxChar entryPerspective[] = wxT( "Perspective" );
61 62


63

64 65 66 67 68 69
EDA_BASE_FRAME::EDA_BASE_FRAME( wxWindow* aParent,
                                ID_DRAWFRAME_TYPE aFrameType,
                                const wxString& aTitle,
                                const wxPoint& aPos, const wxSize& aSize,
                                long aStyle, const wxString & aFrameName ) :
    wxFrame( aParent, wxID_ANY, aTitle, aPos, aSize, aStyle, aFrameName )
plyatov's avatar
plyatov committed
70
{
71 72
    wxSize minsize;

73
    m_Ident = aFrameType;
74
    m_mainToolBar = NULL;
75 76 77 78 79
    m_FrameIsActive = true;
    m_hasAutoSave = false;
    m_autoSaveState = false;
    m_autoSaveInterval = -1;
    m_autoSaveTimer = new wxTimer( this, ID_AUTO_SAVE_TIMER );
80

81
    minsize.x = 470;
82
    minsize.y = 350;
83

84 85
    SetSizeHints( minsize.x, minsize.y, -1, -1, -1, -1 );

86
    if( ( aSize.x < minsize.x ) || ( aSize.y < minsize.y ) )
87 88 89
        SetSize( 0, 0, minsize.x, minsize.y );

    // Create child subwindows.
90 91 92 93

    // Dimensions of the user area of the main window.
    GetClientSize( &m_FrameSize.x, &m_FrameSize.y );

94
    m_FramePos.x = m_FramePos.y = 0;
95

96 97
    Connect( ID_HELP_COPY_VERSION_STRING,
             wxEVT_COMMAND_MENU_SELECTED,
98
             wxCommandEventHandler( EDA_BASE_FRAME::CopyVersionInfoToClipboard ) );
99 100 101

    Connect( ID_AUTO_SAVE_TIMER, wxEVT_TIMER,
             wxTimerEventHandler( EDA_BASE_FRAME::onAutoSaveTimer ) );
102 103 104 105 106 107 108 109 110 111 112 113

    // hook wxEVT_CLOSE_WINDOW so we can call SaveSettings().  This function seems
    // to be called before any other hook for wxCloseEvent, which is necessary.
    Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( EDA_BASE_FRAME::windowClosing ) );
}


void EDA_BASE_FRAME::windowClosing( wxCloseEvent& event )
{
    SaveSettings();     // virtual, wxFrame specific

    event.Skip();       // we did not "handle" the event, only eavesdropped on it.
plyatov's avatar
plyatov committed
114 115
}

116

117
EDA_BASE_FRAME::~EDA_BASE_FRAME()
118
{
119 120
    if( wxGetApp().GetHtmlHelpController() )
        wxGetApp().SetHtmlHelpController( NULL );
121

122 123
    delete m_autoSaveTimer;

124 125
    // This is needed for OSX: avoids further OnDraw processing after this
    // destructor and before the native window is destroyed
126
    this->Freeze();
plyatov's avatar
plyatov committed
127 128
}

129

130
bool EDA_BASE_FRAME::ProcessEvent( wxEvent& aEvent )
plyatov's avatar
plyatov committed
131
{
132 133
    if( !wxFrame::ProcessEvent( aEvent ) )
        return false;
134

135
    if( m_hasAutoSave && (m_autoSaveState != isAutoSaveRequired()) && (m_autoSaveInterval > 0) )
136 137 138
    {
        if( !m_autoSaveState )
        {
139
            wxLogTrace( traceAutoSave, wxT( "Starting auto save timer." ) );
140 141 142
            m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
            m_autoSaveState = true;
        }
143
        else if( m_autoSaveTimer->IsRunning() )
144
        {
145
            wxLogTrace( traceAutoSave, wxT( "Stopping auto save timer." ) );
146 147 148 149 150 151
            m_autoSaveTimer->Stop();
            m_autoSaveState = false;
        }
    }

    return true;
plyatov's avatar
plyatov committed
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

void EDA_BASE_FRAME::onAutoSaveTimer( wxTimerEvent& aEvent )
{
    if( !doAutoSave() )
        m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
}


bool EDA_BASE_FRAME::doAutoSave()
{
    wxCHECK_MSG( false, true, wxT( "Auto save timer function not overridden.  Bad programmer!" ) );
}


void EDA_BASE_FRAME::ReCreateMenuBar()
{
}


173
void EDA_BASE_FRAME::SetLanguage( wxCommandEvent& event )
174 175 176 177
{
    int id = event.GetId();

    wxGetApp().SetLanguageIdentifier( id );
jean-pierre charras's avatar
jean-pierre charras committed
178 179 180
    wxGetApp().SetLanguage();
    ReCreateMenuBar();
    GetMenuBar()->Refresh();
181 182
}

plyatov's avatar
plyatov committed
183

184
void EDA_BASE_FRAME::LoadSettings()
plyatov's avatar
plyatov committed
185
{
186 187 188 189
    wxString  text;
    int       Ypos_min;
    wxConfig* config;

190
    config = wxGetApp().GetSettings();
191

192
    int maximized = 0;
193

194
    if( config )
195 196
    {
        text = m_FrameName + wxT( "Pos_x" );
197
        config->Read( text, &m_FramePos.x );
198

199
        text = m_FrameName + wxT( "Pos_y" );
200
        config->Read( text, &m_FramePos.y );
201

202
        text = m_FrameName + wxT( "Size_x" );
203
        config->Read( text, &m_FrameSize.x, 600 );
204

205
        text = m_FrameName + wxT( "Size_y" );
206
        config->Read( text, &m_FrameSize.y, 400 );
207

208 209
        text = m_FrameName + wxT( "Maximized" );
        config->Read( text, &maximized, 0 );
210 211 212 213 214 215

        if( m_hasAutoSave )
        {
            text = m_FrameName + entryAutoSaveInterval;
            config->Read( text, &m_autoSaveInterval, DEFAULT_AUTO_SAVE_INTERVAL );
        }
216 217 218
    }

    // Ensure Window title bar is visible
219
#if defined( __WXMAC__ )
220
    // for macOSX, the window must be below system (macOSX) toolbar
221
//    Ypos_min = GetMBarHeight(); seems no more exist in new API (subject to change)
222
    Ypos_min = 20;
plyatov's avatar
plyatov committed
223
#else
224
    Ypos_min = 0;
plyatov's avatar
plyatov committed
225
#endif
226 227
    if( m_FramePos.y < Ypos_min )
        m_FramePos.y = Ypos_min;
228 229 230

    if( maximized )
        Maximize();
231 232 233 234

    // Once this is fully implemented, wxAuiManager will be used to maintain the persistance of
    // the main frame and all it's managed windows and all of the legacy frame persistence
    // position code can be removed.
235 236
    if( config )
        config->Read( m_FrameName + entryPerspective, &m_perspective );
plyatov's avatar
plyatov committed
237 238 239
}


240
void EDA_BASE_FRAME::SaveSettings()
plyatov's avatar
plyatov committed
241
{
242 243
    wxString    text;
    wxConfig*   config = wxGetApp().GetSettings();
plyatov's avatar
plyatov committed
244

245
    if( !config || IsIconized() )
246
        return;
plyatov's avatar
plyatov committed
247

248 249
    m_FrameSize = GetSize();
    m_FramePos  = GetPosition();
plyatov's avatar
plyatov committed
250

251
    text = m_FrameName + wxT( "Pos_x" );
252
    config->Write( text, (long) m_FramePos.x );
253

254
    text = m_FrameName + wxT( "Pos_y" );
255
    config->Write( text, (long) m_FramePos.y );
256

257
    text = m_FrameName + wxT( "Size_x" );
258
    config->Write( text, (long) m_FrameSize.x );
259

260
    text = m_FrameName + wxT( "Size_y" );
261
    config->Write( text, (long) m_FrameSize.y );
262

263 264
    text = m_FrameName + wxT( "Maximized" );
    config->Write( text, IsMaximized() );
265 266 267 268 269 270

    if( m_hasAutoSave )
    {
        text = m_FrameName + entryAutoSaveInterval;
        config->Write( text, m_autoSaveInterval );
    }
271

272 273 274 275 276 277 278 279 280
    // Once this is fully implemented, wxAuiManager will be used to maintain
    // the persistance of the main frame and all it's managed windows and
    // all of the legacy frame persistence position code can be removed.
    wxString perspective = m_auimgr.SavePerspective();

    // printf( "perspective(%s): %s\n",
    //    TO_UTF8( m_FrameName + entryPerspective ), TO_UTF8( perspective ) );

    config->Write( m_FrameName + entryPerspective, perspective );
plyatov's avatar
plyatov committed
281 282 283
}


284
void EDA_BASE_FRAME::PrintMsg( const wxString& text )
plyatov's avatar
plyatov committed
285
{
286
    SetStatusText( text );
plyatov's avatar
plyatov committed
287 288
}

289

290
void EDA_BASE_FRAME::UpdateFileHistory( const wxString& FullFileName,
291
                                        wxFileHistory * aFileHistory )
plyatov's avatar
plyatov committed
292
{
293
    wxFileHistory* fileHistory = aFileHistory;
294

295
    if( fileHistory == NULL )
296
        fileHistory = & wxGetApp().GetFileHistory();
297 298

    fileHistory->AddFileToHistory( FullFileName );
299
}
plyatov's avatar
plyatov committed
300 301


302
wxString EDA_BASE_FRAME::GetFileFromHistory( int cmdId, const wxString& type,
303
                                             wxFileHistory* aFileHistory )
304 305 306
{
    wxString fn, msg;
    size_t   i;
307
    wxFileHistory* fileHistory = aFileHistory;
308

309
    if( fileHistory == NULL )
310
        fileHistory = & wxGetApp().GetFileHistory();
311

312
    int baseId = fileHistory->GetBaseId();
313

314
    wxASSERT( cmdId >= baseId && cmdId < baseId + ( int )fileHistory->GetCount() );
315 316 317

    i = ( size_t )( cmdId - baseId );

318
    if( i < fileHistory->GetCount() )
319
    {
320
        fn = fileHistory->GetHistoryFile( i );
321

322
        if( !wxFileName::FileExists( fn ) )
323
        {
324
            msg.Printf( wxT( "file <%s> was not found." ), GetChars( fn ) );
325
            wxMessageBox( msg );
326
            fileHistory->RemoveFileFromHistory( i );
327
            fn = wxEmptyString;
328 329
        }
    }
plyatov's avatar
plyatov committed
330

331
    return fn;
plyatov's avatar
plyatov committed
332 333 334
}


335
void EDA_BASE_FRAME::GetKicadHelp( wxCommandEvent& event )
plyatov's avatar
plyatov committed
336
{
337
    wxString msg;
338

339 340 341
    /* We have to get document for beginners,
     * or the the full specific doc
     * if event id is wxID_INDEX, we want the document for beginners.
342
     * else the specific doc file (its name is in wxGetApp().GetHelpFileName())
343
     * The document for beginners is the same for all KiCad utilities
344 345 346 347
     */
    if( event.GetId() == wxID_INDEX )
    {
        // Temporary change the help filename
348
        wxString tmp = wxGetApp().GetHelpFileName();
349 350

        // Search for "getting_started_in_kicad.pdf" or "Getting_Started_in_KiCad.pdf"
351
        wxGetApp().SetHelpFileName( wxT( "getting_started_in_kicad.pdf" ) );
352
        wxString helpFile = wxGetApp().GetHelpFile();
353

354 355
        if( !helpFile )
        {   // Try to find "Getting_Started_in_KiCad.pdf"
356
            wxGetApp().SetHelpFileName( wxT( "Getting_Started_in_KiCad.pdf" ) );
357
            helpFile = wxGetApp().GetHelpFile();
358 359
        }

360 361 362
        if( !helpFile )
        {
            msg.Printf( _( "Help file %s could not be found." ),
363
                        GetChars( wxGetApp().GetHelpFileName() ) );
364
            wxMessageBox( msg );
365 366
        }
        else
367
        {
368
            GetAssociatedDocument( this, helpFile );
369 370
        }

371
        wxGetApp().SetHelpFileName( tmp );
372 373 374
        return;
    }

375
#if defined ONLINE_HELP_FILES_FORMAT_IS_HTML
376

377
    if( wxGetApp().GetHtmlHelpController() == NULL )
378
    {
379
        wxGetApp().InitOnLineHelp();
380 381 382
    }


383
    if( wxGetApp().GetHtmlHelpController() )
384
    {
385 386
        wxGetApp().GetHtmlHelpController()->DisplayContents();
        wxGetApp().GetHtmlHelpController()->Display( wxGetApp().GetHelpFileName() );
387 388 389
    }
    else
    {
390
        msg.Printf( _( "Help file %s could not be found." ), GetChars( wxGetApp().GetHelpFileName() ) );
391
        wxMessageBox( msg );
392
    }
393

394
#elif defined ONLINE_HELP_FILES_FORMAT_IS_PDF
395
    wxString helpFile = wxGetApp().GetHelpFile();
396

397
    if( !helpFile )
398
    {
399
        msg.Printf( _( "Help file %s could not be found." ),
400
                    GetChars( wxGetApp().GetHelpFileName() ) );
401
        wxMessageBox( msg );
402
    }
403
    else
404
    {
405
        GetAssociatedDocument( this, helpFile );
406
    }
407 408

#else
409
#   error Help files format not defined
410
#endif
plyatov's avatar
plyatov committed
411 412
}

413

414 415
void EDA_BASE_FRAME::OnSelectPreferredEditor( wxCommandEvent& event )
{
416
    wxFileName fn = wxGetApp().GetEditorName();
417 418 419 420 421 422
    wxString wildcard( wxT( "*" ) );

#ifdef __WINDOWS__
    wildcard += wxT( ".exe" );
#endif

423
    wildcard.Printf( _( "Executable file (%s)|%s" ),
424
                     GetChars( wildcard ), GetChars( wildcard ) );
425

426
    wxFileDialog dlg( this, _( "Select Preferred Editor" ), fn.GetPath(),
427 428 429 430 431 432
                      fn.GetFullName(), wildcard,
                      wxFD_OPEN | wxFD_FILE_MUST_EXIST );

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

433
    wxASSERT( wxGetApp().GetCommonSettings() );
434

435 436 437
    wxConfig* cfg = wxGetApp().GetCommonSettings();
    wxGetApp().SetEditorName( dlg.GetPath() );
    cfg->Write( wxT( "Editor" ), wxGetApp().GetEditorName() );
438 439
}

440

441
void EDA_BASE_FRAME::GetKicadAbout( wxCommandEvent& event )
plyatov's avatar
plyatov committed
442
{
443 444
    bool ShowAboutDialog(wxWindow * parent);
    ShowAboutDialog(this);
plyatov's avatar
plyatov committed
445
}
446 447


448
void EDA_BASE_FRAME::AddHelpVersionInfoMenuEntry( wxMenu* aMenu )
449 450 451 452
{
    wxASSERT( aMenu != NULL );

    // Copy version string to clipboard for bug report purposes.
453 454 455 456
    AddMenuItem( aMenu, ID_HELP_COPY_VERSION_STRING,
                 _( "Copy &Version Information" ),
                 _( "Copy the version string to clipboard to send with bug reports" ),
                 KiBitmap( copy_button_xpm ) );
457 458 459
}


460 461 462
// This is an enhanced version of the compiler build macro provided by wxWidgets
// in <wx/build.h>. Please do not make any of these strings translatable.  They
// are used for conveying troubleshooting information to developers.
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

#if defined(__GXX_ABI_VERSION)
    #define __ABI_VERSION  ",compiler with C++ ABI " __WX_BO_STRINGIZE(__GXX_ABI_VERSION)
#else
    #define __ABI_VERSION  ",compiler without C++ ABI "
#endif

#if defined(__INTEL_COMPILER)
    #define __BO_COMPILER ",Intel C++"
#elif defined(__GNUG__)
    #define __BO_COMPILER ",GCC " \
            __WX_BO_STRINGIZE(__GNUC__) "." \
            __WX_BO_STRINGIZE(__GNUC_MINOR__) "." \
            __WX_BO_STRINGIZE(__GNUC_PATCHLEVEL__)
#elif defined(__VISUALC__)
    #define __BO_COMPILER ",Visual C++"
#elif defined(__BORLANDC__)
    #define __BO_COMPILER ",Borland C++"
#elif defined(__DIGITALMARS__)
    #define __BO_COMPILER ",DigitalMars"
#elif defined(__WATCOMC__)
    #define __BO_COMPILER ",Watcom C++"
#else
    #define __BO_COMPILER ",unknown"
#endif

489
#if wxCHECK_VERSION( 2, 9, 0 )
490 491 492 493

static inline const char* KICAD_BUILD_OPTIONS_SIGNATURE()
{
    return
494 495 496 497 498 499
#ifdef __WXDEBUG__
    " (debug,"
#else
    " (release,"
#endif
    __WX_BO_UNICODE __ABI_VERSION __BO_COMPILER __WX_BO_STL
500 501 502 503 504 505

#if !wxCHECK_VERSION( 3, 0, 0 )
    __WX_BO_WXWIN_COMPAT_2_6
#endif

    __WX_BO_WXWIN_COMPAT_2_8 ")"
506 507 508
    ;
}

509
#else
510 511 512 513

static inline const char* KICAD_BUILD_OPTIONS_SIGNATURE()
{
    return
514 515 516 517 518
#ifdef __WXDEBUG__
    " (debug,"
#else
    " (release,"
#endif
519 520 521 522 523
    __WX_BO_UNICODE __ABI_VERSION __BO_COMPILER __WX_BO_STL
    __WX_BO_WXWIN_COMPAT_2_4 __WX_BO_WXWIN_COMPAT_2_6 ")"
    ;
}

524
#endif
525

526
void EDA_BASE_FRAME::CopyVersionInfoToClipboard( wxCommandEvent&  event )
527 528 529 530 531 532 533 534 535 536 537
{
    if( !wxTheClipboard->Open() )
    {
        wxMessageBox( _( "Could not open clipboard to write version information." ),
                      _( "Clipboard Error" ), wxOK | wxICON_EXCLAMATION, this );
        return;
    }

    wxString tmp;
    wxPlatformInfo info;

538
    tmp = wxT( "Application: " ) + wxGetApp().GetTitle() + wxT( "\n" );
539 540 541 542 543 544 545 546
    tmp << wxT( "Version: " ) << GetBuildVersion()
#ifdef DEBUG
        << wxT( " Debug" )
#else
        << wxT( " Release" )
#endif
        << wxT( " build\n" );
    tmp << wxT( "wxWidgets: Version " ) << FROM_UTF8( wxVERSION_NUM_DOT_STRING )
547
        << FROM_UTF8( KICAD_BUILD_OPTIONS_SIGNATURE() ) << wxT( "\n" )
548 549
        << wxT( "Platform: " ) << wxGetOsDescription() << wxT( ", " )
        << info.GetArchName() << wxT( ", " ) << info.GetEndiannessName() << wxT( ", " )
550 551
        << info.GetPortIdName() << wxT( "\n" );

552 553 554 555 556
    // Just in case someone builds KiCad with the platform native of Boost instead of
    // the version included with the KiCad source.
    tmp << wxT( "Boost version: " ) << ( BOOST_VERSION / 100000 ) << wxT( "." )
        << ( BOOST_VERSION / 100 % 1000 ) << wxT( "." ) << ( BOOST_VERSION % 100 ) << wxT( "\n" );

557 558 559 560 561 562 563 564 565 566 567 568 569 570
    tmp << wxT( "         USE_WX_GRAPHICS_CONTEXT=" );
#ifdef USE_WX_GRAPHICS_CONTEXT
    tmp << wxT( "ON\n" );
#else
    tmp << wxT( "OFF\n" );
#endif

    tmp << wxT( "         USE_WX_OVERLAY=" );
#ifdef USE_WX_OVERLAY
    tmp << wxT( "ON\n" );
#else
    tmp << wxT( "OFF\n" );
#endif

571 572 573 574 575 576 577
    tmp << wxT( "         KICAD_SCRIPTING=" );
#ifdef KICAD_SCRIPTING
    tmp << wxT( "ON\n" );
#else
    tmp << wxT( "OFF\n" );
#endif

578 579 580 581 582 583 584 585 586 587 588 589 590
    tmp << wxT( "         KICAD_SCRIPTING_MODULES=" );
#ifdef KICAD_SCRIPTING_MODULES
    tmp << wxT( "ON\n" );
#else
    tmp << wxT( "OFF\n" );
#endif

    tmp << wxT( "         KICAD_SCRIPTING_WXPYTHON=" );
#ifdef KICAD_SCRIPTING_WXPYTHON
    tmp << wxT( "ON\n" );
#else
    tmp << wxT( "OFF\n" );
#endif
591

592
    tmp << wxT( "         USE_FP_LIB_TABLE=HARD_CODED_ON\n" );
593

594 595
    tmp << wxT( "         BUILD_GITHUB_PLUGIN=" );
#ifdef BUILD_GITHUB_PLUGIN
596 597 598 599 600
    tmp << wxT( "ON\n" );
#else
    tmp << wxT( "OFF\n" );
#endif

601 602
    wxMessageBox( tmp, _("Version Information (copied to the clipboard)") );

603 604 605
    wxTheClipboard->SetData( new wxTextDataObject( tmp ) );
    wxTheClipboard->Close();
}
606 607 608 609 610


bool EDA_BASE_FRAME::IsWritable( const wxFileName& aFileName )
{
    wxString msg;
611
    wxFileName fn = aFileName;
612

613 614 615 616 617 618 619 620
    // Check for absence of a file path with a file name.  Unfortunately KiCad
    // uses paths relative to the current project path without the ./ part which
    // confuses wxFileName. Making the file name path absolute may be less than
    // elegant but it solves the problem.
    if( fn.GetPath().IsEmpty() && fn.HasName() )
        fn.MakeAbsolute();

    wxCHECK_MSG( fn.IsOk(), false,
621
                 wxT( "File name object is invalid.  Bad programmer!" ) );
622 623
    wxCHECK_MSG( !fn.GetPath().IsEmpty(), false,
                 wxT( "File name object path <" ) + fn.GetFullPath() +
624
                 wxT( "> is not set.  Bad programmer!" ) );
625

626
    if( fn.IsDir() && !fn.IsDirWritable() )
627 628
    {
        msg.Printf( _( "You do not have write permissions to folder <%s>." ),
629
                    GetChars( fn.GetPath() ) );
630
    }
631
    else if( !fn.FileExists() && !fn.IsDirWritable() )
632
    {
633
        msg.Printf( _( "You do not have write permissions to save file <%s> to folder <%s>." ),
634
                    GetChars( fn.GetFullName() ), GetChars( fn.GetPath() ) );
635
    }
636
    else if( fn.FileExists() && !fn.IsFileWritable() )
637 638
    {
        msg.Printf( _( "You do not have write permissions to save file <%s>." ),
639
                    GetChars( fn.GetFullPath() ) );
640 641 642 643
    }

    if( !msg.IsEmpty() )
    {
644
        wxMessageBox( msg );
645 646 647 648 649
        return false;
    }

    return true;
}
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696


void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName,
                                           const wxString&   aBackupFileExtension )
{
    wxCHECK_RET( aFileName.IsOk(), wxT( "Invalid file name!" ) );
    wxCHECK_RET( !aBackupFileExtension.IsEmpty(), wxT( "Invalid backup file extension!" ) );

    wxFileName autoSaveFileName = aFileName;

    // Check for auto save file.
    autoSaveFileName.SetName( wxT( "$" ) + aFileName.GetName() );

    wxLogTrace( traceAutoSave,
                wxT( "Checking for auto save file " ) + autoSaveFileName.GetFullPath() );

    if( !autoSaveFileName.FileExists() )
        return;

    wxString msg;

    msg.Printf( _( "Well this is potentially embarrassing!  It appears that the last time \
you were editing the file <%s> it was not saved properly.  Do you wish to restore the last \
edits you made?" ),
                GetChars( aFileName.GetFullName() ) );

    int response = wxMessageBox( msg, wxGetApp().GetAppName(), wxYES_NO | wxICON_QUESTION, this );

    // Make a backup of the current file, delete the file, and rename the auto save file to
    // the file name.
    if( response == wxYES )
    {
        // Get the backup file name.
        wxFileName backupFileName = aFileName;
        backupFileName.SetExt( aBackupFileExtension );

        // If an old backup file exists, delete it.  If an old copy of the file exists, rename
        // it to the backup file name
        if( aFileName.FileExists() )
        {
            // Remove the old file backup file.
            if( backupFileName.FileExists() )
                wxRemoveFile( backupFileName.GetFullPath() );

            // Rename the old file to the backup file name.
            if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath() ) )
            {
697 698
                msg.Printf( _( "Could not create backup file <%s>" ),
                            GetChars( backupFileName.GetFullPath() ) );
699
                wxMessageBox( msg );
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
            }
        }

        if( !wxRenameFile( autoSaveFileName.GetFullPath(), aFileName.GetFullPath() ) )
        {
            wxMessageBox( _( "The auto save file could not be renamed to the board file name." ),
                          wxGetApp().GetAppName(), wxOK | wxICON_EXCLAMATION, this );
        }
    }
    else
    {
        wxLogTrace( traceAutoSave,
                    wxT( "Removing auto save file " ) + autoSaveFileName.GetFullPath() );

        // Remove the auto save file when using the previous file as is.
        wxRemoveFile( autoSaveFileName.GetFullPath() );
    }
}
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

/**
 * Function SetModalMode
 * Disable or enable all other windows, to emulate a dialog behavior
 * Useful when the frame is used to show and selec items
 * (see FOOTPRINT_VIEWER_FRAME and LIB_VIEW_FRAME)
 *
 * @param aModal = true to disable all other opened windows (i.e.
 * this windows is in dialog mode
 *               = false to enable other windows
 * This function is analog to MakeModal( aModal ), deprecated since wxWidgets 2.9.4
 */
void EDA_BASE_FRAME::SetModalMode( bool aModal )
{
    // Disable all other windows
#if wxCHECK_VERSION(2, 9, 4)
    if ( IsTopLevel() )
    {
        wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
        while (node)
        {
            wxWindow *win = node->GetData();
            if (win != this)
                win->Enable(!aModal);

            node = node->GetNext();
        }
    }
#else
    // Deprecated since wxWidgets 2.9.4
    MakeModal( aModal );
#endif
}