KiCad PCB EDA Suite
eda_base_frame.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <bitmaps.h>
27 #include <dialog_shim.h>
29 #include <filehistory.h>
30 #include <id.h>
31 #include <kiface_i.h>
32 #include <menus_helpers.h>
33 #include <panel_hotkeys_editor.h>
34 #include <pgm_base.h>
35 #include <settings/app_settings.h>
38 #include <tool/action_manager.h>
39 #include <tool/action_menu.h>
40 #include <tool/actions.h>
41 #include <tool/common_control.h>
42 #include <tool/tool_manager.h>
43 #include <trace_helpers.h>
44 #include <widgets/paged_dialog.h>
45 #include <wx/display.h>
46 #include <wx/stdpaths.h>
47 #include <wx/string.h>
48 
49 
50 // Minimum window size
51 static const int s_minsize_x = 500;
52 static const int s_minsize_y = 400;
53 
54 
55 BEGIN_EVENT_TABLE( EDA_BASE_FRAME, wxFrame )
56  EVT_MENU( wxID_ABOUT, EDA_BASE_FRAME::OnKicadAbout )
57  EVT_MENU( wxID_PREFERENCES, EDA_BASE_FRAME::OnPreferences )
58 
59  EVT_CHAR_HOOK( EDA_BASE_FRAME::OnCharHook )
60  EVT_MENU_OPEN( EDA_BASE_FRAME::OnMenuOpen )
61  EVT_MENU_CLOSE( EDA_BASE_FRAME::OnMenuOpen )
62  EVT_MENU_HIGHLIGHT_ALL( EDA_BASE_FRAME::OnMenuOpen )
63  EVT_MOVE( EDA_BASE_FRAME::OnMove )
64 END_EVENT_TABLE()
65 
66 EDA_BASE_FRAME::EDA_BASE_FRAME( wxWindow* aParent, FRAME_T aFrameType,
67  const wxString& aTitle, const wxPoint& aPos, const wxSize& aSize,
68  long aStyle, const wxString& aFrameName, KIWAY* aKiway ) :
69  wxFrame( aParent, wxID_ANY, aTitle, aPos, aSize, aStyle, aFrameName ),
70  KIWAY_HOLDER( aKiway, KIWAY_HOLDER::FRAME ),
71  m_actions( nullptr ),
72  m_immediateActions( true ),
73  m_dragSelects( true ),
74  m_moveWarpsCursor( true ),
75  m_userUnits( EDA_UNITS::MILLIMETRES )
76 {
77  m_Ident = aFrameType;
78  m_hasAutoSave = false;
79  m_autoSaveState = false;
80  m_autoSaveInterval = -1;
81  m_autoSaveTimer = new wxTimer( this, ID_AUTO_SAVE_TIMER );
82  m_mruPath = wxStandardPaths::Get().GetDocumentsDir();
83  m_toolManager = nullptr;
84  m_settingsManager = nullptr;
85 
86  // Set a reasonable minimal size for the frame
87  SetSizeHints( s_minsize_x, s_minsize_y, -1, -1, -1, -1 );
88 
89  // Store dimensions of the user area of the main window.
90  GetClientSize( &m_FrameSize.x, &m_FrameSize.y );
91 
92  Connect( ID_AUTO_SAVE_TIMER, wxEVT_TIMER,
93  wxTimerEventHandler( EDA_BASE_FRAME::onAutoSaveTimer ) );
94 
95  // hook wxEVT_CLOSE_WINDOW so we can call SaveSettings(). This function seems
96  // to be called before any other hook for wxCloseEvent, which is necessary.
97  Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( EDA_BASE_FRAME::windowClosing ) );
98 }
99 
100 
102 {
103  for( auto& iter : GetChildren() )
104  {
105  DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( iter );
106  if( dlg && dlg->IsQuasiModal() )
107  return dlg;
108  }
109 
110  // FIXME: CvPcb is currently implemented on top of KIWAY_PLAYER rather than DIALOG_SHIM,
111  // so we have to look for it separately.
112  if( m_Ident == FRAME_SCH )
113  {
114  wxWindow* cvpcb = wxWindow::FindWindowByName( "CvpcbFrame" );
115  if( cvpcb )
116  return cvpcb;
117  }
118 
119  return nullptr;
120 }
121 
122 
123 void EDA_BASE_FRAME::windowClosing( wxCloseEvent& event )
124 {
125  // Don't allow closing when a quasi-modal is open.
126  wxWindow* quasiModal = findQuasiModalDialog();
127 
128  if( quasiModal )
129  {
130  // Raise and notify; don't give the user a warning regarding "quasi-modal dialogs"
131  // when they have no idea what those are.
132  quasiModal->Raise();
133  wxBell();
134 
135  event.Veto();
136  return;
137  }
138 
139  APP_SETTINGS_BASE* cfg = config();
140 
141  if( cfg )
142  SaveSettings( cfg ); // virtual, wxFrame specific
143 
144  event.Skip(); // we did not "handle" the event, only eavesdropped on it.
145 }
146 
147 
149 {
150  delete m_autoSaveTimer;
151 
153  {
155  }
156 }
157 
158 
160 {
161 #if defined( _WIN32 )
162  return true;
163 #else
164  return false;
165 #endif
166 }
167 
168 
170 {
171 #if defined( _WIN32 )
172  // Windows: Destroys any block reason that may have existed
173  ShutdownBlockReasonDestroy( GetHandle() );
174 #endif
175 }
176 
177 
178 void EDA_BASE_FRAME::SetShutdownBlockReason( const wxString& aReason )
179 {
180 #if defined( _WIN32 )
181  // Windows: sets up the pretty message on the shutdown page on why it's being "blocked"
182  // This is used in conjunction with handling WM_QUERYENDSESSION (wxCloseEvent)
183  // ShutdownBlockReasonCreate does not block by itself
184 
185  ShutdownBlockReasonDestroy( GetHandle() ); // Destroys any existing or nonexisting reason
186 
187  if( !ShutdownBlockReasonCreate( GetHandle(), aReason.wc_str() ) )
188  {
189  // Nothing bad happens if this fails, at worst it uses a generic application is preventing shutdown message
190  wxLogDebug( wxT( "ShutdownBlockReasonCreate failed to set reason: %s" ), aReason );
191  }
192 #endif
193 }
194 
195 
196 // TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
197 void EDA_BASE_FRAME::PushTool( const std::string& actionName )
198 {
199  m_toolStack.push_back( actionName );
200 
201  // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
202  if( m_toolStack.size() > 3 )
203  m_toolStack.erase( m_toolStack.begin() );
204 
205  TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
206 
207  if( action )
208  DisplayToolMsg( action->GetLabel() );
209  else
210  DisplayToolMsg( actionName );
211 }
212 
213 
214 void EDA_BASE_FRAME::PopTool( const std::string& actionName )
215 {
216  // Push/pop events can get out of order (such as when they're generated by the Simulator
217  // frame but not processed until the mouse is back in the Schematic frame), so make sure
218  // we're popping the right stack frame.
219 
220  for( int i = m_toolStack.size() - 1; i >= 0; --i )
221  {
222  if( m_toolStack[ i ] == actionName )
223  {
224  m_toolStack.erase( m_toolStack.begin() + i );
225 
226  // If there's something underneath us, and it's now the top of the stack, then
227  // re-activate it
228  if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
229  {
230  std::string back = m_toolStack[ i ];
232 
233  if( action )
234  {
235  // Pop the action as running it will push it back onto the stack
236  m_toolStack.pop_back();
237 
238  TOOL_EVENT evt = action->MakeEvent();
239  evt.SetHasPosition( false );
240  GetToolManager()->PostEvent( evt );
241  }
242  }
243  else
245 
246  return;
247  }
248  }
249 }
250 
251 
253 {
254  if( m_toolStack.empty() )
255  return ACTIONS::selectionTool.GetName();
256  else
257  return m_toolStack.back();
258 }
259 
260 
261 bool EDA_BASE_FRAME::IsCurrentTool( const TOOL_ACTION& aAction ) const
262 {
263  if( m_toolStack.empty() )
264  return &aAction == &ACTIONS::selectionTool;
265  else
266  return m_toolStack.back() == aAction.GetName();
267 }
268 
269 
270 bool EDA_BASE_FRAME::ProcessEvent( wxEvent& aEvent )
271 {
272 #ifdef __WXMAC__
273  // Apple in its infinite wisdom will raise a disabled window before even passing
274  // us the event, so we have no way to stop it. Instead, we have to catch an
275  // improperly ordered disabled window and quasi-modal dialog here and reorder
276  // them.
277  if( !IsEnabled() && IsActive() )
278  {
279  wxWindow* dlg = findQuasiModalDialog();
280  if( dlg )
281  dlg->Raise();
282  }
283 #endif
284 
285  if( !wxFrame::ProcessEvent( aEvent ) )
286  return false;
287 
288  if( IsShown() && m_hasAutoSave && IsActive() &&
290  {
291  if( !m_autoSaveState )
292  {
293  wxLogTrace( traceAutoSave, wxT( "Starting auto save timer." ) );
294  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
295  m_autoSaveState = true;
296  }
297  else if( m_autoSaveTimer->IsRunning() )
298  {
299  wxLogTrace( traceAutoSave, wxT( "Stopping auto save timer." ) );
300  m_autoSaveTimer->Stop();
301  m_autoSaveState = false;
302  }
303  }
304 
305  return true;
306 }
307 
308 
310 {
311  m_autoSaveInterval = aInterval;
312 
313  if( m_autoSaveTimer->IsRunning() )
314  {
315  if( m_autoSaveInterval > 0 )
316  {
317  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
318  }
319  else
320  {
321  m_autoSaveTimer->Stop();
322  m_autoSaveState = false;
323  }
324  }
325 }
326 
327 
328 void EDA_BASE_FRAME::onAutoSaveTimer( wxTimerEvent& aEvent )
329 {
330  if( !doAutoSave() )
331  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
332 }
333 
334 
336 {
337  wxCHECK_MSG( false, true, wxT( "Auto save timer function not overridden. Bad programmer!" ) );
338 }
339 
340 
341 void EDA_BASE_FRAME::OnCharHook( wxKeyEvent& event )
342 {
343  wxLogTrace( kicadTraceKeyEvent, "EDA_BASE_FRAME::OnCharHook %s", dump( event ) );
344  // Key events can be filtered here.
345  // Currently no filtering is made.
346  event.Skip();
347 }
348 
349 
350 void EDA_BASE_FRAME::OnMenuOpen( wxMenuEvent& event )
351 {
352  //
353  // wxWidgets has several issues that we have to work around:
354  //
355  // 1) wxWidgets 3.0.x Windows has a bug where wxEVT_MENU_OPEN and wxEVT_MENU_HIGHLIGHT
356  // events are not captured by the ACTON_MENU menus. So we forward them here.
357  // (FWIW, this one is fixed in wxWidgets 3.1.x.)
358  //
359  // 2) wxWidgets doesn't pass the menu pointer for wxEVT_MENU_HIGHLIGHT events. So we
360  // store the menu pointer from the wxEVT_MENU_OPEN call.
361  //
362  // 3) wxWidgets has no way to tell whether a command is from a menu selection or a
363  // hotkey. So we keep track of menu highlighting so we can differentiate.
364  //
365 
366  static ACTION_MENU* currentMenu;
367 
368  if( event.GetEventType() == wxEVT_MENU_OPEN )
369  {
370  currentMenu = dynamic_cast<ACTION_MENU*>( event.GetMenu() );
371 
372  if( currentMenu )
373  currentMenu->OnMenuEvent( event );
374  }
375  else if( event.GetEventType() == wxEVT_MENU_HIGHLIGHT )
376  {
377  if( currentMenu )
378  currentMenu->OnMenuEvent( event );
379  }
380  else if( event.GetEventType() == wxEVT_MENU_CLOSE )
381  {
382  if( currentMenu )
383  currentMenu->OnMenuEvent( event );
384 
385  currentMenu = nullptr;
386  }
387 
388  event.Skip();
389 }
390 
391 
393 {
394 }
395 
396 
397 void EDA_BASE_FRAME::AddStandardHelpMenu( wxMenuBar* aMenuBar )
398 {
399  COMMON_CONTROL* commonControl = m_toolManager->GetTool<COMMON_CONTROL>();
400  ACTION_MENU* helpMenu = new ACTION_MENU( false );
401 
402  helpMenu->SetTool( commonControl );
403 
404  helpMenu->Add( ACTIONS::help );
405  helpMenu->Add( ACTIONS::gettingStarted );
406  helpMenu->Add( ACTIONS::listHotKeys );
407  helpMenu->Add( ACTIONS::getInvolved );
408 
409  helpMenu->AppendSeparator();
410  helpMenu->Add( _( "&About KiCad" ), "", wxID_ABOUT, about_xpm );
411 
412  aMenuBar->Append( helpMenu, _( "&Help" ) );
413 }
414 
415 
417 {
418  if( GetMenuBar() )
419  {
420  ReCreateMenuBar();
421  GetMenuBar()->Refresh();
422  }
423 }
424 
425 
426 void EDA_BASE_FRAME::CommonSettingsChanged( bool aEnvVarsChanged )
427 {
428  if( GetToolManager() )
430 
431  if( GetMenuBar() )
432  {
433  // For icons in menus, icon scaling & hotkeys
434  ReCreateMenuBar();
435  GetMenuBar()->Refresh();
436  }
437 
438  COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
439 
443 }
444 
445 
447 {
448  m_FramePos.x = aCfg->pos_x;
449  m_FramePos.y = aCfg->pos_y;
450  m_FrameSize.x = aCfg->size_x;
451  m_FrameSize.y = aCfg->size_y;
452 
453  // Ensure minimum size is set if the stored config was zero-initialized
455  {
458  }
459 
460  // Ensure window isn't bigger than can be displayed
461  int displayIndex = wxDisplay::GetFromPoint( m_FramePos );
462 
463  if( displayIndex == wxNOT_FOUND )
464  displayIndex = 0;
465 
466  wxDisplay display( displayIndex );
467  wxRect clientSize = display.GetClientArea();
468 
469  // The window may have been saved on a display that is no longer present.
470  // First, check the window origin and move it if it's off the chosen display
471 
472  if( m_FramePos.x >= clientSize.x + clientSize.width ||
473  m_FramePos.y >= clientSize.y + clientSize.height )
474  m_FramePos = wxDefaultPosition;
475 
476  // Now, fix up the size if needed
477 
478  if( m_FrameSize.x + m_FramePos.x > clientSize.x + clientSize.width )
479  {
480  m_FrameSize.x = clientSize.width;
481  m_FramePos.x = 0;
482  }
483 
484  if( m_FrameSize.y + m_FramePos.y > clientSize.y + clientSize.height )
485  {
486  m_FrameSize.y = clientSize.height;
487  m_FramePos.y = 0;
488  }
489 
490  if( m_hasAutoSave )
491  m_autoSaveInterval = Pgm().GetCommonSettings()->m_System.autosave_interval;
492 
493  // Ensure Window title bar is visible
494 #if defined( __WXMAC__ )
495  // for macOSX, the window must be below system (macOSX) toolbar
496  // Ypos_min = GetMBarHeight(); seems no more exist in new API (subject to change)
497  int Ypos_min = 20;
498 #else
499  int Ypos_min = 0;
500 #endif
501  if( m_FramePos.y < Ypos_min )
502  {
503  if( m_FrameSize.y + ( Ypos_min - m_FramePos.y ) > clientSize.height)
504  m_FrameSize.y = clientSize.height - Ypos_min;
505 
506  m_FramePos.y = Ypos_min;
507  }
508 
509  if( aCfg->maximized )
510  Maximize();
511 
512  m_perspective = aCfg->perspective;
513  m_mruPath = aCfg->mru_path;
514 
515  COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
516 
520 }
521 
522 
524 {
525  wxString text;
526 
527  if( IsIconized() )
528  return;
529 
530  wxString baseCfgName = ConfigBaseName();
531 
532  m_FrameSize = GetSize();
533  m_FramePos = GetPosition();
534 
535  aCfg->pos_x = m_FramePos.x;
536  aCfg->pos_y = m_FramePos.y;
537  aCfg->size_x = m_FrameSize.x;
538  aCfg->size_y = m_FrameSize.y;
539  aCfg->maximized = IsMaximized();
540 
541  // TODO(JE) should auto-save in common settings be overwritten by every app?
542  if( m_hasAutoSave )
543  Pgm().GetCommonSettings()->m_System.autosave_interval = m_autoSaveInterval;
544 
545  // Once this is fully implemented, wxAuiManager will be used to maintain
546  // the persistance of the main frame and all it's managed windows and
547  // all of the legacy frame persistence position code can be removed.
548  aCfg->perspective = m_auimgr.SavePerspective().ToStdString();
549 
550  aCfg->mru_path = m_mruPath;
551 }
552 
553 
555 {
557 }
558 
559 
561 {
563 }
564 
565 
567 {
568  return &aCfg->m_Window;
569 }
570 
571 
573 {
574  // KICAD_MANAGER_FRAME overrides this
576  //wxASSERT( ret );
577  return ret;
578 }
579 
580 
582 {
583  return Kiface().KifaceSearch();
584 }
585 
586 
588 {
589  return Kiface().GetHelpFileName();
590 }
591 
592 
593 void EDA_BASE_FRAME::PrintMsg( const wxString& text )
594 {
595  SetStatusText( text );
596 }
597 
598 
599 void EDA_BASE_FRAME::UpdateFileHistory( const wxString& FullFileName, FILE_HISTORY* aFileHistory )
600 {
601  FILE_HISTORY* fileHistory = aFileHistory;
602 
603  if( !fileHistory )
604  fileHistory = &Kiface().GetFileHistory();
605 
606  fileHistory->AddFileToHistory( FullFileName );
607 }
608 
609 
610 wxString EDA_BASE_FRAME::GetFileFromHistory( int cmdId, const wxString& type,
611  FILE_HISTORY* aFileHistory )
612 {
613  FILE_HISTORY* fileHistory = aFileHistory;
614 
615  if( !fileHistory )
616  fileHistory = &Kiface().GetFileHistory();
617 
618  int baseId = fileHistory->GetBaseId();
619 
620  wxASSERT( cmdId >= baseId && cmdId < baseId + (int) fileHistory->GetCount() );
621 
622  unsigned i = cmdId - baseId;
623 
624  if( i < fileHistory->GetCount() )
625  {
626  wxString fn = fileHistory->GetHistoryFile( i );
627 
628  if( wxFileName::FileExists( fn ) )
629  return fn;
630  else
631  {
632  wxString msg = wxString::Format( _( "File \"%s\" was not found." ), fn );
633  wxMessageBox( msg );
634 
635  fileHistory->RemoveFileFromHistory( i );
636  }
637  }
638 
639  return wxEmptyString;
640 }
641 
642 
644 {
645  if( !aFileHistory )
646  aFileHistory = &Kiface().GetFileHistory();
647 
648  aFileHistory->ClearFileHistory();
649 }
650 
651 
652 void EDA_BASE_FRAME::OnKicadAbout( wxCommandEvent& event )
653 {
654  void ShowAboutDialog(EDA_BASE_FRAME * aParent); // See AboutDialog_main.cpp
655  ShowAboutDialog( this );
656 }
657 
658 
659 void EDA_BASE_FRAME::OnPreferences( wxCommandEvent& event )
660 {
661  PAGED_DIALOG dlg( this, _( "Preferences" ) );
662  wxTreebook* book = dlg.GetTreebook();
663 
664  book->AddPage( new PANEL_COMMON_SETTINGS( &dlg, book ), _( "Common" ) );
665 
666  PANEL_HOTKEYS_EDITOR* hotkeysPanel = new PANEL_HOTKEYS_EDITOR( this, book, false );
667  book->AddPage( hotkeysPanel, _( "Hotkeys" ) );
668 
669  for( unsigned i = 0; i < KIWAY_PLAYER_COUNT; ++i )
670  {
671  KIWAY_PLAYER* frame = dlg.Kiway().Player( (FRAME_T) i, false );
672 
673  if( frame )
674  frame->InstallPreferences( &dlg, hotkeysPanel );
675  }
676 
677  // The Kicad manager frame is not a player so we have to add it by hand
678  wxWindow* manager = wxFindWindowByName( KICAD_MANAGER_FRAME_NAME );
679 
680  if( manager )
681  static_cast<EDA_BASE_FRAME*>( manager )->InstallPreferences( &dlg, hotkeysPanel );
682 
683  for( int i = 0; i < book->GetPageCount(); ++i )
684  book->GetPage( i )->Layout();
685 
686  if( dlg.ShowModal() == wxID_OK )
687  dlg.Kiway().CommonSettingsChanged( false );
688 }
689 
690 
691 bool EDA_BASE_FRAME::IsWritable( const wxFileName& aFileName )
692 {
693  wxString msg;
694  wxFileName fn = aFileName;
695 
696  // Check for absence of a file path with a file name. Unfortunately KiCad
697  // uses paths relative to the current project path without the ./ part which
698  // confuses wxFileName. Making the file name path absolute may be less than
699  // elegant but it solves the problem.
700  if( fn.GetPath().IsEmpty() && fn.HasName() )
701  fn.MakeAbsolute();
702 
703  wxCHECK_MSG( fn.IsOk(), false,
704  wxT( "File name object is invalid. Bad programmer!" ) );
705  wxCHECK_MSG( !fn.GetPath().IsEmpty(), false,
706  wxT( "File name object path <" ) + fn.GetFullPath() +
707  wxT( "> is not set. Bad programmer!" ) );
708 
709  if( fn.IsDir() && !fn.IsDirWritable() )
710  {
711  msg.Printf( _( "You do not have write permissions to folder \"%s\"." ),
712  GetChars( fn.GetPath() ) );
713  }
714  else if( !fn.FileExists() && !fn.IsDirWritable() )
715  {
716  msg.Printf( _( "You do not have write permissions to save file \"%s\" to folder \"%s\"." ),
717  GetChars( fn.GetFullName() ), GetChars( fn.GetPath() ) );
718  }
719  else if( fn.FileExists() && !fn.IsFileWritable() )
720  {
721  msg.Printf( _( "You do not have write permissions to save file \"%s\"." ),
722  GetChars( fn.GetFullPath() ) );
723  }
724 
725  if( !msg.IsEmpty() )
726  {
727  wxMessageBox( msg );
728  return false;
729  }
730 
731  return true;
732 }
733 
734 
735 void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
736 {
737  wxCHECK_RET( aFileName.IsOk(), wxT( "Invalid file name!" ) );
738 
739  wxFileName autoSaveFileName = aFileName;
740 
741  // Check for auto save file.
742  autoSaveFileName.SetName( GetAutoSaveFilePrefix() + aFileName.GetName() );
743 
744  wxLogTrace( traceAutoSave,
745  wxT( "Checking for auto save file " ) + autoSaveFileName.GetFullPath() );
746 
747  if( !autoSaveFileName.FileExists() )
748  return;
749 
750  wxString msg = wxString::Format( _(
751  "Well this is potentially embarrassing!\n"
752  "It appears that the last time you were editing the file\n"
753  "\"%s\"\n"
754  "it was not saved properly. Do you wish to restore the last saved edits you made?" ),
755  GetChars( aFileName.GetFullName() )
756  );
757 
758  int response = wxMessageBox( msg, Pgm().App().GetAppName(), wxYES_NO | wxICON_QUESTION, this );
759 
760  // Make a backup of the current file, delete the file, and rename the auto save file to
761  // the file name.
762  if( response == wxYES )
763  {
764  // Get the backup file name.
765  wxFileName backupFileName = aFileName;
766  backupFileName.SetExt( aFileName.GetExt() + GetBackupSuffix() );
767 
768  // If an old backup file exists, delete it. If an old copy of the file exists, rename
769  // it to the backup file name
770  if( aFileName.FileExists() )
771  {
772  // Rename the old file to the backup file name.
773  if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath(), true ) )
774  {
775  msg.Printf( _( "Could not create backup file \"%s\"" ),
776  GetChars( backupFileName.GetFullPath() ) );
777  wxMessageBox( msg );
778  }
779  }
780 
781  if( !wxRenameFile( autoSaveFileName.GetFullPath(), aFileName.GetFullPath() ) )
782  {
783  wxMessageBox( _( "The auto save file could not be renamed to the board file name." ),
784  Pgm().App().GetAppName(), wxOK | wxICON_EXCLAMATION, this );
785  }
786  }
787  else
788  {
789  wxLogTrace( traceAutoSave,
790  wxT( "Removing auto save file " ) + autoSaveFileName.GetFullPath() );
791 
792  // Remove the auto save file when using the previous file as is.
793  wxRemoveFile( autoSaveFileName.GetFullPath() );
794  }
795 }
796 
797 
799 {
800  // This function should be overridden in child classes
801  return false;
802 }
EDA_UNITS
Definition: common.h:184
wxString mru_path
Definition: app_settings.h:55
static TOOL_ACTION listHotKeys
Definition: actions.h:171
void PrintMsg(const wxString &text)
KIWAY_HOLDER is a mix in class which holds the location of a wxWindow's KIWAY.
Definition: kiway_holder.h:39
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of ...
Definition: kiway_player.h:59
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
wxString m_mruPath
void SetShutdownBlockReason(const wxString &reason)
Sets the block reason why the window/application is preventing OS shutdown.
virtual bool doAutoSave()
This should be overridden by the derived class to handle the auto save feature.
const wxChar *const traceAutoSave
Flag to enable auto save feature debug tracing.
virtual void SaveSettings(APP_SETTINGS_BASE *aCfg)
Saves common frame parameters to a configuration data file.
ACTION_MENU.
Definition: action_menu.h:43
VTBL_ENTRY void CommonSettingsChanged(bool aEnvVarsChanged)
Function CommonSettingsChanged Calls CommonSettingsChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:471
void SaveWindowSettings(WINDOW_SETTINGS *aCfg)
Saves window settings to the given settings object Normally called by SaveSettings unless the window ...
FRAME_T
Enum FRAME_T is the set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:34
void windowClosing(wxCloseEvent &event)
(with its unexpected name so it does not collide with the real OnWindowClose() function provided in d...
virtual bool isAutoSaveRequired() const
Return the auto save status of the application.
void onAutoSaveTimer(wxTimerEvent &aEvent)
Handle the auto save timer event.
wxString ConfigBaseName()
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_i.h:103
virtual bool IsContentModified()
Get if the contents of the frame have been modified since the last save.
void UpdateHotKeys(bool aFullUpdate)
Function UpdateHotKeys() Optionally reads the hotkey config files and then rebuilds the internal hotk...
SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:84
wxAuiManager m_auimgr
bool IsWritable(const wxFileName &aFileName)
Checks if aFileName can be written.
This class implements a file history object to store a list of files, that can then be added to a men...
Definition: filehistory.h:42
wxTimer * m_autoSaveTimer
void ClearFileHistory(FILE_HISTORY *aFileHistory=NULL)
Removes all files from the file history.
void ClearFileHistory()
Clear all entries from the file history.
TOOL_ACTION * FindAction(const std::string &aActionName) const
Function FindAction() Finds an action with a given name (if there is one available).
wxString m_perspective
void UpdateFileHistory(const wxString &FullFileName, FILE_HISTORY *aFileHistory=NULL)
Update the list of recently opened files.
wxTreebook * GetTreebook()
Definition: paged_dialog.h:43
void AddFileToHistory(const wxString &aFile) override
Adds a file to the history.
Definition: filehistory.cpp:94
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_i.h:129
wxWindow * findQuasiModalDialog()
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:52
const wxString & GetHelpFileName() const
Function GetHelpFileName returns just the basename portion of the current help file.
Definition: kiface_i.h:123
void ShowAboutDialog(EDA_BASE_FRAME *aParent)
virtual void OnCharHook(wxKeyEvent &event)
Capture the key event before it is sent to the GUI.
wxString perspective
Definition: app_settings.h:58
const BITMAP_OPAQUE about_xpm[1]
Definition: about.cpp:84
void CheckForAutoSaveFile(const wxFileName &aFileName)
Check if an auto save file exists for aFileName and takes the appropriate action depending on the use...
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:75
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:341
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
static wxString GetBackupSuffix()
virtual const SEARCH_STACK & sys_search()
Return a SEARCH_STACK pertaining to entire program.
virtual void DisplayToolMsg(const wxString &msg)
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
TOOL_EVENT.
Definition: tool_event.h:171
void AddStandardHelpMenu(wxMenuBar *aMenuBar)
Adds the standard KiCad help menu to the menubar.
bool IsQuasiModal()
Definition: dialog_shim.h:124
virtual WINDOW_SETTINGS * GetWindowSettings(APP_SETTINGS_BASE *aCfg)
Returns a pointer to the window settings for this frame.
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
wxLogTrace helper definitions.
void OnKicadAbout(wxCommandEvent &event)
COMMON_CONTROL.
void LoadWindowSettings(WINDOW_SETTINGS *aCfg)
Loads window settings from the given settings object Normally called by LoadSettings unless the windo...
void OnMenuOpen(wxMenuEvent &event)
Workaround some issues in wxWidgets where the menu events aren't captured by the menus themselves.
bool SupportsShutdownBlockReason()
Whether or not the window supports setting a shutdown block reason.
void OnMenuEvent(wxMenuEvent &aEvent)
void RemoveShutdownBlockReason()
Removes any shutdown block reason set.
virtual void OnMove(wxMoveEvent &aEvent)
virtual void CommonSettingsChanged(bool aEnvVarsChanged)
Notification event that some of the common (suite-wide) settings have changed.
static wxString GetAutoSaveFilePrefix()
WINDOW_SETTINGS m_Window
Definition: app_settings.h:132
void SetAutoSaveInterval(int aInterval)
wxString GetFileFromHistory(int cmdId, const wxString &type, FILE_HISTORY *aFileHistory=NULL)
Fetches the file name from the file history list.
wxString GetLabel() const
Definition: tool_action.cpp:69
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:101
see class PGM_BASE
std::vector< std::string > m_toolStack
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
ACTION_MANAGER * GetActionManager()
Definition: tool_manager.h:193
#define KICAD_MANAGER_FRAME_NAME
#define _(s)
Definition: 3d_actions.cpp:33
FILE_HISTORY & GetFileHistory()
Definition: kiface_i.h:125
The base frame for deriving all KiCad main window classes.
TOOL_ACTION.
Definition: tool_action.h:46
TOOL_EVENT MakeEvent() const
Function MakeEvent() Returns the event associated with the action (i.e.
Definition: tool_action.h:107
static TOOL_ACTION help
Definition: actions.h:170
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
virtual wxString help_name()
virtual void PopTool(const std::string &actionName)
void OnPreferences(wxCommandEvent &event)
static const int s_minsize_x
virtual void InstallPreferences(PAGED_DIALOG *, PANEL_HOTKEYS_EDITOR *)
Function InstallPreferences Allow a frame to load its preference panels (if any) into the preferences...
TOOL_MANAGER * m_toolManager
std::string CurrentToolName() const
static const int s_minsize_y
virtual APP_SETTINGS_BASE * config()
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
static TOOL_ACTION getInvolved
Definition: actions.h:172
void PostEvent(const TOOL_EVENT &aEvent)
Puts an event to the event queue to be processed at the end of event processing cycle.
Definition: tool_manager.h:267
bool IsCurrentTool(const TOOL_ACTION &aAction) const
virtual void ShowChangedLanguage()
Redraw the menus and what not in current language.
static TOOL_ACTION gettingStarted
Definition: actions.h:169
bool ProcessEvent(wxEvent &aEvent) override
Override the default process event handler to implement the auto save feature.
static TOOL_ACTION selectionTool
Definition: actions.h:146
void SetHasPosition(bool aHasPosition)
Definition: tool_event.h:261
virtual void LoadSettings(APP_SETTINGS_BASE *aCfg)
Load common frame parameters from a configuration file.
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78
virtual void ReCreateMenuBar()
Recreates the menu bar.