]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/evtloop.cpp
cleanup
[wxWidgets.git] / src / mgl / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/evtloop.cpp
3 // Purpose: implements wxEventLoop for MGL
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // License: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
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"
25 #include "wx/timer.h"
26 #endif //WX_PRECOMP
27
28 #include "wx/evtloop.h"
29
30 #include "wx/generic/private/timer.h"
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);
44 SetKeepLooping(true);
45 }
46
47 // process an event
48 void Dispatch();
49
50 // generate an idle event, return true if more idle time requested
51 bool SendIdleEvent();
52
53 // set/get the exit code
54 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
55 int GetExitCode() const { return m_exitcode; }
56
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;
64 // false if the loop should end
65 bool m_keepLooping;
66 };
67
68 // ============================================================================
69 // wxEventLoopImpl implementation
70 // ============================================================================
71
72 void wxEventLoopImpl::Dispatch()
73 {
74 event_t evt;
75
76 // VS: The code bellow is equal to MGL's EVT_halt implementation, with
77 // two things added: sleeping (busy waiting is stupid, lets make CPU's
78 // life a bit easier) and timers updating
79
80 // EVT_halt(&evt, EVT_EVERYEVT);
81 for (;;)
82 {
83 #if wxUSE_TIMER
84 wxGenericTimerImpl::NotifyTimers();
85 #endif
86 MGL_wmUpdateDC(g_winMng);
87
88 EVT_pollJoystick();
89 if ( EVT_getNext(&evt, EVT_EVERYEVT) ) break;
90 PM_sleep(10);
91 }
92 // end of EVT_halt
93
94 MGL_wmProcessEvent(g_winMng, &evt);
95 }
96
97 bool wxEventLoopImpl::SendIdleEvent()
98 {
99 return wxTheApp->ProcessIdle();
100 }
101
102 // ============================================================================
103 // wxGUIEventLoop implementation
104 // ============================================================================
105
106 // ----------------------------------------------------------------------------
107 // wxGUIEventLoop running and exiting
108 // ----------------------------------------------------------------------------
109
110 wxGUIEventLoop::~wxGUIEventLoop()
111 {
112 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
113 }
114
115 int wxGUIEventLoop::Run()
116 {
117 // event loops are not recursive, you need to create another loop!
118 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
119
120 m_impl = new wxEventLoopImpl;
121
122 wxEventLoopActivator activate(this);
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
132 while ( !Pending() && m_impl->SendIdleEvent() ) {}
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
143 OnExit();
144
145 int exitcode = m_impl->GetExitCode();
146 delete m_impl;
147 m_impl = NULL;
148
149 return exitcode;
150 }
151
152 void wxGUIEventLoop::Exit(int rc)
153 {
154 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
155
156 m_impl->SetExitCode(rc);
157 m_impl->SetKeepLooping(false);
158
159 // Send a dummy event so that the app won't block in EVT_halt if there
160 // are no user-generated events in the queue:
161 EVT_post(0, EVT_USEREVT, 0, 0);
162 }
163
164 // ----------------------------------------------------------------------------
165 // wxEventLoop message processing dispatching
166 // ----------------------------------------------------------------------------
167
168 bool wxGUIEventLoop::Pending() const
169 {
170 // update the display here, so that wxYield refreshes display and
171 // changes take effect immediately, not after emptying events queue:
172 MGL_wmUpdateDC(g_winMng);
173
174 // is there an event in the queue?
175 event_t evt;
176 return (bool)(EVT_peekNext(&evt, EVT_EVERYEVT));
177 }
178
179 bool wxGUIEventLoop::Dispatch()
180 {
181 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
182
183 m_impl->Dispatch();
184 return m_impl->GetKeepLooping();
185 }
186