1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/evtloopcmn.cpp
3 // Purpose: common wxEventLoop-related stuff
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/evtloop.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 wxEventLoopBase
*wxEventLoopBase::ms_activeLoop
= NULL
;
31 wxEventLoopBase::wxEventLoopBase()
33 m_isInsideYield
= false;
34 m_eventsToProcessInsideYield
= wxEVT_CATEGORY_ALL
;
37 bool wxEventLoopBase::IsMain() const
40 return wxTheApp
->GetMainLoop() == this;
45 void wxEventLoopBase::SetActive(wxEventLoopBase
* loop
)
50 wxTheApp
->OnEventLoopEnter(loop
);
53 void wxEventLoopBase::OnExit()
56 wxTheApp
->OnEventLoopExit(this);
59 void wxEventLoopBase::WakeUpIdle()
64 bool wxEventLoopBase::ProcessIdle()
66 return wxTheApp
&& wxTheApp
->ProcessIdle();
69 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
71 if ( m_isInsideYield
)
75 wxFAIL_MSG( wxT("wxYield called recursively" ) );
81 return YieldFor(wxEVT_CATEGORY_ALL
);
84 // wxEventLoopManual is unused in the other ports
85 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
87 // ============================================================================
88 // wxEventLoopManual implementation
89 // ============================================================================
91 wxEventLoopManual::wxEventLoopManual()
97 bool wxEventLoopManual::ProcessEvents()
99 // process pending wx events first as they correspond to low-level events
100 // which happened before, i.e. typically pending events were queued by a
101 // previous call to Dispatch() and if we didn't process them now the next
102 // call to it might enqueue them again (as happens with e.g. socket events
103 // which would be generated as long as there is input available on socket
104 // and this input is only removed from it when pending event handlers are
107 wxTheApp
->ProcessPendingEvents();
112 int wxEventLoopManual::Run()
114 // event loops are not recursive, you need to create another loop!
115 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
117 // ProcessIdle() and ProcessEvents() below may throw so the code here should
118 // be exception-safe, hence we must use local objects for all actions we
120 wxEventLoopActivator
activate(this);
122 // we must ensure that OnExit() is called even if an exception is thrown
123 // from inside ProcessEvents() but we must call it from Exit() in normal
124 // situations because it is supposed to be called synchronously,
125 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
126 // something similar here)
132 #endif // wxUSE_EXCEPTIONS
134 // this is the event loop itself
137 // give them the possibility to do whatever they want
140 // generate and process idle events for as long as we don't
141 // have anything else to do
142 while ( !Pending() && ProcessIdle() && !m_shouldExit
)
145 // if the "should exit" flag is set, the loop should terminate
146 // but not before processing any remaining messages so while
147 // Pending() returns true, do process them
156 // a message came or no more idle processing to do, dispatch
157 // all the pending events and call Dispatch() to wait for the
159 if ( !ProcessEvents() )
167 // exit the outer loop as well
174 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
179 //else: continue running the event loop
183 // OnException() throwed, possibly rethrowing the same
184 // exception again: very good, but we still need OnExit() to
191 #endif // wxUSE_EXCEPTIONS
196 void wxEventLoopManual::Exit(int rc
)
198 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
205 // all we have to do to exit from the loop is to (maybe) wake it up so that
206 // it can notice that Exit() had been called
208 // in particular, do *not* use here calls such as PostQuitMessage() (under
209 // MSW) which terminate the current event loop here because we're not sure
210 // that it is going to be processed by the correct event loop: it would be
211 // possible that another one is started and terminated by mistake if we do
216 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__