1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // ----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/window.h"
24 #include "wx/thread.h"
28 #include "wx/evtloop.h"
30 #include "wx/generic/private/timer.h"
31 #include "wx/mgl/private.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 class WXDLLEXPORT wxEventLoopImpl
50 // generate an idle event, return true if more idle time requested
53 // set/get the exit code
54 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
55 int GetExitCode() const { return m_exitcode
; }
57 void SetKeepLooping(bool k
) { m_keepLooping
= k
; }
58 bool GetKeepLooping() const { return m_keepLooping
; }
62 // the exit code of the event loop
64 // false if the loop should end
68 // ============================================================================
69 // wxEventLoopImpl implementation
70 // ============================================================================
72 void wxEventLoopImpl::Dispatch()
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
80 // EVT_halt(&evt, EVT_EVERYEVT);
84 wxGenericTimerImpl::NotifyTimers();
86 MGL_wmUpdateDC(g_winMng
);
89 if ( EVT_getNext(&evt
, EVT_EVERYEVT
) ) break;
94 MGL_wmProcessEvent(g_winMng
, &evt
);
97 bool wxEventLoopImpl::SendIdleEvent()
99 return wxTheApp
->ProcessIdle();
102 // ============================================================================
103 // wxGUIEventLoop implementation
104 // ============================================================================
106 // ----------------------------------------------------------------------------
107 // wxGUIEventLoop running and exiting
108 // ----------------------------------------------------------------------------
110 wxGUIEventLoop::~wxGUIEventLoop()
112 wxASSERT_MSG( !m_impl
, wxT("should have been deleted in Run()") );
115 int wxGUIEventLoop::Run()
117 // event loops are not recursive, you need to create another loop!
118 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
120 m_impl
= new wxEventLoopImpl
;
122 wxEventLoopActivator
activate(this);
127 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
128 #endif // wxUSE_THREADS
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() ) {}
134 // a message came or no more idle processing to do, sit in Dispatch()
135 // waiting for the next message
145 int exitcode
= m_impl
->GetExitCode();
151 void wxGUIEventLoop::Exit(int rc
)
153 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
155 m_impl
->SetExitCode(rc
);
156 m_impl
->SetKeepLooping(false);
158 // Send a dummy event so that the app won't block in EVT_halt if there
159 // are no user-generated events in the queue:
160 EVT_post(0, EVT_USEREVT
, 0, 0);
163 // ----------------------------------------------------------------------------
164 // wxEventLoop message processing dispatching
165 // ----------------------------------------------------------------------------
167 bool wxGUIEventLoop::Pending() const
169 // update the display here, so that wxYield refreshes display and
170 // changes take effect immediately, not after emptying events queue:
171 MGL_wmUpdateDC(g_winMng
);
173 // is there an event in the queue?
175 return (bool)(EVT_peekNext(&evt
, EVT_EVERYEVT
));
178 bool wxGUIEventLoop::Dispatch()
180 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
183 return m_impl
->GetKeepLooping();
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
194 if ( !wxThread::IsMain() )
196 // can't process events from other threads, MGL is thread-unsafe
199 #endif // wxUSE_THREADS
201 m_isInsideYield
= true;
202 m_eventsToProcessInsideYield
= eventsToProcess
;
206 // TODO: implement event filtering using the eventsToProcess mask
211 /* it's necessary to call ProcessIdle() to update the frames sizes which
212 might have been changed (it also will update other things set from
213 OnUpdateUI() which is a nice (and desired) side effect) */
214 while (wxTheApp
->ProcessIdle()) { }
218 m_isInsideYield
= false;