-
-
Notifications
You must be signed in to change notification settings - Fork 476
/
kicad_manager_frame.cpp
1024 lines (783 loc) · 32 KB
/
kicad_manager_frame.cpp
1
2
3
4
5
6
7
8
9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
99
100
101
102
103
104
105
106
107
108
109
110
111
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
153
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
182
183
184
185
186
187
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2013 CERN (www.cern.ch)
* Copyright (C) 2004-2023 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
*/
#include "kicad_id.h"
#include "pcm.h"
#include "pgm_kicad.h"
#include "project_tree_pane.h"
#include "widgets/bitmap_button.h"
#include <advanced_config.h>
#include <background_jobs_monitor.h>
#include <bitmaps.h>
#include <build_version.h>
#include <dialogs/panel_kicad_launcher.h>
#include <dialogs/dialog_update_check_prompt.h>
#include <eda_base_frame.h>
#include <executable_names.h>
#include <file_history.h>
#include <policy_keys.h>
#include <gestfich.h>
#include <kiplatform/app.h>
#include <kiplatform/policy.h>
#include <build_version.h>
#include <kiway.h>
#include <kiway_express.h>
#include <launch_ext.h>
#include <notifications_manager.h>
#include <reporter.h>
#include <project/project_local_settings.h>
#include <sch_file_versions.h>
#include <settings/settings_manager.h>
#include <tool/action_manager.h>
#include <tool/action_toolbar.h>
#include <tool/common_control.h>
#include <tool/tool_dispatcher.h>
#include <tool/tool_manager.h>
#include <tools/kicad_manager_actions.h>
#include <tools/kicad_manager_control.h>
#include <wildcards_and_files_ext.h>
#include <widgets/app_progress_dialog.h>
#include <widgets/kistatusbar.h>
#include <wx/ffile.h>
#include <wx/filedlg.h>
#include <wx/dnd.h>
#include <wx/process.h>
#include <atomic>
#include <update_manager.h>
#include <../pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr.h> // for SEXPR_BOARD_FILE_VERSION def
#ifdef __WXMAC__
#include <MacTypes.h>
#include <ApplicationServices/ApplicationServices.h>
#endif
#include "kicad_manager_frame.h"
#include "settings/kicad_settings.h"
#define SEP() wxFileName::GetPathSeparator()
// Menubar and toolbar event table
BEGIN_EVENT_TABLE( KICAD_MANAGER_FRAME, EDA_BASE_FRAME )
// Window events
EVT_SIZE( KICAD_MANAGER_FRAME::OnSize )
EVT_IDLE( KICAD_MANAGER_FRAME::OnIdle )
// Menu events
EVT_MENU( wxID_EXIT, KICAD_MANAGER_FRAME::OnExit )
EVT_MENU( ID_EDIT_LOCAL_FILE_IN_TEXT_EDITOR, KICAD_MANAGER_FRAME::OnOpenFileInTextEditor )
EVT_MENU( ID_BROWSE_IN_FILE_EXPLORER, KICAD_MANAGER_FRAME::OnBrowseInFileExplorer )
EVT_MENU( ID_SAVE_AND_ZIP_FILES, KICAD_MANAGER_FRAME::OnArchiveFiles )
EVT_MENU( ID_READ_ZIP_ARCHIVE, KICAD_MANAGER_FRAME::OnUnarchiveFiles )
EVT_MENU( ID_IMPORT_CADSTAR_ARCHIVE_PROJECT, KICAD_MANAGER_FRAME::OnImportCadstarArchiveFiles )
EVT_MENU( ID_IMPORT_EAGLE_PROJECT, KICAD_MANAGER_FRAME::OnImportEagleFiles )
EVT_MENU( ID_IMPORT_EASYEDA_PROJECT, KICAD_MANAGER_FRAME::OnImportEasyEdaFiles )
EVT_MENU( ID_IMPORT_EASYEDAPRO_PROJECT, KICAD_MANAGER_FRAME::OnImportEasyEdaProFiles )
// Range menu events
EVT_MENU_RANGE( ID_LANGUAGE_CHOICE, ID_LANGUAGE_CHOICE_END,
KICAD_MANAGER_FRAME::language_change )
EVT_MENU_RANGE( ID_FILE1, ID_FILEMAX, KICAD_MANAGER_FRAME::OnFileHistory )
EVT_MENU( ID_FILE_LIST_CLEAR, KICAD_MANAGER_FRAME::OnClearFileHistory )
// Special functions
EVT_MENU( ID_INIT_WATCHED_PATHS, KICAD_MANAGER_FRAME::OnChangeWatchedPaths )
// Drop files event
EVT_DROP_FILES( KICAD_MANAGER_FRAME::OnDropFiles )
END_EVENT_TABLE()
// See below the purpose of this include
#include <wx/xml/xml.h>
KICAD_MANAGER_FRAME::KICAD_MANAGER_FRAME( wxWindow* parent, const wxString& title,
const wxPoint& pos, const wxSize& size ) :
EDA_BASE_FRAME( parent, KICAD_MAIN_FRAME_T, title, pos, size, KICAD_DEFAULT_DRAWFRAME_STYLE,
KICAD_MANAGER_FRAME_NAME, &::Kiway, unityScale ),
m_leftWin( nullptr ),
m_launcher( nullptr ),
m_mainToolBar( nullptr ),
m_lastToolbarIconSize( 0 )
{
const int defaultLeftWinWidth = FromDIP( 250 );
m_active_project = false;
m_leftWinWidth = defaultLeftWinWidth; // Default value
m_aboutTitle = "KiCad";
// JPC: A very ugly hack to fix an issue on Linux: if the wxbase315u_xml_gcc_custom.so is
// used **only** in PCM, it is not found in some cases at run time.
// So just use it in the main module to avoid a not found issue
// wxbase315u_xml_gcc_custom shared object when launching Kicad
wxXmlDocument dummy;
// Create the status line (bottom of the frame). Left half is for project name; right half
// is for Reporter (currently used by archiver/unarchiver and PCM).
// Note: this is a KISTATUSBAR status bar. Therefore the specified number of fields
// is the extra number of fields, not the full field count.
// We need here 2 fields: the extra fiels to display the project name, and another field
// to display a info (specific to Windows) using the FIELD_OFFSET_BGJOB_TEXT id offset (=1)
// So the extra field count is 1
CreateStatusBar( 1 );
Pgm().GetBackgroundJobMonitor().RegisterStatusBar( (KISTATUSBAR*) GetStatusBar() );
Pgm().GetNotificationsManager().RegisterStatusBar( (KISTATUSBAR*) GetStatusBar() );
GetStatusBar()->SetFont( KIUI::GetStatusFont( this ) );
// Give an icon
wxIcon icon;
wxIconBundle icon_bundle;
if( IsNightlyVersion())
{
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad_nightly, 48 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad_nightly, 128 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad_nightly, 256 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad_nightly_32 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad_nightly_16 ) );
icon_bundle.AddIcon( icon );
}
else
{
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad, 48 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad, 128 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad, 256 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad_32 ) );
icon_bundle.AddIcon( icon );
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_kicad_16 ) );
icon_bundle.AddIcon( icon );
}
SetIcons( icon_bundle );
// Load the settings
LoadSettings( config() );
m_pcmButton = nullptr;
m_pcmUpdateCount = 0;
// Left window: is the box which display tree project
m_leftWin = new PROJECT_TREE_PANE( this );
setupTools();
setupUIConditions();
m_launcher = new PANEL_KICAD_LAUNCHER( this );
RecreateBaseHToolbar();
ReCreateMenuBar();
m_auimgr.SetManagedWindow( this );
m_auimgr.SetFlags( wxAUI_MGR_LIVE_RESIZE );
m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" ).Left()
.Layer( 2 ) );
// BestSize() does not always set the actual pane size of m_leftWin to the required value.
// It happens when m_leftWin is too large (roughly > 1/3 of the kicad manager frame width.
// (Well, BestSize() sets the best size... not the window size)
// A trick is to use MinSize() to set the required pane width,
// and after give a reasonable MinSize value
m_auimgr.AddPane( m_leftWin, EDA_PANE().Palette().Name( "ProjectTree" ).Left().Layer( 1 )
.Caption( _( "Project Files" ) ).PaneBorder( false )
.MinSize( m_leftWinWidth, -1 ).BestSize( m_leftWinWidth, -1 ) );
m_auimgr.AddPane( m_launcher, EDA_PANE().Canvas().Name( "Launcher" ).Center()
.Caption( _( "Editors" ) ).PaneBorder( false )
.MinSize( m_launcher->GetBestSize() ) );
m_auimgr.Update();
// Now the actual m_leftWin size is set, give it a reasonable min width
m_auimgr.GetPane( m_leftWin ).MinSize( defaultLeftWinWidth, -1 );
wxSizer* mainSizer = GetSizer();
// Only fit the initial window size the first time KiCad is run.
if( mainSizer && config()->m_Window.state.size_x == 0 && config()->m_Window.state.size_y == 0 )
mainSizer->Fit( this );
if( ADVANCED_CFG::GetCfg().m_HideVersionFromTitle )
SetTitle( wxT( "KiCad" ) );
else
SetTitle( wxString( "KiCad " ) + GetMajorMinorVersion() );
// Do not let the messages window have initial focus
m_leftWin->SetFocus();
// Init for dropping files
m_acceptedExts.emplace( FILEEXT::ProjectFileExtension, &KICAD_MANAGER_ACTIONS::loadProject );
m_acceptedExts.emplace( FILEEXT::LegacyProjectFileExtension, &KICAD_MANAGER_ACTIONS::loadProject );
// Gerber files
// Note that all gerber files are aliased as GerberFileExtension
m_acceptedExts.emplace( FILEEXT::GerberFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
m_acceptedExts.emplace( FILEEXT::GerberJobFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
m_acceptedExts.emplace( FILEEXT::DrillFileExtension, &KICAD_MANAGER_ACTIONS::viewDroppedGerbers );
DragAcceptFiles( true );
// Ensure the window is on top
Raise();
}
KICAD_MANAGER_FRAME::~KICAD_MANAGER_FRAME()
{
Pgm().GetBackgroundJobMonitor().UnregisterStatusBar( (KISTATUSBAR*) GetStatusBar() );
Pgm().GetNotificationsManager().UnregisterStatusBar( (KISTATUSBAR*) GetStatusBar() );
// Shutdown all running tools
if( m_toolManager )
m_toolManager->ShutdownAllTools();
if( m_pcm )
m_pcm->StopBackgroundUpdate();
delete m_actions;
delete m_toolManager;
delete m_toolDispatcher;
m_auimgr.UnInit();
}
wxStatusBar* KICAD_MANAGER_FRAME::OnCreateStatusBar( int number, long style, wxWindowID id,
const wxString& name )
{
return new KISTATUSBAR( number, this, id );
}
void KICAD_MANAGER_FRAME::CreatePCM()
{
// creates the PLUGIN_CONTENT_MANAGER, if not exists
if( m_pcm )
return;
m_pcm = std::make_shared<PLUGIN_CONTENT_MANAGER>(
[this]( int aUpdateCount )
{
m_pcmUpdateCount = aUpdateCount;
if( aUpdateCount > 0 )
{
Pgm().GetNotificationsManager().CreateOrUpdate(
wxS( "pcm" ),
_( "PCM Updates Available" ),
wxString::Format( _( "%d package update(s) avaliable" ), aUpdateCount ),
wxT( "" ) );
}
else
{
Pgm().GetNotificationsManager().Remove( wxS( "pcm" ) );
}
CallAfter(
[this]()
{
updatePcmButtonBadge();
} );
});
m_pcm->SetRepositoryList( kicadSettings()->m_PcmRepositories );
}
void KICAD_MANAGER_FRAME::setupTools()
{
// Create the manager
m_toolManager = new TOOL_MANAGER;
m_toolManager->SetEnvironment( nullptr, nullptr, nullptr, config(), this );
m_actions = new KICAD_MANAGER_ACTIONS();
m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager );
// Attach the events to the tool dispatcher
Bind( wxEVT_CHAR, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
Bind( wxEVT_CHAR_HOOK, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
// Register tools
m_toolManager->RegisterTool( new COMMON_CONTROL );
m_toolManager->RegisterTool( new KICAD_MANAGER_CONTROL );
m_toolManager->InitTools();
}
void KICAD_MANAGER_FRAME::setupUIConditions()
{
EDA_BASE_FRAME::setupUIConditions();
ACTION_MANAGER* manager = m_toolManager->GetActionManager();
wxASSERT( manager );
auto activeProject =
[this] ( const SELECTION& )
{
return m_active_project;
};
#define ENABLE( x ) ACTION_CONDITIONS().Enable( x )
ACTION_CONDITIONS activeProjectCond;
activeProjectCond.Enable( activeProject );
manager->SetConditions( ACTIONS::saveAs, activeProjectCond );
manager->SetConditions( KICAD_MANAGER_ACTIONS::closeProject, activeProjectCond );
// These are just here for text boxes, search boxes, etc. in places such as the standard
// file dialogs.
manager->SetConditions( ACTIONS::cut, ENABLE( SELECTION_CONDITIONS::ShowNever ) );
manager->SetConditions( ACTIONS::copy, ENABLE( SELECTION_CONDITIONS::ShowNever ) );
manager->SetConditions( ACTIONS::paste, ENABLE( SELECTION_CONDITIONS::ShowNever ) );
// TODO: Switch this to an action
RegisterUIUpdateHandler( ID_SAVE_AND_ZIP_FILES, activeProjectCond );
#undef ENABLE
}
wxWindow* KICAD_MANAGER_FRAME::GetToolCanvas() const
{
return m_leftWin;
}
APP_SETTINGS_BASE* KICAD_MANAGER_FRAME::config() const
{
APP_SETTINGS_BASE* ret = PgmTop().PgmSettings();
wxASSERT( ret );
return ret;
}
KICAD_SETTINGS* KICAD_MANAGER_FRAME::kicadSettings() const
{
KICAD_SETTINGS* ret = dynamic_cast<KICAD_SETTINGS*>( config() );
wxASSERT( ret );
return ret;
}
const wxString KICAD_MANAGER_FRAME::GetProjectFileName() const
{
return Pgm().GetSettingsManager().IsProjectOpen() ? Prj().GetProjectFullName() :
wxString( wxEmptyString );
}
const wxString KICAD_MANAGER_FRAME::SchFileName()
{
wxFileName fn( GetProjectFileName() );
fn.SetExt( FILEEXT::KiCadSchematicFileExtension );
return fn.GetFullPath();
}
const wxString KICAD_MANAGER_FRAME::SchLegacyFileName()
{
wxFileName fn( GetProjectFileName() );
fn.SetExt( FILEEXT::LegacySchematicFileExtension );
return fn.GetFullPath();
}
const wxString KICAD_MANAGER_FRAME::PcbFileName()
{
wxFileName fn( GetProjectFileName() );
fn.SetExt( FILEEXT::PcbFileExtension );
return fn.GetFullPath();
}
const wxString KICAD_MANAGER_FRAME::PcbLegacyFileName()
{
wxFileName fn( GetProjectFileName() );
fn.SetExt( FILEEXT::LegacyPcbFileExtension );
return fn.GetFullPath();
}
void KICAD_MANAGER_FRAME::ReCreateTreePrj()
{
m_leftWin->ReCreateTreePrj();
}
const SEARCH_STACK& KICAD_MANAGER_FRAME::sys_search()
{
return PgmTop().SysSearch();
}
wxString KICAD_MANAGER_FRAME::help_name()
{
return PgmTop().GetHelpFileName();
}
void KICAD_MANAGER_FRAME::OnSize( wxSizeEvent& event )
{
if( m_auimgr.GetManagedWindow() )
m_auimgr.Update();
PrintPrjInfo();
#if defined( _WIN32 )
KISTATUSBAR* statusBar = static_cast<KISTATUSBAR*>( GetStatusBar() );
statusBar->SetEllipsedTextField( m_FileWatcherInfo, 1 );
#endif
event.Skip();
}
void KICAD_MANAGER_FRAME::DoWithAcceptedFiles()
{
// All fileNames are now in m_AcceptedFiles vector.
// Check if contains a project file name and load project.
// If not, open files in dedicated app.
for( const wxFileName& fileName : m_AcceptedFiles )
{
wxString ext = fileName.GetExt();
if( ext == FILEEXT::ProjectFileExtension || ext == FILEEXT::LegacyProjectFileExtension )
{
wxString fn = fileName.GetFullPath();
m_toolManager->RunAction<wxString*>( *m_acceptedExts.at( fileName.GetExt() ), &fn );
return;
}
}
// Then stock gerber files in gerberFiles and run action for other files.
wxString gerberFiles;
// Gerbview editor should be able to open Gerber and drill files
for( const wxFileName& fileName : m_AcceptedFiles )
{
wxString ext = fileName.GetExt();
if( ext == FILEEXT::GerberJobFileExtension || ext == FILEEXT::DrillFileExtension
|| FILEEXT::IsGerberFileExtension( ext ) )
{
gerberFiles += wxT( '\"' );
gerberFiles += fileName.GetFullPath() + wxT( '\"' );
gerberFiles = gerberFiles.Pad( 1 );
}
else
{
wxString fn = fileName.GetFullPath();
m_toolManager->RunAction<wxString*>( *m_acceptedExts.at( fileName.GetExt() ), &fn );
}
}
// Execute Gerbviewer
if( !gerberFiles.IsEmpty() )
{
wxString fullEditorName = FindKicadFile( GERBVIEW_EXE );
if( wxFileExists( fullEditorName ) )
{
wxString command = fullEditorName + " " + gerberFiles;
m_toolManager->RunAction<wxString*>( *m_acceptedExts.at( FILEEXT::GerberFileExtension ),
&command );
}
}
}
bool KICAD_MANAGER_FRAME::canCloseWindow( wxCloseEvent& aEvent )
{
KICAD_SETTINGS* settings = kicadSettings();
settings->m_OpenProjects = GetSettingsManager()->GetOpenProjects();
// CloseProject will recursively ask all the open editors if they need to save changes.
// If any of them cancel then we need to cancel closing the KICAD_MANAGER_FRAME.
if( CloseProject( true ) )
{
// Don't propagate event to frames which have already been closed
aEvent.StopPropagation();
return true;
}
else
{
if( aEvent.CanVeto() )
aEvent.Veto();
return false;
}
}
void KICAD_MANAGER_FRAME::doCloseWindow()
{
#ifdef _WINDOWS_
// For some obscure reason, on Windows, when killing Kicad from the Windows task manager
// if a editor frame (schematic, library, board editor or fp editor) is open and has
// some edition to save, OnCloseWindow is run twice *at the same time*, creating race
// conditions between OnCloseWindow() code.
// Therefore I added (JPC) a ugly hack to discard the second call (unwanted) during
// execution of the first call (only one call is right).
// Note also if there is no change made in editors, this behavior does not happen.
static std::atomic<unsigned int> lock_close_event( 0 );
if( ++lock_close_event > 1 ) // Skip extra calls
{
return;
}
#endif
m_leftWin->Show( false );
Pgm().m_Quitting = true;
Destroy();
#ifdef _WINDOWS_
lock_close_event = 0; // Reenable event management
#endif
}
void KICAD_MANAGER_FRAME::OnExit( wxCommandEvent& event )
{
Close( true );
}
bool KICAD_MANAGER_FRAME::CloseProject( bool aSave )
{
if( !Kiway().PlayersClose( false ) )
return false;
// Save the project file for the currently loaded project.
if( m_active_project )
{
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
mgr.TriggerBackupIfNeeded( NULL_REPORTER::GetInstance() );
if( aSave )
mgr.SaveProject();
m_active_project = false;
mgr.UnloadProject( &Prj() );
}
SetStatusText( "" );
m_leftWin->EmptyTreePrj();
return true;
}
void KICAD_MANAGER_FRAME::LoadProject( const wxFileName& aProjectFileName )
{
// The project file should be valid by the time we get here or something has gone wrong.
if( !aProjectFileName.Exists() )
return;
// Any open KIFACE's must be closed if they are not part of the new project.
// (We never want a KIWAY_PLAYER open on a KIWAY that isn't in the same project.)
// User is prompted here to close those KIWAY_PLAYERs:
if( !CloseProject( true ) )
return;
m_active_project = true;
Pgm().GetSettingsManager().LoadProject( aProjectFileName.GetFullPath() );
LoadWindowState( aProjectFileName.GetFullName() );
if( aProjectFileName.IsDirWritable() )
SetMruPath( Prj().GetProjectPath() ); // Only set MRU path if we have write access. Why?
// Save history & window state to disk now. Don't wait around for a crash.
KICAD_SETTINGS* settings = kicadSettings();
SaveSettings( settings );
settings->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( settings ) );
m_leftWin->ReCreateTreePrj();
// Rebuild the list of watched paths.
// however this is possible only when the main loop event handler is running,
// so we use it to run the rebuild function.
wxCommandEvent cmd( wxEVT_COMMAND_MENU_SELECTED, ID_INIT_WATCHED_PATHS );
wxPostEvent( this, cmd );
PrintPrjInfo();
KIPLATFORM::APP::RegisterApplicationRestart( aProjectFileName.GetFullPath() );
m_openSavedWindows = true;
}
void KICAD_MANAGER_FRAME::CreateNewProject( const wxFileName& aProjectFileName,
bool aCreateStubFiles )
{
wxCHECK_RET( aProjectFileName.DirExists() && aProjectFileName.IsDirWritable(),
"Project folder must exist and be writable to create a new project." );
// If the project is legacy, convert it
if( !aProjectFileName.FileExists() )
{
wxFileName legacyPro( aProjectFileName );
legacyPro.SetExt( FILEEXT::LegacyProjectFileExtension );
if( legacyPro.FileExists() )
{
GetSettingsManager()->LoadProject( legacyPro.GetFullPath() );
GetSettingsManager()->SaveProject();
wxRemoveFile( legacyPro.GetFullPath() );
}
else
{
// Copy template project file from template folder.
wxString srcFileName = sys_search().FindValidPath( "kicad.kicad_pro" );
wxFileName destFileName( aProjectFileName );
destFileName.SetExt( FILEEXT::ProjectFileExtension );
// Create a minimal project file if the template project file could not be copied
if( !wxFileName::FileExists( srcFileName )
|| !wxCopyFile( srcFileName, destFileName.GetFullPath() ) )
{
wxFFile file( destFileName.GetFullPath(), "wb" );
if( file.IsOpened() )
file.Write( wxT( "{\n}\n") );
// wxFFile dtor will close the file
}
}
}
// Create a "stub" for a schematic root sheet and a board if requested.
// It will avoid messages from the schematic editor or the board editor to create a new file
// And forces the user to create main files under the right name for the project manager
if( aCreateStubFiles )
{
wxFileName fn( aProjectFileName.GetFullPath() );
fn.SetExt( FILEEXT::KiCadSchematicFileExtension );
// If a <project>.kicad_sch file does not exist, create a "stub" file ( minimal schematic
// file ).
if( !fn.FileExists() )
{
wxFFile file( fn.GetFullPath(), "wb" );
if( file.IsOpened() )
file.Write( wxString::Format( "(kicad_sch (version %d) (generator \"eeschema\") (generator_version \"%s\")\n"
" (paper \"A4\")\n (lib_symbols)\n"
" (symbol_instances)\n)\n",
SEXPR_SCHEMATIC_FILE_VERSION, GetMajorMinorVersion() ) );
// wxFFile dtor will close the file
}
// If a <project>.kicad_pcb or <project>.brd file does not exist,
// create a .kicad_pcb "stub" file
fn.SetExt( FILEEXT::KiCadPcbFileExtension );
wxFileName leg_fn( fn );
leg_fn.SetExt( FILEEXT::LegacyPcbFileExtension );
if( !fn.FileExists() && !leg_fn.FileExists() )
{
wxFFile file( fn.GetFullPath(), "wb" );
if( file.IsOpened() )
// Create a small dummy file as a stub for pcbnew:
file.Write( wxString::Format( "(kicad_pcb (version %d) (generator \"pcbnew\") (generator_version \"%s\")\n)",
SEXPR_BOARD_FILE_VERSION, GetMajorMinorVersion() ) );
// wxFFile dtor will close the file
}
}
// Save history & window state to disk now. Don't wait around for a crash.
KICAD_SETTINGS* settings = kicadSettings();
SaveSettings( settings );
settings->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( settings ) );
m_openSavedWindows = true;
}
void KICAD_MANAGER_FRAME::OnOpenFileInTextEditor( wxCommandEvent& event )
{
// show all files in file dialog (in Kicad all files are editable texts):
wxString wildcard = FILEEXT::AllFilesWildcard();
wxString default_dir = Prj().GetProjectPath();
wxFileDialog dlg( this, _( "Edit File in Text Editor" ), default_dir, wxEmptyString, wildcard,
wxFD_OPEN );
if( dlg.ShowModal() == wxID_CANCEL )
return;
wxString filename = dlg.GetPath();
if( !dlg.GetPath().IsEmpty() && !Pgm().GetTextEditor().IsEmpty() )
m_toolManager->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::openTextEditor, &filename );
}
void KICAD_MANAGER_FRAME::OnBrowseInFileExplorer( wxCommandEvent& event )
{
// open project directory in host OS's file explorer
LaunchExternal( Prj().GetProjectPath() );
}
void KICAD_MANAGER_FRAME::RefreshProjectTree()
{
m_leftWin->ReCreateTreePrj();
}
void KICAD_MANAGER_FRAME::language_change( wxCommandEvent& event )
{
int id = event.GetId();
Kiway().SetLanguage( id );
}
void KICAD_MANAGER_FRAME::ShowChangedLanguage()
{
// call my base class
EDA_BASE_FRAME::ShowChangedLanguage();
// tooltips in toolbars
RecreateBaseHToolbar();
m_launcher->CreateLaunchers();
PrintPrjInfo();
}
void KICAD_MANAGER_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
{
EDA_BASE_FRAME::CommonSettingsChanged( aEnvVarsChanged, aTextVarsChanged );
if( m_pcm && aEnvVarsChanged )
{
m_pcm->ReadEnvVar();
}
COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
if( m_lastToolbarIconSize == 0
|| m_lastToolbarIconSize != settings->m_Appearance.toolbar_icon_size )
{
onToolbarSizeChanged();
m_lastToolbarIconSize = settings->m_Appearance.toolbar_icon_size;
}
}
void KICAD_MANAGER_FRAME::ProjectChanged()
{
wxString file = GetProjectFileName();
wxString title;
if( !file.IsEmpty() )
{
wxFileName fn( file );
title = fn.GetName();
if( !fn.IsDirWritable() )
title += wxS( " " ) + _( "[Read Only]" );
}
else
{
title = _( "[no project loaded]" );
}
if( ADVANCED_CFG::GetCfg().m_HideVersionFromTitle )
title += wxT( " \u2014 " ) + wxString( wxS( "KiCad" ) );
else
title += wxT( " \u2014 " ) + wxString( wxS( "KiCad " ) ) + GetMajorMinorVersion();
SetTitle( title );
}
void KICAD_MANAGER_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
{
EDA_BASE_FRAME::LoadSettings( aCfg );
auto settings = dynamic_cast<KICAD_SETTINGS*>( aCfg );
wxCHECK( settings, /*void*/ );
m_leftWinWidth = settings->m_LeftWinWidth;
}
void KICAD_MANAGER_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
{
EDA_BASE_FRAME::SaveSettings( aCfg );
auto settings = dynamic_cast<KICAD_SETTINGS*>( aCfg );
wxCHECK( settings, /*void*/);
settings->m_LeftWinWidth = m_leftWin->GetSize().x;
if( !m_isClosing )
settings->m_OpenProjects = GetSettingsManager()->GetOpenProjects();
}
void KICAD_MANAGER_FRAME::PrintPrjInfo()
{
// wxStatusBar's wxELLIPSIZE_MIDDLE flag doesn't work (at least on Mac).
wxString status = wxString::Format( _( "Project: %s" ), Prj().GetProjectFullName() );
KISTATUSBAR* statusBar = static_cast<KISTATUSBAR*>( GetStatusBar() );
statusBar->SetEllipsedTextField( status, 0 );
}
bool KICAD_MANAGER_FRAME::IsProjectActive()
{
return m_active_project;
}
void KICAD_MANAGER_FRAME::OnIdle( wxIdleEvent& aEvent )
{
/**
* We start loading the saved previously open windows on idle to avoid locking up the GUI
* earlier in project loading. This gives us the visual effect of a opened KiCad project but
* with a "busy" progress reporter
*/
if( !m_openSavedWindows )
return;
m_openSavedWindows = false;
if( Pgm().GetCommonSettings()->m_Session.remember_open_files )
{
int previousOpenCount =
std::count_if( Prj().GetLocalSettings().m_files.begin(),
Prj().GetLocalSettings().m_files.end(),
[&]( const PROJECT_FILE_STATE& f )
{
return !f.fileName.EndsWith( FILEEXT::ProjectFileExtension ) && f.open;
} );
if( previousOpenCount > 0 )
{
APP_PROGRESS_DIALOG progressReporter( _( "Restoring session" ), wxEmptyString,
previousOpenCount, this );
// We don't currently support opening more than one view per file
std::set<wxString> openedFiles;
int i = 0;
for( const PROJECT_FILE_STATE& file : Prj().GetLocalSettings().m_files )
{
if( file.open && !openedFiles.count( file.fileName ) )
{
progressReporter.Update( i++,
wxString::Format( _( "Restoring '%s'" ), file.fileName ) );
openedFiles.insert( file.fileName );
wxFileName fn( file.fileName );
if( fn.GetExt() == FILEEXT::LegacySchematicFileExtension
|| fn.GetExt() == FILEEXT::KiCadSchematicFileExtension )
{
GetToolManager()->RunAction( KICAD_MANAGER_ACTIONS::editSchematic );
}
else if( fn.GetExt() == FILEEXT::LegacyPcbFileExtension
|| fn.GetExt() == FILEEXT::KiCadPcbFileExtension )
{
GetToolManager()->RunAction( KICAD_MANAGER_ACTIONS::editPCB );
}
}
wxYield();
}
}
}
// clear file states regardless if we opened windows or not due to setting
Prj().GetLocalSettings().ClearFileState();
KICAD_SETTINGS* settings = kicadSettings();
if( !Pgm().GetCommonSettings()->m_DoNotShowAgain.update_check_prompt )
{
auto prompt = new DIALOG_UPDATE_CHECK_PROMPT( this );
prompt->ShowModal();
Pgm().GetCommonSettings()->m_DoNotShowAgain.update_check_prompt = true;
}
if( KIPLATFORM::POLICY::GetPolicyBool( POLICY_KEY_PCM ) != KIPLATFORM::POLICY::PBOOL::DISABLED
&& settings->m_PcmUpdateCheck )
{
if( !m_pcm )
CreatePCM();
m_pcm->RunBackgroundUpdate();
}
#ifdef KICAD_UPDATE_CHECK
if( !m_updateManager && settings->m_KiCadUpdateCheck )
{
m_updateManager = std::make_unique<UPDATE_MANAGER>();
m_updateManager->CheckForUpdate( this );
}
#endif
}
void KICAD_MANAGER_FRAME::SetPcmButton( BITMAP_BUTTON* aButton )
{
m_pcmButton = aButton;
updatePcmButtonBadge();
}
void KICAD_MANAGER_FRAME::updatePcmButtonBadge()
{
if( m_pcmButton )
{
if( m_pcmUpdateCount > 0 )
{