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::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
61 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
63 // move the handler from the list of handlers with processable pending events
64 // to the list of handlers with pending events which needs to be processed later
65 m_handlersWithPendingEvents
.Remove(toDelay
);
67 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
68 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
70 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
73 void wxEventLoopBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
75 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
77 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
79 m_handlersWithPendingEvents
.Remove(toRemove
);
81 // check that the handler was present only once in the list
82 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
83 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
85 //else: it wasn't in this list at all, it's ok
87 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
89 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
91 // check that the handler was present only once in the list
92 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
93 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
95 //else: it wasn't in this list at all, it's ok
97 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
100 void wxEventLoopBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
102 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
104 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
105 m_handlersWithPendingEvents
.Add(toAppend
);
107 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
110 bool wxEventLoopBase::HasPendingEvents() const
112 wxENTER_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
114 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
116 wxLEAVE_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
121 void wxEventLoopBase::SuspendProcessingOfPendingEvents()
123 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
126 void wxEventLoopBase::ResumeProcessingOfPendingEvents()
128 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
131 void wxEventLoopBase::ProcessPendingEvents()
133 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
135 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
136 "this helper list should be empty" );
138 // iterate until the list becomes empty: the handlers remove themselves
139 // from it when they don't have any more pending events
140 while (!m_handlersWithPendingEvents
.IsEmpty())
142 // In ProcessPendingEvents(), new handlers might be added
143 // and we can safely leave the critical section here.
144 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
146 // NOTE: we always call ProcessPendingEvents() on the first event handler
147 // with pending events because handlers auto-remove themselves
148 // from this list (see RemovePendingEventHandler) if they have no
149 // more pending events.
150 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
152 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
155 // now the wxHandlersWithPendingEvents is surely empty; however some event
156 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
157 // because of a selective wxYield call in progress.
158 // Now we need to move them back to wxHandlersWithPendingEvents so the next
159 // call to this function has the chance of processing them:
160 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
162 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
163 m_handlersWithPendingDelayedEvents
.Clear();
166 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
169 void wxEventLoopBase::WakeUpIdle()
174 bool wxEventLoopBase::ProcessIdle()
176 // process pending wx events before sending idle events
177 ProcessPendingEvents();
181 event
.SetEventObject(wxTheApp
);
182 wxTheApp
->ProcessEvent(event
);
183 return event
.MoreRequested();
186 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
188 if ( m_isInsideYield
)
192 wxFAIL_MSG( wxT("wxYield called recursively" ) );
198 return YieldFor(wxEVT_CATEGORY_ALL
);
201 // wxEventLoopManual is unused in the other ports
202 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
204 // ============================================================================
205 // wxEventLoopManual implementation
206 // ============================================================================
208 wxEventLoopManual::wxEventLoopManual()
211 m_shouldExit
= false;
214 int wxEventLoopManual::Run()
216 // event loops are not recursive, you need to create another loop!
217 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
219 // ProcessIdle() and Dispatch() below may throw so the code here should
220 // be exception-safe, hence we must use local objects for all actions we
222 wxEventLoopActivator
activate(this);
224 // we must ensure that OnExit() is called even if an exception is thrown
225 // from inside Dispatch() but we must call it from Exit() in normal
226 // situations because it is supposed to be called synchronously,
227 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
228 // something similar here)
234 #endif // wxUSE_EXCEPTIONS
236 // this is the event loop itself
239 // give them the possibility to do whatever they want
242 // generate and process idle events for as long as we don't
243 // have anything else to do
244 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
247 // if the "should exit" flag is set, the loop should terminate
248 // but not before processing any remaining messages so while
249 // Pending() returns true, do process them
258 // a message came or no more idle processing to do, sit in
259 // Dispatch() waiting for the next message
268 // exit the outer loop as well
275 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
280 //else: continue running the event loop
284 // OnException() throwed, possibly rethrowing the same
285 // exception again: very good, but we still need OnExit() to
292 #endif // wxUSE_EXCEPTIONS
297 void wxEventLoopManual::Exit(int rc
)
299 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
306 // all we have to do to exit from the loop is to (maybe) wake it up so that
307 // it can notice that Exit() had been called
309 // in particular, do *not* use here calls such as PostQuitMessage() (under
310 // MSW) which terminate the current event loop here because we're not sure
311 // that it is going to be processed by the correct event loop: it would be
312 // possible that another one is started and terminated by mistake if we do
317 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__