]> git.saurik.com Git - wxWidgets.git/blame - include/wx/evtloop.h
rename wxRendererNative::DrawRadioButton() to DrawOptionButton() to avoid conflict...
[wxWidgets.git] / include / wx / evtloop.h
CommitLineData
3808e191
JS
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>
65571936 9// Licence: wxWindows licence
3808e191
JS
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_EVTLOOP_H_
13#define _WX_EVTLOOP_H_
14
752464f9
VS
15#include "wx/utils.h"
16
3808e191 17// ----------------------------------------------------------------------------
b46b1d59 18// wxEventLoopBase: interface for wxEventLoop
3808e191
JS
19// ----------------------------------------------------------------------------
20
b46b1d59 21class WXDLLIMPEXP_BASE wxEventLoopBase
3808e191
JS
22{
23public:
3754265e
VZ
24 // trivial, but needed (because of wxEventLoopBase) ctor
25 wxEventLoopBase() { }
3808e191
JS
26
27 // dtor
3754265e 28 virtual ~wxEventLoopBase() { }
3808e191 29
b46b1d59
VZ
30 // use this to check whether the event loop was successfully created before
31 // using it
32 virtual bool IsOk() const { return true; }
33
9af42efd 34
3808e191 35 // start the event loop, return the exit code when it is finished
3754265e 36 virtual int Run() = 0;
3808e191
JS
37
38 // exit from the loop with the given exit code
3754265e 39 virtual void Exit(int rc = 0) = 0;
3808e191 40
1a18887b 41 // return true if any events are available
3754265e 42 virtual bool Pending() const = 0;
3808e191 43
1a18887b 44 // dispatch a single event, return false if we should exit from the loop
3754265e 45 virtual bool Dispatch() = 0;
3808e191 46
9af42efd
VZ
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;
51
52
b9f246f7 53 // return currently active (running) event loop, may be NULL
2ddff00c 54 static wxEventLoopBase *GetActive() { return ms_activeLoop; }
3808e191 55
df0e1b64 56 // set currently active (running) event loop
2ddff00c 57 static void SetActive(wxEventLoopBase* loop) { ms_activeLoop = loop; }
df0e1b64 58
77fb1a02
VZ
59 // is this event loop running now?
60 //
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;
64
b46b1d59
VZ
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;
68
3808e191 69protected:
4300caa7
VZ
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() { }
74
75
b9f246f7 76 // the pointer to currently active loop
2ddff00c 77 static wxEventLoopBase *ms_activeLoop;
22f3361e 78
3754265e
VZ
79 DECLARE_NO_COPY_CLASS(wxEventLoopBase)
80};
81
b46b1d59 82#if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
c8026dea
VZ
83
84// this class can be used to implement a standard event loop logic using
85// Pending() and Dispatch()
86//
87// it also handles idle processing automatically
b46b1d59 88class WXDLLIMPEXP_BASE wxEventLoopManual : public wxEventLoopBase
c8026dea
VZ
89{
90public:
91 wxEventLoopManual();
92
93 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
94 // terminating when Exit() is called
95 virtual int Run();
96
97 // sets the "should exit" flag and wakes up the loop so that it terminates
98 // soon
5b87e74a 99 virtual void Exit(int rc = 0);
c8026dea
VZ
100
101protected:
c8026dea
VZ
102 // may be overridden to perform some action at the start of each new event
103 // loop iteration
104 virtual void OnNextIteration() { }
105
106
107 // the loop exit code
108 int m_exitcode;
109
110 // should we exit the loop?
111 bool m_shouldExit;
112};
113
114#endif // platforms using "manual" loop
115
3754265e
VZ
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.
4055ed82 121#if defined(__WXPALMOS__)
ffecfa5a
JS
122 #include "wx/palmos/evtloop.h"
123#elif defined(__WXMSW__)
3754265e 124 #include "wx/msw/evtloop.h"
4d90072c 125#elif defined(__WXMAC__)
ef0e9220 126 #include "wx/osx/evtloop.h"
1df4b194
VZ
127#elif defined(__WXCOCOA__)
128 #include "wx/cocoa/evtloop.h"
b3c86150
VS
129#elif defined(__WXDFB__)
130 #include "wx/dfb/evtloop.h"
564c7fc4
VZ
131#elif defined(__WXGTK20__)
132 #include "wx/gtk/evtloop.h"
4d90072c 133#else // other platform
3754265e 134
9c26672d 135#include "wx/stopwatch.h" // for wxMilliClock_t
9af42efd 136
b5dbe15d 137class WXDLLIMPEXP_FWD_CORE wxEventLoopImpl;
3754265e 138
53a2db12 139class WXDLLIMPEXP_CORE wxGUIEventLoop : public wxEventLoopBase
3754265e
VZ
140{
141public:
b46b1d59
VZ
142 wxGUIEventLoop() { m_impl = NULL; }
143 virtual ~wxGUIEventLoop();
1a18887b 144
3754265e
VZ
145 virtual int Run();
146 virtual void Exit(int rc = 0);
147 virtual bool Pending() const;
148 virtual bool Dispatch();
9c26672d
VZ
149 virtual int DispatchTimeout(unsigned long timeout)
150 {
151 // TODO: this is, of course, horribly inefficient and a proper wait with
152 // timeout should be implemented for all ports natively...
153 const wxMilliClock_t timeEnd = wxGetLocalTimeMillis() + timeout;
154 for ( ;; )
155 {
156 if ( Pending() )
157 return Dispatch();
158
159 if ( wxGetLocalTimeMillis() >= timeEnd )
160 return -1;
161 }
162 }
b46b1d59 163 virtual void WakeUp() { }
3754265e
VZ
164
165protected:
4300caa7
VZ
166 // the pointer to the port specific implementation class
167 wxEventLoopImpl *m_impl;
168
b46b1d59 169 DECLARE_NO_COPY_CLASS(wxGUIEventLoop)
4d90072c 170};
3754265e 171
4d90072c 172#endif // platforms
3808e191 173
b46b1d59
VZ
174// also include the header defining wxConsoleEventLoop for Unix systems
175#if defined(__UNIX__)
176 #include "wx/unix/evtloop.h"
177#endif
178
2ddff00c
VZ
179// we use a class rather than a typedef because wxEventLoop is forward-declared
180// in many places
b46b1d59 181#if wxUSE_GUI
2ddff00c
VZ
182 class wxEventLoop : public wxGUIEventLoop { };
183#else // !GUI
184 // we can't define wxEventLoop differently in GUI and base libraries so use
185 // a #define to still allow writing wxEventLoop in the user code
1a7cfc94 186 #if wxUSE_CONSOLE_EVENTLOOP && (defined(__WXMSW__) || defined(__UNIX__))
2ddff00c
VZ
187 #define wxEventLoop wxConsoleEventLoop
188 #else // we still must define it somehow for the code below...
189 #define wxEventLoop wxEventLoopBase
190 #endif
b46b1d59
VZ
191#endif
192
77fb1a02
VZ
193inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
194
b46b1d59 195#if wxUSE_GUI
4300caa7
VZ
196// ----------------------------------------------------------------------------
197// wxModalEventLoop
198// ----------------------------------------------------------------------------
199
200// this is a naive generic implementation which uses wxWindowDisabler to
201// implement modality, we will surely need platform-specific implementations
202// too, this generic implementation is here only temporarily to see how it
203// works
53a2db12 204class WXDLLIMPEXP_CORE wxModalEventLoop : public wxGUIEventLoop
4300caa7
VZ
205{
206public:
207 wxModalEventLoop(wxWindow *winModal)
208 {
209 m_windowDisabler = new wxWindowDisabler(winModal);
210 }
211
212protected:
213 virtual void OnExit()
214 {
215 delete m_windowDisabler;
216 m_windowDisabler = NULL;
217
b46b1d59 218 wxGUIEventLoop::OnExit();
4300caa7
VZ
219 }
220
221private:
222 wxWindowDisabler *m_windowDisabler;
223};
224
b46b1d59
VZ
225#endif //wxUSE_GUI
226
77fb1a02
VZ
227// ----------------------------------------------------------------------------
228// wxEventLoopActivator: helper class for wxEventLoop implementations
229// ----------------------------------------------------------------------------
230
231// this object sets the wxEventLoop given to the ctor as the currently active
232// one and unsets it in its dtor, this is especially useful in presence of
233// exceptions but is more tidy even when we don't use them
234class wxEventLoopActivator
235{
236public:
b46b1d59 237 wxEventLoopActivator(wxEventLoopBase *evtLoop)
77fb1a02 238 {
b46b1d59 239 m_evtLoopOld = wxEventLoopBase::GetActive();
2ddff00c 240 wxEventLoopBase::SetActive(evtLoop);
77fb1a02
VZ
241 }
242
243 ~wxEventLoopActivator()
244 {
245 // restore the previously active event loop
b46b1d59 246 wxEventLoopBase::SetActive(m_evtLoopOld);
77fb1a02
VZ
247 }
248
249private:
2ddff00c 250 wxEventLoopBase *m_evtLoopOld;
77fb1a02
VZ
251};
252
1a7cfc94
VZ
253#if wxUSE_CONSOLE_EVENTLOOP
254
f212e222
SN
255class wxEventLoopGuarantor
256{
257public:
258 wxEventLoopGuarantor()
259 {
260 m_evtLoopNew = NULL;
261 if (!wxEventLoop::GetActive())
262 {
263 m_evtLoopNew = new wxEventLoop;
264 wxEventLoop::SetActive(m_evtLoopNew);
265 }
266 }
267
268 ~wxEventLoopGuarantor()
269 {
270 if (m_evtLoopNew)
271 {
272 wxEventLoop::SetActive(NULL);
273 delete m_evtLoopNew;
274 }
275 }
276
277private:
278 wxEventLoop *m_evtLoopNew;
279};
280
1a7cfc94
VZ
281#endif // wxUSE_CONSOLE_EVENTLOOP
282
3808e191 283#endif // _WX_EVTLOOP_H_