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; }
34 // start the event loop, return the exit code when it is finished
35 virtual int Run() = 0;
37 // exit from the loop with the given exit code
38 virtual void Exit(int rc
= 0) = 0;
40 // return true if any events are available
41 virtual bool Pending() const = 0;
43 // dispatch a single event, return false if we should exit from the loop
44 virtual bool Dispatch() = 0;
46 // return currently active (running) event loop, may be NULL
47 static wxEventLoopBase
*GetActive() { return ms_activeLoop
; }
49 // set currently active (running) event loop
50 static void SetActive(wxEventLoopBase
* loop
) { ms_activeLoop
= loop
; }
52 // is this event loop running now?
54 // notice that even if this event loop hasn't terminated yet but has just
55 // spawned a nested (e.g. modal) event loop, this would return false
56 bool IsRunning() const;
58 // implement this to wake up the loop: usually done by posting a dummy event
59 // to it (can be called from non main thread)
60 virtual void WakeUp() = 0;
63 // this function should be called before the event loop terminates, whether
64 // this happens normally (because of Exit() call) or abnormally (because of
65 // an exception thrown from inside the loop)
66 virtual void OnExit() { }
69 // the pointer to currently active loop
70 static wxEventLoopBase
*ms_activeLoop
;
72 DECLARE_NO_COPY_CLASS(wxEventLoopBase
)
75 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
77 // this class can be used to implement a standard event loop logic using
78 // Pending() and Dispatch()
80 // it also handles idle processing automatically
81 class WXDLLIMPEXP_BASE wxEventLoopManual
: public wxEventLoopBase
86 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
87 // terminating when Exit() is called
90 // sets the "should exit" flag and wakes up the loop so that it terminates
92 virtual void Exit(int rc
= 0);
95 // may be overridden to perform some action at the start of each new event
97 virtual void OnNextIteration() { }
100 // the loop exit code
103 // should we exit the loop?
107 #endif // platforms using "manual" loop
109 // we're moving away from old m_impl wxEventLoop model as otherwise the user
110 // code doesn't have access to platform-specific wxEventLoop methods and this
111 // can sometimes be very useful (e.g. under MSW this is necessary for
112 // integration with MFC) but currently this is done for MSW only, other ports
113 // should follow a.s.a.p.
114 #if defined(__WXPALMOS__)
115 #include "wx/palmos/evtloop.h"
116 #elif defined(__WXMSW__)
117 #include "wx/msw/evtloop.h"
118 #elif defined(__WXMAC__)
119 #include "wx/mac/evtloop.h"
120 #elif defined(__WXDFB__)
121 #include "wx/dfb/evtloop.h"
122 #else // other platform
124 class WXDLLIMPEXP_FWD_CORE wxEventLoopImpl
;
126 class WXDLLIMPEXP_CORE wxGUIEventLoop
: public wxEventLoopBase
129 wxGUIEventLoop() { m_impl
= NULL
; }
130 virtual ~wxGUIEventLoop();
133 virtual void Exit(int rc
= 0);
134 virtual bool Pending() const;
135 virtual bool Dispatch();
136 virtual void WakeUp() { }
139 // the pointer to the port specific implementation class
140 wxEventLoopImpl
*m_impl
;
142 DECLARE_NO_COPY_CLASS(wxGUIEventLoop
)
147 // also include the header defining wxConsoleEventLoop for Unix systems
148 #if defined(__UNIX__)
149 #include "wx/unix/evtloop.h"
152 // we use a class rather than a typedef because wxEventLoop is forward-declared
155 class wxEventLoop
: public wxGUIEventLoop
{ };
157 // we can't define wxEventLoop differently in GUI and base libraries so use
158 // a #define to still allow writing wxEventLoop in the user code
159 #if defined(__WXMSW__) || defined(__UNIX__)
160 #define wxEventLoop wxConsoleEventLoop
161 #else // we still must define it somehow for the code below...
162 #define wxEventLoop wxEventLoopBase
166 inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 // this is a naive generic implementation which uses wxWindowDisabler to
174 // implement modality, we will surely need platform-specific implementations
175 // too, this generic implementation is here only temporarily to see how it
177 class WXDLLIMPEXP_CORE wxModalEventLoop
: public wxGUIEventLoop
180 wxModalEventLoop(wxWindow
*winModal
)
182 m_windowDisabler
= new wxWindowDisabler(winModal
);
186 virtual void OnExit()
188 delete m_windowDisabler
;
189 m_windowDisabler
= NULL
;
191 wxGUIEventLoop::OnExit();
195 wxWindowDisabler
*m_windowDisabler
;
200 // ----------------------------------------------------------------------------
201 // wxEventLoopActivator: helper class for wxEventLoop implementations
202 // ----------------------------------------------------------------------------
204 // this object sets the wxEventLoop given to the ctor as the currently active
205 // one and unsets it in its dtor, this is especially useful in presence of
206 // exceptions but is more tidy even when we don't use them
207 class wxEventLoopActivator
210 wxEventLoopActivator(wxEventLoopBase
*evtLoop
)
212 m_evtLoopOld
= wxEventLoopBase::GetActive();
213 wxEventLoopBase::SetActive(evtLoop
);
216 ~wxEventLoopActivator()
218 // restore the previously active event loop
219 wxEventLoopBase::SetActive(m_evtLoopOld
);
223 wxEventLoopBase
*m_evtLoopOld
;
226 class wxEventLoopGuarantor
229 wxEventLoopGuarantor()
232 if (!wxEventLoop::GetActive())
234 m_evtLoopNew
= new wxEventLoop
;
235 wxEventLoop::SetActive(m_evtLoopNew
);
239 ~wxEventLoopGuarantor()
243 wxEventLoop::SetActive(NULL
);
249 wxEventLoop
*m_evtLoopNew
;
252 #endif // _WX_EVTLOOP_H_