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 class WXDLLEXPORT wxEventLoop
;
19 // ----------------------------------------------------------------------------
20 // wxEventLoopBase: interface for wxEventLoop
21 // ----------------------------------------------------------------------------
23 class WXDLLIMPEXP_BASE wxEventLoopBase
26 // trivial, but needed (because of wxEventLoopBase) ctor
30 virtual ~wxEventLoopBase() { }
32 // use this to check whether the event loop was successfully created before
34 virtual bool IsOk() const { return true; }
36 // start the event loop, return the exit code when it is finished
37 virtual int Run() = 0;
39 // exit from the loop with the given exit code
40 virtual void Exit(int rc
= 0) = 0;
42 // return true if any events are available
43 virtual bool Pending() const = 0;
45 // dispatch a single event, return false if we should exit from the loop
46 virtual bool Dispatch() = 0;
48 // return currently active (running) event loop, may be NULL
49 static wxEventLoop
*GetActive() { return ms_activeLoop
; }
51 // set currently active (running) event loop
52 static void SetActive(wxEventLoop
* loop
) { ms_activeLoop
= loop
; }
54 // is this event loop running now?
56 // notice that even if this event loop hasn't terminated yet but has just
57 // spawned a nested (e.g. modal) event loop, this would return false
58 bool IsRunning() const;
60 // implement this to wake up the loop: usually done by posting a dummy event
61 // to it (can be called from non main thread)
62 virtual void WakeUp() = 0;
65 // this function should be called before the event loop terminates, whether
66 // this happens normally (because of Exit() call) or abnormally (because of
67 // an exception thrown from inside the loop)
68 virtual void OnExit() { }
71 // the pointer to currently active loop
72 static wxEventLoop
*ms_activeLoop
;
74 DECLARE_NO_COPY_CLASS(wxEventLoopBase
)
77 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
79 // this class can be used to implement a standard event loop logic using
80 // Pending() and Dispatch()
82 // it also handles idle processing automatically
83 class WXDLLIMPEXP_BASE wxEventLoopManual
: public wxEventLoopBase
88 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
89 // terminating when Exit() is called
92 // sets the "should exit" flag and wakes up the loop so that it terminates
94 virtual void Exit(int rc
= 0);
97 // may be overridden to perform some action at the start of each new event
99 virtual void OnNextIteration() { }
102 // the loop exit code
105 // should we exit the loop?
109 #endif // platforms using "manual" loop
111 // we're moving away from old m_impl wxEventLoop model as otherwise the user
112 // code doesn't have access to platform-specific wxEventLoop methods and this
113 // can sometimes be very useful (e.g. under MSW this is necessary for
114 // integration with MFC) but currently this is done for MSW only, other ports
115 // should follow a.s.a.p.
116 #if defined(__WXPALMOS__)
117 #include "wx/palmos/evtloop.h"
118 #elif defined(__WXMSW__)
119 #include "wx/msw/evtloop.h"
120 #elif defined(__WXMAC__)
121 #include "wx/mac/evtloop.h"
122 #elif defined(__WXDFB__)
123 #include "wx/dfb/evtloop.h"
124 #else // other platform
126 class WXDLLEXPORT wxEventLoopImpl
;
128 class WXDLLEXPORT wxGUIEventLoop
: public wxEventLoopBase
131 wxGUIEventLoop() { m_impl
= NULL
; }
132 virtual ~wxGUIEventLoop();
135 virtual void Exit(int rc
= 0);
136 virtual bool Pending() const;
137 virtual bool Dispatch();
138 virtual void WakeUp() { }
141 // the pointer to the port specific implementation class
142 wxEventLoopImpl
*m_impl
;
144 DECLARE_NO_COPY_CLASS(wxGUIEventLoop
)
149 // also include the header defining wxConsoleEventLoop for Unix systems
150 #if defined(__UNIX__)
151 #include "wx/unix/evtloop.h"
154 // cannot use typedef because wxEventLoop is forward-declared in many places
156 class wxEventLoop
: public wxGUIEventLoop
{ };
157 #elif defined(__WXMSW__) || defined(__UNIX__)
158 class wxEventLoop
: public wxConsoleEventLoop
{ };
159 #else // we still must define it somehow for the code below...
160 class wxEventLoop
: public wxEventLoopBase
{ };
163 inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 // this is a naive generic implementation which uses wxWindowDisabler to
171 // implement modality, we will surely need platform-specific implementations
172 // too, this generic implementation is here only temporarily to see how it
174 class WXDLLEXPORT wxModalEventLoop
: public wxGUIEventLoop
177 wxModalEventLoop(wxWindow
*winModal
)
179 m_windowDisabler
= new wxWindowDisabler(winModal
);
183 virtual void OnExit()
185 delete m_windowDisabler
;
186 m_windowDisabler
= NULL
;
188 wxGUIEventLoop::OnExit();
192 wxWindowDisabler
*m_windowDisabler
;
197 // ----------------------------------------------------------------------------
198 // wxEventLoopActivator: helper class for wxEventLoop implementations
199 // ----------------------------------------------------------------------------
201 // this object sets the wxEventLoop given to the ctor as the currently active
202 // one and unsets it in its dtor, this is especially useful in presence of
203 // exceptions but is more tidy even when we don't use them
204 class wxEventLoopActivator
207 wxEventLoopActivator(wxEventLoopBase
*evtLoop
)
209 m_evtLoopOld
= wxEventLoopBase::GetActive();
210 wxEventLoopBase::SetActive(wx_static_cast(wxEventLoop
*, evtLoop
));
213 ~wxEventLoopActivator()
215 // restore the previously active event loop
216 wxEventLoopBase::SetActive(m_evtLoopOld
);
220 wxEventLoop
*m_evtLoopOld
;
223 #endif // _WX_EVTLOOP_H_