bugfixes, bugfixes, bugfixes...
[wxWidgets.git] / src / mgl / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/evtloop.cpp
3 // Purpose: implements wxEventLoop for MGL
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // License: wxWindows license
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #ifdef __GNUG__
15 #pragma implementation "evtloop.h"
16 #endif
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/window.h"
27 #include "wx/app.h"
28 #include "wx/thread.h"
29 #endif //WX_PRECOMP
30
31 #include "wx/evtloop.h"
32
33 #include "wx/mgl/private.h"
34
35 // ----------------------------------------------------------------------------
36 // wxEventLoopImpl
37 // ----------------------------------------------------------------------------
38
39 class WXDLLEXPORT wxEventLoopImpl
40 {
41 public:
42 // ctor
43 wxEventLoopImpl()
44 {
45 SetExitCode(0);
46 SetKeepLooping(TRUE);
47 }
48
49 // process an event
50 void Dispatch();
51
52 // generate an idle event, return TRUE if more idle time requested
53 bool SendIdleEvent();
54
55 // set/get the exit code
56 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
57 int GetExitCode() const { return m_exitcode; }
58
59 void SetKeepLooping(bool k) { m_keepLooping = k; }
60 bool GetKeepLooping() const { return m_keepLooping; }
61
62 private:
63
64 // the exit code of the event loop
65 int m_exitcode;
66 // FALSE if the loop should end
67 bool m_keepLooping;
68 };
69
70 // ============================================================================
71 // wxEventLoopImpl implementation
72 // ============================================================================
73
74 void wxEventLoopImpl::Dispatch()
75 {
76 event_t evt;
77 ibool rc;
78
79 MGL_wmUpdateDC(g_winMng);
80
81 EVT_halt(&evt, EVT_EVERYEVT);
82 MGL_wmProcessEvent(g_winMng, &evt);
83 }
84
85 bool wxEventLoopImpl::SendIdleEvent()
86 {
87 wxIdleEvent event;
88
89 return wxTheApp->ProcessEvent(event) && event.MoreRequested();
90 }
91
92 // ============================================================================
93 // wxEventLoop implementation
94 // ============================================================================
95
96 // ----------------------------------------------------------------------------
97 // wxEventLoop running and exiting
98 // ----------------------------------------------------------------------------
99
100 wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
101
102 wxEventLoop::~wxEventLoop()
103 {
104 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
105 }
106
107 bool wxEventLoop::IsRunning() const
108 {
109 return m_impl != NULL;
110 }
111
112 int wxEventLoop::Run()
113 {
114 // event loops are not recursive, you need to create another loop!
115 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
116
117 m_impl = new wxEventLoopImpl;
118
119 wxEventLoop *oldLoop = ms_activeLoop;
120 ms_activeLoop = this;
121
122 for ( ;; )
123 {
124 #if wxUSE_THREADS
125 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
126 #endif // wxUSE_THREADS
127
128 // generate and process idle events for as long as we don't have
129 // anything else to do
130 while ( !Pending() && m_impl->SendIdleEvent() ) {}
131
132 // a message came or no more idle processing to do, sit in Dispatch()
133 // waiting for the next message
134 if ( !Dispatch() )
135 {
136 // app terminated
137 break;
138 }
139 }
140
141 int exitcode = m_impl->GetExitCode();
142 delete m_impl;
143 m_impl = NULL;
144
145 ms_activeLoop = oldLoop;
146
147 return exitcode;
148 }
149
150 void wxEventLoop::Exit(int rc)
151 {
152 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
153
154 m_impl->SetExitCode(rc);
155 m_impl->SetKeepLooping(FALSE);
156 }
157
158 // ----------------------------------------------------------------------------
159 // wxEventLoop message processing dispatching
160 // ----------------------------------------------------------------------------
161
162 bool wxEventLoop::Pending() const
163 {
164 event_t evt;
165 return EVT_peekNext(&evt, EVT_EVERYEVT);
166 }
167
168 bool wxEventLoop::Dispatch()
169 {
170 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
171
172 m_impl->Dispatch();
173 return m_impl->GetKeepLooping();
174 }