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