mainframe.cpp 9.08 KB
Newer Older
1 2 3 4
/**
 * @file kicad/mainframe.cpp
 * @brief KICAD_MANAGER_FRAME is the KiCad main frame.
 */
5

6 7 8 9
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2012 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
10
 * Copyright (C) 2013 CERN (www.cern.ch)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Copyright (C) 2004-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
 */
30

31 32 33 34
#include <fctsys.h>
#include <appl_wxstruct.h>
#include <confirm.h>
#include <gestfich.h>
35

36 37
#include <kicad.h>
#include <tree_project_frame.h>
38
#include <wildcards_and_files_ext.h>
39
#include <menus_helpers.h>
40 41


42 43
static const wxString TreeFrameWidthEntry( wxT( "LeftWinWidth" ) );

44
#define KICAD_MANAGER_FRAME_NAME wxT( "KicadFrame" )
45

46
KICAD_MANAGER_FRAME::KICAD_MANAGER_FRAME( wxWindow*       parent,
47 48 49
                                          const wxString& title,
                                          const wxPoint&  pos,
                                          const wxSize&   size ) :
50 51
    EDA_BASE_FRAME( parent, KICAD_MAIN_FRAME_TYPE, title, pos, size,
                    KICAD_DEFAULT_DRAWFRAME_STYLE, KICAD_MANAGER_FRAME_NAME )
52
{
charras's avatar
charras committed
53 54 55 56
    wxString msg;
    wxString line;
    wxSize   clientsize;

57
    m_FrameName            = KICAD_MANAGER_FRAME_NAME;
charras's avatar
charras committed
58 59 60 61 62
    m_VToolBar             = NULL;              // No Vertical tooolbar used here
    m_LeftWin              = NULL;              // A shashwindow that contains the project tree
    m_RightWin             = NULL;              /* A shashwindow that contains the buttons
                                                 *  and the window display text
                                                 */
63
    m_LeftWin_Width        = std::max( 60, GetSize().x/3 );
64

65
    LoadSettings();
66 67 68 69

    SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );

    // Create the status line (bottom of the frame
70 71
    static const int dims[3] = { -1, -1, 100 };

72 73 74 75
    CreateStatusBar( 3 );
    SetStatusWidths( 3, dims );

    // Give an icon
76 77 78
    wxIcon icon;
    icon.CopyFromBitmap( KiBitmap( icon_kicad_xpm ) );
    SetIcon( icon );
79 80 81 82

    clientsize = GetClientSize();

    // Left window: is the box which display tree project
83
    m_LeftWin = new TREE_PROJECT_FRAME( this );
84 85 86 87

    // Bottom Window: box to display messages
    m_RightWin = new RIGHT_KM_FRAME( this );

88 89 90 91
    msg = wxGetCwd();
    line.Printf( _( "Ready\nWorking dir: %s\n" ), msg.GetData() );
    PrintMsg( line );

92
    RecreateBaseHToolbar();
93
    ReCreateMenuBar();
94

charras's avatar
charras committed
95
    m_auimgr.SetManagedWindow( this );
96

97 98 99 100 101
    EDA_PANEINFO horiz;
    horiz.HorizontalToolbarPane();

    EDA_PANEINFO info;
    info.InfoToolbarPane();
102

103 104 105
    if( m_mainToolBar )
        m_auimgr.AddPane( m_mainToolBar,
                          wxAuiPaneInfo( horiz ).Name( wxT( "m_mainToolBar" ) ).Top().Layer( 1 ) );
106 107 108 109 110 111 112

    if( m_RightWin )
        m_auimgr.AddPane( m_RightWin,
                          wxAuiPaneInfo().Name( wxT( "m_RightWin" ) ).CentrePane().Layer( 1 ) );

    if( m_LeftWin )
        m_auimgr.AddPane( m_LeftWin,
113 114 115
                          wxAuiPaneInfo(info).Name( wxT( "m_LeftWin" ) ).Left().
                          BestSize( m_LeftWin_Width, clientsize.y ).
                          Layer( 1 ) );
116

117
    m_auimgr.Update();
118 119 120
}


121
KICAD_MANAGER_FRAME::~KICAD_MANAGER_FRAME()
122
{
charras's avatar
charras committed
123
    m_auimgr.UnInit();
124 125 126
}


127
void KICAD_MANAGER_FRAME::PrintMsg( const wxString& aText )
128
{
129
    m_RightWin->m_MessagesBox->AppendText( aText );
130 131 132
}


133
void KICAD_MANAGER_FRAME::OnSashDrag( wxSashEvent& event )
134
{
charras's avatar
charras committed
135
    event.Skip();
136 137 138
}


139
void KICAD_MANAGER_FRAME::OnSize( wxSizeEvent& event )
140
{
charras's avatar
charras committed
141 142
    if( m_auimgr.GetManagedWindow() )
        m_auimgr.Update();
143

charras's avatar
charras committed
144
    event.Skip();
145 146 147
}


148
void KICAD_MANAGER_FRAME::OnCloseWindow( wxCloseEvent& Event )
149 150 151
{
    int px, py;

152
    UpdateFileHistory( m_ProjectFileName.GetFullPath() );
153 154 155 156 157 158 159 160 161 162 163 164

    if( !IsIconized() )   // save main frame position and size
    {
        GetPosition( &px, &py );
        m_FramePos.x = px;
        m_FramePos.y = py;

        GetSize( &px, &py );
        m_FrameSize.x = px;
        m_FrameSize.y = py;
    }

165
    Event.SetCanVeto( true );
166 167 168 169

    SaveSettings();

    // Close the help frame
170
    if( wxGetApp().GetHtmlHelpController() )
171
    {
172 173
        if( wxGetApp().GetHtmlHelpController()->GetFrame() ) // returns NULL if no help frame active
            wxGetApp().GetHtmlHelpController()->GetFrame()->Close( true );
174

175
        wxGetApp().SetHtmlHelpController( NULL );
176
    }
177

charras's avatar
charras committed
178
    m_LeftWin->Show( false );
179 180 181 182 183

    Destroy();
}


184
void KICAD_MANAGER_FRAME::OnExit( wxCommandEvent& event )
185
{
186 187
    Close( true );
}
188

189

190 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
void KICAD_MANAGER_FRAME::PROCESS_TERMINATE_EVENT_HANDLER::
                        OnTerminate( int pid, int status )
{

    wxString msg;

    msg.Printf( appName + _( " closed [pid=%d]\n" ), pid );
    ( (KICAD_MANAGER_FRAME*) wxGetApp().GetTopWindow() )->PrintMsg( msg );

    delete this;
}


void KICAD_MANAGER_FRAME::Execute( wxWindow* frame, const wxString& execFile,
                                   const wxString& param )
{

    PROCESS_TERMINATE_EVENT_HANDLER* callback;
    long pid;
    wxString msg;

    callback = new PROCESS_TERMINATE_EVENT_HANDLER( execFile );
    pid = ExecuteFile( frame, execFile, param, callback );

    if( pid > 0 )
    {
        msg.Printf( execFile + _( " opened [pid=%ld]\n" ), pid );
        PrintMsg( msg );
    }
    else
    {
        delete callback;
    }
}


226
void KICAD_MANAGER_FRAME::OnRunBitmapConverter( wxCommandEvent& event )
227
{
228
    Execute( this, BITMAPCONVERTER_EXE );
229 230
}

231

232 233
void KICAD_MANAGER_FRAME::OnRunPcbCalculator( wxCommandEvent& event )
{
234
    Execute( this, PCB_CALCULATOR_EXE );
235
}
236

237 238 239 240 241
void KICAD_MANAGER_FRAME::OnRunPageLayoutEditor( wxCommandEvent& event )
{
    Execute( this, PL_EDITOR_EXE );
}

242

243
void KICAD_MANAGER_FRAME::OnRunPcbNew( wxCommandEvent& event )
244
{
245 246 247 248 249
    wxFileName  legacy_board( m_ProjectFileName );
    wxFileName  kicad_board( m_ProjectFileName );

    legacy_board.SetExt( LegacyPcbFileExtension );
    kicad_board.SetExt( KiCadPcbFileExtension );
charras's avatar
charras committed
250

251
    if( !legacy_board.FileExists() || kicad_board.FileExists() )
252
        Execute( this, PCBNEW_EXE, QuoteFullPath( kicad_board ) );
253
    else
254
        Execute( this, PCBNEW_EXE, QuoteFullPath( legacy_board ) );
255 256 257
}


258
void KICAD_MANAGER_FRAME::OnRunCvpcb( wxCommandEvent& event )
259
{
260
    wxFileName fn( m_ProjectFileName );
charras's avatar
charras committed
261

262
    fn.SetExt( NetlistFileExtension );
263
    Execute( this, CVPCB_EXE, QuoteFullPath( fn ) );
264
}
265

266
void KICAD_MANAGER_FRAME::OnRunEeschema( wxCommandEvent& event )
267 268
{
    wxFileName fn( m_ProjectFileName );
charras's avatar
charras committed
269

270
    fn.SetExt( SchematicFileExtension );
271
    Execute( this, EESCHEMA_EXE, QuoteFullPath( fn ) );
272

273
}
274

275
void KICAD_MANAGER_FRAME::OnRunGerbview( wxCommandEvent& event )
276 277
{
    wxFileName fn( m_ProjectFileName );
278 279
    wxString path = wxT( "\"" );
    path += fn.GetPath( wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME ) + wxT( "\"" );
charras's avatar
charras committed
280

281
    Execute( this, GERBVIEW_EXE, path );
282
}
283 284


285
void KICAD_MANAGER_FRAME::OnOpenTextEditor( wxCommandEvent& event )
286 287 288 289
{
    wxString editorname = wxGetApp().GetEditorName();

    if( !editorname.IsEmpty() )
290
        Execute( this, editorname, wxEmptyString );
291 292
}

293

294
void KICAD_MANAGER_FRAME::OnOpenFileInTextEditor( wxCommandEvent& event )
295 296
{
    wxString mask( wxT( "*" ) );
charras's avatar
charras committed
297

298
#ifdef __WINDOWS__
299
    mask += wxT( ".*" );
300 301
#endif

302
    mask = _( "Text file (" ) + mask + wxT( ")|" ) + mask;
303
    wxString default_dir = wxGetCwd();
304

305
    wxFileDialog dlg( this, _( "Load File to Edit" ), default_dir,
charras's avatar
charras committed
306
                      wxEmptyString, mask, wxFD_OPEN );
307

308 309 310
    if( dlg.ShowModal() == wxID_CANCEL )
        return;

311 312 313
    wxString filename = wxT( "\"" );
    filename += dlg.GetPath() + wxT( "\"" );

charras's avatar
charras committed
314
    if( !dlg.GetPath().IsEmpty() &&  !wxGetApp().GetEditorName().IsEmpty() )
315
        Execute( this, wxGetApp().GetEditorName(), filename );
316 317 318
}


319
void KICAD_MANAGER_FRAME::OnRefresh( wxCommandEvent& event )
320 321 322 323 324
{
    m_LeftWin->ReCreateTreePrj();
}


325
void KICAD_MANAGER_FRAME::ClearMsg()
326
{
327
    m_RightWin->m_MessagesBox->Clear();
328 329
}

330

331
void KICAD_MANAGER_FRAME::LoadSettings()
332
{
333
    wxASSERT( wxGetApp().GetSettings() != NULL );
334

335
    wxConfig* cfg = wxGetApp().GetSettings();
336

337
    EDA_BASE_FRAME::LoadSettings();
338 339 340 341
    cfg->Read( TreeFrameWidthEntry, &m_LeftWin_Width );
}


342
void KICAD_MANAGER_FRAME::SaveSettings()
343
{
344
    wxASSERT( wxGetApp().GetSettings() != NULL );
345

346
    wxConfig* cfg = wxGetApp().GetSettings();
347

348
    EDA_BASE_FRAME::SaveSettings();
349 350 351

    cfg->Write( TreeFrameWidthEntry, m_LeftWin->GetSize().x );
}