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 license
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 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
105 // ============================================================================
106 // wxEventLoop implementation
107 // ============================================================================
109 // ----------------------------------------------------------------------------
110 // wxEventLoop running and exiting
111 // ----------------------------------------------------------------------------
113 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
115 wxEventLoop::~wxEventLoop()
117 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
120 bool wxEventLoop::IsRunning() const
122 return m_impl
!= NULL
;
125 int wxEventLoop::Run()
127 // event loops are not recursive, you need to create another loop!
128 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
130 m_impl
= new wxEventLoopImpl
;
132 wxEventLoop
*oldLoop
= ms_activeLoop
;
133 ms_activeLoop
= this;
138 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
139 #endif // wxUSE_THREADS
141 // generate and process idle events for as long as we don't have
142 // anything else to do
143 while ( !Pending() && m_impl
->SendIdleEvent() ) {}
145 // a message came or no more idle processing to do, sit in Dispatch()
146 // waiting for the next message
154 int exitcode
= m_impl
->GetExitCode();
158 ms_activeLoop
= oldLoop
;
163 void wxEventLoop::Exit(int rc
)
165 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
167 m_impl
->SetExitCode(rc
);
168 m_impl
->SetKeepLooping(FALSE
);
170 // Send a dummy event so that the app won't block in EVT_halt if there
171 // are no user-generated events in the queue:
172 EVT_post(0, EVT_USEREVT
, 0, 0);
175 // ----------------------------------------------------------------------------
176 // wxEventLoop message processing dispatching
177 // ----------------------------------------------------------------------------
179 bool wxEventLoop::Pending() const
181 // update the display here, so that wxYield refreshes display and
182 // changes take effect immediately, not after emptying events queue:
183 MGL_wmUpdateDC(g_winMng
);
185 // is there an event in the queue?
187 return EVT_peekNext(&evt
, EVT_EVERYEVT
);
190 bool wxEventLoop::Dispatch()
192 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
195 return m_impl
->GetKeepLooping();