]> git.saurik.com Git - wxWidgets.git/blob - include/wx/evtloop.h
restored accidentally commented-out code
[wxWidgets.git] / include / wx / evtloop.h
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>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_EVTLOOP_H_
13 #define _WX_EVTLOOP_H_
14
15 #include "wx/utils.h"
16
17 class WXDLLEXPORT wxEventLoop;
18
19 // ----------------------------------------------------------------------------
20 // wxEventLoopBase: interface for wxEventLoop
21 // ----------------------------------------------------------------------------
22
23 class WXDLLIMPEXP_BASE wxEventLoopBase
24 {
25 public:
26 // trivial, but needed (because of wxEventLoopBase) ctor
27 wxEventLoopBase() { }
28
29 // dtor
30 virtual ~wxEventLoopBase() { }
31
32 // use this to check whether the event loop was successfully created before
33 // using it
34 virtual bool IsOk() const { return true; }
35
36 // start the event loop, return the exit code when it is finished
37 virtual int Run() = 0;
38
39 // exit from the loop with the given exit code
40 virtual void Exit(int rc = 0) = 0;
41
42 // return true if any events are available
43 virtual bool Pending() const = 0;
44
45 // dispatch a single event, return false if we should exit from the loop
46 virtual bool Dispatch() = 0;
47
48 // return currently active (running) event loop, may be NULL
49 static wxEventLoop *GetActive() { return ms_activeLoop; }
50
51 // set currently active (running) event loop
52 static void SetActive(wxEventLoop* loop) { ms_activeLoop = loop; }
53
54 // is this event loop running now?
55 //
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;
59
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;
63
64 protected:
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() { }
69
70
71 // the pointer to currently active loop
72 static wxEventLoop *ms_activeLoop;
73
74 DECLARE_NO_COPY_CLASS(wxEventLoopBase)
75 };
76
77 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
78
79 // this class can be used to implement a standard event loop logic using
80 // Pending() and Dispatch()
81 //
82 // it also handles idle processing automatically
83 class WXDLLIMPEXP_BASE wxEventLoopManual : public wxEventLoopBase
84 {
85 public:
86 wxEventLoopManual();
87
88 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
89 // terminating when Exit() is called
90 virtual int Run();
91
92 // sets the "should exit" flag and wakes up the loop so that it terminates
93 // soon
94 virtual void Exit(int rc = 0);
95
96 protected:
97 // may be overridden to perform some action at the start of each new event
98 // loop iteration
99 virtual void OnNextIteration() { }
100
101
102 // the loop exit code
103 int m_exitcode;
104
105 // should we exit the loop?
106 bool m_shouldExit;
107 };
108
109 #endif // platforms using "manual" loop
110
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
125
126 class WXDLLEXPORT wxEventLoopImpl;
127
128 class WXDLLEXPORT wxGUIEventLoop : public wxEventLoopBase
129 {
130 public:
131 wxGUIEventLoop() { m_impl = NULL; }
132 virtual ~wxGUIEventLoop();
133
134 virtual int Run();
135 virtual void Exit(int rc = 0);
136 virtual bool Pending() const;
137 virtual bool Dispatch();
138 virtual void WakeUp() { }
139
140 protected:
141 // the pointer to the port specific implementation class
142 wxEventLoopImpl *m_impl;
143
144 DECLARE_NO_COPY_CLASS(wxGUIEventLoop)
145 };
146
147 #endif // platforms
148
149 // also include the header defining wxConsoleEventLoop for Unix systems
150 #if defined(__UNIX__)
151 #include "wx/unix/evtloop.h"
152 #endif
153
154 // cannot use typedef because wxEventLoop is forward-declared in many places
155 #if wxUSE_GUI
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 { };
161 #endif
162
163 inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
164
165 #if wxUSE_GUI
166 // ----------------------------------------------------------------------------
167 // wxModalEventLoop
168 // ----------------------------------------------------------------------------
169
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
173 // works
174 class WXDLLEXPORT wxModalEventLoop : public wxGUIEventLoop
175 {
176 public:
177 wxModalEventLoop(wxWindow *winModal)
178 {
179 m_windowDisabler = new wxWindowDisabler(winModal);
180 }
181
182 protected:
183 virtual void OnExit()
184 {
185 delete m_windowDisabler;
186 m_windowDisabler = NULL;
187
188 wxGUIEventLoop::OnExit();
189 }
190
191 private:
192 wxWindowDisabler *m_windowDisabler;
193 };
194
195 #endif //wxUSE_GUI
196
197 // ----------------------------------------------------------------------------
198 // wxEventLoopActivator: helper class for wxEventLoop implementations
199 // ----------------------------------------------------------------------------
200
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
205 {
206 public:
207 wxEventLoopActivator(wxEventLoopBase *evtLoop)
208 {
209 m_evtLoopOld = wxEventLoopBase::GetActive();
210 wxEventLoopBase::SetActive(wx_static_cast(wxEventLoop *, evtLoop));
211 }
212
213 ~wxEventLoopActivator()
214 {
215 // restore the previously active event loop
216 wxEventLoopBase::SetActive(m_evtLoopOld);
217 }
218
219 private:
220 wxEventLoop *m_evtLoopOld;
221 };
222
223 #endif // _WX_EVTLOOP_H_