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()
69 // process pending wx events before sending idle events
70 wxTheApp
->ProcessPendingEvents();
72 // synthetize an idle event and send it to wxApp
74 event
.SetEventObject(wxTheApp
);
75 wxTheApp
->ProcessEvent(event
);
77 return event
.MoreRequested();
80 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
82 if ( m_isInsideYield
)
86 wxFAIL_MSG( wxT("wxYield called recursively" ) );
92 return YieldFor(wxEVT_CATEGORY_ALL
);
95 // wxEventLoopManual is unused in the other ports
96 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
98 // ============================================================================
99 // wxEventLoopManual implementation
100 // ============================================================================
102 wxEventLoopManual::wxEventLoopManual()
105 m_shouldExit
= false;
108 int wxEventLoopManual::Run()
110 // event loops are not recursive, you need to create another loop!
111 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
113 // ProcessIdle() and Dispatch() below may throw so the code here should
114 // be exception-safe, hence we must use local objects for all actions we
116 wxEventLoopActivator
activate(this);
118 // we must ensure that OnExit() is called even if an exception is thrown
119 // from inside Dispatch() but we must call it from Exit() in normal
120 // situations because it is supposed to be called synchronously,
121 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
122 // something similar here)
128 #endif // wxUSE_EXCEPTIONS
130 // this is the event loop itself
133 // give them the possibility to do whatever they want
136 // generate and process idle events for as long as we don't
137 // have anything else to do
138 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
141 // if the "should exit" flag is set, the loop should terminate
142 // but not before processing any remaining messages so while
143 // Pending() returns true, do process them
152 // a message came or no more idle processing to do, sit in
153 // Dispatch() waiting for the next message
162 // exit the outer loop as well
169 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
174 //else: continue running the event loop
178 // OnException() throwed, possibly rethrowing the same
179 // exception again: very good, but we still need OnExit() to
186 #endif // wxUSE_EXCEPTIONS
191 void wxEventLoopManual::Exit(int rc
)
193 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
200 // all we have to do to exit from the loop is to (maybe) wake it up so that
201 // it can notice that Exit() had been called
203 // in particular, do *not* use here calls such as PostQuitMessage() (under
204 // MSW) which terminate the current event loop here because we're not sure
205 // that it is going to be processed by the correct event loop: it would be
206 // possible that another one is started and terminated by mistake if we do
211 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__