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
108 wxTheApp
->ProcessPendingEvents();
110 // One of the pending event handlers could have decided to exit the
111 // loop so check for the flag before trying to dispatch more events
112 // (which could block indefinitely if no more are coming).
120 int wxEventLoopManual::Run()
122 // event loops are not recursive, you need to create another loop!
123 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
125 // ProcessIdle() and ProcessEvents() below may throw so the code here should
126 // be exception-safe, hence we must use local objects for all actions we
128 wxEventLoopActivator
activate(this);
130 // we must ensure that OnExit() is called even if an exception is thrown
131 // from inside ProcessEvents() but we must call it from Exit() in normal
132 // situations because it is supposed to be called synchronously,
133 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
134 // something similar here)
140 #endif // wxUSE_EXCEPTIONS
142 // this is the event loop itself
145 // give them the possibility to do whatever they want
148 // generate and process idle events for as long as we don't
149 // have anything else to do
150 while ( !m_shouldExit
&& !Pending() && ProcessIdle() )
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() )
166 // Process the remaining queued messages, both at the level of the
167 // underlying toolkit level (Pending/Dispatch()) and wx level
168 // (Has/ProcessPendingEvents()).
170 // We do run the risk of never exiting this loop if pending event
171 // handlers endlessly generate new events but they shouldn't do
172 // this in a well-behaved program and we shouldn't just discard the
173 // events we already have, they might be important.
176 bool hasMoreEvents
= false;
177 if ( wxTheApp
&& wxTheApp
->HasPendingEvents() )
179 wxTheApp
->ProcessPendingEvents();
180 hasMoreEvents
= true;
186 hasMoreEvents
= true;
189 if ( !hasMoreEvents
)
194 // exit the outer loop as well
201 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
206 //else: continue running the event loop
210 // OnException() throwed, possibly rethrowing the same
211 // exception again: very good, but we still need OnExit() to
218 #endif // wxUSE_EXCEPTIONS
223 void wxEventLoopManual::Exit(int rc
)
225 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
232 // all we have to do to exit from the loop is to (maybe) wake it up so that
233 // it can notice that Exit() had been called
235 // in particular, do *not* use here calls such as PostQuitMessage() (under
236 // MSW) which terminate the current event loop here because we're not sure
237 // that it is going to be processed by the correct event loop: it would be
238 // possible that another one is started and terminated by mistake if we do
243 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__