1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: declares wxEventLoop class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_EVTLOOP_H_
13 #define _WX_EVTLOOP_H_
17 // ----------------------------------------------------------------------------
18 // wxEventLoopBase: interface for wxEventLoop
19 // ----------------------------------------------------------------------------
21 class WXDLLIMPEXP_BASE wxEventLoopBase
24 // trivial, but needed (because of wxEventLoopBase) ctor
28 virtual ~wxEventLoopBase() { }
30 // use this to check whether the event loop was successfully created before
32 virtual bool IsOk() const { return true; }
35 // start the event loop, return the exit code when it is finished
36 virtual int Run() = 0;
38 // exit from the loop with the given exit code
39 virtual void Exit(int rc
= 0) = 0;
41 // return true if any events are available
42 virtual bool Pending() const = 0;
44 // dispatch a single event, return false if we should exit from the loop
45 virtual bool Dispatch() = 0;
47 // same as Dispatch() but doesn't wait for longer than the specified (in
48 // ms) timeout, return true if an event was processed, false if we should
49 // exit the loop or -1 if timeout expired
50 virtual int DispatchTimeout(unsigned long timeout
) = 0;
53 // return currently active (running) event loop, may be NULL
54 static wxEventLoopBase
*GetActive() { return ms_activeLoop
; }
56 // set currently active (running) event loop
57 static void SetActive(wxEventLoopBase
* loop
) { ms_activeLoop
= loop
; }
59 // is this event loop running now?
61 // notice that even if this event loop hasn't terminated yet but has just
62 // spawned a nested (e.g. modal) event loop, this would return false
63 bool IsRunning() const;
65 // implement this to wake up the loop: usually done by posting a dummy event
66 // to it (can be called from non main thread)
67 virtual void WakeUp() = 0;
70 // this function should be called before the event loop terminates, whether
71 // this happens normally (because of Exit() call) or abnormally (because of
72 // an exception thrown from inside the loop)
73 virtual void OnExit() { }
76 // the pointer to currently active loop
77 static wxEventLoopBase
*ms_activeLoop
;
79 DECLARE_NO_COPY_CLASS(wxEventLoopBase
)
82 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
84 // this class can be used to implement a standard event loop logic using
85 // Pending() and Dispatch()
87 // it also handles idle processing automatically
88 class WXDLLIMPEXP_BASE wxEventLoopManual
: public wxEventLoopBase
93 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
94 // terminating when Exit() is called
97 // sets the "should exit" flag and wakes up the loop so that it terminates
99 virtual void Exit(int rc
= 0);
102 // may be overridden to perform some action at the start of each new event
104 virtual void OnNextIteration() { }
107 // the loop exit code
110 // should we exit the loop?
114 #endif // platforms using "manual" loop
116 // we're moving away from old m_impl wxEventLoop model as otherwise the user
117 // code doesn't have access to platform-specific wxEventLoop methods and this
118 // can sometimes be very useful (e.g. under MSW this is necessary for
119 // integration with MFC) but currently this is done for MSW only, other ports
120 // should follow a.s.a.p.
121 #if defined(__WXPALMOS__)
122 #include "wx/palmos/evtloop.h"
123 #elif defined(__WXMSW__)
124 #include "wx/msw/evtloop.h"
125 #elif defined(__WXMAC__)
126 #include "wx/osx/evtloop.h"
127 #elif defined(__WXDFB__)
128 #include "wx/dfb/evtloop.h"
129 #else // other platform
131 #define wxNEEDS_GENERIC_DISPATCH_TIMEOUT
133 class WXDLLIMPEXP_FWD_CORE wxEventLoopImpl
;
135 class WXDLLIMPEXP_CORE wxGUIEventLoop
: public wxEventLoopBase
138 wxGUIEventLoop() { m_impl
= NULL
; }
139 virtual ~wxGUIEventLoop();
142 virtual void Exit(int rc
= 0);
143 virtual bool Pending() const;
144 virtual bool Dispatch();
145 virtual int DispatchTimeout(unsigned long timeout
);
146 virtual void WakeUp() { }
149 // the pointer to the port specific implementation class
150 wxEventLoopImpl
*m_impl
;
152 DECLARE_NO_COPY_CLASS(wxGUIEventLoop
)
157 // also include the header defining wxConsoleEventLoop for Unix systems
158 #if defined(__UNIX__)
159 #include "wx/unix/evtloop.h"
162 // we use a class rather than a typedef because wxEventLoop is forward-declared
165 class wxEventLoop
: public wxGUIEventLoop
{ };
167 // we can't define wxEventLoop differently in GUI and base libraries so use
168 // a #define to still allow writing wxEventLoop in the user code
169 #if wxUSE_CONSOLE_EVENTLOOP && (defined(__WXMSW__) || defined(__UNIX__))
170 #define wxEventLoop wxConsoleEventLoop
171 #else // we still must define it somehow for the code below...
172 #define wxEventLoop wxEventLoopBase
176 inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 // this is a naive generic implementation which uses wxWindowDisabler to
184 // implement modality, we will surely need platform-specific implementations
185 // too, this generic implementation is here only temporarily to see how it
187 class WXDLLIMPEXP_CORE wxModalEventLoop
: public wxGUIEventLoop
190 wxModalEventLoop(wxWindow
*winModal
)
192 m_windowDisabler
= new wxWindowDisabler(winModal
);
196 virtual void OnExit()
198 delete m_windowDisabler
;
199 m_windowDisabler
= NULL
;
201 wxGUIEventLoop::OnExit();
205 wxWindowDisabler
*m_windowDisabler
;
210 // ----------------------------------------------------------------------------
211 // wxEventLoopActivator: helper class for wxEventLoop implementations
212 // ----------------------------------------------------------------------------
214 // this object sets the wxEventLoop given to the ctor as the currently active
215 // one and unsets it in its dtor, this is especially useful in presence of
216 // exceptions but is more tidy even when we don't use them
217 class wxEventLoopActivator
220 wxEventLoopActivator(wxEventLoopBase
*evtLoop
)
222 m_evtLoopOld
= wxEventLoopBase::GetActive();
223 wxEventLoopBase::SetActive(evtLoop
);
226 ~wxEventLoopActivator()
228 // restore the previously active event loop
229 wxEventLoopBase::SetActive(m_evtLoopOld
);
233 wxEventLoopBase
*m_evtLoopOld
;
236 #if wxUSE_CONSOLE_EVENTLOOP
238 class wxEventLoopGuarantor
241 wxEventLoopGuarantor()
244 if (!wxEventLoop::GetActive())
246 m_evtLoopNew
= new wxEventLoop
;
247 wxEventLoop::SetActive(m_evtLoopNew
);
251 ~wxEventLoopGuarantor()
255 wxEventLoop::SetActive(NULL
);
261 wxEventLoop
*m_evtLoopNew
;
264 #endif // wxUSE_CONSOLE_EVENTLOOP
266 #endif // _WX_EVTLOOP_H_