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 void wxEventLoopBase::OnExit()
58 wxTheApp
->OnEventLoopExit(this);
61 void wxEventLoopBase::WakeUpIdle()
66 bool wxEventLoopBase::ProcessIdle()
68 return wxTheApp
&& wxTheApp
->ProcessIdle();
71 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
73 if ( m_isInsideYield
)
77 wxFAIL_MSG( wxT("wxYield called recursively" ) );
83 return YieldFor(wxEVT_CATEGORY_ALL
);
86 // wxEventLoopManual is unused in the other ports
87 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
89 // ============================================================================
90 // wxEventLoopManual implementation
91 // ============================================================================
93 wxEventLoopManual::wxEventLoopManual()
98 bool wxEventLoopManual::ProcessEvents()
100 // process pending wx events first as they correspond to low-level events
101 // which happened before, i.e. typically pending events were queued by a
102 // previous call to Dispatch() and if we didn't process them now the next
103 // call to it might enqueue them again (as happens with e.g. socket events
104 // which would be generated as long as there is input available on socket
105 // and this input is only removed from it when pending event handlers are
109 wxTheApp
->ProcessPendingEvents();
111 // One of the pending event handlers could have decided to exit the
112 // loop so check for the flag before trying to dispatch more events
113 // (which could block indefinitely if no more are coming).
121 int wxEventLoopManual::Run()
123 // event loops are not recursive, you need to create another loop!
124 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
126 // ProcessIdle() and ProcessEvents() below may throw so the code here should
127 // be exception-safe, hence we must use local objects for all actions we
129 wxEventLoopActivator
activate(this);
131 // we must ensure that OnExit() is called even if an exception is thrown
132 // from inside ProcessEvents() but we must call it from Exit() in normal
133 // situations because it is supposed to be called synchronously,
134 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
135 // something similar here)
141 #endif // wxUSE_EXCEPTIONS
143 // this is the event loop itself
146 // give them the possibility to do whatever they want
149 // generate and process idle events for as long as we don't
150 // have anything else to do
151 while ( !m_shouldExit
&& !Pending() && ProcessIdle() )
157 // a message came or no more idle processing to do, dispatch
158 // all the pending events and call Dispatch() to wait for the
160 if ( !ProcessEvents() )
167 // Process the remaining queued messages, both at the level of the
168 // underlying toolkit level (Pending/Dispatch()) and wx level
169 // (Has/ProcessPendingEvents()).
171 // We do run the risk of never exiting this loop if pending event
172 // handlers endlessly generate new events but they shouldn't do
173 // this in a well-behaved program and we shouldn't just discard the
174 // events we already have, they might be important.
177 bool hasMoreEvents
= false;
178 if ( wxTheApp
&& wxTheApp
->HasPendingEvents() )
180 wxTheApp
->ProcessPendingEvents();
181 hasMoreEvents
= true;
187 hasMoreEvents
= true;
190 if ( !hasMoreEvents
)
195 // exit the outer loop as well
202 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
207 //else: continue running the event loop
211 // OnException() throwed, possibly rethrowing the same
212 // exception again: very good, but we still need OnExit() to
219 #endif // wxUSE_EXCEPTIONS
224 void wxEventLoopManual::Exit(int rc
)
226 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
233 // all we have to do to exit from the loop is to (maybe) wake it up so that
234 // it can notice that Exit() had been called
236 // in particular, do *not* use here calls such as PostQuitMessage() (under
237 // MSW) which terminate the current event loop here because we're not sure
238 // that it is going to be processed by the correct event loop: it would be
239 // possible that another one is started and terminated by mistake if we do
244 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__