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);
58 #if wxUSE_EVENTLOOP_SOURCE
59 // unregister all sources
60 (void) RemoveAllSources();
64 void wxEventLoopBase::WakeUpIdle()
69 bool wxEventLoopBase::ProcessIdle()
71 return wxTheApp
&& wxTheApp
->ProcessIdle();
74 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
76 if ( m_isInsideYield
)
80 wxFAIL_MSG( wxT("wxYield called recursively" ) );
86 return YieldFor(wxEVT_CATEGORY_ALL
);
89 // wxEventLoopManual is unused in the other ports
90 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
92 // ============================================================================
93 // wxEventLoopManual implementation
94 // ============================================================================
96 wxEventLoopManual::wxEventLoopManual()
102 bool wxEventLoopManual::ProcessEvents()
104 // process pending wx events first as they correspond to low-level events
105 // which happened before, i.e. typically pending events were queued by a
106 // previous call to Dispatch() and if we didn't process them now the next
107 // call to it might enqueue them again (as happens with e.g. socket events
108 // which would be generated as long as there is input available on socket
109 // and this input is only removed from it when pending event handlers are
112 wxTheApp
->ProcessPendingEvents();
117 int wxEventLoopManual::Run()
119 // event loops are not recursive, you need to create another loop!
120 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
122 // ProcessIdle() and ProcessEvents() below may throw so the code here should
123 // be exception-safe, hence we must use local objects for all actions we
125 wxEventLoopActivator
activate(this);
127 // we must ensure that OnExit() is called even if an exception is thrown
128 // from inside ProcessEvents() but we must call it from Exit() in normal
129 // situations because it is supposed to be called synchronously,
130 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
131 // something similar here)
137 #endif // wxUSE_EXCEPTIONS
139 // this is the event loop itself
142 // give them the possibility to do whatever they want
145 // generate and process idle events for as long as we don't
146 // have anything else to do
147 while ( !Pending() && ProcessIdle() )
150 // if the "should exit" flag is set, the loop should terminate
151 // but not before processing any remaining messages so while
152 // Pending() returns true, do process them
161 // a message came or no more idle processing to do, dispatch
162 // all the pending events and call Dispatch() to wait for the
164 if ( !ProcessEvents() )
172 // exit the outer loop as well
179 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
184 //else: continue running the event loop
188 // OnException() throwed, possibly rethrowing the same
189 // exception again: very good, but we still need OnExit() to
196 #endif // wxUSE_EXCEPTIONS
201 void wxEventLoopManual::Exit(int rc
)
203 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
210 // all we have to do to exit from the loop is to (maybe) wake it up so that
211 // it can notice that Exit() had been called
213 // in particular, do *not* use here calls such as PostQuitMessage() (under
214 // MSW) which terminate the current event loop here because we're not sure
215 // that it is going to be processed by the correct event loop: it would be
216 // possible that another one is started and terminated by mistake if we do
221 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__