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