From 9f128f942cffe0a230cb91de02fa64c8907496e1 Mon Sep 17 00:00:00 2001 From: Mark Roszko Date: Tue, 1 Sep 2020 10:14:51 +0000 Subject: [PATCH] Move shutdown blocker to kiplatform --- common/eda_base_frame.cpp | 41 ++---------------------- cvpcb/cvpcb_mainframe.cpp | 6 ++-- eeschema/files-io.cpp | 3 +- eeschema/libedit/lib_edit_frame.cpp | 7 ++-- eeschema/sch_edit_frame.cpp | 7 ++-- include/eda_base_frame.h | 18 ----------- libs/kiplatform/gtk/app.cpp | 16 +++++++++ libs/kiplatform/include/kiplatform/app.h | 40 ++++++++++++++++++----- libs/kiplatform/msw/app.cpp | 27 ++++++++++++++++ libs/kiplatform/osx/app.mm | 16 +++++++++ pagelayout_editor/pl_editor_frame.cpp | 7 ++-- pcbnew/footprint_edit_frame.cpp | 5 +-- pcbnew/pcb_edit_frame.cpp | 9 +++--- 13 files changed, 119 insertions(+), 83 deletions(-) diff --git a/common/eda_base_frame.cpp b/common/eda_base_frame.cpp index dc600d61b6..fa510356bc 100644 --- a/common/eda_base_frame.cpp +++ b/common/eda_base_frame.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include @@ -186,45 +187,7 @@ EDA_BASE_FRAME::~EDA_BASE_FRAME() ClearUndoRedoList(); - if( SupportsShutdownBlockReason() ) - RemoveShutdownBlockReason(); -} - - -bool EDA_BASE_FRAME::SupportsShutdownBlockReason() -{ -#if defined( _WIN32 ) - return true; -#else - return false; -#endif -} - - -void EDA_BASE_FRAME::RemoveShutdownBlockReason() -{ -#if defined( _WIN32 ) - // Windows: Destroys any block reason that may have existed - ShutdownBlockReasonDestroy( GetHandle() ); -#endif -} - - -void EDA_BASE_FRAME::SetShutdownBlockReason( const wxString& aReason ) -{ -#if defined( _WIN32 ) - // Windows: sets up the pretty message on the shutdown page on why it's being "blocked" - // This is used in conjunction with handling WM_QUERYENDSESSION (wxCloseEvent) - // ShutdownBlockReasonCreate does not block by itself - - ShutdownBlockReasonDestroy( GetHandle() ); // Destroys any existing or nonexisting reason - - if( !ShutdownBlockReasonCreate( GetHandle(), aReason.wc_str() ) ) - { - // Nothing bad happens if this fails, at worst it uses a generic application is - // preventing shutdown message - } -#endif + KIPLATFORM::APP::RemoveShutdownBlockReason( this ); } diff --git a/cvpcb/cvpcb_mainframe.cpp b/cvpcb/cvpcb_mainframe.cpp index 8f865263af..afa2ddde37 100644 --- a/cvpcb/cvpcb_mainframe.cpp +++ b/cvpcb/cvpcb_mainframe.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -167,7 +168,7 @@ CVPCB_MAINFRAME::CVPCB_MAINFRAME( KIWAY* aKiway, wxWindow* aParent ) : // Start the main processing loop m_toolManager->InvokeTool( "cvpcb.Control" ); - SetShutdownBlockReason( _( "Symbol to footprint changes are unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Symbol to footprint changes are unsaved" ) ); } @@ -340,7 +341,8 @@ bool CVPCB_MAINFRAME::canCloseWindow( wxCloseEvent& aEvent ) if( m_modified ) { // Shutdown blocks must be determined and vetoed as early as possible - if( SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION ) + if( KIPLATFORM::APP::SupportsShutdownBlockReason() + && aEvent.GetId() == wxEVT_QUERY_END_SESSION ) { return false; } diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index f6afca69d8..d5c7c59054 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -333,7 +333,8 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector& aFileSet, in rfn.MakeRelativeTo( Prj().GetProjectPath() ); LoadWindowState( rfn.GetFullPath() ); - SetShutdownBlockReason( _( "Schematic file changes are unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Schematic file changes are unsaved" ) ); + if( Kiface().IsSingle() ) { KIPLATFORM::APP::RegisterApplicationRestart( fullFileName ); diff --git a/eeschema/libedit/lib_edit_frame.cpp b/eeschema/libedit/lib_edit_frame.cpp index 89ad10ce13..3cd861ac13 100644 --- a/eeschema/libedit/lib_edit_frame.cpp +++ b/eeschema/libedit/lib_edit_frame.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -186,7 +187,7 @@ LIB_EDIT_FRAME::LIB_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) : m_toolManager->RunAction( ACTIONS::zoomFitScreen, true ); - SetShutdownBlockReason( _( "Library changes are unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Library changes are unsaved" ) ); // Ensure the window is on top Raise(); @@ -433,8 +434,8 @@ void LIB_EDIT_FRAME::setupUIConditions() bool LIB_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent ) { // Shutdown blocks must be determined and vetoed as early as possible - if( SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION - && IsContentModified() ) + if( KIPLATFORM::APP::SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION + && IsContentModified() ) { return false; } diff --git a/eeschema/sch_edit_frame.cpp b/eeschema/sch_edit_frame.cpp index 0cfa59c5fd..f0b16876a1 100644 --- a/eeschema/sch_edit_frame.cpp +++ b/eeschema/sch_edit_frame.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -291,7 +292,7 @@ SCH_EDIT_FRAME::SCH_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ): UpdateTitle(); // Default shutdown reason until a file is loaded - SetShutdownBlockReason( _( "New schematic file is unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "New schematic file is unsaved" ) ); // Ensure the window is on top Raise(); @@ -573,8 +574,8 @@ void SCH_EDIT_FRAME::HardRedraw() bool SCH_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent ) { // Shutdown blocks must be determined and vetoed as early as possible - if( SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION - && Schematic().GetSheets().IsModified() ) + if( KIPLATFORM::APP::SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION + && Schematic().GetSheets().IsModified() ) { return false; } diff --git a/include/eda_base_frame.h b/include/eda_base_frame.h index 9da058c522..8d23b162b3 100644 --- a/include/eda_base_frame.h +++ b/include/eda_base_frame.h @@ -564,24 +564,6 @@ public: const wxString& GetAboutTitle() const { return m_AboutTitle; } - /** - * Sets the block reason why the window/application is preventing OS shutdown. - * This should be set far ahead of any close event. - * - * This is mainly intended for Windows platforms where this is a native feature. - */ - void SetShutdownBlockReason( const wxString& reason ); - - /** - * Removes any shutdown block reason set - */ - void RemoveShutdownBlockReason(); - - /** - * Whether or not the window supports setting a shutdown block reason - */ - bool SupportsShutdownBlockReason(); - /** * Get if the contents of the frame have been modified since the last save. * diff --git a/libs/kiplatform/gtk/app.cpp b/libs/kiplatform/gtk/app.cpp index bf071c00cf..87240c9af2 100644 --- a/libs/kiplatform/gtk/app.cpp +++ b/libs/kiplatform/gtk/app.cpp @@ -34,4 +34,20 @@ bool KIPLATFORM::APP::UnregisterApplicationRestart() { // Not implemented on this platform return true; +} + + +bool KIPLATFORM::APP::SupportsShutdownBlockReason() +{ + return false; +} + + +void KIPLATFORM::APP::RemoveShutdownBlockReason( wxWindow* aWindow ) +{ +} + + +void KIPLATFORM::APP::SetShutdownBlockReason( wxWindow* aWindow, const wxString& aReason ) +{ } \ No newline at end of file diff --git a/libs/kiplatform/include/kiplatform/app.h b/libs/kiplatform/include/kiplatform/app.h index 73743a8d37..ac66011f63 100644 --- a/libs/kiplatform/include/kiplatform/app.h +++ b/libs/kiplatform/include/kiplatform/app.h @@ -22,24 +22,48 @@ #define KIPLATFORM_APP_H_ class wxString; +class wxWindow; namespace KIPLATFORM { namespace APP { /** - * Registers the application for restart with the OS with the given command line string to pass as args - * - * @param aCommandLine is string the OS will invoke the application with - */ + * Registers the application for restart with the OS with the given command line string to pass as args + * + * @param aCommandLine is string the OS will invoke the application with + */ bool RegisterApplicationRestart( const wxString& aCommandLine ); /** - * Unregisters the application from automatic restart - * - * Depending on OS, this may not be required - */ + * Unregisters the application from automatic restart + * + * Depending on OS, this may not be required + */ bool UnregisterApplicationRestart(); + + /** + * Whether or not the window supports setting a shutdown block reason + */ + bool SupportsShutdownBlockReason(); + + /** + * Sets the block reason why the window/application is preventing OS shutdown. + * This should be set far ahead of any close event. + * + * This is mainly intended for Windows platforms where this is a native feature. + * + * @param aWindow that will have a shutdown blocker message + * @param aReason to display why the shutdown block is occuring + */ + void SetShutdownBlockReason( wxWindow* aWindow, const wxString& aReason ); + + /** + * Removes any shutdown block reason set + * + * @param aWindow that has a shutdown block reason set + */ + void RemoveShutdownBlockReason( wxWindow* aWindow ); } } diff --git a/libs/kiplatform/msw/app.cpp b/libs/kiplatform/msw/app.cpp index a4b3b4d408..ae6976a362 100644 --- a/libs/kiplatform/msw/app.cpp +++ b/libs/kiplatform/msw/app.cpp @@ -20,7 +20,9 @@ #include +#include #include +#include #include #include @@ -44,4 +46,29 @@ bool KIPLATFORM::APP::UnregisterApplicationRestart() { // Note, this isn't required to be used on Windows if you are just closing the program return SUCCEEDED( ::UnregisterApplicationRestart() ); +} + + +bool KIPLATFORM::APP::SupportsShutdownBlockReason() +{ + return true; +} + + +void KIPLATFORM::APP::RemoveShutdownBlockReason( wxWindow* aWindow ) +{ + // Destroys any block reason that may have existed + ShutdownBlockReasonDestroy( aWindow->GetHandle() ); +} + + +void KIPLATFORM::APP::SetShutdownBlockReason( wxWindow* aWindow, const wxString& aReason ) +{ + // Sets up the pretty message on the shutdown page on why it's being "blocked" + // This is used in conjunction with handling WM_QUERYENDSESSION (wxCloseEvent) + // ShutdownBlockReasonCreate does not block by itself + + ShutdownBlockReasonDestroy( aWindow->GetHandle() ); // Destroys any existing or nonexisting reason + + ShutdownBlockReasonCreate( aWindow->GetHandle(), aReason.wc_str() ); } \ No newline at end of file diff --git a/libs/kiplatform/osx/app.mm b/libs/kiplatform/osx/app.mm index bf071c00cf..87240c9af2 100644 --- a/libs/kiplatform/osx/app.mm +++ b/libs/kiplatform/osx/app.mm @@ -34,4 +34,20 @@ bool KIPLATFORM::APP::UnregisterApplicationRestart() { // Not implemented on this platform return true; +} + + +bool KIPLATFORM::APP::SupportsShutdownBlockReason() +{ + return false; +} + + +void KIPLATFORM::APP::RemoveShutdownBlockReason( wxWindow* aWindow ) +{ +} + + +void KIPLATFORM::APP::SetShutdownBlockReason( wxWindow* aWindow, const wxString& aReason ) +{ } \ No newline at end of file diff --git a/pagelayout_editor/pl_editor_frame.cpp b/pagelayout_editor/pl_editor_frame.cpp index ef41f5451d..ce1f2dce58 100644 --- a/pagelayout_editor/pl_editor_frame.cpp +++ b/pagelayout_editor/pl_editor_frame.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -326,7 +327,7 @@ void PL_EDITOR_FRAME::OnExit( wxCommandEvent& aEvent ) bool PL_EDITOR_FRAME::canCloseWindow( wxCloseEvent& aEvent ) { // Shutdown blocks must be determined and vetoed as early as possible - if( SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION + if( KIPLATFORM::APP::SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION && IsContentModified() ) { return false; @@ -882,11 +883,11 @@ void PL_EDITOR_FRAME::OnNewPageLayout() if( GetCurrentFileName().IsEmpty() ) { // Default shutdown reason until a file is loaded - SetShutdownBlockReason( _( "New page layout file is unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "New page layout file is unsaved" ) ); } else { - SetShutdownBlockReason( _( "Page layout changes are unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Page layout changes are unsaved" ) ); } } diff --git a/pcbnew/footprint_edit_frame.cpp b/pcbnew/footprint_edit_frame.cpp index 47d3f4432f..19e06ee40c 100644 --- a/pcbnew/footprint_edit_frame.cpp +++ b/pcbnew/footprint_edit_frame.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -246,7 +247,7 @@ FOOTPRINT_EDIT_FRAME::FOOTPRINT_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent, InitExitKey(); // Default shutdown reason until a file is loaded - SetShutdownBlockReason( _( "Footprint changes are unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "Footprint changes are unsaved" ) ); // Ensure the window is on top Raise(); @@ -540,7 +541,7 @@ bool FOOTPRINT_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent ) if( IsContentModified() ) { // Shutdown blocks must be determined and vetoed as early as possible - if( SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION ) + if( KIPLATFORM::APP::SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION ) { aEvent.Veto(); return false; diff --git a/pcbnew/pcb_edit_frame.cpp b/pcbnew/pcb_edit_frame.cpp index 365827484f..aba100ed7f 100644 --- a/pcbnew/pcb_edit_frame.cpp +++ b/pcbnew/pcb_edit_frame.cpp @@ -92,6 +92,7 @@ #include #include #include +#include #include @@ -360,7 +361,7 @@ PCB_EDIT_FRAME::PCB_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) : ActivateGalCanvas(); // Default shutdown reason until a file is loaded - SetShutdownBlockReason( _( "New PCB file is unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "New PCB file is unsaved" ) ); // disable Export STEP item if kicad2step does not exist wxString strK2S = Pgm().GetExecutablePath(); @@ -796,8 +797,8 @@ void PCB_EDIT_FRAME::ResolveDRCExclusions() bool PCB_EDIT_FRAME::canCloseWindow( wxCloseEvent& aEvent ) { // Shutdown blocks must be determined and vetoed as early as possible - if( SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION - && IsContentModified() ) + if( KIPLATFORM::APP::SupportsShutdownBlockReason() && aEvent.GetId() == wxEVT_QUERY_END_SESSION + && IsContentModified() ) { return false; } @@ -1045,7 +1046,7 @@ void PCB_EDIT_FRAME::onBoardLoaded() m_toolManager->GetTool()->LoadRules(); - SetShutdownBlockReason( _( "PCB file changes are unsaved" ) ); + KIPLATFORM::APP::SetShutdownBlockReason( this, _( "PCB file changes are unsaved" ) ); }