]> git.saurik.com Git - wxWidgets.git/blob - include/wx/evtloop.h
build fix
[wxWidgets.git] / include / wx / evtloop.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/evtloop.h
3 // Purpose: declares wxEventLoop class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.06.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_EVTLOOP_H_
13 #define _WX_EVTLOOP_H_
14
15 #include "wx/utils.h"
16
17
18 /*
19 NOTE ABOUT wxEventLoopBase::YieldFor LOGIC
20 ------------------------------------------
21
22 The YieldFor() function helps to avoid re-entrancy problems and problems
23 caused by out-of-order event processing
24 (see "wxYield-like problems" and "wxProgressDialog+threading BUG" wx-dev threads).
25
26 The logic behind YieldFor() is simple: it analyzes the queue of the native
27 events generated by the underlying GUI toolkit and picks out and processes
28 only those matching the given mask.
29
30 It's important to note that YieldFor() is used to selectively process the
31 events generated by the NATIVE toolkit.
32 Events syntethized by wxWidgets code or by user code are instead selectively
33 processed thanks to the logic built into wxEvtHandler::ProcessPendingEvents().
34 In fact, when wxEvtHandler::ProcessPendingEvents gets called from inside a
35 YieldFor() call, wxEventLoopBase::IsEventAllowedInsideYield is used to decide
36 if the pending events for that event handler can be processed.
37 If all the pending events associated with that event handler result as "not processable",
38 the event handler "delays" itself calling wxEventLoopBase::DelayPendingEventHandler
39 (so it's moved: m_handlersWithPendingEvents => m_handlersWithPendingDelayedEvents).
40 Last, wxEventLoopBase::ProcessPendingEvents() before exiting moves the delayed
41 event handlers back into the list of handlers with pending events
42 (m_handlersWithPendingDelayedEvents => m_handlersWithPendingEvents) so that
43 a later call to ProcessPendingEvents() (possibly outside the YieldFor() call)
44 will process all pending events as usual.
45 */
46
47 // ----------------------------------------------------------------------------
48 // wxEventLoopBase: interface for wxEventLoop
49 // ----------------------------------------------------------------------------
50
51 class WXDLLIMPEXP_BASE wxEventLoopBase
52 {
53 public:
54 // trivial, but needed (because of wxEventLoopBase) ctor
55 wxEventLoopBase();
56
57 // dtor
58 virtual ~wxEventLoopBase() { }
59
60 // use this to check whether the event loop was successfully created before
61 // using it
62 virtual bool IsOk() const { return true; }
63
64
65 // dispatch&processing
66 // -------------------
67
68 // start the event loop, return the exit code when it is finished
69 virtual int Run() = 0;
70
71 // is this event loop running now?
72 //
73 // notice that even if this event loop hasn't terminated yet but has just
74 // spawned a nested (e.g. modal) event loop, this would return false
75 bool IsRunning() const;
76
77 // exit from the loop with the given exit code
78 virtual void Exit(int rc = 0) = 0;
79
80 // return true if any events are available
81 virtual bool Pending() const = 0;
82
83 // dispatch a single event, return false if we should exit from the loop
84 virtual bool Dispatch() = 0;
85
86 // same as Dispatch() but doesn't wait for longer than the specified (in
87 // ms) timeout, return true if an event was processed, false if we should
88 // exit the loop or -1 if timeout expired
89 virtual int DispatchTimeout(unsigned long timeout) = 0;
90
91 // implement this to wake up the loop: usually done by posting a dummy event
92 // to it (can be called from non main thread)
93 virtual void WakeUp() = 0;
94
95
96 // pending events
97 // --------------
98
99 // process all events in the wxHandlersWithPendingEvents list -- it is necessary
100 // to call this function to process posted events. This happens during each
101 // event loop iteration in GUI mode but if there is no main loop, it may be
102 // also called directly.
103 virtual void ProcessPendingEvents();
104
105 // check if there are pending events on global pending event list
106 bool HasPendingEvents() const;
107
108 // temporary suspends processing of the pending events
109 void SuspendProcessingOfPendingEvents();
110
111 // resume processing of the pending events previously stopped because of a
112 // call to SuspendProcessingOfPendingEvents()
113 void ResumeProcessingOfPendingEvents();
114
115 // called by ~wxEvtHandler to (eventually) remove the handler from the list of
116 // the handlers with pending events
117 void RemovePendingEventHandler(wxEvtHandler* toRemove);
118
119 // adds an event handler to the list of the handlers with pending events
120 void AppendPendingEventHandler(wxEvtHandler* toAppend);
121
122 // moves the event handler from the list of the handlers with pending events
123 //to the list of the handlers with _delayed_ pending events
124 void DelayPendingEventHandler(wxEvtHandler* toDelay);
125
126
127 // idle handling
128 // -------------
129
130 // make sure that idle events are sent again
131 virtual void WakeUpIdle();
132
133 // this virtual function is called when the application
134 // becomes idle and normally just sends wxIdleEvent to all interested
135 // parties
136 //
137 // it should return true if more idle events are needed, false if not
138 virtual bool ProcessIdle();
139
140
141 // Yield-related hooks
142 // -------------------
143
144 // process all currently pending events right now
145 //
146 // it is an error to call Yield() recursively unless the value of
147 // onlyIfNeeded is true
148 //
149 // WARNING: this function is dangerous as it can lead to unexpected
150 // reentrancies (i.e. when called from an event handler it
151 // may result in calling the same event handler again), use
152 // with _extreme_ care or, better, don't use at all!
153 bool Yield(bool onlyIfNeeded = false);
154 virtual bool YieldFor(long eventsToProcess) = 0;
155
156 // returns true if the main thread is inside a Yield() call
157 virtual bool IsYielding() const
158 { return m_isInsideYield; }
159
160 // returns true if events of the given event category should be immediately
161 // processed inside a wxApp::Yield() call or rather should be queued for
162 // later processing by the main event loop
163 virtual bool IsEventAllowedInsideYield(wxEventCategory cat) const
164 { return (m_eventsToProcessInsideYield & cat) != 0; }
165
166 // no SafeYield hooks since it uses wxWindow which is not available when wxUSE_GUI=0
167
168
169 // active loop
170 // -----------
171
172 // return currently active (running) event loop, may be NULL
173 static wxEventLoopBase *GetActive() { return ms_activeLoop; }
174
175 // set currently active (running) event loop
176 static void SetActive(wxEventLoopBase* loop) { ms_activeLoop = loop; }
177
178
179 protected:
180 // this function should be called before the event loop terminates, whether
181 // this happens normally (because of Exit() call) or abnormally (because of
182 // an exception thrown from inside the loop)
183 virtual void OnExit() { }
184
185 // the pointer to currently active loop
186 static wxEventLoopBase *ms_activeLoop;
187
188 // the array of the handlers with pending events which needs to be processed
189 // inside ProcessPendingEvents()
190 wxEvtHandlerArray m_handlersWithPendingEvents;
191
192 // helper array used by ProcessPendingEvents()
193 wxEvtHandlerArray m_handlersWithPendingDelayedEvents;
194
195 #if wxUSE_THREADS
196 // this critical section protects both the lists above
197 wxCriticalSection m_handlersWithPendingEventsLocker;
198 #endif
199
200 // Yield() helpers:
201 bool m_isInsideYield;
202 long m_eventsToProcessInsideYield;
203
204 wxDECLARE_NO_COPY_CLASS(wxEventLoopBase);
205 };
206
207 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
208
209 // this class can be used to implement a standard event loop logic using
210 // Pending() and Dispatch()
211 //
212 // it also handles idle processing automatically
213 class WXDLLIMPEXP_BASE wxEventLoopManual : public wxEventLoopBase
214 {
215 public:
216 wxEventLoopManual();
217
218 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
219 // terminating when Exit() is called
220 virtual int Run();
221
222 // sets the "should exit" flag and wakes up the loop so that it terminates
223 // soon
224 virtual void Exit(int rc = 0);
225
226 protected:
227 // may be overridden to perform some action at the start of each new event
228 // loop iteration
229 virtual void OnNextIteration() { }
230
231
232 // the loop exit code
233 int m_exitcode;
234
235 // should we exit the loop?
236 bool m_shouldExit;
237 };
238
239 #endif // platforms using "manual" loop
240
241 // we're moving away from old m_impl wxEventLoop model as otherwise the user
242 // code doesn't have access to platform-specific wxEventLoop methods and this
243 // can sometimes be very useful (e.g. under MSW this is necessary for
244 // integration with MFC) but currently this is done for MSW only, other ports
245 // should follow a.s.a.p.
246 #if defined(__WXPALMOS__)
247 #include "wx/palmos/evtloop.h"
248 #elif defined(__WXMSW__)
249 #include "wx/msw/evtloop.h"
250 #elif defined(__WXMAC__)
251 #include "wx/osx/evtloop.h"
252 #elif defined(__WXCOCOA__)
253 #include "wx/cocoa/evtloop.h"
254 #elif defined(__WXDFB__)
255 #include "wx/dfb/evtloop.h"
256 #elif defined(__WXGTK20__)
257 #include "wx/gtk/evtloop.h"
258 #else // other platform
259
260 #include "wx/stopwatch.h" // for wxMilliClock_t
261
262 class WXDLLIMPEXP_FWD_CORE wxEventLoopImpl;
263
264 class WXDLLIMPEXP_CORE wxGUIEventLoop : public wxEventLoopBase
265 {
266 public:
267 wxGUIEventLoop() { m_impl = NULL; }
268 virtual ~wxGUIEventLoop();
269
270 virtual int Run();
271 virtual void Exit(int rc = 0);
272 virtual bool Pending() const;
273 virtual bool Dispatch();
274 virtual int DispatchTimeout(unsigned long timeout)
275 {
276 // TODO: this is, of course, horribly inefficient and a proper wait with
277 // timeout should be implemented for all ports natively...
278 const wxMilliClock_t timeEnd = wxGetLocalTimeMillis() + timeout;
279 for ( ;; )
280 {
281 if ( Pending() )
282 return Dispatch();
283
284 if ( wxGetLocalTimeMillis() >= timeEnd )
285 return -1;
286 }
287 }
288 virtual void WakeUp() { }
289 virtual bool YieldFor(long eventsToProcess);
290
291 protected:
292 // the pointer to the port specific implementation class
293 wxEventLoopImpl *m_impl;
294
295 wxDECLARE_NO_COPY_CLASS(wxGUIEventLoop);
296 };
297
298 #endif // platforms
299
300 // also include the header defining wxConsoleEventLoop for Unix systems
301 #if defined(__UNIX__)
302 #include "wx/unix/evtloop.h"
303 #endif
304
305 // we use a class rather than a typedef because wxEventLoop is forward-declared
306 // in many places
307 #if wxUSE_GUI
308 class wxEventLoop : public wxGUIEventLoop { };
309 #else // !GUI
310 // we can't define wxEventLoop differently in GUI and base libraries so use
311 // a #define to still allow writing wxEventLoop in the user code
312 #if wxUSE_CONSOLE_EVENTLOOP && (defined(__WXMSW__) || defined(__UNIX__))
313 #define wxEventLoop wxConsoleEventLoop
314 #else // we still must define it somehow for the code below...
315 #define wxEventLoop wxEventLoopBase
316 #endif
317 #endif
318
319 inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
320
321 #if wxUSE_GUI
322 // ----------------------------------------------------------------------------
323 // wxModalEventLoop
324 // ----------------------------------------------------------------------------
325
326 // this is a naive generic implementation which uses wxWindowDisabler to
327 // implement modality, we will surely need platform-specific implementations
328 // too, this generic implementation is here only temporarily to see how it
329 // works
330 class WXDLLIMPEXP_CORE wxModalEventLoop : public wxGUIEventLoop
331 {
332 public:
333 wxModalEventLoop(wxWindow *winModal)
334 {
335 m_windowDisabler = new wxWindowDisabler(winModal);
336 }
337
338 protected:
339 virtual void OnExit()
340 {
341 delete m_windowDisabler;
342 m_windowDisabler = NULL;
343
344 wxGUIEventLoop::OnExit();
345 }
346
347 private:
348 wxWindowDisabler *m_windowDisabler;
349 };
350
351 #endif //wxUSE_GUI
352
353 // ----------------------------------------------------------------------------
354 // wxEventLoopActivator: helper class for wxEventLoop implementations
355 // ----------------------------------------------------------------------------
356
357 // this object sets the wxEventLoop given to the ctor as the currently active
358 // one and unsets it in its dtor, this is especially useful in presence of
359 // exceptions but is more tidy even when we don't use them
360 class wxEventLoopActivator
361 {
362 public:
363 wxEventLoopActivator(wxEventLoopBase *evtLoop)
364 {
365 m_evtLoopOld = wxEventLoopBase::GetActive();
366 wxEventLoopBase::SetActive(evtLoop);
367 }
368
369 ~wxEventLoopActivator()
370 {
371 // restore the previously active event loop
372 wxEventLoopBase::SetActive(m_evtLoopOld);
373 }
374
375 private:
376 wxEventLoopBase *m_evtLoopOld;
377 };
378
379 #if wxUSE_CONSOLE_EVENTLOOP
380
381 class wxEventLoopGuarantor
382 {
383 public:
384 wxEventLoopGuarantor()
385 {
386 m_evtLoopNew = NULL;
387 if (!wxEventLoop::GetActive())
388 {
389 m_evtLoopNew = new wxEventLoop;
390 wxEventLoop::SetActive(m_evtLoopNew);
391 }
392 }
393
394 ~wxEventLoopGuarantor()
395 {
396 if (m_evtLoopNew)
397 {
398 wxEventLoop::SetActive(NULL);
399 delete m_evtLoopNew;
400 }
401 }
402
403 private:
404 wxEventLoop *m_evtLoopNew;
405 };
406
407 #endif // wxUSE_CONSOLE_EVENTLOOP
408
409 #endif // _WX_EVTLOOP_H_