From c738d187e9af897f9540b30e7e46f4e496dd5754 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 3 Jul 2013 00:25:23 +0000 Subject: [PATCH] Add wxEventLoopBase::DoRun(). Call it from public Run() after setting the loop as active and resetting m_shouldExit flag. No real changes, just cut down on the code duplication among the ports and make it easier to implement the upcoming changes. see #10258. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74333 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/cocoa/evtloop.h | 3 ++- include/wx/evtloop.h | 19 +++++++++++++------ include/wx/gtk/evtloop.h | 4 +++- include/wx/osx/core/evtloop.h | 8 ++++---- src/cocoa/evtloop.mm | 7 +------ src/common/evtloopcmn.cpp | 28 +++++++++++++++++++--------- src/gtk/evtloop.cpp | 7 +------ src/gtk1/evtloop.cpp | 7 +------ src/motif/evtloop.cpp | 7 +------ src/osx/core/evtloop_cf.cpp | 10 +--------- src/x11/evtloop.cpp | 7 +------ 11 files changed, 47 insertions(+), 60 deletions(-) diff --git a/include/wx/cocoa/evtloop.h b/include/wx/cocoa/evtloop.h index 7e0d476809..0790014c9a 100644 --- a/include/wx/cocoa/evtloop.h +++ b/include/wx/cocoa/evtloop.h @@ -20,7 +20,6 @@ class WXDLLIMPEXP_CORE wxGUIEventLoop : public wxEventLoopBase public: wxGUIEventLoop() { m_exitcode = 0; } - virtual int Run(); virtual void Exit(int rc = 0); virtual bool Pending() const; virtual bool Dispatch(); @@ -29,6 +28,8 @@ public: virtual bool YieldFor(long eventsToProcess); protected: + virtual int DoRun(); + int m_exitcode; wxDECLARE_NO_COPY_CLASS(wxGUIEventLoop); diff --git a/include/wx/evtloop.h b/include/wx/evtloop.h index 24ea711ece..52084d997d 100644 --- a/include/wx/evtloop.h +++ b/include/wx/evtloop.h @@ -88,7 +88,10 @@ public: // ------------------- // start the event loop, return the exit code when it is finished - virtual int Run() = 0; + // + // notice that wx ports should override DoRun(), this method is virtual + // only to allow overriding it in the user code for custom event loops + virtual int Run(); // is this event loop running now? // @@ -169,6 +172,9 @@ public: protected: + // real implementation of Run() + virtual int DoRun() = 0; + // this function should be called before the event loop terminates, whether // this happens normally (because of Exit() call) or abnormally (because of // an exception thrown from inside the loop) @@ -198,15 +204,15 @@ class WXDLLIMPEXP_BASE wxEventLoopManual : public wxEventLoopBase public: wxEventLoopManual(); - // enters a loop calling OnNextIteration(), Pending() and Dispatch() and - // terminating when Exit() is called - virtual int Run(); - // sets the "should exit" flag and wakes up the loop so that it terminates // soon virtual void Exit(int rc = 0); protected: + // enters a loop calling OnNextIteration(), Pending() and Dispatch() and + // terminating when Exit() is called + virtual int DoRun(); + // may be overridden to perform some action at the start of each new event // loop iteration virtual void OnNextIteration() { } @@ -285,7 +291,6 @@ public: } #endif // wxUSE_EVENTLOOP_SOURCE - virtual int Run(); virtual void Exit(int rc = 0); virtual bool Pending() const; virtual bool Dispatch(); @@ -307,6 +312,8 @@ public: virtual bool YieldFor(long eventsToProcess); protected: + virtual int DoRun(); + // the pointer to the port specific implementation class wxEventLoopImpl *m_impl; diff --git a/include/wx/gtk/evtloop.h b/include/wx/gtk/evtloop.h index d083567862..d7f5205f37 100644 --- a/include/wx/gtk/evtloop.h +++ b/include/wx/gtk/evtloop.h @@ -22,7 +22,6 @@ class WXDLLIMPEXP_CORE wxGUIEventLoop : public wxEventLoopBase public: wxGUIEventLoop(); - virtual int Run(); virtual void Exit(int rc = 0); virtual bool Pending() const; virtual bool Dispatch(); @@ -38,6 +37,9 @@ public: void StoreGdkEventForLaterProcessing(GdkEvent* ev) { m_arrGdkEvents.Add(ev); } +protected: + virtual int DoRun(); + private: // the exit code of this event loop int m_exitcode; diff --git a/include/wx/osx/core/evtloop.h b/include/wx/osx/core/evtloop.h index 85f2a6c991..02830f8c40 100644 --- a/include/wx/osx/core/evtloop.h +++ b/include/wx/osx/core/evtloop.h @@ -24,10 +24,6 @@ public: wxCFEventLoop(); virtual ~wxCFEventLoop(); - // enters a loop calling OnNextIteration(), Pending() and Dispatch() and - // terminating when Exit() is called - virtual int Run(); - // sets the "should exit" flag and wakes up the loop so that it terminates // soon virtual void Exit(int rc = 0); @@ -63,6 +59,10 @@ public: void SetShouldWaitForEvent(bool should) { m_shouldWaitForEvent = should; } #endif protected: + // enters a loop calling OnNextIteration(), Pending() and Dispatch() and + // terminating when Exit() is called + virtual int DoRun(); + void CommonModeObserverCallBack(CFRunLoopObserverRef observer, int activity); void DefaultModeObserverCallBack(CFRunLoopObserverRef observer, int activity); diff --git a/src/cocoa/evtloop.mm b/src/cocoa/evtloop.mm index a017688edc..c73a2307d1 100644 --- a/src/cocoa/evtloop.mm +++ b/src/cocoa/evtloop.mm @@ -30,13 +30,8 @@ // wxGUIEventLoop running and exiting // ---------------------------------------------------------------------------- -int wxGUIEventLoop::Run() +int wxGUIEventLoop::DoRun() { - // event loops are not recursive, you need to create another loop! - wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); - - wxEventLoopActivator activate(this); - [[NSApplication sharedApplication] run]; OnExit(); diff --git a/src/common/evtloopcmn.cpp b/src/common/evtloopcmn.cpp index a9f7bfccf4..2f922c248d 100644 --- a/src/common/evtloopcmn.cpp +++ b/src/common/evtloopcmn.cpp @@ -52,6 +52,24 @@ void wxEventLoopBase::SetActive(wxEventLoopBase* loop) wxTheApp->OnEventLoopEnter(loop); } +int wxEventLoopBase::Run() +{ + // event loops are not recursive, you need to create another loop! + wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); + + // ProcessIdle() and ProcessEvents() below may throw so the code here should + // be exception-safe, hence we must use local objects for all actions we + // should undo + wxEventLoopActivator activate(this); + + // We might be called again, after a previous call to ScheduleExit(), so + // reset this flag. + m_shouldExit = false; + + // Finally really run the loop. + return DoRun(); +} + void wxEventLoopBase::OnExit() { if (wxTheApp) @@ -118,16 +136,8 @@ bool wxEventLoopManual::ProcessEvents() return Dispatch(); } -int wxEventLoopManual::Run() +int wxEventLoopManual::DoRun() { - // event loops are not recursive, you need to create another loop! - wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); - - // ProcessIdle() and ProcessEvents() below may throw so the code here should - // be exception-safe, hence we must use local objects for all actions we - // should undo - wxEventLoopActivator activate(this); - // we must ensure that OnExit() is called even if an exception is thrown // from inside ProcessEvents() but we must call it from Exit() in normal // situations because it is supposed to be called synchronously, diff --git a/src/gtk/evtloop.cpp b/src/gtk/evtloop.cpp index cc118104d5..68f65545e4 100644 --- a/src/gtk/evtloop.cpp +++ b/src/gtk/evtloop.cpp @@ -50,13 +50,8 @@ wxGUIEventLoop::wxGUIEventLoop() m_exitcode = 0; } -int wxGUIEventLoop::Run() +int wxGUIEventLoop::DoRun() { - // event loops are not recursive, you need to create another loop! - wxCHECK_MSG( !IsRunning(), -1, "can't reenter a message loop" ); - - wxEventLoopActivator activate(this); - gtk_main(); OnExit(); diff --git a/src/gtk1/evtloop.cpp b/src/gtk1/evtloop.cpp index d688c36365..5f4a91f995 100644 --- a/src/gtk1/evtloop.cpp +++ b/src/gtk1/evtloop.cpp @@ -65,13 +65,8 @@ wxGUIEventLoop::~wxGUIEventLoop() wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") ); } -int wxGUIEventLoop::Run() +int wxGUIEventLoop::DoRun() { - // event loops are not recursive, you need to create another loop! - wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); - - wxEventLoopActivator activate(this); - m_impl = new wxEventLoopImpl; gtk_main(); diff --git a/src/motif/evtloop.cpp b/src/motif/evtloop.cpp index f5041784f4..7ec41cd123 100644 --- a/src/motif/evtloop.cpp +++ b/src/motif/evtloop.cpp @@ -102,13 +102,8 @@ wxGUIEventLoop::~wxGUIEventLoop() wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") ); } -int wxGUIEventLoop::Run() +int wxGUIEventLoop::DoRun() { - // event loops are not recursive, you need to create another loop! - wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); - - wxEventLoopActivator activate(this); - m_impl = new wxEventLoopImpl; m_impl->SetKeepGoing( true ); diff --git a/src/osx/core/evtloop_cf.cpp b/src/osx/core/evtloop_cf.cpp index 66178cccb7..5ef10ae432 100644 --- a/src/osx/core/evtloop_cf.cpp +++ b/src/osx/core/evtloop_cf.cpp @@ -392,16 +392,8 @@ void wxCFEventLoop::OSXDoStop() // enters a loop calling OnNextIteration(), Pending() and Dispatch() and // terminating when Exit() is called -int wxCFEventLoop::Run() +int wxCFEventLoop::DoRun() { - // event loops are not recursive, you need to create another loop! - wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); - - // ProcessIdle() and ProcessEvents() below may throw so the code here should - // be exception-safe, hence we must use local objects for all actions we - // should undo - wxEventLoopActivator activate(this); - // we must ensure that OnExit() is called even if an exception is thrown // from inside ProcessEvents() but we must call it from Exit() in normal // situations because it is supposed to be called synchronously, diff --git a/src/x11/evtloop.cpp b/src/x11/evtloop.cpp index 146fabde14..1f988f9762 100644 --- a/src/x11/evtloop.cpp +++ b/src/x11/evtloop.cpp @@ -125,15 +125,10 @@ wxGUIEventLoop::~wxGUIEventLoop() wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") ); } -int wxGUIEventLoop::Run() +int wxGUIEventLoop::DoRun() { - // event loops are not recursive, you need to create another loop! - wxCHECK_MSG( !m_impl, -1, wxT("can't reenter a message loop") ); - m_impl = new wxEventLoopImpl; - wxEventLoopActivator activate(this); - m_impl->m_keepGoing = true; while ( m_impl->m_keepGoing ) { -- 2.45.2