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