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"
32 #include "wx/evtloop.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 wxEventLoopBase
*wxEventLoopBase::ms_activeLoop
= NULL
;
40 wxEventLoopBase::wxEventLoopBase()
42 m_isInsideYield
= false;
43 m_eventsToProcessInsideYield
= wxEVT_CATEGORY_ALL
;
46 void wxEventLoopBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
48 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
50 // move the handler from the list of handlers with processable pending events
51 // to the list of handlers with pending events which needs to be processed later
52 m_handlersWithPendingEvents
.Remove(toDelay
);
54 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
55 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
57 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
60 void wxEventLoopBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
62 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
64 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
66 m_handlersWithPendingEvents
.Remove(toRemove
);
68 // check that the handler was present only once in the list
69 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
70 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
72 //else: it wasn't in this list at all, it's ok
74 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
76 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
78 // check that the handler was present only once in the list
79 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
80 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
82 //else: it wasn't in this list at all, it's ok
84 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
87 void wxEventLoopBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
89 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
91 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
92 m_handlersWithPendingEvents
.Add(toAppend
);
94 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
97 bool wxEventLoopBase::HasPendingEvents() const
99 wxENTER_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
101 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
103 wxLEAVE_CRIT_SECT(const_cast<wxEventLoopBase
*>(this)->m_handlersWithPendingEventsLocker
);
108 void wxEventLoopBase::SuspendProcessingOfPendingEvents()
110 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
113 void wxEventLoopBase::ResumeProcessingOfPendingEvents()
115 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
118 void wxEventLoopBase::ProcessPendingEvents()
120 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
122 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
123 "this helper list should be empty" );
125 // iterate until the list becomes empty: the handlers remove themselves
126 // from it when they don't have any more pending events
127 while (!m_handlersWithPendingEvents
.IsEmpty())
129 // In ProcessPendingEvents(), new handlers might be added
130 // and we can safely leave the critical section here.
131 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
133 // NOTE: we always call ProcessPendingEvents() on the first event handler
134 // with pending events because handlers auto-remove themselves
135 // from this list (see RemovePendingEventHandler) if they have no
136 // more pending events.
137 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
139 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
142 // now the wxHandlersWithPendingEvents is surely empty; however some event
143 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
144 // because of a selective wxYield call in progress.
145 // Now we need to move them back to wxHandlersWithPendingEvents so the next
146 // call to this function has the chance of processing them:
147 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
149 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
150 m_handlersWithPendingDelayedEvents
.Clear();
153 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
156 void wxEventLoopBase::WakeUpIdle()
161 bool wxEventLoopBase::ProcessIdle()
163 // process pending wx events before sending idle events
164 ProcessPendingEvents();
168 event
.SetEventObject(wxTheApp
);
169 wxTheApp
->ProcessEvent(event
);
170 return event
.MoreRequested();
173 bool wxEventLoopBase::Yield(bool onlyIfNeeded
)
175 if ( m_isInsideYield
)
179 wxFAIL_MSG( wxT("wxYield called recursively" ) );
185 return YieldFor(wxEVT_CATEGORY_ALL
);
188 // wxEventLoopManual is unused in the other ports
189 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
191 // ============================================================================
192 // wxEventLoopManual implementation
193 // ============================================================================
195 wxEventLoopManual::wxEventLoopManual()
198 m_shouldExit
= false;
201 int wxEventLoopManual::Run()
203 // event loops are not recursive, you need to create another loop!
204 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
206 // ProcessIdle() and Dispatch() below may throw so the code here should
207 // be exception-safe, hence we must use local objects for all actions we
209 wxEventLoopActivator
activate(this);
211 // we must ensure that OnExit() is called even if an exception is thrown
212 // from inside Dispatch() but we must call it from Exit() in normal
213 // situations because it is supposed to be called synchronously,
214 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
215 // something similar here)
221 #endif // wxUSE_EXCEPTIONS
223 // this is the event loop itself
226 // give them the possibility to do whatever they want
229 // generate and process idle events for as long as we don't
230 // have anything else to do
231 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
234 // if the "should exit" flag is set, the loop should terminate
235 // but not before processing any remaining messages so while
236 // Pending() returns true, do process them
245 // a message came or no more idle processing to do, sit in
246 // Dispatch() waiting for the next message
255 // exit the outer loop as well
262 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
267 //else: continue running the event loop
271 // OnException() throwed, possibly rethrowing the same
272 // exception again: very good, but we still need OnExit() to
279 #endif // wxUSE_EXCEPTIONS
284 void wxEventLoopManual::Exit(int rc
)
286 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
293 // all we have to do to exit from the loop is to (maybe) wake it up so that
294 // it can notice that Exit() had been called
296 // in particular, do *not* use here calls such as PostQuitMessage() (under
297 // MSW) which terminate the current event loop here because we're not sure
298 // that it is going to be processed by the correct event loop: it would be
299 // possible that another one is started and terminated by mistake if we do
304 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__