1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/evtloop.cpp
3 // Purpose: implements wxEventLoop for MGL
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001 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"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class WXDLLEXPORT wxEventLoopImpl
53 // generate an idle event, return TRUE if more idle time requested
56 // set/get the exit code
57 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
58 int GetExitCode() const { return m_exitcode
; }
60 void SetKeepLooping(bool k
) { m_keepLooping
= k
; }
61 bool GetKeepLooping() const { return m_keepLooping
; }
65 // the exit code of the event loop
67 // FALSE if the loop should end
71 // ============================================================================
72 // wxEventLoopImpl implementation
73 // ============================================================================
75 void wxEventLoopImpl::Dispatch()
79 // VS: The code bellow is equal to MGL's EVT_halt implementation, with
80 // two things added: sleeping (busy waiting is stupid, lets make CPU's
81 // life a bit easier) and timers updating
83 // EVT_halt(&evt, EVT_EVERYEVT);
87 wxTimer::NotifyTimers();
88 MGL_wmUpdateDC(g_winMng
);
91 if ( EVT_getNext(&evt
, EVT_EVERYEVT
) ) break;
96 MGL_wmProcessEvent(g_winMng
, &evt
);
99 bool wxEventLoopImpl::SendIdleEvent()
103 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
106 // ============================================================================
107 // wxEventLoop implementation
108 // ============================================================================
110 // ----------------------------------------------------------------------------
111 // wxEventLoop running and exiting
112 // ----------------------------------------------------------------------------
114 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
116 wxEventLoop::~wxEventLoop()
118 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
121 bool wxEventLoop::IsRunning() const
123 return m_impl
!= NULL
;
126 int wxEventLoop::Run()
128 // event loops are not recursive, you need to create another loop!
129 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
131 m_impl
= new wxEventLoopImpl
;
133 wxEventLoop
*oldLoop
= ms_activeLoop
;
134 ms_activeLoop
= this;
139 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
140 #endif // wxUSE_THREADS
142 // generate and process idle events for as long as we don't have
143 // anything else to do
144 while ( !Pending() && m_impl
->SendIdleEvent() ) {}
146 // a message came or no more idle processing to do, sit in Dispatch()
147 // waiting for the next message
155 int exitcode
= m_impl
->GetExitCode();
159 ms_activeLoop
= oldLoop
;
164 void wxEventLoop::Exit(int rc
)
166 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
168 m_impl
->SetExitCode(rc
);
169 m_impl
->SetKeepLooping(FALSE
);
171 // Send a dummy event so that the app won't block in EVT_halt if there
172 // are no user-generated events in the queue:
173 EVT_post(0, EVT_USEREVT
, 0, 0);
176 // ----------------------------------------------------------------------------
177 // wxEventLoop message processing dispatching
178 // ----------------------------------------------------------------------------
180 bool wxEventLoop::Pending() const
183 return EVT_peekNext(&evt
, EVT_EVERYEVT
);
186 bool wxEventLoop::Dispatch()
188 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
191 return m_impl
->GetKeepLooping();