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 // TODO: Implement an RAII mechanism for the stack PushTool/PopTool pairs
165 void EDA_BASE_FRAME::PushTool( const std::string& actionName )
166 {
167  m_toolStack.push_back( actionName );
168 
169  // Human cognitive stacking is very shallow; deeper tool stacks just get annoying
170  if( m_toolStack.size() > 3 )
171  m_toolStack.erase( m_toolStack.begin() );
172 
173  TOOL_ACTION* action = m_toolManager->GetActionManager()->FindAction( actionName );
174 
175  if( action )
176  DisplayToolMsg( action->GetLabel() );
177  else
178  DisplayToolMsg( actionName );
179 }
180 
181 
182 void EDA_BASE_FRAME::PopTool( const std::string& actionName )
183 {
184  // Push/pop events can get out of order (such as when they're generated by the Simulator
185  // frame but not processed until the mouse is back in the Schematic frame), so make sure
186  // we're popping the right stack frame.
187 
188  for( int i = m_toolStack.size() - 1; i >= 0; --i )
189  {
190  if( m_toolStack[ i ] == actionName )
191  {
192  m_toolStack.erase( m_toolStack.begin() + i );
193 
194  // If there's something underneath us, and it's now the top of the stack, then
195  // re-activate it
196  if( ( --i ) >= 0 && i == (int)m_toolStack.size() - 1 )
197  {
198  std::string back = m_toolStack[ i ];
200 
201  if( action )
202  {
203  // Pop the action as running it will push it back onto the stack
204  m_toolStack.pop_back();
205 
206  TOOL_EVENT evt = action->MakeEvent();
207  evt.SetHasPosition( false );
208  GetToolManager()->PostEvent( evt );
209  }
210  }
211  else
213 
214  return;
215  }
216  }
217 }
218 
219 
221 {
222  if( m_toolStack.empty() )
223  return ACTIONS::selectionTool.GetName();
224  else
225  return m_toolStack.back();
226 }
227 
228 
229 bool EDA_BASE_FRAME::IsCurrentTool( const TOOL_ACTION& aAction ) const
230 {
231  if( m_toolStack.empty() )
232  return &aAction == &ACTIONS::selectionTool;
233  else
234  return m_toolStack.back() == aAction.GetName();
235 }
236 
237 
238 bool EDA_BASE_FRAME::ProcessEvent( wxEvent& aEvent )
239 {
240 #ifdef __WXMAC__
241  // Apple in its infinite wisdom will raise a disabled window before even passing
242  // us the event, so we have no way to stop it. Instead, we have to catch an
243  // improperly ordered disabled window and quasi-modal dialog here and reorder
244  // them.
245  if( !IsEnabled() && IsActive() )
246  {
247  wxWindow* dlg = findQuasiModalDialog();
248  if( dlg )
249  dlg->Raise();
250  }
251 #endif
252 
253  if( !wxFrame::ProcessEvent( aEvent ) )
254  return false;
255 
256  if( IsShown() && m_hasAutoSave && IsActive() &&
258  {
259  if( !m_autoSaveState )
260  {
261  wxLogTrace( traceAutoSave, wxT( "Starting auto save timer." ) );
262  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
263  m_autoSaveState = true;
264  }
265  else if( m_autoSaveTimer->IsRunning() )
266  {
267  wxLogTrace( traceAutoSave, wxT( "Stopping auto save timer." ) );
268  m_autoSaveTimer->Stop();
269  m_autoSaveState = false;
270  }
271  }
272 
273  return true;
274 }
275 
276 
278 {
279  m_autoSaveInterval = aInterval;
280 
281  if( m_autoSaveTimer->IsRunning() )
282  {
283  if( m_autoSaveInterval > 0 )
284  {
285  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
286  }
287  else
288  {
289  m_autoSaveTimer->Stop();
290  m_autoSaveState = false;
291  }
292  }
293 }
294 
295 
296 void EDA_BASE_FRAME::onAutoSaveTimer( wxTimerEvent& aEvent )
297 {
298  if( !doAutoSave() )
299  m_autoSaveTimer->Start( m_autoSaveInterval * 1000, wxTIMER_ONE_SHOT );
300 }
301 
302 
304 {
305  wxCHECK_MSG( false, true, wxT( "Auto save timer function not overridden. Bad programmer!" ) );
306 }
307 
308 
309 void EDA_BASE_FRAME::OnCharHook( wxKeyEvent& event )
310 {
311  wxLogTrace( kicadTraceKeyEvent, "EDA_BASE_FRAME::OnCharHook %s", dump( event ) );
312  // Key events can be filtered here.
313  // Currently no filtering is made.
314  event.Skip();
315 }
316 
317 
318 void EDA_BASE_FRAME::OnMenuOpen( wxMenuEvent& event )
319 {
320  //
321  // wxWidgets has several issues that we have to work around:
322  //
323  // 1) wxWidgets 3.0.x Windows has a bug where wxEVT_MENU_OPEN and wxEVT_MENU_HIGHLIGHT
324  // events are not captured by the ACTON_MENU menus. So we forward them here.
325  // (FWIW, this one is fixed in wxWidgets 3.1.x.)
326  //
327  // 2) wxWidgets doesn't pass the menu pointer for wxEVT_MENU_HIGHLIGHT events. So we
328  // store the menu pointer from the wxEVT_MENU_OPEN call.
329  //
330  // 3) wxWidgets has no way to tell whether a command is from a menu selection or a
331  // hotkey. So we keep track of menu highlighting so we can differentiate.
332  //
333 
334  static ACTION_MENU* currentMenu;
335 
336  if( event.GetEventType() == wxEVT_MENU_OPEN )
337  {
338  currentMenu = dynamic_cast<ACTION_MENU*>( event.GetMenu() );
339 
340  if( currentMenu )
341  currentMenu->OnMenuEvent( event );
342  }
343  else if( event.GetEventType() == wxEVT_MENU_HIGHLIGHT )
344  {
345  if( currentMenu )
346  currentMenu->OnMenuEvent( event );
347  }
348  else if( event.GetEventType() == wxEVT_MENU_CLOSE )
349  {
350  if( currentMenu )
351  currentMenu->OnMenuEvent( event );
352 
353  currentMenu = nullptr;
354  }
355 
356  event.Skip();
357 }
358 
359 
361 {
362 }
363 
364 
365 void EDA_BASE_FRAME::AddStandardHelpMenu( wxMenuBar* aMenuBar )
366 {
367  COMMON_CONTROL* commonControl = m_toolManager->GetTool<COMMON_CONTROL>();
368  ACTION_MENU* helpMenu = new ACTION_MENU( false );
369 
370  helpMenu->SetTool( commonControl );
371 
372  helpMenu->Add( ACTIONS::help );
373  helpMenu->Add( ACTIONS::gettingStarted );
374  helpMenu->Add( ACTIONS::listHotKeys );
375  helpMenu->Add( ACTIONS::getInvolved );
376 
377  helpMenu->AppendSeparator();
378  helpMenu->Add( _( "&About KiCad" ), "", wxID_ABOUT, about_xpm );
379 
380  aMenuBar->Append( helpMenu, _( "&Help" ) );
381 }
382 
383 
385 {
386  if( GetMenuBar() )
387  {
388  ReCreateMenuBar();
389  GetMenuBar()->Refresh();
390  }
391 }
392 
393 
394 void EDA_BASE_FRAME::CommonSettingsChanged( bool aEnvVarsChanged )
395 {
396  if( GetToolManager() )
398 
399  if( GetMenuBar() )
400  {
401  // For icons in menus, icon scaling & hotkeys
402  ReCreateMenuBar();
403  GetMenuBar()->Refresh();
404  }
405 
406  wxConfigBase* settings = Pgm().CommonSettings();
407 
408  settings->Read( WARP_MOUSE_ON_MOVE_KEY, &m_moveWarpsCursor );
409  settings->Read( PREFER_SELECT_TO_DRAG_KEY, &m_dragSelects );
410  settings->Read( IMMEDIATE_ACTIONS_KEY, &m_immediateActions );
411 }
412 
413 
414 void EDA_BASE_FRAME::LoadSettings( wxConfigBase* aCfg )
415 {
416  int maximized = 0;
417 
418  wxString baseCfgName = ConfigBaseName();
419 
420  wxString text = baseCfgName + entryPosX;
421  aCfg->Read( text, &m_FramePos.x, m_FramePos.x );
422 
423  text = baseCfgName + entryPosY;
424  aCfg->Read( text, &m_FramePos.y, m_FramePos.y );
425 
426  text = baseCfgName + entrySizeX;
427  aCfg->Read( text, &m_FrameSize.x, m_FrameSize.x );
428 
429  text = baseCfgName + entrySizeY;
430  aCfg->Read( text, &m_FrameSize.y, m_FrameSize.y );
431 
432  text = baseCfgName + entryMaximized;
433  aCfg->Read( text, &maximized, 0 );
434 
435  if( m_hasAutoSave )
436  {
437  text = baseCfgName + entryAutoSaveInterval;
438  aCfg->Read( text, &m_autoSaveInterval, DEFAULT_AUTO_SAVE_INTERVAL );
439  }
440 
441  // Ensure the window is on a connected display, and is visible.
442  // (at least a corner of the frame must be visible on screen)
443  // Sometimes, if a window was moved on an auxiliary display, and when this
444  // display is no more available, it is not the case.
445  wxRect rect( m_FramePos, m_FrameSize );
446 
447  if( wxDisplay::GetFromPoint( rect.GetTopLeft() ) == wxNOT_FOUND &&
448  wxDisplay::GetFromPoint( rect.GetTopRight() ) == wxNOT_FOUND &&
449  wxDisplay::GetFromPoint( rect.GetBottomLeft() ) == wxNOT_FOUND &&
450  wxDisplay::GetFromPoint( rect.GetBottomRight() ) == wxNOT_FOUND )
451  {
452  m_FramePos = wxDefaultPosition;
453  }
454 
455  // Ensure Window title bar is visible
456 #if defined( __WXMAC__ )
457  // for macOSX, the window must be below system (macOSX) toolbar
458  // Ypos_min = GetMBarHeight(); seems no more exist in new API (subject to change)
459  int Ypos_min = 20;
460 #else
461  int Ypos_min = 0;
462 #endif
463  if( m_FramePos.y < Ypos_min )
464  m_FramePos.y = Ypos_min;
465 
466  if( maximized )
467  Maximize();
468 
469  aCfg->Read( baseCfgName + entryPerspective, &m_perspective );
470  aCfg->Read( baseCfgName + entryMruPath, &m_mruPath );
471 
472  wxConfigBase* settings = Pgm().CommonSettings();
473 
474  if( !settings->Read( WARP_MOUSE_ON_MOVE_KEY, &m_moveWarpsCursor ) )
475  {
476  // Legacy versions stored the property only for Eeschema, so see if we have it there
477  std::unique_ptr<wxConfigBase> pcbSettings = GetNewConfig( wxT( "eeschema" ) );
478  pcbSettings->Read( "MoveWarpsCursor", &m_moveWarpsCursor, true );
479  }
480 
481  if( !settings->Read( PREFER_SELECT_TO_DRAG_KEY, &m_dragSelects ) )
482  {
483  // Legacy versions stored the property only for PCBNew, so see if we have it there
484  std::unique_ptr<wxConfigBase> pcbSettings = GetNewConfig( wxT( "pcbnew" ) );
485  pcbSettings->Read( "DragSelects", &m_dragSelects, true );
486  }
487 
488  settings->Read( IMMEDIATE_ACTIONS_KEY, &m_immediateActions, false );
489 }
490 
491 
492 void EDA_BASE_FRAME::SaveSettings( wxConfigBase* aCfg )
493 {
494  wxString text;
495 
496  if( IsIconized() )
497  return;
498 
499  wxString baseCfgName = ConfigBaseName();
500 
501  m_FrameSize = GetSize();
502  m_FramePos = GetPosition();
503 
504  text = baseCfgName + wxT( "Pos_x" );
505  aCfg->Write( text, (long) m_FramePos.x );
506 
507  text = baseCfgName + wxT( "Pos_y" );
508  aCfg->Write( text, (long) m_FramePos.y );
509 
510  text = baseCfgName + wxT( "Size_x" );
511  aCfg->Write( text, (long) m_FrameSize.x );
512 
513  text = baseCfgName + wxT( "Size_y" );
514  aCfg->Write( text, (long) m_FrameSize.y );
515 
516  text = baseCfgName + wxT( "Maximized" );
517  aCfg->Write( text, IsMaximized() );
518 
519  if( m_hasAutoSave )
520  {
521  text = baseCfgName + entryAutoSaveInterval;
522  aCfg->Write( text, m_autoSaveInterval );
523  }
524 
525  // Once this is fully implemented, wxAuiManager will be used to maintain
526  // the persistance of the main frame and all it's managed windows and
527  // all of the legacy frame persistence position code can be removed.
528  wxString perspective = m_auimgr.SavePerspective();
529 
530  // printf( "perspective(%s): %s\n",
531  // TO_UTF8( m_FrameName + entryPerspective ), TO_UTF8( perspective ) );
532  aCfg->Write( baseCfgName + entryPerspective, perspective );
533  aCfg->Write( baseCfgName + entryMruPath, m_mruPath );
534 }
535 
536 
537 wxConfigBase* EDA_BASE_FRAME::config()
538 {
539  // KICAD_MANAGER_FRAME overrides this
540  wxConfigBase* ret = Kiface().KifaceSettings();
541  //wxASSERT( ret );
542  return ret;
543 }
544 
545 
547 {
548  return Kiface().KifaceSearch();
549 }
550 
551 
553 {
554  return Kiface().GetHelpFileName();
555 }
556 
557 
558 void EDA_BASE_FRAME::PrintMsg( const wxString& text )
559 {
560  SetStatusText( text );
561 }
562 
563 
564 void EDA_BASE_FRAME::UpdateFileHistory( const wxString& FullFileName,
565  wxFileHistory* aFileHistory )
566 {
567  wxFileHistory* fileHistory = aFileHistory;
568 
569  if( !fileHistory )
570  fileHistory = &Kiface().GetFileHistory();
571 
572  fileHistory->AddFileToHistory( FullFileName );
573 }
574 
575 
576 wxString EDA_BASE_FRAME::GetFileFromHistory( int cmdId, const wxString& type,
577  wxFileHistory* aFileHistory )
578 {
579  wxFileHistory* fileHistory = aFileHistory;
580 
581  if( !fileHistory )
582  fileHistory = &Kiface().GetFileHistory();
583 
584  int baseId = fileHistory->GetBaseId();
585 
586  wxASSERT( cmdId >= baseId && cmdId < baseId + (int) fileHistory->GetCount() );
587 
588  unsigned i = cmdId - baseId;
589 
590  if( i < fileHistory->GetCount() )
591  {
592  wxString fn = fileHistory->GetHistoryFile( i );
593 
594  if( wxFileName::FileExists( fn ) )
595  return fn;
596  else
597  {
598  wxString msg = wxString::Format( _( "File \"%s\" was not found." ), fn );
599  wxMessageBox( msg );
600 
601  fileHistory->RemoveFileFromHistory( i );
602  }
603  }
604 
605  return wxEmptyString;
606 }
607 
608 
609 void EDA_BASE_FRAME::OnKicadAbout( wxCommandEvent& event )
610 {
611  void ShowAboutDialog(EDA_BASE_FRAME * aParent); // See AboutDialog_main.cpp
612  ShowAboutDialog( this );
613 }
614 
615 
616 void EDA_BASE_FRAME::OnPreferences( wxCommandEvent& event )
617 {
618  PAGED_DIALOG dlg( this, _( "Preferences" ) );
619  wxTreebook* book = dlg.GetTreebook();
620 
621  book->AddPage( new PANEL_COMMON_SETTINGS( &dlg, book ), _( "Common" ) );
622 
623  PANEL_HOTKEYS_EDITOR* hotkeysPanel = new PANEL_HOTKEYS_EDITOR( this, book, false );
624  book->AddPage( hotkeysPanel, _( "Hotkeys" ) );
625 
626  for( unsigned i = 0; i < KIWAY_PLAYER_COUNT; ++i )
627  {
628  KIWAY_PLAYER* frame = dlg.Kiway().Player( (FRAME_T) i, false );
629 
630  if( frame )
631  frame->InstallPreferences( &dlg, hotkeysPanel );
632  }
633 
634  // The Kicad manager frame is not a player so we have to add it by hand
635  wxWindow* manager = wxFindWindowByName( KICAD_MANAGER_FRAME_NAME );
636 
637  if( manager )
638  static_cast<EDA_BASE_FRAME*>( manager )->InstallPreferences( &dlg, hotkeysPanel );
639 
640  if( dlg.ShowModal() == wxID_OK )
641  dlg.Kiway().CommonSettingsChanged( false );
642 }
643 
644 
645 bool EDA_BASE_FRAME::IsWritable( const wxFileName& aFileName )
646 {
647  wxString msg;
648  wxFileName fn = aFileName;
649 
650  // Check for absence of a file path with a file name. Unfortunately KiCad
651  // uses paths relative to the current project path without the ./ part which
652  // confuses wxFileName. Making the file name path absolute may be less than
653  // elegant but it solves the problem.
654  if( fn.GetPath().IsEmpty() && fn.HasName() )
655  fn.MakeAbsolute();
656 
657  wxCHECK_MSG( fn.IsOk(), false,
658  wxT( "File name object is invalid. Bad programmer!" ) );
659  wxCHECK_MSG( !fn.GetPath().IsEmpty(), false,
660  wxT( "File name object path <" ) + fn.GetFullPath() +
661  wxT( "> is not set. Bad programmer!" ) );
662 
663  if( fn.IsDir() && !fn.IsDirWritable() )
664  {
665  msg.Printf( _( "You do not have write permissions to folder \"%s\"." ),
666  GetChars( fn.GetPath() ) );
667  }
668  else if( !fn.FileExists() && !fn.IsDirWritable() )
669  {
670  msg.Printf( _( "You do not have write permissions to save file \"%s\" to folder \"%s\"." ),
671  GetChars( fn.GetFullName() ), GetChars( fn.GetPath() ) );
672  }
673  else if( fn.FileExists() && !fn.IsFileWritable() )
674  {
675  msg.Printf( _( "You do not have write permissions to save file \"%s\"." ),
676  GetChars( fn.GetFullPath() ) );
677  }
678 
679  if( !msg.IsEmpty() )
680  {
681  wxMessageBox( msg );
682  return false;
683  }
684 
685  return true;
686 }
687 
688 
689 void EDA_BASE_FRAME::CheckForAutoSaveFile( const wxFileName& aFileName )
690 {
691  wxCHECK_RET( aFileName.IsOk(), wxT( "Invalid file name!" ) );
692 
693  wxFileName autoSaveFileName = aFileName;
694 
695  // Check for auto save file.
696  autoSaveFileName.SetName( GetAutoSaveFilePrefix() + aFileName.GetName() );
697 
698  wxLogTrace( traceAutoSave,
699  wxT( "Checking for auto save file " ) + autoSaveFileName.GetFullPath() );
700 
701  if( !autoSaveFileName.FileExists() )
702  return;
703 
704  wxString msg = wxString::Format( _(
705  "Well this is potentially embarrassing!\n"
706  "It appears that the last time you were editing the file\n"
707  "\"%s\"\n"
708  "it was not saved properly. Do you wish to restore the last saved edits you made?" ),
709  GetChars( aFileName.GetFullName() )
710  );
711 
712  int response = wxMessageBox( msg, Pgm().App().GetAppName(), wxYES_NO | wxICON_QUESTION, this );
713 
714  // Make a backup of the current file, delete the file, and rename the auto save file to
715  // the file name.
716  if( response == wxYES )
717  {
718  // Get the backup file name.
719  wxFileName backupFileName = aFileName;
720  backupFileName.SetExt( aFileName.GetExt() + GetBackupSuffix() );
721 
722  // If an old backup file exists, delete it. If an old copy of the file exists, rename
723  // it to the backup file name
724  if( aFileName.FileExists() )
725  {
726  // Rename the old file to the backup file name.
727  if( !wxRenameFile( aFileName.GetFullPath(), backupFileName.GetFullPath(), true ) )
728  {
729  msg.Printf( _( "Could not create backup file \"%s\"" ),
730  GetChars( backupFileName.GetFullPath() ) );
731  wxMessageBox( msg );
732  }
733  }
734 
735  if( !wxRenameFile( autoSaveFileName.GetFullPath(), aFileName.GetFullPath() ) )
736  {
737  wxMessageBox( _( "The auto save file could not be renamed to the board file name." ),
738  Pgm().App().GetAppName(), wxOK | wxICON_EXCLAMATION, this );
739  }
740  }
741  else
742  {
743  wxLogTrace( traceAutoSave,
744  wxT( "Removing auto save file " ) + autoSaveFileName.GetFullPath() );
745 
746  // Remove the auto save file when using the previous file as is.
747  wxRemoveFile( autoSaveFileName.GetFullPath() );
748  }
749 }
750 
751 
#define IMMEDIATE_ACTIONS_KEY
Definition: pgm_base.h:59
static TOOL_ACTION listHotKeys
Definition: actions.h:167
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:458
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:328
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:171
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:274
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:163
#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:649
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:166
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:168
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:237
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:165
bool ProcessEvent(wxEvent &aEvent) override
Override the default process event handler to implement the auto save feature.
static TOOL_ACTION selectionTool
Definition: actions.h:143
void SetHasPosition(bool aHasPosition)
Definition: tool_event.h:261
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.