]>
Commit | Line | Data |
---|---|---|
c8026dea VZ |
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 | ||
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 VZ |
24 | |
25 | // ---------------------------------------------------------------------------- | |
8b93348e | 26 | // wxEventLoopBase |
c8026dea VZ |
27 | // ---------------------------------------------------------------------------- |
28 | ||
2ddff00c | 29 | wxEventLoopBase *wxEventLoopBase::ms_activeLoop = NULL; |
c8026dea | 30 | |
dde19c21 FM |
31 | wxEventLoopBase::wxEventLoopBase() |
32 | { | |
33 | m_isInsideYield = false; | |
34 | m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL; | |
35 | } | |
36 | ||
ec38d07d FM |
37 | bool wxEventLoopBase::IsMain() const |
38 | { | |
39 | if (wxTheApp) | |
40 | return wxTheApp->GetMainLoop() == this; | |
41 | return false; | |
42 | } | |
43 | ||
44 | /* static */ | |
45 | void wxEventLoopBase::SetActive(wxEventLoopBase* loop) | |
46 | { | |
47 | ms_activeLoop = loop; | |
48 | ||
49 | if (wxTheApp) | |
50 | wxTheApp->OnEventLoopEnter(loop); | |
51 | } | |
52 | ||
53 | void wxEventLoopBase::OnExit() | |
54 | { | |
55 | if (wxTheApp) | |
56 | wxTheApp->OnEventLoopExit(this); | |
57 | } | |
58 | ||
dde19c21 FM |
59 | void wxEventLoopBase::WakeUpIdle() |
60 | { | |
61 | WakeUp(); | |
62 | } | |
63 | ||
64 | bool wxEventLoopBase::ProcessIdle() | |
65 | { | |
a758f601 | 66 | return wxTheApp && wxTheApp->ProcessIdle(); |
dde19c21 FM |
67 | } |
68 | ||
69 | bool wxEventLoopBase::Yield(bool onlyIfNeeded) | |
70 | { | |
71 | if ( m_isInsideYield ) | |
72 | { | |
73 | if ( !onlyIfNeeded ) | |
74 | { | |
75 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
76 | } | |
77 | ||
78 | return false; | |
79 | } | |
80 | ||
81 | return YieldFor(wxEVT_CATEGORY_ALL); | |
82 | } | |
83 | ||
52c9b349 | 84 | // wxEventLoopManual is unused in the other ports |
d98a58c5 | 85 | #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE) |
52c9b349 | 86 | |
c8026dea VZ |
87 | // ============================================================================ |
88 | // wxEventLoopManual implementation | |
89 | // ============================================================================ | |
90 | ||
91 | wxEventLoopManual::wxEventLoopManual() | |
92 | { | |
93 | m_exitcode = 0; | |
94 | m_shouldExit = false; | |
95 | } | |
96 | ||
26bacb82 VZ |
97 | bool wxEventLoopManual::ProcessEvents() |
98 | { | |
99 | // process pending wx events first as they correspond to low-level events | |
100 | // which happened before, i.e. typically pending events were queued by a | |
101 | // previous call to Dispatch() and if we didn't process them now the next | |
102 | // call to it might enqueue them again (as happens with e.g. socket events | |
103 | // which would be generated as long as there is input available on socket | |
104 | // and this input is only removed from it when pending event handlers are | |
105 | // executed) | |
106 | if ( wxTheApp ) | |
586c4551 | 107 | { |
26bacb82 VZ |
108 | wxTheApp->ProcessPendingEvents(); |
109 | ||
586c4551 VZ |
110 | // One of the pending event handlers could have decided to exit the |
111 | // loop so check for the flag before trying to dispatch more events | |
112 | // (which could block indefinitely if no more are coming). | |
113 | if ( m_shouldExit ) | |
114 | { | |
115 | // We still need to dispatch any remaining pending events, just as | |
116 | // we do in the event loop in Run() if the loop is exited from a | |
117 | // normal event handler. | |
118 | while ( wxTheApp->HasPendingEvents() ) | |
119 | wxTheApp->ProcessPendingEvents(); | |
120 | ||
121 | return false; | |
122 | } | |
123 | } | |
124 | ||
26bacb82 VZ |
125 | return Dispatch(); |
126 | } | |
127 | ||
c8026dea VZ |
128 | int wxEventLoopManual::Run() |
129 | { | |
130 | // event loops are not recursive, you need to create another loop! | |
9a83f860 | 131 | wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); |
c8026dea | 132 | |
26bacb82 | 133 | // ProcessIdle() and ProcessEvents() below may throw so the code here should |
c8026dea VZ |
134 | // be exception-safe, hence we must use local objects for all actions we |
135 | // should undo | |
b46b1d59 | 136 | wxEventLoopActivator activate(this); |
c8026dea VZ |
137 | |
138 | // we must ensure that OnExit() is called even if an exception is thrown | |
26bacb82 | 139 | // from inside ProcessEvents() but we must call it from Exit() in normal |
c8026dea VZ |
140 | // situations because it is supposed to be called synchronously, |
141 | // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or | |
142 | // something similar here) | |
143 | #if wxUSE_EXCEPTIONS | |
144 | for ( ;; ) | |
145 | { | |
146 | try | |
147 | { | |
148 | #endif // wxUSE_EXCEPTIONS | |
149 | ||
150 | // this is the event loop itself | |
151 | for ( ;; ) | |
152 | { | |
153 | // give them the possibility to do whatever they want | |
154 | OnNextIteration(); | |
155 | ||
156 | // generate and process idle events for as long as we don't | |
157 | // have anything else to do | |
e819ca3a | 158 | while ( !Pending() && ProcessIdle() && !m_shouldExit ) |
c8026dea VZ |
159 | ; |
160 | ||
161 | // if the "should exit" flag is set, the loop should terminate | |
162 | // but not before processing any remaining messages so while | |
163 | // Pending() returns true, do process them | |
164 | if ( m_shouldExit ) | |
165 | { | |
166 | while ( Pending() ) | |
26bacb82 | 167 | ProcessEvents(); |
c8026dea VZ |
168 | |
169 | break; | |
170 | } | |
171 | ||
26bacb82 VZ |
172 | // a message came or no more idle processing to do, dispatch |
173 | // all the pending events and call Dispatch() to wait for the | |
174 | // next message | |
175 | if ( !ProcessEvents() ) | |
c8026dea VZ |
176 | { |
177 | // we got WM_QUIT | |
178 | break; | |
179 | } | |
180 | } | |
181 | ||
182 | #if wxUSE_EXCEPTIONS | |
183 | // exit the outer loop as well | |
184 | break; | |
185 | } | |
186 | catch ( ... ) | |
187 | { | |
188 | try | |
189 | { | |
190 | if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() ) | |
191 | { | |
192 | OnExit(); | |
193 | break; | |
194 | } | |
195 | //else: continue running the event loop | |
196 | } | |
197 | catch ( ... ) | |
198 | { | |
199 | // OnException() throwed, possibly rethrowing the same | |
200 | // exception again: very good, but we still need OnExit() to | |
201 | // be called | |
202 | OnExit(); | |
203 | throw; | |
204 | } | |
205 | } | |
206 | } | |
207 | #endif // wxUSE_EXCEPTIONS | |
208 | ||
209 | return m_exitcode; | |
210 | } | |
211 | ||
212 | void wxEventLoopManual::Exit(int rc) | |
213 | { | |
9a83f860 | 214 | wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") ); |
c8026dea VZ |
215 | |
216 | m_exitcode = rc; | |
217 | m_shouldExit = true; | |
218 | ||
219 | OnExit(); | |
220 | ||
221 | // all we have to do to exit from the loop is to (maybe) wake it up so that | |
222 | // it can notice that Exit() had been called | |
223 | // | |
224 | // in particular, do *not* use here calls such as PostQuitMessage() (under | |
225 | // MSW) which terminate the current event loop here because we're not sure | |
226 | // that it is going to be processed by the correct event loop: it would be | |
227 | // possible that another one is started and terminated by mistake if we do | |
228 | // this | |
229 | WakeUp(); | |
230 | } | |
231 | ||
d98a58c5 | 232 | #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__ |
9af42efd | 233 |