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 <wx/stdpaths.h>
27 #include <wx/string.h>
28 #include <wx/display.h>
29 #include <dialog_shim.h>
30 #include <eda_doc.h>
31 #include <id.h>
32 #include <kiface_i.h>
33 #include <pgm_base.h>
34 #include <trace_helpers.h>
35 #include <panel_hotkeys_editor.h>
37 #include <widgets/paged_dialog.h>
38 #include <bitmaps.h>
39 #include <tool/action_menu.h>
40 #include <tool/common_control.h>
41 #include <tool/tool_manager.h>
42 #include <tool/action_manager.h>
43 #include <menus_helpers.h>
44 #include <tool/actions.h>
45 
46 
48 #define DEFAULT_AUTO_SAVE_INTERVAL 600
49 
52 
54 static const wxString entryAutoSaveInterval = "AutoSaveInterval";
55 
57 static const wxString entryPerspective = "Perspective";
58 
60 static const wxString entryMruPath = "MostRecentlyUsedPath";
61 
62 static const wxString entryPosY = "Pos_y";
63 static const wxString entryPosX = "Pos_x";
64 static const wxString entrySizeY = "Size_y";
65 static const wxString entrySizeX = "Size_x";
66 static const wxString entryMaximized = "Maximized";
67 
69 
70 BEGIN_EVENT_TABLE( EDA_BASE_FRAME, wxFrame )
71  EVT_MENU( wxID_ABOUT, EDA_BASE_FRAME::OnKicadAbout )
72  EVT_MENU( wxID_PREFERENCES, EDA_BASE_FRAME::OnPreferences )
73 
74  EVT_CHAR_HOOK( EDA_BASE_FRAME::OnCharHook )
75  EVT_MENU_OPEN( EDA_BASE_FRAME::OnMenuOpen )
76  EVT_MENU_CLOSE( EDA_BASE_FRAME::OnMenuOpen )
77  EVT_MENU_HIGHLIGHT_ALL( EDA_BASE_FRAME::OnMenuOpen )
78 END_EVENT_TABLE()
79 
80 EDA_BASE_FRAME::EDA_BASE_FRAME( wxWindow* aParent, FRAME_T aFrameType,
81  const wxString& aTitle, const wxPoint& aPos, const wxSize& aSize,
82  long aStyle, const wxString& aFrameName, KIWAY* aKiway ) :
83  wxFrame( aParent, wxID_ANY, aTitle, aPos, aSize, aStyle, aFrameName ),
84  KIWAY_HOLDER( aKiway, KIWAY_HOLDER::FRAME )
85 {
86  m_Ident = aFrameType;
87  m_hasAutoSave = false;
88  m_autoSaveState = false;
89  m_autoSaveInterval = -1;
90  m_autoSaveTimer = new wxTimer( this, ID_AUTO_SAVE_TIMER );
91  m_mruPath = wxStandardPaths::Get().GetDocumentsDir();
92  m_toolManager = nullptr;
93 
94  // Gives a reasonable minimal size to the frame:
95  const int minsize_x = 500;
96  const int minsize_y = 400;
97  SetSizeHints( minsize_x, minsize_y, -1, -1, -1, -1 );
98 
99  // Store dimensions of the user area of the main window.
100  GetClientSize( &m_FrameSize.x, &m_FrameSize.y );
101 
102  Connect( ID_AUTO_SAVE_TIMER, wxEVT_TIMER,
103  wxTimerEventHandler( EDA_BASE_FRAME::onAutoSaveTimer ) );
104 
105  // hook wxEVT_CLOSE_WINDOW so we can call SaveSettings(). This function seems
106  // to be called before any other hook for wxCloseEvent, which is necessary.
107  Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( EDA_BASE_FRAME::windowClosing ) );
108 }
109 
110 
112 {
113  for( auto& iter : GetChildren() )
114  {
115  DIALOG_SHIM* dlg = dynamic_cast<DIALOG_SHIM*>( iter );
116  if( dlg && dlg->IsQuasiModal() )
117  return dlg;
118  }
119 
120  // FIXME: CvPcb is currently implemented on top of KIWAY_PLAYER rather than DIALOG_SHIM,
121  // so we have to look for it separately.
122  if( m_Ident == FRAME_SCH )
123  {
124  wxWindow* cvpcb = wxWindow::FindWindowByName( "CvpcbFrame" );
125  if( cvpcb )
126  return cvpcb;
127  }
128 
129  return nullptr;
130 }
131 
132 
133 void EDA_BASE_FRAME::windowClosing( wxCloseEvent& event )
134 {
135  // Don't allow closing when a quasi-modal is open.
136  wxWindow* quasiModal = findQuasiModalDialog();
137 
138  if( quasiModal )
139  {
140  // Raise and notify; don't give the user a warning regarding "quasi-modal dialogs"
141  // when they have no idea what those are.
142  quasiModal->Raise();
143  wxBell();
144 
145  event.Veto();
146  return;
147  }
148 
149  wxConfigBase* cfg = config();
150 
151  if( cfg )
152  SaveSettings( cfg ); // virtual, wxFrame specific
153 
154  event.Skip(); // we did not "handle" the event, only eavesdropped on it.
155 }
156 
157 
159 {
160  delete m_autoSaveTimer;
161 }
162 
163 
164 void EDA_BASE_FRAME::PushTool( const std::string& actionName )
165 {
166  m_toolStack.push_back( actionName );
167 
168  // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
169  if( m_toolStack.size() > 3 )
170  m_toolStack.erase( m_toolStack.begin() );
171 
172  TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
173 
174  if( action )
175  DisplayToolMsg( action->GetLabel() );
176  else
177  DisplayToolMsg( actionName );
178 }
179 
180 
181 void EDA_BASE_FRAME::PopTool( const std::string& actionName )
182 {
183  // Push/pop events can get out of order (such as when they're generated by the Simulator
184  // frame but not processed until the mouse is back in the Schematic frame), so make sure
185  // we're popping the right stack frame.
186 
187  for( int i = m_toolStack.size() - 1; i >= 0; --i )
188  {
189  if( m_toolStack[ i ] == actionName )
190  {
191  m_toolStack.erase( m_toolStack.begin() + i );
192 
193  // If there's something underneath us, and it's now the top of the stack, then
194  // re-activate it
195  if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
196  {
197  std::string back = m_toolStack[ i ];
199 
200  if( action )
201  {
202  // Pop the action as running it will push it back onto the stack
203  m_toolStack.pop_back();
204 
205  TOOL_EVENT evt = action->MakeEvent();
206  evt.SetHasPosition( false );
207  GetToolManager()->PostEvent( evt );
208  }
209  }
210  else
212 
213  return;
214  }
215  }
216 }
217 
218 
220 {
221  if( m_toolStack.empty() )
222  return ACTIONS::selectionTool.GetName();
223  else
224  return m_toolStack.back();
225 }
226 
227 
228 bool EDA_BASE_FRAME::IsCurrentTool( const TOOL_ACTION& aAction ) const
229 {
230  if( m_toolStack.empty() )
231  return &aAction == &ACTIONS::selectionTool;
232  else
233  return m_toolStack.back() == aAction.GetName();
234 }
235 
236 
237 bool EDA_BASE_FRAME::ProcessEvent( wxEvent& aEvent )
238 {
239 #ifdef __WXMAC__
240  // Apple in its infinite wisdom will raise a disabled window before even passing
241  // us the event, so we have no way to stop it. Instead, we have to catch an
242  // improperly ordered disabled window and quasi-modal dialog here and reorder
243  // them.
244  if( !IsEnabled() && IsActive() )
245  {
246  wxWindow* dlg = findQuasiModalDialog();
247  if( dlg )
248  dlg->Raise();
249  }
250 #endif
251 
252  if( !wxFrame::ProcessEvent( aEvent ) )
253  return false;
254 
255  if( IsShown() && m_hasAutoSave && IsActive() &&
257  {
258  if( !m_autoSaveState )
259  {
260  wxLogTrace( traceAutoSave, wxT( "Starting auto save timer." ) );
261  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
262  m_autoSaveState = true;
263  }
264  else if( m_autoSaveTimer->IsRunning() )
265  {
266  wxLogTrace( traceAutoSave, wxT( "Stopping auto save timer." ) );
267  m_autoSaveTimer->Stop();
268  m_autoSaveState = false;
269  }
270  }
271 
272  return true;
273 }
274 
275 
277 {
278  m_autoSaveInterval = aInterval;
279 
280  if( m_autoSaveTimer->IsRunning() )
281  {
282  if( m_autoSaveInterval > 0 )
283  {
284  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
285  }
286  else
287  {
288  m_autoSaveTimer->Stop();
289  m_autoSaveState = false;
290  }
291  }
292 }
293 
294 
295 void EDA_BASE_FRAME::onAutoSaveTimer( wxTimerEvent& aEvent )
296 {
297  if( !doAutoSave() )
298  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
299 }
300 
301 
303 {
304  wxCHECK_MSG( false, true, wxT( "Auto save timer function not overridden. Bad programmer!" ) );
305 }
306 
307 
308 void EDA_BASE_FRAME::OnCharHook( wxKeyEvent& event )
309 {
310  wxLogTrace( kicadTraceKeyEvent, "EDA_BASE_FRAME::OnCharHook %s", dump( event ) );
311  // Key events can be filtered here.
312  // Currently no filtering is made.
313  event.Skip();
314 }
315 
316 
317 void EDA_BASE_FRAME::OnMenuOpen( wxMenuEvent& event )
318 {
319  //
320  // wxWidgets has several issues that we have to work around:
321  //
322  // 1) wxWidgets 3.0.x Windows has a bug where wxEVT_MENU_OPEN and wxEVT_MENU_HIGHLIGHT
323  // events are not captured by the ACTON_MENU menus. So we forward them here.
324  // (FWIW, this one is fixed in wxWidgets 3.1.x.)
325  //
326  // 2) wxWidgets doesn't pass the menu pointer for wxEVT_MENU_HIGHLIGHT events. So we
327  // store the menu pointer from the wxEVT_MENU_OPEN call.
328  //
329  // 3) wxWidgets has no way to tell whether a command is from a menu selection or a
330  // hotkey. So we keep track of menu highlighting so we can differentiate.
331  //
332 
333  static ACTION_MENU* currentMenu;
334 
335  if( event.GetEventType() == wxEVT_MENU_OPEN )
336  {
337  currentMenu = dynamic_cast<ACTION_MENU*>( event.GetMenu() );
338 
339  if( currentMenu )
340  currentMenu->OnMenuEvent( event );
341  }
342  else if( event.GetEventType() == wxEVT_MENU_HIGHLIGHT )
343  {
344  if( currentMenu )
345  currentMenu->OnMenuEvent( event );
346  }
347  else if( event.GetEventType() == wxEVT_MENU_CLOSE )
348  {
349  if( currentMenu )
350  currentMenu->OnMenuEvent( event );
351 
352  currentMenu = nullptr;
353  }
354 
355  event.Skip();
356 }
357 
358 
360 {
361 }
362 
363 
364 void EDA_BASE_FRAME::AddStandardHelpMenu( wxMenuBar* aMenuBar )
365 {
366  COMMON_CONTROL* commonControl = m_toolManager->GetTool<COMMON_CONTROL>();
367  ACTION_MENU* helpMenu = new ACTION_MENU( false );
368 
369  helpMenu->SetTool( commonControl );
370 
371  helpMenu->Add( ACTIONS::help );
372  helpMenu->Add( ACTIONS::gettingStarted );
373  helpMenu->Add( ACTIONS::listHotKeys );
374  helpMenu->Add( ACTIONS::getInvolved );
375 
376  helpMenu->AppendSeparator();
377  helpMenu->Add( _( "&About KiCad" ), "", wxID_ABOUT, about_xpm );
378 
379  aMenuBar->Append( helpMenu, _( "&Help" ) );
380 }
381 
382 
384 {
385  if( GetMenuBar() )
386  {
387  ReCreateMenuBar();
388  GetMenuBar()->Refresh();
389  }
390 }
391 
392 
393 void EDA_BASE_FRAME::CommonSettingsChanged( bool aEnvVarsChanged )
394 {
396 
397  if( GetMenuBar() )
398  {
399  // For icons in menus, icon scaling & hotkeys
400  ReCreateMenuBar();
401  GetMenuBar()->Refresh();
402  }
403 
404  wxConfigBase* settings = Pgm().CommonSettings();
405 
406  settings->Read( WARP_MOUSE_ON_MOVE_KEY, &m_moveWarpsCursor );
407  settings->Read( PREFER_SELECT_TO_DRAG_KEY, &m_dragSelects );
408  settings->Read( IMMEDIATE_ACTIONS_KEY, &m_immediateActions );
409 }
410 
411 
412 void EDA_BASE_FRAME::LoadSettings( wxConfigBase* aCfg )
413 {
414  int maximized = 0;
415 
416  wxString baseCfgName = ConfigBaseName();
417 
418  wxString text = baseCfgName + entryPosX;
419  aCfg->Read( text, &m_FramePos.x, m_FramePos.x );
420 
421  text = baseCfgName + entryPosY;
422  aCfg->Read( text, &m_FramePos.y, m_FramePos.y );
423 
424  text = baseCfgName + entrySizeX;
425  aCfg->Read( text, &m_FrameSize.x, m_FrameSize.x );
426 
427  text = baseCfgName + entrySizeY;
428  aCfg->Read( text, &m_FrameSize.y, m_FrameSize.y );
429 
430  text = baseCfgName + entryMaximized;
431  aCfg->Read( text, &maximized, 0 );
432 
433  if( m_hasAutoSave )
434  {
435  text = baseCfgName + entryAutoSaveInterval;
436  aCfg->Read( text, &m_autoSaveInterval, DEFAULT_AUTO_SAVE_INTERVAL );
437  }
438 
439  // Ensure the window is on a connected display, and is visible.
440  // (at least a corner of the frame must be visible on screen)
441  // Sometimes, if a window was moved on an auxiliary display, and when this
442  // display is no more available, it is not the case.
443  wxRect rect( m_FramePos, m_FrameSize );
444 
445  if( wxDisplay::GetFromPoint( rect.GetTopLeft() ) == wxNOT_FOUND &&
446  wxDisplay::GetFromPoint( rect.GetTopRight() ) == wxNOT_FOUND &&
447  wxDisplay::GetFromPoint( rect.GetBottomLeft() ) == wxNOT_FOUND &&
448  wxDisplay::GetFromPoint( rect.GetBottomRight() ) == wxNOT_FOUND )
449  {
450  m_FramePos = wxDefaultPosition;
451  }
452 
453  // Ensure Window title bar is visible
454 #if defined( __WXMAC__ )
455  // for macOSX, the window must be below system (macOSX) toolbar
456  // Ypos_min = GetMBarHeight(); seems no more exist in new API (subject to change)
457  int Ypos_min = 20;
458 #else
459  int Ypos_min = 0;
460 #endif
461  if( m_FramePos.y < Ypos_min )
462  m_FramePos.y = Ypos_min;
463 
464  if( maximized )
465  Maximize();
466 
467  aCfg->Read( baseCfgName + entryPerspective, &m_perspective );
468  aCfg->Read( baseCfgName + entryMruPath, &m_mruPath );
469 
470  wxConfigBase* settings = Pgm().CommonSettings();
471 
472  if( !settings->Read( WARP_MOUSE_ON_MOVE_KEY, &m_moveWarpsCursor ) )
473  {
474  // Legacy versions stored the property only for Eeschema, so see if we have it there
475  std::unique_ptr<wxConfigBase> pcbSettings = GetNewConfig( wxT( "eeschema" ) );
476  pcbSettings->Read( "MoveWarpsCursor", &m_moveWarpsCursor, true );
477  }
478 
479  if( !settings->Read( PREFER_SELECT_TO_DRAG_KEY, &m_dragSelects ) )
480  {
481  // Legacy versions stored the property only for PCBNew, so see if we have it there
482  std::unique_ptr<wxConfigBase> pcbSettings = GetNewConfig( wxT( "pcbnew" ) );
483  pcbSettings->Read( "DragSelects", &m_dragSelects, true );
484  }
485 
486  settings->Read( IMMEDIATE_ACTIONS_KEY, &m_immediateActions, false );
487 }
488 
489 
490 void EDA_BASE_FRAME::SaveSettings( wxConfigBase* aCfg )
491 {
492  wxString text;
493 
494  if( IsIconized() )
495  return;
496 
497  wxString baseCfgName = ConfigBaseName();
498 
499  m_FrameSize = GetSize();
500  m_FramePos = GetPosition();
501 
502  text = baseCfgName + wxT( "Pos_x" );
503  aCfg->Write( text, (long) m_FramePos.x );
504 
505  text = baseCfgName + wxT( "Pos_y" );
506  aCfg->Write( text, (long) m_FramePos.y );
507 
508  text = baseCfgName + wxT( "Size_x" );
509  aCfg->Write( text, (long) m_FrameSize.x );
510 
511  text = baseCfgName + wxT( "Size_y" );
512  aCfg->Write( text, (long) m_FrameSize.y );
513 
514  text = baseCfgName + wxT( "Maximized" );
515  aCfg->Write( text, IsMaximized() );
516 
517  if( m_hasAutoSave )
518  {
519  text = baseCfgName + entryAutoSaveInterval;
520  aCfg->Write( text, m_autoSaveInterval );
521  }
522 
523  // Once this is fully implemented, wxAuiManager will be used to maintain
524  // the persistance of the main frame and all it's managed windows and
525  // all of the legacy frame persistence position code can be removed.
526  wxString perspective = m_auimgr.SavePerspective();
527 
528  // printf( "perspective(%s): %s\n",
529  // TO_UTF8( m_FrameName + entryPerspective ), TO_UTF8( perspective ) );
530  aCfg->Write( baseCfgName + entryPerspective, perspective );
531  aCfg->Write( baseCfgName + entryMruPath, m_mruPath );
532 }
533 
534 
535 wxConfigBase* EDA_BASE_FRAME::config()
536 {
537  // KICAD_MANAGER_FRAME overrides this
538  wxConfigBase* ret = Kiface().KifaceSettings();
539  //wxASSERT( ret );
540  return ret;
541 }
542 
543 
545 {
546  return Kiface().KifaceSearch();
547 }
548 
549 
551 {
552  return Kiface().GetHelpFileName();
553 }
554 
555 
556 void EDA_BASE_FRAME::PrintMsg( const wxString& text )
557 {
558  SetStatusText( text );
559 }
560 
561 
562 void EDA_BASE_FRAME::UpdateFileHistory( const wxString& FullFileName,
563  wxFileHistory* aFileHistory )
564 {
565  wxFileHistory* fileHistory = aFileHistory;
566 
567  if( !fileHistory )
568  fileHistory = &Kiface().GetFileHistory();
569 
570  fileHistory->AddFileToHistory( FullFileName );
571 }
572 
573 
574 wxString EDA_BASE_FRAME::GetFileFromHistory( int cmdId, const wxString& type,
575  wxFileHistory* aFileHistory )
576 {
577  wxFileHistory* fileHistory = aFileHistory;
578 
579  if( !fileHistory )
580  fileHistory = &Kiface().GetFileHistory();
581 
582  int baseId = fileHistory->GetBaseId();
583 
584  wxASSERT( cmdId >= baseId && cmdId < baseId + (int) fileHistory->GetCount() );
585 
586  unsigned i = cmdId - baseId;
587 
588  if( i < fileHistory->GetCount() )
589  {
590  wxString fn = fileHistory->GetHistoryFile( i );
591 
592  if( wxFileName::FileExists( fn ) )
593  return fn;
594  else
595  {
596  wxString msg = wxString::Format( _( "File \"%s\" was not found." ), fn );
597  wxMessageBox( msg );
598 
599  fileHistory->RemoveFileFromHistory( i );
600  }
601  }
602 
603  return wxEmptyString;
604 }
605 
606 
607 void EDA_BASE_FRAME::OnKicadAbout( wxCommandEvent& event )
608 {
609  void ShowAboutDialog(EDA_BASE_FRAME * aParent); // See AboutDialog_main.cpp
610  ShowAboutDialog( this );
611 }
612 
613 
614 void EDA_BASE_FRAME::OnPreferences( wxCommandEvent& event )
615 {
616  PAGED_DIALOG dlg( this, _( "Preferences" ) );
617  wxTreebook* book = dlg.GetTreebook();
618 
619  book->AddPage( new PANEL_COMMON_SETTINGS( &dlg, book ), _( "Common" ) );
620 
621  PANEL_HOTKEYS_EDITOR* hotkeysPanel = new PANEL_HOTKEYS_EDITOR( this, book, false );
622  book->AddPage( hotkeysPanel, _( "Hotkeys" ) );
623 
624  for( unsigned i = 0; i < KIWAY_PLAYER_COUNT; ++i )
625  {
626  KIWAY_PLAYER* frame = dlg.Kiway().Player( (FRAME_T) i, false );
627 
628  if( frame )
629  frame->InstallPreferences( &dlg, hotkeysPanel );
630  }
631 
632  // The Kicad manager frame is not a player so we have to add it by hand
633  wxWindow* manager = wxFindWindowByName( KICAD_MANAGER_FRAME_NAME );
634 
635  if( manager )
636  static_cast<EDA_BASE_FRAME*>( manager )->InstallPreferences( &dlg, hotkeysPanel );
637 
638  if( dlg.ShowModal() == wxID_OK )
639  dlg.Kiway().CommonSettingsChanged( false );
640 }
641 
642 
643 bool EDA_BASE_FRAME::IsWritable( const wxFileName& aFileName )
644 {
645  wxString msg;
646  wxFileName fn = aFileName;
647 
648  // Check for absence of a file path with a file name. Unfortunately KiCad
649  // uses paths relative to the current project path without the ./ part which
650  // confuses wxFileName. Making the file name path absolute may be less than
651  // elegant but it solves the problem.
652  if( fn.GetPath().IsEmpty() && fn.HasName() )
653  fn.MakeAbsolute();
654 
655  wxCHECK_MSG( fn.IsOk(), false,
656  wxT( "File name object is invalid. Bad programmer!" ) );
657  wxCHECK_MSG( !fn.GetPath().IsEmpty(), false,
658  wxT( "File name object path <" ) + fn.GetFullPath() +
659  wxT( "> is not set. Bad programmer!" ) );
660 
661  if( fn.IsDir() && !fn.IsDirWritable() )
662  {
663  msg.Printf( _( "You do not have write permissions to folder \"%s\"." ),
664  GetChars( fn.GetPath() ) );
665  }
666  else if( !fn.FileExists() && !fn.IsDirWritable() )
667  {
668  msg.Printf( _( "You do not have write permissions to save file \"%s\" to folder \"%s\"." ),
669  GetChars( fn.GetFullName() ), GetChars( fn.GetPath() ) );
670  }
671  else if( fn.FileExists() && !fn.IsFileWritable() )
672  {
673  msg.Printf( _( "You do not have write permissions to save file \"%s\"." ),
674  GetChars( fn.GetFullPath() ) );
675  }
676 
677  if( !msg.IsEmpty() )
678  {
679  wxMessageBox( msg );
680  return false;
681  }
682 
683  return true;
684 }
685 
686 
687 void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
688 {
689  wxCHECK_RET( aFileName.IsOk(), wxT( "Invalid file name!" ) );
690 
691  wxFileName autoSaveFileName = aFileName;
692 
693  // Check for auto save file.
694  autoSaveFileName.SetName( GetAutoSaveFilePrefix() + aFileName.GetName() );
695 
696  wxLogTrace( traceAutoSave,
697  wxT( "Checking for auto save file " ) + autoSaveFileName.GetFullPath() );
698 
699  if( !autoSaveFileName.FileExists() )
700  return;
701 
702  wxString msg = wxString::Format( _(
703  "Well this is potentially embarrassing!\n"
704  "It appears that the last time you were editing the file\n"
705  "\"%s\"\n"
706  "it was not saved properly. Do you wish to restore the last saved edits you made?" ),
707  GetChars( aFileName.GetFullName() )
708  );
709 
710  int response = wxMessageBox( msg, Pgm().App().GetAppName(), wxYES_NO | wxICON_QUESTION, this );
711 
712  // Make a backup of the current file, delete the file, and rename the auto save file to
713  // the file name.
714  if( response == wxYES )
715  {
716  // Get the backup file name.
717  wxFileName backupFileName = aFileName;
718  backupFileName.SetExt( aFileName.GetExt() + GetBackupSuffix() );
719 
720  // If an old backup file exists, delete it. If an old copy of the file exists, rename
721  // it to the backup file name
722  if( aFileName.FileExists() )
723  {
724  // Rename the old file to the backup file name.
725  if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath(), true ) )
726  {
727  msg.Printf( _( "Could not create backup file \"%s\"" ),
728  GetChars( backupFileName.GetFullPath() ) );
729  wxMessageBox( msg );
730  }
731  }
732 
733  if( !wxRenameFile( autoSaveFileName.GetFullPath(), aFileName.GetFullPath() ) )
734  {
735  wxMessageBox( _( "The auto save file could not be renamed to the board file name." ),
736  Pgm().App().GetAppName(), wxOK | wxICON_EXCLAMATION, this );
737  }
738  }
739  else
740  {
741  wxLogTrace( traceAutoSave,
742  wxT( "Removing auto save file " ) + autoSaveFileName.GetFullPath() );
743 
744  // Remove the auto save file when using the previous file as is.
745  wxRemoveFile( autoSaveFileName.GetFullPath() );
746  }
747 }
748 
749 
#define IMMEDIATE_ACTIONS_KEY
Definition: pgm_base.h:59
static TOOL_ACTION listHotKeys
Definition: actions.h:166
void PrintMsg(const wxString &text)
Class 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.
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
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.
#define PREFER_SELECT_TO_DRAG_KEY
Definition: pgm_base.h:60
wxString m_mruPath
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.
std::unique_ptr< wxConfigBase > GetNewConfig(const wxString &aProgName)
Create a new wxConfig so we can put configuration files in a more proper place for each platform.
Definition: common.cpp:256
Class ACTION_MENU.
Definition: action_menu.h:43
VTBL_ENTRY void CommonSettingsChanged(bool aEnvVarsChanged)
Function CommonSettingsChanged Calls CommonSettingsChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:451
This file is part of the common library.
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 void SaveSettings(wxConfigBase *aCfg)
Saves common frame parameters to a configuration data file.
virtual bool isAutoSaveRequired() const
Return the auto save status of the application.
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
void onAutoSaveTimer(wxTimerEvent &aEvent)
Handle the auto save timer event.
wxString ConfigBaseName()
virtual wxConfigBase * config()
Returns the wxConfigBase used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
void UpdateHotKeys(bool aFullUpdate)
Function UpdateHotKeys() Optionally reads the hotkey config files and then rebuilds the internal hotk...
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:83
wxAuiManager m_auimgr
bool IsWritable(const wxFileName &aFileName)
Checks if aFileName can be written.
wxTimer * m_autoSaveTimer
void UpdateFileHistory(const wxString &FullFileName, wxFileHistory *aFileHistory=NULL)
Update the list of recently opened files.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:51
static const wxString entryPosX
X position of frame, in pixels (suffix)
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
wxTreebook * GetTreebook()
Definition: paged_dialog.h:43
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_i.h:127
wxWindow * findQuasiModalDialog()
const wxString & GetHelpFileName() const
Function GetHelpFileName returns just the basename portion of the current help file.
Definition: kiface_i.h:121
void ShowAboutDialog(EDA_BASE_FRAME *aParent)
virtual void OnCharHook(wxKeyEvent &event)
Capture the key event before it is sent to the GUI.
void CheckForAutoSaveFile(const wxFileName &aFileName)
Check if an auto save file exists for aFileName and takes the appropriate action depending on the use...
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:321
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)
virtual void LoadSettings(wxConfigBase *aCfg)
Load common frame parameters from a configuration file.
Class TOOL_EVENT.
Definition: tool_event.h:168
void AddStandardHelpMenu(wxMenuBar *aMenuBar)
Adds the standard KiCad help menu to the menubar.
static const wxString entryPerspective
Configuration file entry for wxAuiManger perspective.
bool IsQuasiModal()
Definition: dialog_shim.h:127
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
wxLogTrace helper definitions.
#define DEFAULT_AUTO_SAVE_INTERVAL
The default auto save interval is 10 minutes.
void OnKicadAbout(wxCommandEvent &event)
Class COMMON_CONTROL.
void OnMenuOpen(wxMenuEvent &event)
Workaround some issues in wxWidgets where the menu events aren't captured by the menus themselves.
#define _(s)
VTBL_ENTRY wxConfigBase * CommonSettings() const
Definition: pgm_base.h:204
void OnMenuEvent(wxMenuEvent &aEvent)
virtual void CommonSettingsChanged(bool aEnvVarsChanged)
Notification event that some of the common (suite-wide) settings have changed.
static wxString GetAutoSaveFilePrefix()
void SetAutoSaveInterval(int aInterval)
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
static const wxString entrySizeX
Width of frame, in pixels (suffix)
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:154
#define KICAD_MANAGER_FRAME_NAME
FILE_HISTORY & GetFileHistory()
Definition: kiface_i.h:123
The base frame for deriving all KiCad main window classes.
Class TOOL_ACTION.
Definition: tool_action.h:46
size_t i
Definition: json11.cpp:597
static const wxString entrySizeY
Height of frame, in pixels (suffix)
wxString GetFileFromHistory(int cmdId, const wxString &type, wxFileHistory *aFileHistory=NULL)
Fetches the file name from the file history list.
static const wxString entryPosY
Y position of frame, in pixels (suffix)
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:165
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
virtual wxString help_name()
static const wxString entryAutoSaveInterval
Configuration file entry name for auto save interval.
virtual void PopTool(const std::string &actionName)
void OnPreferences(wxCommandEvent &event)
static const wxString entryMaximized
Nonzero iff frame is maximized (suffix)
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 wxString entryMruPath
Configuration file entry for most recently used path.
static TOOL_ACTION getInvolved
Definition: actions.h:167
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:228
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:164
bool ProcessEvent(wxEvent &aEvent) override
Override the default process event handler to implement the auto save feature.
static TOOL_ACTION selectionTool
Definition: actions.h:142
void SetHasPosition(bool aHasPosition)
Definition: tool_event.h:258
const std::string & GetName() const
Function GetName() Returns name of the action.
Definition: tool_action.h:78
#define WARP_MOUSE_ON_MOVE_KEY
Definition: pgm_base.h:58
virtual void ReCreateMenuBar()
Recreates the menu bar.