1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/evtloop.cpp
3 // Purpose: implements wxEventLoop for MGL
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // License: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
15 #pragma implementation "evtloop.h"
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
26 #include "wx/window.h"
28 #include "wx/thread.h"
31 #include "wx/evtloop.h"
33 #include "wx/mgl/private.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxEventLoopImpl
52 // generate an idle event, return TRUE if more idle time requested
55 // set/get the exit code
56 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
57 int GetExitCode() const { return m_exitcode
; }
59 void SetKeepLooping(bool k
) { m_keepLooping
= k
; }
60 bool GetKeepLooping() const { return m_keepLooping
; }
64 // the exit code of the event loop
66 // FALSE if the loop should end
70 // ============================================================================
71 // wxEventLoopImpl implementation
72 // ============================================================================
74 void wxEventLoopImpl::Dispatch()
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
82 // EVT_halt(&evt, EVT_EVERYEVT);
86 wxTimer::NotifyTimers();
87 MGL_wmUpdateDC(g_winMng
);
90 if ( EVT_getNext(&evt
, EVT_EVERYEVT
) ) break;
95 MGL_wmProcessEvent(g_winMng
, &evt
);
98 bool wxEventLoopImpl::SendIdleEvent()
102 bool processed
= wxTheApp
->ProcessEvent(event
);
104 wxUpdateUIEvent::ResetUpdateTime();
106 return processed
&& event
.MoreRequested();
109 // ============================================================================
110 // wxEventLoop implementation
111 // ============================================================================
113 // ----------------------------------------------------------------------------
114 // wxEventLoop running and exiting
115 // ----------------------------------------------------------------------------
117 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
119 wxEventLoop::~wxEventLoop()
121 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
124 bool wxEventLoop::IsRunning() const
126 return m_impl
!= NULL
;
129 int wxEventLoop::Run()
131 // event loops are not recursive, you need to create another loop!
132 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
134 m_impl
= new wxEventLoopImpl
;
136 wxEventLoop
*oldLoop
= ms_activeLoop
;
137 ms_activeLoop
= this;
142 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
143 #endif // wxUSE_THREADS
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() ) {}
149 // a message came or no more idle processing to do, sit in Dispatch()
150 // waiting for the next message
158 int exitcode
= m_impl
->GetExitCode();
162 ms_activeLoop
= oldLoop
;
167 void wxEventLoop::Exit(int rc
)
169 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
171 m_impl
->SetExitCode(rc
);
172 m_impl
->SetKeepLooping(FALSE
);
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);
179 // ----------------------------------------------------------------------------
180 // wxEventLoop message processing dispatching
181 // ----------------------------------------------------------------------------
183 bool wxEventLoop::Pending() const
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
);
189 // is there an event in the queue?
191 return EVT_peekNext(&evt
, EVT_EVERYEVT
);
194 bool wxEventLoop::Dispatch()
196 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
199 return m_impl
->GetKeepLooping();