]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/evtloop.cpp
more programmer-friendly wxApp::SetDisplayMode
[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 #include "wx/timer.h"
33 #include "wx/mgl/private.h"
34 #include "pmapi.h"
35
36 // ----------------------------------------------------------------------------
37 // wxEventLoopImpl
38 // ----------------------------------------------------------------------------
39
40 class WXDLLEXPORT wxEventLoopImpl
41 {
42 public:
43 // ctor
44 wxEventLoopImpl()
45 {
46 SetExitCode(0);
47 SetKeepLooping(TRUE);
48 }
49
50 // process an event
51 void Dispatch();
52
53 // generate an idle event, return TRUE if more idle time requested
54 bool SendIdleEvent();
55
56 // set/get the exit code
57 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
58 int GetExitCode() const { return m_exitcode; }
59
60 void SetKeepLooping(bool k) { m_keepLooping = k; }
61 bool GetKeepLooping() const { return m_keepLooping; }
62
63 private:
64
65 // the exit code of the event loop
66 int m_exitcode;
67 // FALSE if the loop should end
68 bool m_keepLooping;
69 };
70
71 // ============================================================================
72 // wxEventLoopImpl implementation
73 // ============================================================================
74
75 void wxEventLoopImpl::Dispatch()
76 {
77 event_t evt;
78
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
82
83 // EVT_halt(&evt, EVT_EVERYEVT);
84 for (;;)
85 {
86 #if wxUSE_TIMER
87 wxTimer::NotifyTimers();
88 MGL_wmUpdateDC(g_winMng);
89 #endif
90 EVT_pollJoystick();
91 if ( EVT_getNext(&evt, EVT_EVERYEVT) ) break;
92 PM_sleep(10);
93 }
94 // end of EVT_halt
95
96 MGL_wmProcessEvent(g_winMng, &evt);
97 }
98
99 bool wxEventLoopImpl::SendIdleEvent()
100 {
101 wxIdleEvent event;
102
103 return wxTheApp->ProcessEvent(event) && event.MoreRequested();
104 }
105
106 // ============================================================================
107 // wxEventLoop implementation
108 // ============================================================================
109
110 // ----------------------------------------------------------------------------
111 // wxEventLoop running and exiting
112 // ----------------------------------------------------------------------------
113
114 wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
115
116 wxEventLoop::~wxEventLoop()
117 {
118 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
119 }
120
121 bool wxEventLoop::IsRunning() const
122 {
123 return m_impl != NULL;
124 }
125
126 int wxEventLoop::Run()
127 {
128 // event loops are not recursive, you need to create another loop!
129 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
130
131 m_impl = new wxEventLoopImpl;
132
133 wxEventLoop *oldLoop = ms_activeLoop;
134 ms_activeLoop = this;
135
136 for ( ;; )
137 {
138 #if wxUSE_THREADS
139 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
140 #endif // wxUSE_THREADS
141
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() ) {}
145
146 // a message came or no more idle processing to do, sit in Dispatch()
147 // waiting for the next message
148 if ( !Dispatch() )
149 {
150 // app terminated
151 break;
152 }
153 }
154
155 int exitcode = m_impl->GetExitCode();
156 delete m_impl;
157 m_impl = NULL;
158
159 ms_activeLoop = oldLoop;
160
161 return exitcode;
162 }
163
164 void wxEventLoop::Exit(int rc)
165 {
166 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
167
168 m_impl->SetExitCode(rc);
169 m_impl->SetKeepLooping(FALSE);
170
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);
174 }
175
176 // ----------------------------------------------------------------------------
177 // wxEventLoop message processing dispatching
178 // ----------------------------------------------------------------------------
179
180 bool wxEventLoop::Pending() const
181 {
182 event_t evt;
183 return EVT_peekNext(&evt, EVT_EVERYEVT);
184 }
185
186 bool wxEventLoop::Dispatch()
187 {
188 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
189
190 m_impl->Dispatch();
191 return m_impl->GetKeepLooping();
192 }