]> git.saurik.com Git - wxWidgets.git/blame - include/wx/evtloop.h
make wxSocketImplUnix inherit from wxFDIOHandler as they're used for almost the same...
[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
3808e191 34 // start the event loop, return the exit code when it is finished
3754265e 35 virtual int Run() = 0;
3808e191
JS
36
37 // exit from the loop with the given exit code
3754265e 38 virtual void Exit(int rc = 0) = 0;
3808e191 39
1a18887b 40 // return true if any events are available
3754265e 41 virtual bool Pending() const = 0;
3808e191 42
1a18887b 43 // dispatch a single event, return false if we should exit from the loop
3754265e 44 virtual bool Dispatch() = 0;
3808e191 45
b9f246f7 46 // return currently active (running) event loop, may be NULL
2ddff00c 47 static wxEventLoopBase *GetActive() { return ms_activeLoop; }
3808e191 48
df0e1b64 49 // set currently active (running) event loop
2ddff00c 50 static void SetActive(wxEventLoopBase* loop) { ms_activeLoop = loop; }
df0e1b64 51
77fb1a02
VZ
52 // is this event loop running now?
53 //
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;
57
b46b1d59
VZ
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;
61
3808e191 62protected:
4300caa7
VZ
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() { }
67
68
b9f246f7 69 // the pointer to currently active loop
2ddff00c 70 static wxEventLoopBase *ms_activeLoop;
22f3361e 71
3754265e
VZ
72 DECLARE_NO_COPY_CLASS(wxEventLoopBase)
73};
74
b46b1d59 75#if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
c8026dea
VZ
76
77// this class can be used to implement a standard event loop logic using
78// Pending() and Dispatch()
79//
80// it also handles idle processing automatically
b46b1d59 81class WXDLLIMPEXP_BASE wxEventLoopManual : public wxEventLoopBase
c8026dea
VZ
82{
83public:
84 wxEventLoopManual();
85
86 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
87 // terminating when Exit() is called
88 virtual int Run();
89
90 // sets the "should exit" flag and wakes up the loop so that it terminates
91 // soon
5b87e74a 92 virtual void Exit(int rc = 0);
c8026dea
VZ
93
94protected:
c8026dea
VZ
95 // may be overridden to perform some action at the start of each new event
96 // loop iteration
97 virtual void OnNextIteration() { }
98
99
100 // the loop exit code
101 int m_exitcode;
102
103 // should we exit the loop?
104 bool m_shouldExit;
105};
106
107#endif // platforms using "manual" loop
108
3754265e
VZ
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.
4055ed82 114#if defined(__WXPALMOS__)
ffecfa5a
JS
115 #include "wx/palmos/evtloop.h"
116#elif defined(__WXMSW__)
3754265e 117 #include "wx/msw/evtloop.h"
4d90072c 118#elif defined(__WXMAC__)
ef0e9220 119 #include "wx/osx/evtloop.h"
b3c86150
VS
120#elif defined(__WXDFB__)
121 #include "wx/dfb/evtloop.h"
4d90072c 122#else // other platform
3754265e 123
b5dbe15d 124class WXDLLIMPEXP_FWD_CORE wxEventLoopImpl;
3754265e 125
53a2db12 126class WXDLLIMPEXP_CORE wxGUIEventLoop : public wxEventLoopBase
3754265e
VZ
127{
128public:
b46b1d59
VZ
129 wxGUIEventLoop() { m_impl = NULL; }
130 virtual ~wxGUIEventLoop();
1a18887b 131
3754265e
VZ
132 virtual int Run();
133 virtual void Exit(int rc = 0);
134 virtual bool Pending() const;
135 virtual bool Dispatch();
b46b1d59 136 virtual void WakeUp() { }
3754265e
VZ
137
138protected:
4300caa7
VZ
139 // the pointer to the port specific implementation class
140 wxEventLoopImpl *m_impl;
141
b46b1d59 142 DECLARE_NO_COPY_CLASS(wxGUIEventLoop)
4d90072c 143};
3754265e 144
4d90072c 145#endif // platforms
3808e191 146
b46b1d59
VZ
147// also include the header defining wxConsoleEventLoop for Unix systems
148#if defined(__UNIX__)
149 #include "wx/unix/evtloop.h"
150#endif
151
2ddff00c
VZ
152// we use a class rather than a typedef because wxEventLoop is forward-declared
153// in many places
b46b1d59 154#if wxUSE_GUI
2ddff00c
VZ
155 class wxEventLoop : public wxGUIEventLoop { };
156#else // !GUI
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
1a7cfc94 159 #if wxUSE_CONSOLE_EVENTLOOP && (defined(__WXMSW__) || defined(__UNIX__))
2ddff00c
VZ
160 #define wxEventLoop wxConsoleEventLoop
161 #else // we still must define it somehow for the code below...
162 #define wxEventLoop wxEventLoopBase
163 #endif
b46b1d59
VZ
164#endif
165
77fb1a02
VZ
166inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
167
b46b1d59 168#if wxUSE_GUI
4300caa7
VZ
169// ----------------------------------------------------------------------------
170// wxModalEventLoop
171// ----------------------------------------------------------------------------
172
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
176// works
53a2db12 177class WXDLLIMPEXP_CORE wxModalEventLoop : public wxGUIEventLoop
4300caa7
VZ
178{
179public:
180 wxModalEventLoop(wxWindow *winModal)
181 {
182 m_windowDisabler = new wxWindowDisabler(winModal);
183 }
184
185protected:
186 virtual void OnExit()
187 {
188 delete m_windowDisabler;
189 m_windowDisabler = NULL;
190
b46b1d59 191 wxGUIEventLoop::OnExit();
4300caa7
VZ
192 }
193
194private:
195 wxWindowDisabler *m_windowDisabler;
196};
197
b46b1d59
VZ
198#endif //wxUSE_GUI
199
77fb1a02
VZ
200// ----------------------------------------------------------------------------
201// wxEventLoopActivator: helper class for wxEventLoop implementations
202// ----------------------------------------------------------------------------
203
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
207class wxEventLoopActivator
208{
209public:
b46b1d59 210 wxEventLoopActivator(wxEventLoopBase *evtLoop)
77fb1a02 211 {
b46b1d59 212 m_evtLoopOld = wxEventLoopBase::GetActive();
2ddff00c 213 wxEventLoopBase::SetActive(evtLoop);
77fb1a02
VZ
214 }
215
216 ~wxEventLoopActivator()
217 {
218 // restore the previously active event loop
b46b1d59 219 wxEventLoopBase::SetActive(m_evtLoopOld);
77fb1a02
VZ
220 }
221
222private:
2ddff00c 223 wxEventLoopBase *m_evtLoopOld;
77fb1a02
VZ
224};
225
1a7cfc94
VZ
226#if wxUSE_CONSOLE_EVENTLOOP
227
f212e222
SN
228class wxEventLoopGuarantor
229{
230public:
231 wxEventLoopGuarantor()
232 {
233 m_evtLoopNew = NULL;
234 if (!wxEventLoop::GetActive())
235 {
236 m_evtLoopNew = new wxEventLoop;
237 wxEventLoop::SetActive(m_evtLoopNew);
238 }
239 }
240
241 ~wxEventLoopGuarantor()
242 {
243 if (m_evtLoopNew)
244 {
245 wxEventLoop::SetActive(NULL);
246 delete m_evtLoopNew;
247 }
248 }
249
250private:
251 wxEventLoop *m_evtLoopNew;
252};
253
1a7cfc94
VZ
254#endif // wxUSE_CONSOLE_EVENTLOOP
255
3808e191 256#endif // _WX_EVTLOOP_H_