]> git.saurik.com Git - wxWidgets.git/blob - src/common/evtloopcmn.cpp
fixing forced link of quicktime backend on osx_cocoa
[wxWidgets.git] / src / common / evtloopcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/evtloopcmn.cpp
3 // Purpose: common wxEventLoop-related stuff
4 // Author: Vadim Zeitlin
5 // Created: 2006-01-12
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006, 2013 Vadim Zeitlin <vadim@wxwindows.org>
8 // (c) 2013 Rob Bresalier
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/evtloop.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #endif //WX_PRECOMP
24
25 #include "wx/scopeguard.h"
26 #include "wx/apptrait.h"
27 #include "wx/private/eventloopsourcesmanager.h"
28
29 // ----------------------------------------------------------------------------
30 // wxEventLoopBase
31 // ----------------------------------------------------------------------------
32
33 wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL;
34
35 wxEventLoopBase::wxEventLoopBase()
36 {
37 m_isInsideRun = false;
38 m_shouldExit = false;
39
40 m_isInsideYield = false;
41 m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL;
42 }
43
44 bool wxEventLoopBase::IsMain() const
45 {
46 if (wxTheApp)
47 return wxTheApp->GetMainLoop() == this;
48 return false;
49 }
50
51 /* static */
52 void wxEventLoopBase::SetActive(wxEventLoopBase* loop)
53 {
54 ms_activeLoop = loop;
55
56 if (wxTheApp)
57 wxTheApp->OnEventLoopEnter(loop);
58 }
59
60 int wxEventLoopBase::Run()
61 {
62 // event loops are not recursive, you need to create another loop!
63 wxCHECK_MSG( !IsInsideRun(), -1, wxT("can't reenter a message loop") );
64
65 // ProcessIdle() and ProcessEvents() below may throw so the code here should
66 // be exception-safe, hence we must use local objects for all actions we
67 // should undo
68 wxEventLoopActivator activate(this);
69
70 // We might be called again, after a previous call to ScheduleExit(), so
71 // reset this flag.
72 m_shouldExit = false;
73
74 // Set this variable to true for the duration of this method.
75 m_isInsideRun = true;
76 wxON_BLOCK_EXIT_SET(m_isInsideRun, false);
77
78 // Finally really run the loop.
79 return DoRun();
80 }
81
82 void wxEventLoopBase::Exit(int rc)
83 {
84 wxCHECK_RET( IsRunning(), wxS("Use ScheduleExit() on not running loop") );
85
86 ScheduleExit(rc);
87 }
88
89 void wxEventLoopBase::OnExit()
90 {
91 if (wxTheApp)
92 wxTheApp->OnEventLoopExit(this);
93 }
94
95 void wxEventLoopBase::WakeUpIdle()
96 {
97 WakeUp();
98 }
99
100 bool wxEventLoopBase::ProcessIdle()
101 {
102 return wxTheApp && wxTheApp->ProcessIdle();
103 }
104
105 bool wxEventLoopBase::Yield(bool onlyIfNeeded)
106 {
107 if ( m_isInsideYield )
108 {
109 if ( !onlyIfNeeded )
110 {
111 wxFAIL_MSG( wxT("wxYield called recursively" ) );
112 }
113
114 return false;
115 }
116
117 return YieldFor(wxEVT_CATEGORY_ALL);
118 }
119
120 #if wxUSE_EVENTLOOP_SOURCE
121
122 wxEventLoopSource*
123 wxEventLoopBase::AddSourceForFD(int fd,
124 wxEventLoopSourceHandler *handler,
125 int flags)
126 {
127 // Delegate to the event loop sources manager defined by it.
128 wxEventLoopSourcesManagerBase* const
129 manager = wxApp::GetValidTraits().GetEventLoopSourcesManager();
130 wxCHECK_MSG( manager, NULL, wxS("Must have wxEventLoopSourcesManager") );
131
132 return manager->AddSourceForFD(fd, handler, flags);
133 }
134
135 #endif // wxUSE_EVENTLOOP_SOURCE
136
137 // wxEventLoopManual is unused in the other ports
138 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
139
140 // ============================================================================
141 // wxEventLoopManual implementation
142 // ============================================================================
143
144 wxEventLoopManual::wxEventLoopManual()
145 {
146 m_exitcode = 0;
147 }
148
149 bool wxEventLoopManual::ProcessEvents()
150 {
151 // process pending wx events first as they correspond to low-level events
152 // which happened before, i.e. typically pending events were queued by a
153 // previous call to Dispatch() and if we didn't process them now the next
154 // call to it might enqueue them again (as happens with e.g. socket events
155 // which would be generated as long as there is input available on socket
156 // and this input is only removed from it when pending event handlers are
157 // executed)
158 if ( wxTheApp )
159 {
160 wxTheApp->ProcessPendingEvents();
161
162 // One of the pending event handlers could have decided to exit the
163 // loop so check for the flag before trying to dispatch more events
164 // (which could block indefinitely if no more are coming).
165 if ( m_shouldExit )
166 return false;
167 }
168
169 return Dispatch();
170 }
171
172 int wxEventLoopManual::DoRun()
173 {
174 // we must ensure that OnExit() is called even if an exception is thrown
175 // from inside ProcessEvents() but we must call it from Exit() in normal
176 // situations because it is supposed to be called synchronously,
177 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
178 // something similar here)
179 #if wxUSE_EXCEPTIONS
180 for ( ;; )
181 {
182 try
183 {
184 #endif // wxUSE_EXCEPTIONS
185
186 // this is the event loop itself
187 for ( ;; )
188 {
189 // give them the possibility to do whatever they want
190 OnNextIteration();
191
192 // generate and process idle events for as long as we don't
193 // have anything else to do
194 while ( !m_shouldExit && !Pending() && ProcessIdle() )
195 ;
196
197 if ( m_shouldExit )
198 break;
199
200 // a message came or no more idle processing to do, dispatch
201 // all the pending events and call Dispatch() to wait for the
202 // next message
203 if ( !ProcessEvents() )
204 {
205 // we got WM_QUIT
206 break;
207 }
208 }
209
210 // Process the remaining queued messages, both at the level of the
211 // underlying toolkit level (Pending/Dispatch()) and wx level
212 // (Has/ProcessPendingEvents()).
213 //
214 // We do run the risk of never exiting this loop if pending event
215 // handlers endlessly generate new events but they shouldn't do
216 // this in a well-behaved program and we shouldn't just discard the
217 // events we already have, they might be important.
218 for ( ;; )
219 {
220 bool hasMoreEvents = false;
221 if ( wxTheApp && wxTheApp->HasPendingEvents() )
222 {
223 wxTheApp->ProcessPendingEvents();
224 hasMoreEvents = true;
225 }
226
227 if ( Pending() )
228 {
229 Dispatch();
230 hasMoreEvents = true;
231 }
232
233 if ( !hasMoreEvents )
234 break;
235 }
236
237 #if wxUSE_EXCEPTIONS
238 // exit the outer loop as well
239 break;
240 }
241 catch ( ... )
242 {
243 try
244 {
245 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
246 {
247 OnExit();
248 break;
249 }
250 //else: continue running the event loop
251 }
252 catch ( ... )
253 {
254 // OnException() throwed, possibly rethrowing the same
255 // exception again: very good, but we still need OnExit() to
256 // be called
257 OnExit();
258 throw;
259 }
260 }
261 }
262 #endif // wxUSE_EXCEPTIONS
263
264 return m_exitcode;
265 }
266
267 void wxEventLoopManual::ScheduleExit(int rc)
268 {
269 wxCHECK_RET( IsInsideRun(), wxT("can't call ScheduleExit() if not running") );
270
271 m_exitcode = rc;
272 m_shouldExit = true;
273
274 OnExit();
275
276 // all we have to do to exit from the loop is to (maybe) wake it up so that
277 // it can notice that Exit() had been called
278 //
279 // in particular, do *not* use here calls such as PostQuitMessage() (under
280 // MSW) which terminate the current event loop here because we're not sure
281 // that it is going to be processed by the correct event loop: it would be
282 // possible that another one is started and terminated by mistake if we do
283 // this
284 WakeUp();
285 }
286
287 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__
288