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