From: Vadim Zeitlin Date: Thu, 12 Jan 2006 16:56:48 +0000 (+0000) Subject: share wxEventLoop::IsRunning() implementation between all ports; moved wxEventLoopAct... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/77fb1a02aac56528bbf3229af32634cc02795906 share wxEventLoop::IsRunning() implementation between all ports; moved wxEventLoopActivator used by it in wx/evtloop.h instead of duplicating it in 3 different .cpp files (and not using it at all in 3 other ones) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@36842 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/evtloop.h b/include/wx/evtloop.h index 2612ac024c..76319e3f1e 100644 --- a/include/wx/evtloop.h +++ b/include/wx/evtloop.h @@ -41,15 +41,18 @@ public: // dispatch a single event, return false if we should exit from the loop virtual bool Dispatch() = 0; - // is the event loop running now? - virtual bool IsRunning() const = 0; - // return currently active (running) event loop, may be NULL static wxEventLoop *GetActive() { return ms_activeLoop; } // set currently active (running) event loop static void SetActive(wxEventLoop* loop) { ms_activeLoop = loop; } + // is this event loop running now? + // + // notice that even if this event loop hasn't terminated yet but has just + // spawned a nested (e.g. modal) event loop, this would return false + bool IsRunning() const; + protected: // this function should be called before the event loop terminates, whether // this happens normally (because of Exit() call) or abnormally (because of @@ -86,7 +89,6 @@ public: virtual void Exit(int rc = 0); virtual bool Pending() const; virtual bool Dispatch(); - virtual bool IsRunning() const { return GetActive() == this; } protected: // the pointer to the port specific implementation class @@ -97,6 +99,8 @@ protected: #endif // __WXMSW__/!__WXMSW__ +inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; } + // ---------------------------------------------------------------------------- // wxModalEventLoop // ---------------------------------------------------------------------------- @@ -126,4 +130,30 @@ private: wxWindowDisabler *m_windowDisabler; }; +// ---------------------------------------------------------------------------- +// wxEventLoopActivator: helper class for wxEventLoop implementations +// ---------------------------------------------------------------------------- + +// this object sets the wxEventLoop given to the ctor as the currently active +// one and unsets it in its dtor, this is especially useful in presence of +// exceptions but is more tidy even when we don't use them +class wxEventLoopActivator +{ +public: + wxEventLoopActivator(wxEventLoop *evtLoop) + { + m_evtLoopOld = wxEventLoop::GetActive(); + wxEventLoop::SetActive(evtLoop); + } + + ~wxEventLoopActivator() + { + // restore the previously active event loop + wxEventLoop::SetActive(m_evtLoopOld); + } + +private: + wxEventLoop *m_evtLoopOld; +}; + #endif // _WX_EVTLOOP_H_ diff --git a/include/wx/msw/evtloop.h b/include/wx/msw/evtloop.h index a5c7720028..fe75758c14 100644 --- a/include/wx/msw/evtloop.h +++ b/include/wx/msw/evtloop.h @@ -26,7 +26,6 @@ public: virtual void Exit(int rc = 0); virtual bool Pending() const; virtual bool Dispatch(); - virtual bool IsRunning() const; // MSW-specific methods // -------------------- diff --git a/src/cocoa/evtloop.mm b/src/cocoa/evtloop.mm index 7e122cb12a..4a3a94be67 100644 --- a/src/cocoa/evtloop.mm +++ b/src/cocoa/evtloop.mm @@ -60,8 +60,7 @@ int wxEventLoop::Run() // event loops are not recursive, you need to create another loop! wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); - wxEventLoop *oldLoop = ms_activeLoop; - ms_activeLoop = this; + wxEventLoopActivator activate(this); m_impl = new wxEventLoopImpl; @@ -71,8 +70,6 @@ int wxEventLoop::Run() delete m_impl; m_impl = NULL; - ms_activeLoop = oldLoop; - return exitcode; } diff --git a/src/gtk/evtloop.cpp b/src/gtk/evtloop.cpp index 7fbde67a58..7d9af25874 100644 --- a/src/gtk/evtloop.cpp +++ b/src/gtk/evtloop.cpp @@ -68,8 +68,7 @@ int wxEventLoop::Run() // event loops are not recursive, you need to create another loop! wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); - wxEventLoop *oldLoop = ms_activeLoop; - ms_activeLoop = this; + wxEventLoopActivator activate(this); m_impl = new wxEventLoopImpl; @@ -79,8 +78,6 @@ int wxEventLoop::Run() delete m_impl; m_impl = NULL; - ms_activeLoop = oldLoop; - return exitcode; } diff --git a/src/gtk1/evtloop.cpp b/src/gtk1/evtloop.cpp index 7fbde67a58..7d9af25874 100644 --- a/src/gtk1/evtloop.cpp +++ b/src/gtk1/evtloop.cpp @@ -68,8 +68,7 @@ int wxEventLoop::Run() // event loops are not recursive, you need to create another loop! wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); - wxEventLoop *oldLoop = ms_activeLoop; - ms_activeLoop = this; + wxEventLoopActivator activate(this); m_impl = new wxEventLoopImpl; @@ -79,8 +78,6 @@ int wxEventLoop::Run() delete m_impl; m_impl = NULL; - ms_activeLoop = oldLoop; - return exitcode; } diff --git a/src/mgl/evtloop.cpp b/src/mgl/evtloop.cpp index 688430aee1..10f6b5d12e 100644 --- a/src/mgl/evtloop.cpp +++ b/src/mgl/evtloop.cpp @@ -118,8 +118,7 @@ int wxEventLoop::Run() m_impl = new wxEventLoopImpl; - wxEventLoop *oldLoop = ms_activeLoop; - ms_activeLoop = this; + wxEventLoopActivator activate(this); for ( ;; ) { @@ -144,8 +143,6 @@ int wxEventLoop::Run() delete m_impl; m_impl = NULL; - ms_activeLoop = oldLoop; - return exitcode; } diff --git a/src/motif/evtloop.cpp b/src/motif/evtloop.cpp index 205496ca05..c8380862ba 100644 --- a/src/motif/evtloop.cpp +++ b/src/motif/evtloop.cpp @@ -108,8 +108,7 @@ int wxEventLoop::Run() // event loops are not recursive, you need to create another loop! wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); - wxEventLoop *oldLoop = ms_activeLoop; - ms_activeLoop = this; + wxEventLoopActivator activate(this); m_impl = new wxEventLoopImpl; m_impl->SetKeepGoing( true ); @@ -124,8 +123,6 @@ int wxEventLoop::Run() delete m_impl; m_impl = NULL; - ms_activeLoop = oldLoop; - return exitcode; } diff --git a/src/msw/evtloop.cpp b/src/msw/evtloop.cpp index 252a3c8a84..2b6dd09984 100644 --- a/src/msw/evtloop.cpp +++ b/src/msw/evtloop.cpp @@ -48,34 +48,6 @@ WX_DEFINE_LIST(wxMsgList) #endif // wxUSE_THREADS -// ---------------------------------------------------------------------------- -// helper class -// ---------------------------------------------------------------------------- - -// this object sets the wxEventLoop given to the ctor as the currently active -// one and unsets it in its dtor -class wxEventLoopActivator -{ -public: - wxEventLoopActivator(wxEventLoop **pActive, - wxEventLoop *evtLoop) - { - m_pActive = pActive; - m_evtLoopOld = *pActive; - *pActive = evtLoop; - } - - ~wxEventLoopActivator() - { - // restore the previously active event loop - *m_pActive = m_evtLoopOld; - } - -private: - wxEventLoop *m_evtLoopOld; - wxEventLoop **m_pActive; -}; - // ============================================================================ // wxEventLoop implementation // ============================================================================ @@ -223,11 +195,6 @@ bool wxEventLoop::PreProcessMessage(WXMSG *msg) // wxEventLoop running and exiting // ---------------------------------------------------------------------------- -bool wxEventLoop::IsRunning() const -{ - return ms_activeLoop == this; -} - int wxEventLoop::Run() { // event loops are not recursive, you need to create another loop! @@ -236,7 +203,7 @@ int wxEventLoop::Run() // ProcessIdle() and Dispatch() 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(&ms_activeLoop, this); + wxEventLoopActivator activate(this); // we must ensure that OnExit() is called even if an exception is thrown // from inside Dispatch() but we must call it from Exit() in normal diff --git a/src/os2/evtloop.cpp b/src/os2/evtloop.cpp index 6d72bb674c..08b13d53de 100644 --- a/src/os2/evtloop.cpp +++ b/src/os2/evtloop.cpp @@ -83,30 +83,6 @@ private: wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl); -// this object sets the wxEventLoop given to the ctor as the currently active -// one and unsets it in its dtor -class wxEventLoopActivator -{ -public: - wxEventLoopActivator(wxEventLoop **pActive, - wxEventLoop *evtLoop) - { - m_pActive = pActive; - m_evtLoopOld = *pActive; - *pActive = evtLoop; - } - - ~wxEventLoopActivator() - { - // restore the previously active event loop - *m_pActive = m_evtLoopOld; - } - -private: - wxEventLoop *m_evtLoopOld; - wxEventLoop **m_pActive; -}; - // ============================================================================ // wxEventLoopImpl implementation // ============================================================================ @@ -271,7 +247,7 @@ int wxEventLoop::Run() // SendIdleMessage() and Dispatch() 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(&ms_activeLoop, this); + wxEventLoopActivator activate(this); wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl); CallEventLoopMethod callOnExit(this, &wxEventLoop::OnExit); diff --git a/src/palmos/evtloop.cpp b/src/palmos/evtloop.cpp index 3de13673dc..51dce71214 100644 --- a/src/palmos/evtloop.cpp +++ b/src/palmos/evtloop.cpp @@ -51,34 +51,6 @@ #include #include -// ---------------------------------------------------------------------------- -// helper class -// ---------------------------------------------------------------------------- - -// this object sets the wxEventLoop given to the ctor as the currently active -// one and unsets it in its dtor -class wxEventLoopActivator -{ -public: - wxEventLoopActivator(wxEventLoop **pActive, - wxEventLoop *evtLoop) - { - m_pActive = pActive; - m_evtLoopOld = *pActive; - *pActive = evtLoop; - } - - ~wxEventLoopActivator() - { - // restore the previously active event loop - *m_pActive = m_evtLoopOld; - } - -private: - wxEventLoop *m_evtLoopOld; - wxEventLoop **m_pActive; -}; - // ============================================================================ // wxEventLoop implementation // ============================================================================ @@ -122,6 +94,8 @@ int wxEventLoop::Run() status_t error; EventType event; + wxEventLoopActivator activate(this); + do { wxTheApp && wxTheApp->ProcessIdle(); diff --git a/src/x11/evtloop.cpp b/src/x11/evtloop.cpp index 5167c4b918..b44f46e8de 100644 --- a/src/x11/evtloop.cpp +++ b/src/x11/evtloop.cpp @@ -357,8 +357,7 @@ int wxEventLoop::Run() m_impl = new wxEventLoopImpl; - wxEventLoop *oldLoop = ms_activeLoop; - ms_activeLoop = this; + wxEventLoopActivator activate(this); m_impl->m_keepGoing = TRUE; while ( m_impl->m_keepGoing ) @@ -400,8 +399,6 @@ int wxEventLoop::Run() delete m_impl; m_impl = NULL; - ms_activeLoop = oldLoop; - return exitcode; }