]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/evtloop.cpp
Added wxFrameBase::OnMenuOpen, and wxUSE_IDLEMENUUPDATES in platform.h
[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-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // License: wxWindows licence
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
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
49 // process an event
50 void Dispatch();
51
52 // generate an idle event, return TRUE if more idle time requested
53 bool SendIdleEvent();
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
74 void wxEventLoopImpl::Dispatch()
75 {
76 event_t evt;
77
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);
83 for (;;)
84 {
85 #if wxUSE_TIMER
86 wxTimer::NotifyTimers();
87 MGL_wmUpdateDC(g_winMng);
88 #endif
89 EVT_pollJoystick();
90 if ( EVT_getNext(&evt, EVT_EVERYEVT) ) break;
91 PM_sleep(10);
92 }
93 // end of EVT_halt
94
95 MGL_wmProcessEvent(g_winMng, &evt);
96 }
97
98 bool wxEventLoopImpl::SendIdleEvent()
99 {
100 wxIdleEvent event;
101
102 bool processed = wxTheApp->ProcessEvent(event);
103
104 wxUpdateUIEvent::ResetUpdateTime();
105
106 return processed && event.MoreRequested();
107 }
108
109 // ============================================================================
110 // wxEventLoop implementation
111 // ============================================================================
112
113 // ----------------------------------------------------------------------------
114 // wxEventLoop running and exiting
115 // ----------------------------------------------------------------------------
116
117 wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
118
119 wxEventLoop::~wxEventLoop()
120 {
121 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
122 }
123
124 bool wxEventLoop::IsRunning() const
125 {
126 return m_impl != NULL;
127 }
128
129 int wxEventLoop::Run()
130 {
131 // event loops are not recursive, you need to create another loop!
132 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
133
134 m_impl = new wxEventLoopImpl;
135
136 wxEventLoop *oldLoop = ms_activeLoop;
137 ms_activeLoop = this;
138
139 for ( ;; )
140 {
141 #if wxUSE_THREADS
142 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
143 #endif // wxUSE_THREADS
144
145 // generate and process idle events for as long as we don't have
146 // anything else to do
147 while ( !Pending() && m_impl->SendIdleEvent() ) {}
148
149 // a message came or no more idle processing to do, sit in Dispatch()
150 // waiting for the next message
151 if ( !Dispatch() )
152 {
153 // app terminated
154 break;
155 }
156 }
157
158 int exitcode = m_impl->GetExitCode();
159 delete m_impl;
160 m_impl = NULL;
161
162 ms_activeLoop = oldLoop;
163
164 return exitcode;
165 }
166
167 void wxEventLoop::Exit(int rc)
168 {
169 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
170
171 m_impl->SetExitCode(rc);
172 m_impl->SetKeepLooping(FALSE);
173
174 // Send a dummy event so that the app won't block in EVT_halt if there
175 // are no user-generated events in the queue:
176 EVT_post(0, EVT_USEREVT, 0, 0);
177 }
178
179 // ----------------------------------------------------------------------------
180 // wxEventLoop message processing dispatching
181 // ----------------------------------------------------------------------------
182
183 bool wxEventLoop::Pending() const
184 {
185 // update the display here, so that wxYield refreshes display and
186 // changes take effect immediately, not after emptying events queue:
187 MGL_wmUpdateDC(g_winMng);
188
189 // is there an event in the queue?
190 event_t evt;
191 return EVT_peekNext(&evt, EVT_EVERYEVT);
192 }
193
194 bool wxEventLoop::Dispatch()
195 {
196 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
197
198 m_impl->Dispatch();
199 return m_impl->GetKeepLooping();
200 }