]> git.saurik.com Git - wxWidgets.git/blame - include/wx/evtloop.h
Apply parts of patch #1719888 to fix compilation on Mac and with
[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
3754265e 17class WXDLLEXPORT wxEventLoop;
4300caa7 18
3808e191 19// ----------------------------------------------------------------------------
b46b1d59 20// wxEventLoopBase: interface for wxEventLoop
3808e191
JS
21// ----------------------------------------------------------------------------
22
b46b1d59 23class WXDLLIMPEXP_BASE wxEventLoopBase
3808e191
JS
24{
25public:
3754265e
VZ
26 // trivial, but needed (because of wxEventLoopBase) ctor
27 wxEventLoopBase() { }
3808e191
JS
28
29 // dtor
3754265e 30 virtual ~wxEventLoopBase() { }
3808e191 31
b46b1d59
VZ
32 // use this to check whether the event loop was successfully created before
33 // using it
34 virtual bool IsOk() const { return true; }
35
3808e191 36 // start the event loop, return the exit code when it is finished
3754265e 37 virtual int Run() = 0;
3808e191
JS
38
39 // exit from the loop with the given exit code
3754265e 40 virtual void Exit(int rc = 0) = 0;
3808e191 41
1a18887b 42 // return true if any events are available
3754265e 43 virtual bool Pending() const = 0;
3808e191 44
1a18887b 45 // dispatch a single event, return false if we should exit from the loop
3754265e 46 virtual bool Dispatch() = 0;
3808e191 47
b9f246f7
VS
48 // return currently active (running) event loop, may be NULL
49 static wxEventLoop *GetActive() { return ms_activeLoop; }
3808e191 50
df0e1b64
JS
51 // set currently active (running) event loop
52 static void SetActive(wxEventLoop* loop) { ms_activeLoop = loop; }
53
77fb1a02
VZ
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
b46b1d59
VZ
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
3808e191 64protected:
4300caa7
VZ
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
b9f246f7
VS
71 // the pointer to currently active loop
72 static wxEventLoop *ms_activeLoop;
22f3361e 73
3754265e
VZ
74 DECLARE_NO_COPY_CLASS(wxEventLoopBase)
75};
76
b46b1d59 77#if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXDFB__) || defined(__UNIX__)
c8026dea
VZ
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
b46b1d59 83class WXDLLIMPEXP_BASE wxEventLoopManual : public wxEventLoopBase
c8026dea
VZ
84{
85public:
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
5b87e74a 94 virtual void Exit(int rc = 0);
c8026dea
VZ
95
96protected:
c8026dea
VZ
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
3754265e
VZ
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.
4055ed82 116#if defined(__WXPALMOS__)
ffecfa5a
JS
117 #include "wx/palmos/evtloop.h"
118#elif defined(__WXMSW__)
3754265e 119 #include "wx/msw/evtloop.h"
4d90072c
VZ
120#elif defined(__WXMAC__)
121 #include "wx/mac/evtloop.h"
b3c86150
VS
122#elif defined(__WXDFB__)
123 #include "wx/dfb/evtloop.h"
4d90072c 124#else // other platform
3754265e
VZ
125
126class WXDLLEXPORT wxEventLoopImpl;
127
b46b1d59 128class WXDLLEXPORT wxGUIEventLoop : public wxEventLoopBase
3754265e
VZ
129{
130public:
b46b1d59
VZ
131 wxGUIEventLoop() { m_impl = NULL; }
132 virtual ~wxGUIEventLoop();
1a18887b 133
3754265e
VZ
134 virtual int Run();
135 virtual void Exit(int rc = 0);
136 virtual bool Pending() const;
137 virtual bool Dispatch();
b46b1d59 138 virtual void WakeUp() { }
3754265e
VZ
139
140protected:
4300caa7
VZ
141 // the pointer to the port specific implementation class
142 wxEventLoopImpl *m_impl;
143
b46b1d59 144 DECLARE_NO_COPY_CLASS(wxGUIEventLoop)
4d90072c 145};
3754265e 146
4d90072c 147#endif // platforms
3808e191 148
b46b1d59
VZ
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
156class wxEventLoop : public wxGUIEventLoop { };
157#elif defined(__WXMSW__) || defined(__UNIX__)
158class wxEventLoop : public wxConsoleEventLoop { };
159#else // we still must define it somehow for the code below...
160class wxEventLoop : public wxEventLoopBase { };
161#endif
162
77fb1a02
VZ
163inline bool wxEventLoopBase::IsRunning() const { return GetActive() == this; }
164
b46b1d59 165#if wxUSE_GUI
4300caa7
VZ
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
b46b1d59 174class WXDLLEXPORT wxModalEventLoop : public wxGUIEventLoop
4300caa7
VZ
175{
176public:
177 wxModalEventLoop(wxWindow *winModal)
178 {
179 m_windowDisabler = new wxWindowDisabler(winModal);
180 }
181
182protected:
183 virtual void OnExit()
184 {
185 delete m_windowDisabler;
186 m_windowDisabler = NULL;
187
b46b1d59 188 wxGUIEventLoop::OnExit();
4300caa7
VZ
189 }
190
191private:
192 wxWindowDisabler *m_windowDisabler;
193};
194
b46b1d59
VZ
195#endif //wxUSE_GUI
196
77fb1a02
VZ
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
204class wxEventLoopActivator
205{
206public:
b46b1d59 207 wxEventLoopActivator(wxEventLoopBase *evtLoop)
77fb1a02 208 {
b46b1d59
VZ
209 m_evtLoopOld = wxEventLoopBase::GetActive();
210 wxEventLoopBase::SetActive(wx_static_cast(wxEventLoop *, evtLoop));
77fb1a02
VZ
211 }
212
213 ~wxEventLoopActivator()
214 {
215 // restore the previously active event loop
b46b1d59 216 wxEventLoopBase::SetActive(m_evtLoopOld);
77fb1a02
VZ
217 }
218
219private:
220 wxEventLoop *m_evtLoopOld;
221};
222
3808e191 223#endif // _WX_EVTLOOP_H_