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 #include "wx/scopeguard.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 wxEventLoopBase
*wxEventLoopBase::ms_activeLoop
= NULL
;
33 wxEventLoopBase::wxEventLoopBase()
35 m_isInsideRun
= false;
38 m_isInsideYield
= false;
39 m_eventsToProcessInsideYield
= wxEVT_CATEGORY_ALL
;
42 bool wxEventLoopBase::IsMain() const
45 return wxTheApp
->GetMainLoop() == this;
50 void wxEventLoopBase::SetActive(wxEventLoopBase
* loop
)
55 wxTheApp
->OnEventLoopEnter(loop
);
58 int wxEventLoopBase::Run()
60 // event loops are not recursive, you need to create another loop!
61 wxCHECK_MSG( !IsInsideRun(), -1, wxT("can't reenter a message loop") );
63 // ProcessIdle() and ProcessEvents() below may throw so the code here should
64 // be exception-safe, hence we must use local objects for all actions we
66 wxEventLoopActivator
activate(this);
68 // We might be called again, after a previous call to ScheduleExit(), so
72 // Set this variable to true for the duration of this method.
74 wxON_BLOCK_EXIT_SET(m_isInsideRun
, false);
76 // Finally really run the loop.
80 void wxEventLoopBase::Exit(int rc
)
82 wxCHECK_RET( IsRunning(), wxS("Use ScheduleExit() on not running loop") );
87 void wxEventLoopBase::OnExit()
90 wxTheApp
->OnEventLoopExit(this);
93 void wxEventLoopBase::WakeUpIdle()
98 bool wxEventLoopBase::ProcessIdle()
100 return wxTheApp
&& wxTheApp
->ProcessIdle();
103 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
105 if ( m_isInsideYield
)
109 wxFAIL_MSG( wxT("wxYield called recursively" ) );
115 return YieldFor(wxEVT_CATEGORY_ALL
);
118 // wxEventLoopManual is unused in the other ports
119 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
121 // ============================================================================
122 // wxEventLoopManual implementation
123 // ============================================================================
125 wxEventLoopManual::wxEventLoopManual()
130 bool wxEventLoopManual::ProcessEvents()
132 // process pending wx events first as they correspond to low-level events
133 // which happened before, i.e. typically pending events were queued by a
134 // previous call to Dispatch() and if we didn't process them now the next
135 // call to it might enqueue them again (as happens with e.g. socket events
136 // which would be generated as long as there is input available on socket
137 // and this input is only removed from it when pending event handlers are
141 wxTheApp
->ProcessPendingEvents();
143 // One of the pending event handlers could have decided to exit the
144 // loop so check for the flag before trying to dispatch more events
145 // (which could block indefinitely if no more are coming).
153 int wxEventLoopManual::DoRun()
155 // we must ensure that OnExit() is called even if an exception is thrown
156 // from inside ProcessEvents() but we must call it from Exit() in normal
157 // situations because it is supposed to be called synchronously,
158 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
159 // something similar here)
165 #endif // wxUSE_EXCEPTIONS
167 // this is the event loop itself
170 // give them the possibility to do whatever they want
173 // generate and process idle events for as long as we don't
174 // have anything else to do
175 while ( !m_shouldExit
&& !Pending() && ProcessIdle() )
181 // a message came or no more idle processing to do, dispatch
182 // all the pending events and call Dispatch() to wait for the
184 if ( !ProcessEvents() )
191 // Process the remaining queued messages, both at the level of the
192 // underlying toolkit level (Pending/Dispatch()) and wx level
193 // (Has/ProcessPendingEvents()).
195 // We do run the risk of never exiting this loop if pending event
196 // handlers endlessly generate new events but they shouldn't do
197 // this in a well-behaved program and we shouldn't just discard the
198 // events we already have, they might be important.
201 bool hasMoreEvents
= false;
202 if ( wxTheApp
&& wxTheApp
->HasPendingEvents() )
204 wxTheApp
->ProcessPendingEvents();
205 hasMoreEvents
= true;
211 hasMoreEvents
= true;
214 if ( !hasMoreEvents
)
219 // exit the outer loop as well
226 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
231 //else: continue running the event loop
235 // OnException() throwed, possibly rethrowing the same
236 // exception again: very good, but we still need OnExit() to
243 #endif // wxUSE_EXCEPTIONS
248 void wxEventLoopManual::ScheduleExit(int rc
)
250 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not running") );
257 // all we have to do to exit from the loop is to (maybe) wake it up so that
258 // it can notice that Exit() had been called
260 // in particular, do *not* use here calls such as PostQuitMessage() (under
261 // MSW) which terminate the current event loop here because we're not sure
262 // that it is going to be processed by the correct event loop: it would be
263 // possible that another one is started and terminated by mistake if we do
268 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__