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 // wxEventLoop: a GUI event loop
21 // ----------------------------------------------------------------------------
23 class WXDLLEXPORT wxEventLoopBase
26 // trivial, but needed (because of wxEventLoopBase) ctor
30 virtual ~wxEventLoopBase() { }
32 // start the event loop, return the exit code when it is finished
33 virtual int Run() = 0;
35 // exit from the loop with the given exit code
36 virtual void Exit(int rc
= 0) = 0;
38 // return true if any events are available
39 virtual bool Pending() const = 0;
41 // dispatch a single event, return false if we should exit from the loop
42 virtual bool Dispatch() = 0;
44 // return currently active (running) event loop, may be NULL
45 static wxEventLoop
*GetActive() { return ms_activeLoop
; }
47 // set currently active (running) event loop
48 static void SetActive(wxEventLoop
* loop
) { ms_activeLoop
= loop
; }
50 // is this event loop running now?
52 // notice that even if this event loop hasn't terminated yet but has just
53 // spawned a nested (e.g. modal) event loop, this would return false
54 bool IsRunning() const;
57 // this function should be called before the event loop terminates, whether
58 // this happens normally (because of Exit() call) or abnormally (because of
59 // an exception thrown from inside the loop)
60 virtual void OnExit() { }
63 // the pointer to currently active loop
64 static wxEventLoop
*ms_activeLoop
;
66 DECLARE_NO_COPY_CLASS(wxEventLoopBase
)
69 #if defined(__WXMSW__) || defined(__WXMAC__)
71 // this class can be used to implement a standard event loop logic using
72 // Pending() and Dispatch()
74 // it also handles idle processing automatically
75 class WXDLLEXPORT wxEventLoopManual
: public wxEventLoopBase
80 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
81 // terminating when Exit() is called
84 // sets the "should exit" flag and wakes up the loop so that it terminates
86 virtual void Exit(int rc
);
89 // implement this to wake up the loop: usually done by posting a dummy event
90 // to it (called from Exit())
91 virtual void WakeUp() = 0;
93 // may be overridden to perform some action at the start of each new event
95 virtual void OnNextIteration() { }
101 // should we exit the loop?
105 #endif // platforms using "manual" loop
107 // we're moving away from old m_impl wxEventLoop model as otherwise the user
108 // code doesn't have access to platform-specific wxEventLoop methods and this
109 // can sometimes be very useful (e.g. under MSW this is necessary for
110 // integration with MFC) but currently this is done for MSW only, other ports
111 // should follow a.s.a.p.
112 #if defined(__WXPALMOS__)
113 #include "wx/palmos/evtloop.h"
114 #elif defined(__WXMSW__)
115 #include "wx/msw/evtloop.h"
116 #elif defined(__WXMAC__)
117 #include "wx/mac/evtloop.h"
118 #else // other platform
120 class WXDLLEXPORT wxEventLoopImpl
;
122 class WXDLLEXPORT wxEventLoop
: public wxEventLoopBase
125 wxEventLoop() { m_impl
= NULL
; }
126 virtual ~wxEventLoop();
129 virtual void Exit(int rc
= 0);
130 virtual bool Pending() const;
131 virtual bool Dispatch();
134 // the pointer to the port specific implementation class
135 wxEventLoopImpl
*m_impl
;
137 DECLARE_NO_COPY_CLASS(wxEventLoop
)
142 inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 // this is a naive generic implementation which uses wxWindowDisabler to
149 // implement modality, we will surely need platform-specific implementations
150 // too, this generic implementation is here only temporarily to see how it
152 class WXDLLEXPORT wxModalEventLoop
: public wxEventLoop
155 wxModalEventLoop(wxWindow
*winModal
)
157 m_windowDisabler
= new wxWindowDisabler(winModal
);
161 virtual void OnExit()
163 delete m_windowDisabler
;
164 m_windowDisabler
= NULL
;
166 wxEventLoop::OnExit();
170 wxWindowDisabler
*m_windowDisabler
;
173 // ----------------------------------------------------------------------------
174 // wxEventLoopActivator: helper class for wxEventLoop implementations
175 // ----------------------------------------------------------------------------
177 // this object sets the wxEventLoop given to the ctor as the currently active
178 // one and unsets it in its dtor, this is especially useful in presence of
179 // exceptions but is more tidy even when we don't use them
180 class wxEventLoopActivator
183 wxEventLoopActivator(wxEventLoop
*evtLoop
)
185 m_evtLoopOld
= wxEventLoop::GetActive();
186 wxEventLoop::SetActive(evtLoop
);
189 ~wxEventLoopActivator()
191 // restore the previously active event loop
192 wxEventLoop::SetActive(m_evtLoopOld
);
196 wxEventLoop
*m_evtLoopOld
;
199 #endif // _WX_EVTLOOP_H_