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 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/window.h"
24 #include "wx/thread.h"
27 #include "wx/evtloop.h"
29 #include "wx/mgl/private.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 class WXDLLEXPORT wxEventLoopImpl
48 // generate an idle event, return TRUE if more idle time requested
51 // set/get the exit code
52 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
53 int GetExitCode() const { return m_exitcode
; }
55 void SetKeepLooping(bool k
) { m_keepLooping
= k
; }
56 bool GetKeepLooping() const { return m_keepLooping
; }
60 // the exit code of the event loop
62 // FALSE if the loop should end
66 // ============================================================================
67 // wxEventLoopImpl implementation
68 // ============================================================================
70 void wxEventLoopImpl::Dispatch()
74 // VS: The code bellow is equal to MGL's EVT_halt implementation, with
75 // two things added: sleeping (busy waiting is stupid, lets make CPU's
76 // life a bit easier) and timers updating
78 // EVT_halt(&evt, EVT_EVERYEVT);
82 wxTimer::NotifyTimers();
83 MGL_wmUpdateDC(g_winMng
);
86 if ( EVT_getNext(&evt
, EVT_EVERYEVT
) ) break;
91 MGL_wmProcessEvent(g_winMng
, &evt
);
94 bool wxEventLoopImpl::SendIdleEvent()
96 return wxTheApp
->ProcessIdle();
99 // ============================================================================
100 // wxEventLoop implementation
101 // ============================================================================
103 // ----------------------------------------------------------------------------
104 // wxEventLoop running and exiting
105 // ----------------------------------------------------------------------------
107 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
109 wxEventLoop::~wxEventLoop()
111 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
114 int wxEventLoop::Run()
116 // event loops are not recursive, you need to create another loop!
117 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
119 m_impl
= new wxEventLoopImpl
;
121 wxEventLoop
*oldLoop
= ms_activeLoop
;
122 ms_activeLoop
= 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
143 int exitcode
= m_impl
->GetExitCode();
147 ms_activeLoop
= oldLoop
;
152 void wxEventLoop::Exit(int rc
)
154 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
156 m_impl
->SetExitCode(rc
);
157 m_impl
->SetKeepLooping(FALSE
);
159 // Send a dummy event so that the app won't block in EVT_halt if there
160 // are no user-generated events in the queue:
161 EVT_post(0, EVT_USEREVT
, 0, 0);
164 // ----------------------------------------------------------------------------
165 // wxEventLoop message processing dispatching
166 // ----------------------------------------------------------------------------
168 bool wxEventLoop::Pending() const
170 // update the display here, so that wxYield refreshes display and
171 // changes take effect immediately, not after emptying events queue:
172 MGL_wmUpdateDC(g_winMng
);
174 // is there an event in the queue?
176 return EVT_peekNext(&evt
, EVT_EVERYEVT
);
179 bool wxEventLoop::Dispatch()
181 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
184 return m_impl
->GetKeepLooping();