]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
wxApp::DoYield => wxGUIEventLoop::YieldFor (part of r58911)
[wxWidgets.git] / src / common / evtloopcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/evtloopcmn.cpp
3 // Purpose: common wxEventLoop-related stuff
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 2006-01-12
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/event.h"
30 #endif //WX_PRECOMP
31
32 #include "wx/evtloop.h"
33
34 // ----------------------------------------------------------------------------
35 // globals
36 // ----------------------------------------------------------------------------
37
38 wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL;
39
40 wxEventLoopBase::wxEventLoopBase()
41 {
42 m_isInsideYield = false;
43 m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL;
44 }
45
46 void wxEventLoopBase::DelayPendingEventHandler(wxEvtHandler* toDelay)
47 {
48 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker);
49
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);
53
54 if (m_handlersWithPendingDelayedEvents.Index(toDelay) == wxNOT_FOUND)
55 m_handlersWithPendingDelayedEvents.Add(toDelay);
56
57 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker);
58 }
59
60 void wxEventLoopBase::RemovePendingEventHandler(wxEvtHandler* toRemove)
61 {
62 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker);
63
64 if (m_handlersWithPendingEvents.Index(toRemove) != wxNOT_FOUND)
65 {
66 m_handlersWithPendingEvents.Remove(toRemove);
67
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!" );
71 }
72 //else: it wasn't in this list at all, it's ok
73
74 if (m_handlersWithPendingDelayedEvents.Index(toRemove) != wxNOT_FOUND)
75 {
76 m_handlersWithPendingDelayedEvents.Remove(toRemove);
77
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!" );
81 }
82 //else: it wasn't in this list at all, it's ok
83
84 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker);
85 }
86
87 void wxEventLoopBase::AppendPendingEventHandler(wxEvtHandler* toAppend)
88 {
89 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker);
90
91 if ( m_handlersWithPendingEvents.Index(toAppend) == wxNOT_FOUND )
92 m_handlersWithPendingEvents.Add(toAppend);
93
94 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker);
95 }
96
97 bool wxEventLoopBase::HasPendingEvents() const
98 {
99 wxENTER_CRIT_SECT(const_cast<wxEventLoopBase*>(this)->m_handlersWithPendingEventsLocker);
100
101 bool has = !m_handlersWithPendingEvents.IsEmpty();
102
103 wxLEAVE_CRIT_SECT(const_cast<wxEventLoopBase*>(this)->m_handlersWithPendingEventsLocker);
104
105 return has;
106 }
107
108 void wxEventLoopBase::SuspendProcessingOfPendingEvents()
109 {
110 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker);
111 }
112
113 void wxEventLoopBase::ResumeProcessingOfPendingEvents()
114 {
115 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker);
116 }
117
118 void wxEventLoopBase::ProcessPendingEvents()
119 {
120 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker);
121
122 wxCHECK_RET( m_handlersWithPendingDelayedEvents.IsEmpty(),
123 "this helper list should be empty" );
124
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())
128 {
129 // In ProcessPendingEvents(), new handlers might be added
130 // and we can safely leave the critical section here.
131 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker);
132
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();
138
139 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker);
140 }
141
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())
148 {
149 WX_APPEND_ARRAY(m_handlersWithPendingEvents, m_handlersWithPendingDelayedEvents);
150 m_handlersWithPendingDelayedEvents.Clear();
151 }
152
153 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker);
154 }
155
156 void wxEventLoopBase::WakeUpIdle()
157 {
158 WakeUp();
159 }
160
161 bool wxEventLoopBase::ProcessIdle()
162 {
163 // process pending wx events before sending idle events
164 ProcessPendingEvents();
165
166 wxIdleEvent event;
167
168 event.SetEventObject(wxTheApp);
169 wxTheApp->ProcessEvent(event);
170 return event.MoreRequested();
171 }
172
173 bool wxEventLoopBase::Yield(bool onlyIfNeeded)
174 {
175 if ( m_isInsideYield )
176 {
177 if ( !onlyIfNeeded )
178 {
179 wxFAIL_MSG( wxT("wxYield called recursively" ) );
180 }
181
182 return false;
183 }
184
185 return YieldFor(wxEVT_CATEGORY_ALL);
186 }
187
188 // wxEventLoopManual is unused in the other ports
189 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || (defined(__UNIX__) && wxUSE_BASE)
190
191 // ============================================================================
192 // wxEventLoopManual implementation
193 // ============================================================================
194
195 wxEventLoopManual::wxEventLoopManual()
196 {
197 m_exitcode = 0;
198 m_shouldExit = false;
199 }
200
201 int wxEventLoopManual::Run()
202 {
203 // event loops are not recursive, you need to create another loop!
204 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
205
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
208 // should undo
209 wxEventLoopActivator activate(this);
210
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)
216 #if wxUSE_EXCEPTIONS
217 for ( ;; )
218 {
219 try
220 {
221 #endif // wxUSE_EXCEPTIONS
222
223 // this is the event loop itself
224 for ( ;; )
225 {
226 // give them the possibility to do whatever they want
227 OnNextIteration();
228
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()) )
232 ;
233
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
237 if ( m_shouldExit )
238 {
239 while ( Pending() )
240 Dispatch();
241
242 break;
243 }
244
245 // a message came or no more idle processing to do, sit in
246 // Dispatch() waiting for the next message
247 if ( !Dispatch() )
248 {
249 // we got WM_QUIT
250 break;
251 }
252 }
253
254 #if wxUSE_EXCEPTIONS
255 // exit the outer loop as well
256 break;
257 }
258 catch ( ... )
259 {
260 try
261 {
262 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
263 {
264 OnExit();
265 break;
266 }
267 //else: continue running the event loop
268 }
269 catch ( ... )
270 {
271 // OnException() throwed, possibly rethrowing the same
272 // exception again: very good, but we still need OnExit() to
273 // be called
274 OnExit();
275 throw;
276 }
277 }
278 }
279 #endif // wxUSE_EXCEPTIONS
280
281 return m_exitcode;
282 }
283
284 void wxEventLoopManual::Exit(int rc)
285 {
286 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
287
288 m_exitcode = rc;
289 m_shouldExit = true;
290
291 OnExit();
292
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
295 //
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
300 // this
301 WakeUp();
302 }
303
304 #endif // __WXMSW__ || __WXMAC__ || __WXDFB__
305