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