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()
35 m_isInsideYield
= false;
36 m_eventsToProcessInsideYield
= wxEVT_CATEGORY_ALL
;
39 bool wxEventLoopBase::IsMain() const
42 return wxTheApp
->GetMainLoop() == this;
47 void wxEventLoopBase::SetActive(wxEventLoopBase
* loop
)
52 wxTheApp
->OnEventLoopEnter(loop
);
55 int wxEventLoopBase::Run()
57 // event loops are not recursive, you need to create another loop!
58 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
60 // ProcessIdle() and ProcessEvents() below may throw so the code here should
61 // be exception-safe, hence we must use local objects for all actions we
63 wxEventLoopActivator
activate(this);
65 // We might be called again, after a previous call to ScheduleExit(), so
69 // Finally really run the loop.
73 void wxEventLoopBase::OnExit()
76 wxTheApp
->OnEventLoopExit(this);
79 void wxEventLoopBase::WakeUpIdle()
84 bool wxEventLoopBase::ProcessIdle()
86 return wxTheApp
&& wxTheApp
->ProcessIdle();
89 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
91 if ( m_isInsideYield
)
95 wxFAIL_MSG( wxT("wxYield called recursively" ) );
101 return YieldFor(wxEVT_CATEGORY_ALL
);
104 // wxEventLoopManual is unused in the other ports
105 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
107 // ============================================================================
108 // wxEventLoopManual implementation
109 // ============================================================================
111 wxEventLoopManual::wxEventLoopManual()
116 bool wxEventLoopManual::ProcessEvents()
118 // process pending wx events first as they correspond to low-level events
119 // which happened before, i.e. typically pending events were queued by a
120 // previous call to Dispatch() and if we didn't process them now the next
121 // call to it might enqueue them again (as happens with e.g. socket events
122 // which would be generated as long as there is input available on socket
123 // and this input is only removed from it when pending event handlers are
127 wxTheApp
->ProcessPendingEvents();
129 // One of the pending event handlers could have decided to exit the
130 // loop so check for the flag before trying to dispatch more events
131 // (which could block indefinitely if no more are coming).
139 int wxEventLoopManual::DoRun()
141 // we must ensure that OnExit() is called even if an exception is thrown
142 // from inside ProcessEvents() but we must call it from Exit() in normal
143 // situations because it is supposed to be called synchronously,
144 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
145 // something similar here)
151 #endif // wxUSE_EXCEPTIONS
153 // this is the event loop itself
156 // give them the possibility to do whatever they want
159 // generate and process idle events for as long as we don't
160 // have anything else to do
161 while ( !m_shouldExit
&& !Pending() && ProcessIdle() )
167 // a message came or no more idle processing to do, dispatch
168 // all the pending events and call Dispatch() to wait for the
170 if ( !ProcessEvents() )
177 // Process the remaining queued messages, both at the level of the
178 // underlying toolkit level (Pending/Dispatch()) and wx level
179 // (Has/ProcessPendingEvents()).
181 // We do run the risk of never exiting this loop if pending event
182 // handlers endlessly generate new events but they shouldn't do
183 // this in a well-behaved program and we shouldn't just discard the
184 // events we already have, they might be important.
187 bool hasMoreEvents
= false;
188 if ( wxTheApp
&& wxTheApp
->HasPendingEvents() )
190 wxTheApp
->ProcessPendingEvents();
191 hasMoreEvents
= true;
197 hasMoreEvents
= true;
200 if ( !hasMoreEvents
)
205 // exit the outer loop as well
212 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
217 //else: continue running the event loop
221 // OnException() throwed, possibly rethrowing the same
222 // exception again: very good, but we still need OnExit() to
229 #endif // wxUSE_EXCEPTIONS
234 void wxEventLoopManual::Exit(int rc
)
236 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
243 // all we have to do to exit from the loop is to (maybe) wake it up so that
244 // it can notice that Exit() had been called
246 // in particular, do *not* use here calls such as PostQuitMessage() (under
247 // MSW) which terminate the current event loop here because we're not sure
248 // that it is going to be processed by the correct event loop: it would be
249 // possible that another one is started and terminated by mistake if we do
254 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__