]>
Commit | Line | Data |
---|---|---|
7bdc1879 VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: mgl/evtloop.cpp | |
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) |
e3a1e3e0 | 7 | // License: wxWindows licence |
7bdc1879 VS |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #ifdef __GNUG__ | |
15 | #pragma implementation "evtloop.h" | |
16 | #endif | |
17 | ||
18 | // For compilers that support precompilation, includes "wx.h". | |
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/window.h" | |
27 | #include "wx/app.h" | |
28 | #include "wx/thread.h" | |
29 | #endif //WX_PRECOMP | |
30 | ||
31 | #include "wx/evtloop.h" | |
1acd70f9 | 32 | #include "wx/timer.h" |
7bdc1879 VS |
33 | #include "wx/mgl/private.h" |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // wxEventLoopImpl | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | class WXDLLEXPORT wxEventLoopImpl | |
40 | { | |
41 | public: | |
42 | // ctor | |
43 | wxEventLoopImpl() | |
44 | { | |
45 | SetExitCode(0); | |
46 | SetKeepLooping(TRUE); | |
47 | } | |
48 | ||
ef344ff8 VS |
49 | // process an event |
50 | void Dispatch(); | |
7bdc1879 | 51 | |
ef344ff8 VS |
52 | // generate an idle event, return TRUE if more idle time requested |
53 | bool SendIdleEvent(); | |
7bdc1879 VS |
54 | |
55 | // set/get the exit code | |
56 | void SetExitCode(int exitcode) { m_exitcode = exitcode; } | |
57 | int GetExitCode() const { return m_exitcode; } | |
58 | ||
59 | void SetKeepLooping(bool k) { m_keepLooping = k; } | |
60 | bool GetKeepLooping() const { return m_keepLooping; } | |
61 | ||
62 | private: | |
63 | ||
64 | // the exit code of the event loop | |
65 | int m_exitcode; | |
66 | // FALSE if the loop should end | |
67 | bool m_keepLooping; | |
68 | }; | |
69 | ||
70 | // ============================================================================ | |
71 | // wxEventLoopImpl implementation | |
72 | // ============================================================================ | |
73 | ||
ef344ff8 | 74 | void wxEventLoopImpl::Dispatch() |
7bdc1879 | 75 | { |
ef344ff8 | 76 | event_t evt; |
ef344ff8 | 77 | |
1acd70f9 VS |
78 | // VS: The code bellow is equal to MGL's EVT_halt implementation, with |
79 | // two things added: sleeping (busy waiting is stupid, lets make CPU's | |
80 | // life a bit easier) and timers updating | |
81 | ||
82 | // EVT_halt(&evt, EVT_EVERYEVT); | |
88f2a771 | 83 | for (;;) |
1acd70f9 | 84 | { |
1acd70f9 VS |
85 | #if wxUSE_TIMER |
86 | wxTimer::NotifyTimers(); | |
88f2a771 | 87 | MGL_wmUpdateDC(g_winMng); |
1acd70f9 | 88 | #endif |
88f2a771 VS |
89 | EVT_pollJoystick(); |
90 | if ( EVT_getNext(&evt, EVT_EVERYEVT) ) break; | |
1acd70f9 | 91 | PM_sleep(10); |
88f2a771 | 92 | } |
1acd70f9 VS |
93 | // end of EVT_halt |
94 | ||
ef344ff8 | 95 | MGL_wmProcessEvent(g_winMng, &evt); |
7bdc1879 VS |
96 | } |
97 | ||
ef344ff8 | 98 | bool wxEventLoopImpl::SendIdleEvent() |
7bdc1879 | 99 | { |
e39af974 | 100 | return wxTheApp->ProcessIdle(); |
7bdc1879 VS |
101 | } |
102 | ||
103 | // ============================================================================ | |
104 | // wxEventLoop implementation | |
105 | // ============================================================================ | |
106 | ||
107 | // ---------------------------------------------------------------------------- | |
108 | // wxEventLoop running and exiting | |
109 | // ---------------------------------------------------------------------------- | |
110 | ||
ef344ff8 VS |
111 | wxEventLoop *wxEventLoop::ms_activeLoop = NULL; |
112 | ||
7bdc1879 VS |
113 | wxEventLoop::~wxEventLoop() |
114 | { | |
115 | wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") ); | |
116 | } | |
117 | ||
118 | bool wxEventLoop::IsRunning() const | |
119 | { | |
120 | return m_impl != NULL; | |
121 | } | |
122 | ||
123 | int wxEventLoop::Run() | |
124 | { | |
125 | // event loops are not recursive, you need to create another loop! | |
126 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
127 | ||
128 | m_impl = new wxEventLoopImpl; | |
ef344ff8 VS |
129 | |
130 | wxEventLoop *oldLoop = ms_activeLoop; | |
131 | ms_activeLoop = this; | |
7bdc1879 VS |
132 | |
133 | for ( ;; ) | |
134 | { | |
135 | #if wxUSE_THREADS | |
136 | //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh? | |
137 | #endif // wxUSE_THREADS | |
138 | ||
139 | // generate and process idle events for as long as we don't have | |
140 | // anything else to do | |
ef344ff8 | 141 | while ( !Pending() && m_impl->SendIdleEvent() ) {} |
7bdc1879 VS |
142 | |
143 | // a message came or no more idle processing to do, sit in Dispatch() | |
144 | // waiting for the next message | |
145 | if ( !Dispatch() ) | |
146 | { | |
147 | // app terminated | |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
152 | int exitcode = m_impl->GetExitCode(); | |
153 | delete m_impl; | |
154 | m_impl = NULL; | |
155 | ||
ef344ff8 VS |
156 | ms_activeLoop = oldLoop; |
157 | ||
7bdc1879 VS |
158 | return exitcode; |
159 | } | |
160 | ||
161 | void wxEventLoop::Exit(int rc) | |
162 | { | |
163 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
164 | ||
165 | m_impl->SetExitCode(rc); | |
166 | m_impl->SetKeepLooping(FALSE); | |
f41ed3c4 VS |
167 | |
168 | // Send a dummy event so that the app won't block in EVT_halt if there | |
169 | // are no user-generated events in the queue: | |
170 | EVT_post(0, EVT_USEREVT, 0, 0); | |
7bdc1879 VS |
171 | } |
172 | ||
173 | // ---------------------------------------------------------------------------- | |
174 | // wxEventLoop message processing dispatching | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | bool wxEventLoop::Pending() const | |
178 | { | |
c41c20a5 VS |
179 | // update the display here, so that wxYield refreshes display and |
180 | // changes take effect immediately, not after emptying events queue: | |
181 | MGL_wmUpdateDC(g_winMng); | |
182 | ||
183 | // is there an event in the queue? | |
7bdc1879 VS |
184 | event_t evt; |
185 | return EVT_peekNext(&evt, EVT_EVERYEVT); | |
186 | } | |
187 | ||
188 | bool wxEventLoop::Dispatch() | |
189 | { | |
190 | wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") ); | |
191 | ||
ef344ff8 | 192 | m_impl->Dispatch(); |
7bdc1879 VS |
193 | return m_impl->GetKeepLooping(); |
194 | } |