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 // ----------------------------------------------------------------------------
14 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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()
100 return wxTheApp
->ProcessIdle();
103 // ============================================================================
104 // wxEventLoop implementation
105 // ============================================================================
107 // ----------------------------------------------------------------------------
108 // wxEventLoop running and exiting
109 // ----------------------------------------------------------------------------
111 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
113 wxEventLoop::~wxEventLoop()
115 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
118 bool wxEventLoop::IsRunning() const
120 return m_impl
!= NULL
;
123 int wxEventLoop::Run()
125 // event loops are not recursive, you need to create another loop!
126 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
128 m_impl
= new wxEventLoopImpl
;
130 wxEventLoop
*oldLoop
= ms_activeLoop
;
131 ms_activeLoop
= this;
136 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
137 #endif // wxUSE_THREADS
139 // generate and process idle events for as long as we don't have
140 // anything else to do
141 while ( !Pending() && m_impl
->SendIdleEvent() ) {}
143 // a message came or no more idle processing to do, sit in Dispatch()
144 // waiting for the next message
152 int exitcode
= m_impl
->GetExitCode();
156 ms_activeLoop
= oldLoop
;
161 void wxEventLoop::Exit(int rc
)
163 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
165 m_impl
->SetExitCode(rc
);
166 m_impl
->SetKeepLooping(FALSE
);
168 // Send a dummy event so that the app won't block in EVT_halt if there
169 // are no user-generated events in the queue:
170 EVT_post(0, EVT_USEREVT
, 0, 0);
173 // ----------------------------------------------------------------------------
174 // wxEventLoop message processing dispatching
175 // ----------------------------------------------------------------------------
177 bool wxEventLoop::Pending() const
179 // update the display here, so that wxYield refreshes display and
180 // changes take effect immediately, not after emptying events queue:
181 MGL_wmUpdateDC(g_winMng
);
183 // is there an event in the queue?
185 return EVT_peekNext(&evt
, EVT_EVERYEVT
);
188 bool wxEventLoop::Dispatch()
190 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
193 return m_impl
->GetKeepLooping();