]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
Do not allow events to fire after socket is closed (fixes crash on GTK with GDK_Input...
[wxWidgets.git] / src / msw / evtloop.cpp
CommitLineData
3808e191
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/evtloop.cpp
3// Purpose: implements wxEventLoop for MSW
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>
65571936 9// License: wxWindows licence
3808e191
JS
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
3808e191
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
6c0e8b4e
VZ
28 #include "wx/window.h"
29 #include "wx/app.h"
3808e191
JS
30#endif //WX_PRECOMP
31
32#include "wx/evtloop.h"
4300caa7 33
0cd7d9b2 34#include "wx/tooltip.h"
4300caa7
VZ
35#include "wx/except.h"
36#include "wx/ptr_scpd.h"
3808e191
JS
37
38#include "wx/msw/private.h"
39
0cd7d9b2 40#if wxUSE_THREADS
4300caa7
VZ
41 #include "wx/thread.h"
42
9af08eb8
VZ
43 // define the list of MSG strutures
44 WX_DECLARE_LIST(MSG, wxMsgList);
4300caa7 45
9af08eb8 46 #include "wx/listimpl.cpp"
4300caa7 47
259c43f6 48 WX_DEFINE_LIST(wxMsgList)
4300caa7 49#endif // wxUSE_THREADS
0cd7d9b2 50
4300caa7
VZ
51// ----------------------------------------------------------------------------
52// helper class
53// ----------------------------------------------------------------------------
54
4300caa7
VZ
55// this object sets the wxEventLoop given to the ctor as the currently active
56// one and unsets it in its dtor
57class wxEventLoopActivator
58{
59public:
60 wxEventLoopActivator(wxEventLoop **pActive,
61 wxEventLoop *evtLoop)
62 {
63 m_pActive = pActive;
64 m_evtLoopOld = *pActive;
65 *pActive = evtLoop;
66 }
67
68 ~wxEventLoopActivator()
69 {
70 // restore the previously active event loop
71 *m_pActive = m_evtLoopOld;
72 }
73
74private:
75 wxEventLoop *m_evtLoopOld;
76 wxEventLoop **m_pActive;
77};
78
3808e191 79// ============================================================================
3754265e 80// wxEventLoop implementation
3808e191
JS
81// ============================================================================
82
3754265e 83wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
b4626104 84wxWindowMSW *wxEventLoop::ms_winCritical = NULL;
3754265e 85
3808e191 86// ----------------------------------------------------------------------------
3754265e 87// ctor/dtor
3808e191
JS
88// ----------------------------------------------------------------------------
89
3754265e
VZ
90wxEventLoop::wxEventLoop()
91{
92 m_shouldExit = false;
93 m_exitcode = 0;
94}
95
96// ----------------------------------------------------------------------------
97// wxEventLoop message processing
98// ----------------------------------------------------------------------------
99
100void wxEventLoop::ProcessMessage(WXMSG *msg)
3808e191
JS
101{
102 // give us the chance to preprocess the message first
103 if ( !PreProcessMessage(msg) )
104 {
105 // if it wasn't done, dispatch it to the corresponding window
106 ::TranslateMessage(msg);
107 ::DispatchMessage(msg);
108 }
109}
110
b4626104 111bool wxEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
a7b7500c
VZ
112{
113 while ( win )
114 {
115 if ( win == ms_winCritical )
116 return true;
117
118 win = win->GetParent();
119 }
120
121 return false;
122}
123
3754265e 124bool wxEventLoop::PreProcessMessage(WXMSG *msg)
3808e191 125{
ac8d0c11 126 HWND hwnd = msg->hwnd;
a7b7500c
VZ
127 wxWindow * const wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
128 wxWindow *wnd;
ac8d0c11 129
3103e8a9 130 // this may happen if the event occurred in a standard modeless dialog (the
ac8d0c11
VZ
131 // only example of which I know of is the find/replace dialog) - then call
132 // IsDialogMessage() to make TAB navigation in it work
133 if ( !wndThis )
134 {
135 // we need to find the dialog containing this control as
3754265e 136 // IsDialogMessage() just eats all the messages (i.e. returns true for
ac8d0c11
VZ
137 // them) if we call it for the control itself
138 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
139 {
140 hwnd = ::GetParent(hwnd);
141 }
142
143 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
144 }
3808e191 145
a7b7500c
VZ
146 if ( !AllowProcessing(wndThis) )
147 {
148 // not a child of critical window, so we eat the event but take care to
149 // stop an endless stream of WM_PAINTs which would have resulted if we
150 // didn't validate the invalidated part of the window
151 if ( msg->message == WM_PAINT )
152 ::ValidateRect(hwnd, NULL);
153
154 return true;
155 }
156
3808e191
JS
157#if wxUSE_TOOLTIPS
158 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
159 // popup the tooltip bubbles
ac8d0c11 160 if ( msg->message == WM_MOUSEMOVE )
3808e191
JS
161 {
162 wxToolTip *tt = wndThis->GetToolTip();
163 if ( tt )
164 {
165 tt->RelayEvent((WXMSG *)msg);
166 }
167 }
168#endif // wxUSE_TOOLTIPS
169
ac8d0c11
VZ
170 // allow the window to prevent certain messages from being
171 // translated/processed (this is currently used by wxTextCtrl to always
172 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
173 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
174 {
3754265e 175 return false;
ac8d0c11
VZ
176 }
177
178 // try translations first: the accelerators override everything
3808e191
JS
179 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
180 {
ac8d0c11 181 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3754265e 182 return true;
ac8d0c11
VZ
183
184 // stop at first top level window, i.e. don't try to process the key
185 // strokes originating in a dialog using the accelerators of the parent
186 // frame - this doesn't make much sense
187 if ( wnd->IsTopLevel() )
188 break;
3808e191
JS
189 }
190
22cfea03
JS
191 // now try the other hooks (kbd navigation is handled here)
192 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
3808e191 193 {
22cfea03
JS
194 if (wnd != wndThis) // Skip the first since wndThis->MSWProcessMessage() was called above
195 {
196 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
197 return true;
198 }
199
200 // Stop at first top level window (as per comment above).
201 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
202 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
203 if (wnd->IsTopLevel())
204 break;
3808e191
JS
205 }
206
ac8d0c11 207 // no special preprocessing for this message, dispatch it normally
3754265e 208 return false;
cea62f9c
JS
209}
210
3808e191
JS
211// ----------------------------------------------------------------------------
212// wxEventLoop running and exiting
213// ----------------------------------------------------------------------------
214
3808e191
JS
215bool wxEventLoop::IsRunning() const
216{
3754265e 217 return ms_activeLoop == this;
3808e191
JS
218}
219
220int wxEventLoop::Run()
221{
222 // event loops are not recursive, you need to create another loop!
223 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
224
3754265e 225 // ProcessIdle() and Dispatch() below may throw so the code here should
4300caa7
VZ
226 // be exception-safe, hence we must use local objects for all actions we
227 // should undo
228 wxEventLoopActivator activate(&ms_activeLoop, this);
4300caa7 229
96d38c7e
VS
230 // we must ensure that OnExit() is called even if an exception is thrown
231 // from inside Dispatch() but we must call it from Exit() in normal
232 // situations because it is supposed to be called synchronously,
233 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
234 // something similar here)
cea62f9c 235#if wxUSE_EXCEPTIONS
96d38c7e 236 for ( ;; )
3808e191 237 {
96d38c7e 238 try
2a8f35c3 239 {
96d38c7e 240#endif // wxUSE_EXCEPTIONS
d284c075 241
96d38c7e
VS
242 // this is the event loop itself
243 for ( ;; )
244 {
245 #if wxUSE_THREADS
246 wxMutexGuiLeaveOrEnter();
247 #endif // wxUSE_THREADS
248
249 // generate and process idle events for as long as we don't
250 // have anything else to do
251 while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) )
252 ;
253
254 // if the "should exit" flag is set, the loop should terminate
255 // but not before processing any remaining messages so while
256 // Pending() returns true, do process them
257 if ( m_shouldExit )
258 {
259 while ( Pending() )
260 Dispatch();
261
262 break;
263 }
264
265 // a message came or no more idle processing to do, sit in
266 // Dispatch() waiting for the next message
267 if ( !Dispatch() )
268 {
269 // we got WM_QUIT
270 break;
271 }
d284c075 272 }
924b84ab 273
96d38c7e
VS
274#if wxUSE_EXCEPTIONS
275 // exit the outer loop as well
276 break;
277 }
278 catch ( ... )
279 {
280 try
281 {
282 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
283 {
284 OnExit();
285 break;
286 }
287 //else: continue running the event loop
288 }
289 catch ( ... )
d284c075 290 {
96d38c7e
VS
291 // OnException() throwed, possibly rethrowing the same
292 // exception again: very good, but we still need OnExit() to
293 // be called
294 OnExit();
295 throw;
d284c075
VZ
296 }
297 }
3808e191 298 }
d284c075 299#endif // wxUSE_EXCEPTIONS
3808e191 300
3754265e 301 return m_exitcode;
3808e191
JS
302}
303
304void wxEventLoop::Exit(int rc)
305{
306 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
307
3754265e
VZ
308 m_exitcode = rc;
309 m_shouldExit = true;
3808e191 310
2a8f35c3
VZ
311 OnExit();
312
f7550d73
VZ
313 // all we have to do to exit from the loop is to (maybe) wake it up so that
314 // it can notice that Exit() had been called
315 //
316 // in particular, we do *not* use PostQuitMessage() here because we're not
317 // sure that WM_QUIT is going to be processed by the correct event loop: it
318 // is possible that another one is started before this one has a chance to
319 // process WM_QUIT
320 ::PostMessage(NULL, WM_NULL, 0, 0);
3808e191
JS
321}
322
323// ----------------------------------------------------------------------------
324// wxEventLoop message processing dispatching
325// ----------------------------------------------------------------------------
326
327bool wxEventLoop::Pending() const
328{
329 MSG msg;
330 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
331}
332
333bool wxEventLoop::Dispatch()
334{
3754265e 335 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
3808e191
JS
336
337 MSG msg;
338 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
339
340 if ( rc == 0 )
341 {
342 // got WM_QUIT
3754265e 343 return false;
3808e191
JS
344 }
345
346 if ( rc == -1 )
347 {
348 // should never happen, but let's test for it nevertheless
349 wxLogLastError(wxT("GetMessage"));
350
351 // still break from the loop
3754265e 352 return false;
3808e191
JS
353 }
354
355#if wxUSE_THREADS
356 wxASSERT_MSG( wxThread::IsMain(),
357 wxT("only the main thread can process Windows messages") );
358
3754265e 359 static bool s_hadGuiLock = true;
9af08eb8 360 static wxMsgList s_aSavedMessages;
3808e191
JS
361
362 // if a secondary thread owning the mutex is doing GUI calls, save all
363 // messages for later processing - we can't process them right now because
364 // it will lead to recursive library calls (and we're not reentrant)
365 if ( !wxGuiOwnedByMainThread() )
366 {
3754265e 367 s_hadGuiLock = false;
3808e191
JS
368
369 // leave out WM_COMMAND messages: too dangerous, sometimes
370 // the message will be processed twice
6c0e8b4e 371 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 372 {
9af08eb8
VZ
373 MSG* pMsg = new MSG(msg);
374 s_aSavedMessages.Append(pMsg);
3808e191
JS
375 }
376
3754265e 377 return true;
3808e191
JS
378 }
379 else
380 {
381 // have we just regained the GUI lock? if so, post all of the saved
382 // messages
383 //
384 // FIXME of course, it's not _exactly_ the same as processing the
385 // messages normally - expect some things to break...
386 if ( !s_hadGuiLock )
387 {
3754265e 388 s_hadGuiLock = true;
3808e191 389
9af08eb8
VZ
390 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
391 while (node)
3808e191 392 {
9af08eb8
VZ
393 MSG* pMsg = node->GetData();
394 s_aSavedMessages.Erase(node);
395
396 ProcessMessage(pMsg);
397 delete pMsg;
3808e191 398
9af08eb8
VZ
399 node = s_aSavedMessages.GetFirst();
400 }
3808e191
JS
401 }
402 }
403#endif // wxUSE_THREADS
404
3754265e 405 ProcessMessage(&msg);
3808e191 406
3754265e 407 return true;
3808e191
JS
408}
409