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