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 void wxEventLoopBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
39 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
41 // move the handler from the list of handlers with processable pending events
42 // to the list of handlers with pending events which needs to be processed later
43 m_handlersWithPendingEvents
.Remove(toDelay
);
45 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
46 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
48 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
51 void wxEventLoopBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
53 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
55 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
57 m_handlersWithPendingEvents
.Remove(toRemove
);
59 // check that the handler was present only once in the list
60 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
61 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
63 //else: it wasn't in this list at all, it's ok
65 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
67 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
69 // check that the handler was present only once in the list
70 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
71 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
73 //else: it wasn't in this list at all, it's ok
75 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
78 void wxEventLoopBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
80 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
82 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
83 m_handlersWithPendingEvents
.Add(toAppend
);
85 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
88 bool wxEventLoopBase::HasPendingEvents() const
90 wxENTER_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
92 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
94 wxLEAVE_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
99 void wxEventLoopBase::SuspendProcessingOfPendingEvents()
101 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
104 void wxEventLoopBase::ResumeProcessingOfPendingEvents()
106 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
109 void wxEventLoopBase::ProcessPendingEvents()
111 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
113 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
114 "this helper list should be empty" );
116 // iterate until the list becomes empty: the handlers remove themselves
117 // from it when they don't have any more pending events
118 while (!m_handlersWithPendingEvents
.IsEmpty())
120 // In ProcessPendingEvents(), new handlers might be added
121 // and we can safely leave the critical section here.
122 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
124 // NOTE: we always call ProcessPendingEvents() on the first event handler
125 // with pending events because handlers auto-remove themselves
126 // from this list (see RemovePendingEventHandler) if they have no
127 // more pending events.
128 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
130 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
133 // now the wxHandlersWithPendingEvents is surely empty; however some event
134 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
135 // because of a selective wxYield call in progress.
136 // Now we need to move them back to wxHandlersWithPendingEvents so the next
137 // call to this function has the chance of processing them:
138 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
140 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
141 m_handlersWithPendingDelayedEvents
.Clear();
144 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
147 void wxEventLoopBase::WakeUpIdle()
152 bool wxEventLoopBase::ProcessIdle()
154 // process pending wx events before sending idle events
155 ProcessPendingEvents();
159 event
.SetEventObject(wxTheApp
);
160 wxTheApp
->ProcessEvent(event
);
161 return event
.MoreRequested();
164 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
166 if ( m_isInsideYield
)
170 wxFAIL_MSG( wxT("wxYield called recursively" ) );
176 return YieldFor(wxEVT_CATEGORY_ALL
);
179 // wxEventLoopManual is unused in the other ports
180 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
182 // ============================================================================
183 // wxEventLoopManual implementation
184 // ============================================================================
186 wxEventLoopManual::wxEventLoopManual()
189 m_shouldExit
= false;
192 int wxEventLoopManual::Run()
194 // event loops are not recursive, you need to create another loop!
195 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
197 // ProcessIdle() and Dispatch() below may throw so the code here should
198 // be exception-safe, hence we must use local objects for all actions we
200 wxEventLoopActivator
activate(this);
202 // we must ensure that OnExit() is called even if an exception is thrown
203 // from inside Dispatch() but we must call it from Exit() in normal
204 // situations because it is supposed to be called synchronously,
205 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
206 // something similar here)
212 #endif // wxUSE_EXCEPTIONS
214 // this is the event loop itself
217 // give them the possibility to do whatever they want
220 // generate and process idle events for as long as we don't
221 // have anything else to do
222 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
225 // if the "should exit" flag is set, the loop should terminate
226 // but not before processing any remaining messages so while
227 // Pending() returns true, do process them
236 // a message came or no more idle processing to do, sit in
237 // Dispatch() waiting for the next message
246 // exit the outer loop as well
253 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
258 //else: continue running the event loop
262 // OnException() throwed, possibly rethrowing the same
263 // exception again: very good, but we still need OnExit() to
270 #endif // wxUSE_EXCEPTIONS
275 void wxEventLoopManual::Exit(int rc
)
277 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
284 // all we have to do to exit from the loop is to (maybe) wake it up so that
285 // it can notice that Exit() had been called
287 // in particular, do *not* use here calls such as PostQuitMessage() (under
288 // MSW) which terminate the current event loop here because we're not sure
289 // that it is going to be processed by the correct event loop: it would be
290 // possible that another one is started and terminated by mistake if we do
295 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__