]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/evtloop.cpp
check that filename pointer is non-NULL before using it, not after (coverity warning...
[wxWidgets.git] / src / mgl / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/evtloop.cpp
3 // Purpose: implements wxEventLoop for MGL
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // License: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/window.h"
23 #include "wx/app.h"
24 #include "wx/thread.h"
25 #include "wx/timer.h"
26 #endif //WX_PRECOMP
27
28 #include "wx/evtloop.h"
29 #include "wx/mgl/private.h"
30
31 // ----------------------------------------------------------------------------
32 // wxEventLoopImpl
33 // ----------------------------------------------------------------------------
34
35 class WXDLLEXPORT wxEventLoopImpl
36 {
37 public:
38 // ctor
39 wxEventLoopImpl()
40 {
41 SetExitCode(0);
42 SetKeepLooping(true);
43 }
44
45 // process an event
46 void Dispatch();
47
48 // generate an idle event, return true if more idle time requested
49 bool SendIdleEvent();
50
51 // set/get the exit code
52 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
53 int GetExitCode() const { return m_exitcode; }
54
55 void SetKeepLooping(bool k) { m_keepLooping = k; }
56 bool GetKeepLooping() const { return m_keepLooping; }
57
58 private:
59
60 // the exit code of the event loop
61 int m_exitcode;
62 // false if the loop should end
63 bool m_keepLooping;
64 };
65
66 // ============================================================================
67 // wxEventLoopImpl implementation
68 // ============================================================================
69
70 void wxEventLoopImpl::Dispatch()
71 {
72 event_t evt;
73
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
77
78 // EVT_halt(&evt, EVT_EVERYEVT);
79 for (;;)
80 {
81 #if wxUSE_TIMER
82 wxTimer::NotifyTimers();
83 MGL_wmUpdateDC(g_winMng);
84 #endif
85 EVT_pollJoystick();
86 if ( EVT_getNext(&evt, EVT_EVERYEVT) ) break;
87 PM_sleep(10);
88 }
89 // end of EVT_halt
90
91 MGL_wmProcessEvent(g_winMng, &evt);
92 }
93
94 bool wxEventLoopImpl::SendIdleEvent()
95 {
96 return wxTheApp->ProcessIdle();
97 }
98
99 // ============================================================================
100 // wxEventLoop implementation
101 // ============================================================================
102
103 // ----------------------------------------------------------------------------
104 // wxEventLoop running and exiting
105 // ----------------------------------------------------------------------------
106
107 wxEventLoop::~wxEventLoop()
108 {
109 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
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 wxEventLoopActivator activate(this);
120
121 for ( ;; )
122 {
123 #if wxUSE_THREADS
124 //wxMutexGuiLeaveOrEnter(); // FIXME_MGL - huh?
125 #endif // wxUSE_THREADS
126
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() ) {}
130
131 // a message came or no more idle processing to do, sit in Dispatch()
132 // waiting for the next message
133 if ( !Dispatch() )
134 {
135 // app terminated
136 break;
137 }
138 }
139
140 OnExit();
141
142 int exitcode = m_impl->GetExitCode();
143 delete m_impl;
144 m_impl = NULL;
145
146 return exitcode;
147 }
148
149 void wxEventLoop::Exit(int rc)
150 {
151 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
152
153 m_impl->SetExitCode(rc);
154 m_impl->SetKeepLooping(false);
155
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);
159 }
160
161 // ----------------------------------------------------------------------------
162 // wxEventLoop message processing dispatching
163 // ----------------------------------------------------------------------------
164
165 bool wxEventLoop::Pending() const
166 {
167 // update the display here, so that wxYield refreshes display and
168 // changes take effect immediately, not after emptying events queue:
169 MGL_wmUpdateDC(g_winMng);
170
171 // is there an event in the queue?
172 event_t evt;
173 return (bool)(EVT_peekNext(&evt, EVT_EVERYEVT));
174 }
175
176 bool wxEventLoop::Dispatch()
177 {
178 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
179
180 m_impl->Dispatch();
181 return m_impl->GetKeepLooping();
182 }