]>
Commit | Line | Data |
---|---|---|
7bdc1879 | 1 | /////////////////////////////////////////////////////////////////////////////// |
127eab18 | 2 | // Name: src/mgl/evtloop.cpp |
7bdc1879 VS |
3 | // Purpose: implements wxEventLoop for MGL |
4 | // Author: Vaclav Slavik | |
5 | // RCS-ID: $Id$ | |
c41c20a5 | 6 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) |
526954c5 | 7 | // Licence: wxWindows licence |
7bdc1879 VS |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
7bdc1879 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/window.h" | |
23 | #include "wx/app.h" | |
24 | #include "wx/thread.h" | |
c0badb70 | 25 | #include "wx/timer.h" |
7bdc1879 VS |
26 | #endif //WX_PRECOMP |
27 | ||
28 | #include "wx/evtloop.h" | |
b46b1d59 VZ |
29 | |
30 | #include "wx/generic/private/timer.h" | |
7bdc1879 VS |
31 | #include "wx/mgl/private.h" |
32 | ||
33 | // ---------------------------------------------------------------------------- | |
34 | // wxEventLoopImpl | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
37 | class WXDLLEXPORT wxEventLoopImpl | |
38 | { | |
39 | public: | |
40 | // ctor | |
41 | wxEventLoopImpl() | |
42 | { | |
43 | SetExitCode(0); | |
127eab18 | 44 | SetKeepLooping(true); |
7bdc1879 VS |
45 | } |
46 | ||
ef344ff8 VS |
47 | // process an event |
48 | void Dispatch(); | |
7bdc1879 | 49 | |
127eab18 | 50 | // generate an idle event, return true if more idle time requested |
ef344ff8 | 51 | bool SendIdleEvent(); |
7bdc1879 VS |
52 | |
53 | // set/get the exit code | |
54 | void SetExitCode(int exitcode) { m_exitcode = exitcode; } | |
55 | int GetExitCode() const { return m_exitcode; } | |
127eab18 | 56 | |
7bdc1879 VS |
57 | void SetKeepLooping(bool k) { m_keepLooping = k; } |
58 | bool GetKeepLooping() const { return m_keepLooping; } | |
59 | ||
60 | private: | |
61 | ||
62 | // the exit code of the event loop | |
63 | int m_exitcode; | |
127eab18 | 64 | // false if the loop should end |
7bdc1879 VS |
65 | bool m_keepLooping; |
66 | }; | |
67 | ||
68 | // ============================================================================ | |
69 | // wxEventLoopImpl implementation | |
70 | // ============================================================================ | |
71 | ||
ef344ff8 | 72 | void wxEventLoopImpl::Dispatch() |
7bdc1879 | 73 | { |
ef344ff8 | 74 | event_t evt; |
ef344ff8 | 75 | |
4c51a665 DS |
76 | // VS: The code below is equal to MGL's EVT_halt implementation, with |
77 | // two things added: sleeping (busy waiting is stupid, let's make CPU's | |
78 | // life a bit easier) and timers updating. | |
1acd70f9 VS |
79 | |
80 | // EVT_halt(&evt, EVT_EVERYEVT); | |
88f2a771 | 81 | for (;;) |
1acd70f9 | 82 | { |
1acd70f9 | 83 | #if wxUSE_TIMER |
b46b1d59 | 84 | wxGenericTimerImpl::NotifyTimers(); |
1acd70f9 | 85 | #endif |
b46b1d59 VZ |
86 | MGL_wmUpdateDC(g_winMng); |
87 | ||
88f2a771 VS |
88 | EVT_pollJoystick(); |
89 | if ( EVT_getNext(&evt, EVT_EVERYEVT) ) break; | |
1acd70f9 | 90 | PM_sleep(10); |
88f2a771 | 91 | } |
1acd70f9 | 92 | // end of EVT_halt |
127eab18 | 93 | |
ef344ff8 | 94 | MGL_wmProcessEvent(g_winMng, &evt); |
7bdc1879 VS |
95 | } |
96 | ||
ef344ff8 | 97 | bool wxEventLoopImpl::SendIdleEvent() |
7bdc1879 | 98 | { |
e39af974 | 99 | return wxTheApp->ProcessIdle(); |
7bdc1879 VS |
100 | } |
101 | ||
102 | // ============================================================================ | |
b46b1d59 | 103 | // wxGUIEventLoop implementation |
7bdc1879 VS |
104 | // ============================================================================ |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
b46b1d59 | 107 | // wxGUIEventLoop running and exiting |
7bdc1879 VS |
108 | // ---------------------------------------------------------------------------- |
109 | ||
b46b1d59 | 110 | wxGUIEventLoop::~wxGUIEventLoop() |
7bdc1879 | 111 | { |
9a83f860 | 112 | wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") ); |
7bdc1879 VS |
113 | } |
114 | ||
b46b1d59 | 115 | int wxGUIEventLoop::Run() |
7bdc1879 VS |
116 | { |
117 | // event loops are not recursive, you need to create another loop! | |
9a83f860 | 118 | wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); |
7bdc1879 VS |
119 | |
120 | m_impl = new wxEventLoopImpl; | |
127eab18 | 121 | |
77fb1a02 | 122 | wxEventLoopActivator activate(this); |
7bdc1879 VS |
123 | |
124 | for ( ;; ) | |
125 | { | |
126 | #if wxUSE_THREADS | |
127 | //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh? | |
128 | #endif // wxUSE_THREADS | |
129 | ||
130 | // generate and process idle events for as long as we don't have | |
131 | // anything else to do | |
ef344ff8 | 132 | while ( !Pending() && m_impl->SendIdleEvent() ) {} |
7bdc1879 VS |
133 | |
134 | // a message came or no more idle processing to do, sit in Dispatch() | |
135 | // waiting for the next message | |
136 | if ( !Dispatch() ) | |
137 | { | |
138 | // app terminated | |
139 | break; | |
140 | } | |
141 | } | |
142 | ||
16d17da6 VZ |
143 | OnExit(); |
144 | ||
7bdc1879 | 145 | int exitcode = m_impl->GetExitCode(); |
5276b0a5 | 146 | wxDELETE(m_impl); |
7bdc1879 VS |
147 | |
148 | return exitcode; | |
149 | } | |
150 | ||
b46b1d59 | 151 | void wxGUIEventLoop::Exit(int rc) |
7bdc1879 | 152 | { |
9a83f860 | 153 | wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") ); |
7bdc1879 VS |
154 | |
155 | m_impl->SetExitCode(rc); | |
127eab18 WS |
156 | m_impl->SetKeepLooping(false); |
157 | ||
f41ed3c4 VS |
158 | // Send a dummy event so that the app won't block in EVT_halt if there |
159 | // are no user-generated events in the queue: | |
160 | EVT_post(0, EVT_USEREVT, 0, 0); | |
7bdc1879 VS |
161 | } |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // wxEventLoop message processing dispatching | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
b46b1d59 | 167 | bool wxGUIEventLoop::Pending() const |
7bdc1879 | 168 | { |
127eab18 | 169 | // update the display here, so that wxYield refreshes display and |
c41c20a5 VS |
170 | // changes take effect immediately, not after emptying events queue: |
171 | MGL_wmUpdateDC(g_winMng); | |
127eab18 | 172 | |
c41c20a5 | 173 | // is there an event in the queue? |
7bdc1879 | 174 | event_t evt; |
127eab18 | 175 | return (bool)(EVT_peekNext(&evt, EVT_EVERYEVT)); |
7bdc1879 VS |
176 | } |
177 | ||
b46b1d59 | 178 | bool wxGUIEventLoop::Dispatch() |
7bdc1879 | 179 | { |
9a83f860 | 180 | wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") ); |
7bdc1879 | 181 | |
ef344ff8 | 182 | m_impl->Dispatch(); |
7bdc1879 VS |
183 | return m_impl->GetKeepLooping(); |
184 | } | |
b46b1d59 | 185 | |
dde19c21 FM |
186 | |
187 | //----------------------------------------------------------------------------- | |
188 | // wxYield | |
189 | //----------------------------------------------------------------------------- | |
190 | ||
191 | bool wxGUIEventLoop::YieldFor(long eventsToProcess) | |
192 | { | |
193 | #if wxUSE_THREADS | |
194 | if ( !wxThread::IsMain() ) | |
195 | { | |
196 | // can't process events from other threads, MGL is thread-unsafe | |
197 | return true; | |
198 | } | |
199 | #endif // wxUSE_THREADS | |
200 | ||
201 | m_isInsideYield = true; | |
202 | m_eventsToProcessInsideYield = eventsToProcess; | |
203 | ||
204 | wxLog::Suspend(); | |
205 | ||
206 | // TODO: implement event filtering using the eventsToProcess mask | |
207 | ||
208 | while (Pending()) | |
209 | Dispatch(); | |
210 | ||
211 | /* it's necessary to call ProcessIdle() to update the frames sizes which | |
212 | might have been changed (it also will update other things set from | |
213 | OnUpdateUI() which is a nice (and desired) side effect) */ | |
214 | while (wxTheApp->ProcessIdle()) { } | |
215 | ||
216 | wxLog::Resume(); | |
217 | ||
218 | m_isInsideYield = false; | |
219 | ||
220 | return true; | |
221 | } |