]> git.saurik.com Git - wxWidgets.git/blame - src/os2/evtloop.cpp
Don't crash in wxControl::GetLabel when getting the label
[wxWidgets.git] / src / os2 / evtloop.cpp
CommitLineData
251b80c4
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: os2/evtloop.cpp
3// Purpose: implements wxEventLoop for PM
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 01.06.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
6aa89a22 9// License: wxWindows licence
251b80c4
KB
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "evtloop.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/window.h"
33 #include "wx/app.h"
34#endif //WX_PRECOMP
35
36#include "wx/evtloop.h"
37#include "wx/tooltip.h"
38
39#include "wx/os2/private.h"
40
41#if wxUSE_THREADS
42 // define the array of QMSG strutures
43 WX_DECLARE_OBJARRAY(QMSG, wxMsgArray);
44 // VS: this is a bit dirty - it duplicates same declaration in app.cpp
45 // (and there's no WX_DEFINE_OBJARRAY for that reason - it is already
46 // defined in app.cpp).
47#endif
48
49extern HAB vHabMain;
50
51// ----------------------------------------------------------------------------
52// wxEventLoopImpl
53// ----------------------------------------------------------------------------
54
55class WXDLLEXPORT wxEventLoopImpl
56{
57public:
58 // ctor
59 wxEventLoopImpl() { SetExitCode(0); }
60
61 // process a message
62 void ProcessMessage(QMSG *msg);
63
64 // generate an idle message, return TRUE if more idle time requested
65 bool SendIdleMessage();
66
67 // set/get the exit code
68 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
69 int GetExitCode() const { return m_exitcode; }
70
71private:
72 // preprocess a message, return TRUE if processed (i.e. no further
73 // dispatching required)
74 bool PreProcessMessage(QMSG *msg);
75
76 // the exit code of the event loop
77 int m_exitcode;
78};
79
80// ============================================================================
81// wxEventLoopImpl implementation
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// wxEventLoopImpl message processing
86// ----------------------------------------------------------------------------
87
88void wxEventLoopImpl::ProcessMessage(QMSG *msg)
89{
90 // give us the chance to preprocess the message first
91 if ( !PreProcessMessage(msg) )
92 {
93 // if it wasn't done, dispatch it to the corresponding window
94 ::WinDispatchMsg(vHabMain, msg);
95 }
96}
97
98bool wxEventLoopImpl::PreProcessMessage(QMSG *msg)
99{
100 HWND hWnd = msg->hwnd;
101 wxWindow *wndThis = wxFindWinFromHandle((WXHWND)hWnd);
102
103#if wxUSE_TOOLTIPS
104 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
105 // popup the tooltip bubbles
106 if ( wndThis && (msg->message == WM_MOUSEMOVE) )
107 {
108 wxToolTip *tt = wndThis->GetToolTip();
109 if ( tt )
110 {
111 tt->RelayEvent((WXMSG *)msg);
112 }
113 }
114#endif // wxUSE_TOOLTIPS
115
116 // try translations first; find the youngest window with a translation
117 // table.
118 wxWindow *wnd;
119 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
120 {
121 if ( wnd->OS2TranslateMessage((WXMSG *)msg) )
122 return TRUE;
123 }
124
125 // Anyone for a non-translation message? Try youngest descendants first.
126 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
127 {
128 if ( wnd->OS2ProcessMessage((WXMSG *)msg) )
129 return TRUE;
130 }
131
132 return FALSE;
133}
134
135// ----------------------------------------------------------------------------
136// wxEventLoopImpl idle event processing
137// ----------------------------------------------------------------------------
138
139bool wxEventLoopImpl::SendIdleMessage()
140{
e39af974 141 return wxTheApp->ProcessIdle() ;
251b80c4
KB
142}
143
144// ============================================================================
145// wxEventLoop implementation
146// ============================================================================
147
148wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
149
150// ----------------------------------------------------------------------------
151// wxEventLoop running and exiting
152// ----------------------------------------------------------------------------
153
154wxEventLoop::~wxEventLoop()
155{
156 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
157}
158
159bool wxEventLoop::IsRunning() const
160{
161 return m_impl != NULL;
162}
163
164int wxEventLoop::Run()
165{
166 // event loops are not recursive, you need to create another loop!
167 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
168
169 m_impl = new wxEventLoopImpl;
170
171 wxEventLoop *oldLoop = ms_activeLoop;
172 ms_activeLoop = this;
173
174 for ( ;; )
175 {
176#if wxUSE_THREADS
177 wxMutexGuiLeaveOrEnter();
178#endif // wxUSE_THREADS
179
180 // generate and process idle events for as long as we don't have
181 // anything else to do
182 while ( !Pending() && m_impl->SendIdleMessage() )
183 ;
184
185 // a message came or no more idle processing to do, sit in Dispatch()
186 // waiting for the next message
187 if ( !Dispatch() )
188 {
189 // we got WM_QUIT
190 break;
191 }
192 }
193
194 int exitcode = m_impl->GetExitCode();
195 delete m_impl;
196 m_impl = NULL;
197
198 ms_activeLoop = oldLoop;
199
200 return exitcode;
201}
202
203void wxEventLoop::Exit(int rc)
204{
205 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
206
207 m_impl->SetExitCode(rc);
208
209 ::WinPostMsg(NULL, WM_QUIT, 0, 0);
210}
211
212// ----------------------------------------------------------------------------
213// wxEventLoop message processing dispatching
214// ----------------------------------------------------------------------------
215
216bool wxEventLoop::Pending() const
217{
218 QMSG msg;
219 return ::WinPeekMsg(vHabMain, &msg, 0, 0, 0, PM_NOREMOVE) != 0;
220}
221
222bool wxEventLoop::Dispatch()
223{
224 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
225
226 QMSG msg;
227 BOOL rc = ::WinGetMsg(vHabMain, &msg, (HWND) NULL, 0, 0);
228
229 if ( rc == 0 )
230 {
231 // got WM_QUIT
232 return FALSE;
233 }
234
235 if ( rc == -1 )
236 {
237 // should never happen, but let's test for it nevertheless
238 wxLogLastError(wxT("GetMessage"));
239
240 // still break from the loop
241 return FALSE;
242 }
243
244#if wxUSE_THREADS
245 wxASSERT_MSG( wxThread::IsMain(),
246 wxT("only the main thread can process Windows messages") );
247
248 static bool s_hadGuiLock = TRUE;
249 static wxMsgArray s_aSavedMessages;
250
251 // if a secondary thread owning the mutex is doing GUI calls, save all
252 // messages for later processing - we can't process them right now because
253 // it will lead to recursive library calls (and we're not reentrant)
254 if ( !wxGuiOwnedByMainThread() )
255 {
256 s_hadGuiLock = FALSE;
257
258 // leave out WM_COMMAND messages: too dangerous, sometimes
259 // the message will be processed twice
260 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
261 {
262 s_aSavedMessages.Add(msg);
263 }
264
265 return TRUE;
266 }
267 else
268 {
269 // have we just regained the GUI lock? if so, post all of the saved
270 // messages
271 //
272 // FIXME of course, it's not _exactly_ the same as processing the
273 // messages normally - expect some things to break...
274 if ( !s_hadGuiLock )
275 {
276 s_hadGuiLock = TRUE;
277
278 size_t count = s_aSavedMessages.Count();
279 for ( size_t n = 0; n < count; n++ )
280 {
281 MSG& msg = s_aSavedMessages[n];
282 m_impl->ProcessMessage(&msg);
283 }
284
285 s_aSavedMessages.Empty();
286 }
287 }
288#endif // wxUSE_THREADS
289
290 m_impl->ProcessMessage(&msg);
291
292 return TRUE;
293}
294