Add missing wx/scopeguard.h include to fix the build.
[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_isInsideYield = false;
34 m_eventsToProcessInsideYield = wxEVT_CATEGORY_ALL;
35 }
36
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
59 void wxEventLoopBase::WakeUpIdle()
60 {
61 WakeUp();
62 }
63
64 bool wxEventLoopBase::ProcessIdle()
65 {
66 return wxTheApp && wxTheApp->ProcessIdle();
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
84 // wxEventLoopManual is unused in the other ports
85 #if defined(__WINDOWS__) || defined(__WXDFB__) || ( ( defined(__UNIX__) && !defined(__WXOSX__) ) && wxUSE_BASE)
86
87 // ============================================================================
88 // wxEventLoopManual implementation
89 // ============================================================================
90
91 wxEventLoopManual::wxEventLoopManual()
92 {
93 m_exitcode = 0;
94 m_shouldExit = false;
95 }
96
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 )
107 {
108 const bool hadExitedBefore = m_shouldExit;
109
110 wxTheApp->ProcessPendingEvents();
111
112 // One of the pending event handlers could have decided to exit the
113 // loop so check for the flag before trying to dispatch more events
114 // (which could block indefinitely if no more are coming).
115 if ( !hadExitedBefore && m_shouldExit )
116 {
117 // We still need to dispatch any remaining pending events, just as
118 // we do in the event loop in Run() if the loop is exited from a
119 // normal event handler.
120 while ( wxTheApp->HasPendingEvents() )
121 wxTheApp->ProcessPendingEvents();
122
123 return false;
124 }
125 }
126
127 return Dispatch();
128 }
129
130 int wxEventLoopManual::Run()
131 {
132 // event loops are not recursive, you need to create another loop!
133 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
134
135 // ProcessIdle() and ProcessEvents() below may throw so the code here should
136 // be exception-safe, hence we must use local objects for all actions we
137 // should undo
138 wxEventLoopActivator activate(this);
139
140 // we must ensure that OnExit() is called even if an exception is thrown
141 // from inside ProcessEvents() but we must call it from Exit() in normal
142 // situations because it is supposed to be called synchronously,
143 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
144 // something similar here)
145 #if wxUSE_EXCEPTIONS
146 for ( ;; )
147 {
148 try
149 {
150 #endif // wxUSE_EXCEPTIONS
151
152 // this is the event loop itself
153 for ( ;; )
154 {
155 // give them the possibility to do whatever they want
156 OnNextIteration();
157
158 // generate and process idle events for as long as we don't
159 // have anything else to do
160 while ( !Pending() && ProcessIdle() && !m_shouldExit )
161 ;
162
163 // if the "should exit" flag is set, the loop should terminate
164 // but not before processing any remaining messages so while
165 // Pending() returns true, do process them
166 if ( m_shouldExit )
167 {
168 while ( Pending() )
169 ProcessEvents();
170
171 break;
172 }
173
174 // a message came or no more idle processing to do, dispatch
175 // all the pending events and call Dispatch() to wait for the
176 // next message
177 if ( !ProcessEvents() )
178 {
179 // we got WM_QUIT
180 break;
181 }
182 }
183
184 #if wxUSE_EXCEPTIONS
185 // exit the outer loop as well
186 break;
187 }
188 catch ( ... )
189 {
190 try
191 {
192 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
193 {
194 OnExit();
195 break;
196 }
197 //else: continue running the event loop
198 }
199 catch ( ... )
200 {
201 // OnException() throwed, possibly rethrowing the same
202 // exception again: very good, but we still need OnExit() to
203 // be called
204 OnExit();
205 throw;
206 }
207 }
208 }
209 #endif // wxUSE_EXCEPTIONS
210
211 return m_exitcode;
212 }
213
214 void wxEventLoopManual::Exit(int rc)
215 {
216 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
217
218 m_exitcode = rc;
219 m_shouldExit = true;
220
221 OnExit();
222
223 // all we have to do to exit from the loop is to (maybe) wake it up so that
224 // it can notice that Exit() had been called
225 //
226 // in particular, do *not* use here calls such as PostQuitMessage() (under
227 // MSW) which terminate the current event loop here because we're not sure
228 // that it is going to be processed by the correct event loop: it would be
229 // possible that another one is started and terminated by mistake if we do
230 // this
231 WakeUp();
232 }
233
234 #endif // __WINDOWS__ || __WXMAC__ || __WXDFB__
235