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 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/evtloop.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 wxEventLoopBase
*wxEventLoopBase::ms_activeLoop
= NULL
;
39 wxEventLoopBase::wxEventLoopBase()
41 m_isInsideYield
= false;
42 m_eventsToProcessInsideYield
= wxEVT_CATEGORY_ALL
;
45 void wxEventLoopBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
47 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
49 // move the handler from the list of handlers with processable pending events
50 // to the list of handlers with pending events which needs to be processed later
51 m_handlersWithPendingEvents
.Remove(toDelay
);
53 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
54 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
56 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
59 void wxEventLoopBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
61 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
63 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
65 m_handlersWithPendingEvents
.Remove(toRemove
);
67 // check that the handler was present only once in the list
68 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
69 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
71 //else: it wasn't in this list at all, it's ok
73 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
75 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
77 // check that the handler was present only once in the list
78 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
79 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
81 //else: it wasn't in this list at all, it's ok
83 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
86 void wxEventLoopBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
88 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
90 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
91 m_handlersWithPendingEvents
.Add(toAppend
);
93 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
96 bool wxEventLoopBase::HasPendingEvents() const
98 wxENTER_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
100 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
102 wxLEAVE_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
107 void wxEventLoopBase::SuspendProcessingOfPendingEvents()
109 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
112 void wxEventLoopBase::ResumeProcessingOfPendingEvents()
114 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
117 void wxEventLoopBase::ProcessPendingEvents()
119 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
121 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
122 "this helper list should be empty" );
124 // iterate until the list becomes empty: the handlers remove themselves
125 // from it when they don't have any more pending events
126 while (!m_handlersWithPendingEvents
.IsEmpty())
128 // In ProcessPendingEvents(), new handlers might be added
129 // and we can safely leave the critical section here.
130 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
132 // NOTE: we always call ProcessPendingEvents() on the first event handler
133 // with pending events because handlers auto-remove themselves
134 // from this list (see RemovePendingEventHandler) if they have no
135 // more pending events.
136 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
138 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
141 // now the wxHandlersWithPendingEvents is surely empty; however some event
142 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
143 // because of a selective wxYield call in progress.
144 // Now we need to move them back to wxHandlersWithPendingEvents so the next
145 // call to this function has the chance of processing them:
146 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
148 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
149 m_handlersWithPendingDelayedEvents
.Clear();
152 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
155 void wxEventLoopBase::WakeUpIdle()
160 bool wxEventLoopBase::ProcessIdle()
162 // process pending wx events before sending idle events
163 ProcessPendingEvents();
167 event
.SetEventObject(wxTheApp
);
168 wxTheApp
->ProcessEvent(event
);
169 return event
.MoreRequested();
172 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
174 if ( m_isInsideYield
)
178 wxFAIL_MSG( wxT("wxYield called recursively" ) );
184 return YieldFor(wxEVT_CATEGORY_ALL
);
187 // wxEventLoopManual is unused in the other ports
188 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
190 // ============================================================================
191 // wxEventLoopManual implementation
192 // ============================================================================
194 wxEventLoopManual::wxEventLoopManual()
197 m_shouldExit
= false;
200 int wxEventLoopManual::Run()
202 // event loops are not recursive, you need to create another loop!
203 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
205 // ProcessIdle() and Dispatch() below may throw so the code here should
206 // be exception-safe, hence we must use local objects for all actions we
208 wxEventLoopActivator
activate(this);
210 // we must ensure that OnExit() is called even if an exception is thrown
211 // from inside Dispatch() but we must call it from Exit() in normal
212 // situations because it is supposed to be called synchronously,
213 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
214 // something similar here)
220 #endif // wxUSE_EXCEPTIONS
222 // this is the event loop itself
225 // give them the possibility to do whatever they want
228 // generate and process idle events for as long as we don't
229 // have anything else to do
230 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
233 // if the "should exit" flag is set, the loop should terminate
234 // but not before processing any remaining messages so while
235 // Pending() returns true, do process them
244 // a message came or no more idle processing to do, sit in
245 // Dispatch() waiting for the next message
254 // exit the outer loop as well
261 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
266 //else: continue running the event loop
270 // OnException() throwed, possibly rethrowing the same
271 // exception again: very good, but we still need OnExit() to
278 #endif // wxUSE_EXCEPTIONS
283 void wxEventLoopManual::Exit(int rc
)
285 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
292 // all we have to do to exit from the loop is to (maybe) wake it up so that
293 // it can notice that Exit() had been called
295 // in particular, do *not* use here calls such as PostQuitMessage() (under
296 // MSW) which terminate the current event loop here because we're not sure
297 // that it is going to be processed by the correct event loop: it would be
298 // possible that another one is started and terminated by mistake if we do
303 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__