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