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"
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 MGL_wmUpdateDC(g_winMng
);
80 EVT_halt(&evt
, EVT_EVERYEVT
);
81 MGL_wmProcessEvent(g_winMng
, &evt
);
84 bool wxEventLoopImpl::SendIdleEvent()
88 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
91 // ============================================================================
92 // wxEventLoop implementation
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxEventLoop running and exiting
97 // ----------------------------------------------------------------------------
99 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
101 wxEventLoop::~wxEventLoop()
103 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
106 bool wxEventLoop::IsRunning() const
108 return m_impl
!= NULL
;
111 int wxEventLoop::Run()
113 // event loops are not recursive, you need to create another loop!
114 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
116 m_impl
= new wxEventLoopImpl
;
118 wxEventLoop
*oldLoop
= ms_activeLoop
;
119 ms_activeLoop
= this;
124 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
125 #endif // wxUSE_THREADS
127 // generate and process idle events for as long as we don't have
128 // anything else to do
129 while ( !Pending() && m_impl
->SendIdleEvent() ) {}
131 // a message came or no more idle processing to do, sit in Dispatch()
132 // waiting for the next message
140 int exitcode
= m_impl
->GetExitCode();
144 ms_activeLoop
= oldLoop
;
149 void wxEventLoop::Exit(int rc
)
151 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
153 m_impl
->SetExitCode(rc
);
154 m_impl
->SetKeepLooping(FALSE
);
156 // Send a dummy event so that the app won't block in EVT_halt if there
157 // are no user-generated events in the queue:
158 EVT_post(0, EVT_USEREVT
, 0, 0);
161 // ----------------------------------------------------------------------------
162 // wxEventLoop message processing dispatching
163 // ----------------------------------------------------------------------------
165 bool wxEventLoop::Pending() const
168 return EVT_peekNext(&evt
, EVT_EVERYEVT
);
171 bool wxEventLoop::Dispatch()
173 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
176 return m_impl
->GetKeepLooping();