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"
26 #include "wx/apptrait.h"
27 #include "wx/private/eventloopsourcesmanager.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 wxEventLoopBase
*wxEventLoopBase::ms_activeLoop
= NULL
;
35 wxEventLoopBase::wxEventLoopBase()
37 m_isInsideRun
= false;
40 m_isInsideYield
= false;
41 m_eventsToProcessInsideYield
= wxEVT_CATEGORY_ALL
;
44 bool wxEventLoopBase::IsMain() const
47 return wxTheApp
->GetMainLoop() == this;
52 void wxEventLoopBase::SetActive(wxEventLoopBase
* loop
)
57 wxTheApp
->OnEventLoopEnter(loop
);
60 int wxEventLoopBase::Run()
62 // event loops are not recursive, you need to create another loop!
63 wxCHECK_MSG( !IsInsideRun(), -1, wxT("can't reenter a message loop") );
65 // ProcessIdle() and ProcessEvents() below may throw so the code here should
66 // be exception-safe, hence we must use local objects for all actions we
68 wxEventLoopActivator
activate(this);
70 // We might be called again, after a previous call to ScheduleExit(), so
74 // Set this variable to true for the duration of this method.
76 wxON_BLOCK_EXIT_SET(m_isInsideRun
, false);
78 // Finally really run the loop.
82 void wxEventLoopBase::Exit(int rc
)
84 wxCHECK_RET( IsRunning(), wxS("Use ScheduleExit() on not running loop") );
89 void wxEventLoopBase::OnExit()
92 wxTheApp
->OnEventLoopExit(this);
95 void wxEventLoopBase::WakeUpIdle()
100 bool wxEventLoopBase::ProcessIdle()
102 return wxTheApp
&& wxTheApp
->ProcessIdle();
105 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
107 if ( m_isInsideYield
)
111 wxFAIL_MSG( wxT("wxYield called recursively" ) );
117 return YieldFor(wxEVT_CATEGORY_ALL
);
120 #if wxUSE_EVENTLOOP_SOURCE
123 wxEventLoopBase::AddSourceForFD(int fd
,
124 wxEventLoopSourceHandler
*handler
,
127 // Ensure that we have some valid traits.
128 wxConsoleAppTraits traitsConsole
;
129 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
131 traits
= &traitsConsole
;
133 // And delegate to the event loop sources manager defined by it.
134 wxEventLoopSourcesManagerBase
* const
135 manager
= traits
->GetEventLoopSourcesManager();
136 wxCHECK_MSG( manager
, NULL
, wxS("Must have wxEventLoopSourcesManager") );
138 return manager
->AddSourceForFD(fd
, handler
, flags
);
141 #endif // wxUSE_EVENTLOOP_SOURCE
143 // wxEventLoopManual is unused in the other ports
144 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
146 // ============================================================================
147 // wxEventLoopManual implementation
148 // ============================================================================
150 wxEventLoopManual::wxEventLoopManual()
155 bool wxEventLoopManual::ProcessEvents()
157 // process pending wx events first as they correspond to low-level events
158 // which happened before, i.e. typically pending events were queued by a
159 // previous call to Dispatch() and if we didn't process them now the next
160 // call to it might enqueue them again (as happens with e.g. socket events
161 // which would be generated as long as there is input available on socket
162 // and this input is only removed from it when pending event handlers are
166 wxTheApp
->ProcessPendingEvents();
168 // One of the pending event handlers could have decided to exit the
169 // loop so check for the flag before trying to dispatch more events
170 // (which could block indefinitely if no more are coming).
178 int wxEventLoopManual::DoRun()
180 // we must ensure that OnExit() is called even if an exception is thrown
181 // from inside ProcessEvents() but we must call it from Exit() in normal
182 // situations because it is supposed to be called synchronously,
183 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
184 // something similar here)
190 #endif // wxUSE_EXCEPTIONS
192 // this is the event loop itself
195 // give them the possibility to do whatever they want
198 // generate and process idle events for as long as we don't
199 // have anything else to do
200 while ( !m_shouldExit
&& !Pending() && ProcessIdle() )
206 // a message came or no more idle processing to do, dispatch
207 // all the pending events and call Dispatch() to wait for the
209 if ( !ProcessEvents() )
216 // Process the remaining queued messages, both at the level of the
217 // underlying toolkit level (Pending/Dispatch()) and wx level
218 // (Has/ProcessPendingEvents()).
220 // We do run the risk of never exiting this loop if pending event
221 // handlers endlessly generate new events but they shouldn't do
222 // this in a well-behaved program and we shouldn't just discard the
223 // events we already have, they might be important.
226 bool hasMoreEvents
= false;
227 if ( wxTheApp
&& wxTheApp
->HasPendingEvents() )
229 wxTheApp
->ProcessPendingEvents();
230 hasMoreEvents
= true;
236 hasMoreEvents
= true;
239 if ( !hasMoreEvents
)
244 // exit the outer loop as well
251 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
256 //else: continue running the event loop
260 // OnException() throwed, possibly rethrowing the same
261 // exception again: very good, but we still need OnExit() to
268 #endif // wxUSE_EXCEPTIONS
273 void wxEventLoopManual::ScheduleExit(int rc
)
275 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not running") );
282 // all we have to do to exit from the loop is to (maybe) wake it up so that
283 // it can notice that Exit() had been called
285 // in particular, do *not* use here calls such as PostQuitMessage() (under
286 // MSW) which terminate the current event loop here because we're not sure
287 // that it is going to be processed by the correct event loop: it would be
288 // possible that another one is started and terminated by mistake if we do
293 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__