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