]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
missing commit
[wxWidgets.git] / src / msw / evtloop.cpp
CommitLineData
3808e191 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/msw/evtloop.cpp
e27f5540 3// Purpose: implements wxEventLoop for wxMSW port
3808e191
JS
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>
526954c5 9// Licence: 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
1e04d2bf
PC
27#include "wx/evtloop.h"
28
3808e191 29#ifndef WX_PRECOMP
e27f5540 30 #include "wx/window.h"
6c0e8b4e 31 #include "wx/app.h"
1e04d2bf 32 #include "wx/log.h"
3808e191
JS
33#endif //WX_PRECOMP
34
204abcd4 35#include "wx/thread.h"
4300caa7 36#include "wx/except.h"
3808e191 37#include "wx/msw/private.h"
dde19c21 38#include "wx/scopeguard.h"
3808e191 39
e27f5540
VZ
40#include "wx/tooltip.h"
41#if wxUSE_THREADS
42 // define the list of MSG strutures
43 WX_DECLARE_LIST(MSG, wxMsgList);
9af42efd 44
e27f5540 45 #include "wx/listimpl.cpp"
81df6af3 46
e27f5540
VZ
47 WX_DEFINE_LIST(wxMsgList)
48#endif // wxUSE_THREADS
81df6af3
VZ
49
50// ============================================================================
51// GUI wxEventLoop implementation
52// ============================================================================
53
54wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL;
55
56bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
a7b7500c
VZ
57{
58 while ( win )
59 {
60 if ( win == ms_winCritical )
61 return true;
62
63 win = win->GetParent();
64 }
65
66 return false;
67}
68
81df6af3 69bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg)
3808e191 70{
ac8d0c11 71 HWND hwnd = msg->hwnd;
89c83180 72 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
a7b7500c 73 wxWindow *wnd;
ac8d0c11 74
18f5234f
DS
75 // this might happen if we're in a modeless dialog, or if a wx control has
76 // children which themselves were not created by wx (i.e. wxActiveX control children)
ac8d0c11
VZ
77 if ( !wndThis )
78 {
18f5234f 79 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
ac8d0c11
VZ
80 {
81 hwnd = ::GetParent(hwnd);
89c83180 82
18f5234f
DS
83 // If the control has a wx parent, break and give the parent a chance
84 // to process the window message
85 wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
86 if (wndThis != NULL)
87 break;
ac8d0c11
VZ
88 }
89
18f5234f
DS
90 if ( !wndThis )
91 {
92 // this may happen if the event occurred in a standard modeless dialog (the
93 // only example of which I know of is the find/replace dialog) - then call
94 // IsDialogMessage() to make TAB navigation in it work
95
96 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
97 // them) if we call it for the control itself
98 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
99 }
ac8d0c11 100 }
3808e191 101
a7b7500c
VZ
102 if ( !AllowProcessing(wndThis) )
103 {
104 // not a child of critical window, so we eat the event but take care to
105 // stop an endless stream of WM_PAINTs which would have resulted if we
106 // didn't validate the invalidated part of the window
107 if ( msg->message == WM_PAINT )
108 ::ValidateRect(hwnd, NULL);
109
110 return true;
111 }
112
3808e191
JS
113#if wxUSE_TOOLTIPS
114 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
115 // popup the tooltip bubbles
ac8d0c11 116 if ( msg->message == WM_MOUSEMOVE )
3808e191 117 {
c009bf3e
VZ
118 // we should do it if one of window children has an associated tooltip
119 // (and not just if the window has a tooltip itself)
120 if ( wndThis->HasToolTips() )
121 wxToolTip::RelayEvent((WXMSG *)msg);
3808e191
JS
122 }
123#endif // wxUSE_TOOLTIPS
124
ac8d0c11
VZ
125 // allow the window to prevent certain messages from being
126 // translated/processed (this is currently used by wxTextCtrl to always
127 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
128 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
129 {
3754265e 130 return false;
ac8d0c11
VZ
131 }
132
133 // try translations first: the accelerators override everything
3808e191
JS
134 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
135 {
ac8d0c11 136 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3754265e 137 return true;
ac8d0c11
VZ
138
139 // stop at first top level window, i.e. don't try to process the key
140 // strokes originating in a dialog using the accelerators of the parent
141 // frame - this doesn't make much sense
142 if ( wnd->IsTopLevel() )
143 break;
3808e191
JS
144 }
145
22cfea03
JS
146 // now try the other hooks (kbd navigation is handled here)
147 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
3808e191 148 {
f6388667
VZ
149 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
150 return true;
c8026dea 151
f6388667
VZ
152 // also stop at first top level window here, just as above because
153 // if we don't do this, pressing ESC on a modal dialog shown as child
154 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
155 // be closed, for example
156 if ( wnd->IsTopLevel() )
22cfea03 157 break;
3808e191
JS
158 }
159
ac8d0c11 160 // no special preprocessing for this message, dispatch it normally
3754265e 161 return false;
cea62f9c
JS
162}
163
81df6af3 164void wxGUIEventLoop::ProcessMessage(WXMSG *msg)
3808e191 165{
81df6af3
VZ
166 // give us the chance to preprocess the message first
167 if ( !PreProcessMessage(msg) )
168 {
169 // if it wasn't done, dispatch it to the corresponding window
170 ::TranslateMessage(msg);
171 ::DispatchMessage(msg);
172 }
3808e191
JS
173}
174
81df6af3 175bool wxGUIEventLoop::Dispatch()
3808e191 176{
3808e191 177 MSG msg;
81df6af3 178 if ( !GetNextMessage(&msg) )
3754265e 179 return false;
3808e191
JS
180
181#if wxUSE_THREADS
182 wxASSERT_MSG( wxThread::IsMain(),
183 wxT("only the main thread can process Windows messages") );
184
3754265e 185 static bool s_hadGuiLock = true;
9af08eb8 186 static wxMsgList s_aSavedMessages;
3808e191
JS
187
188 // if a secondary thread owning the mutex is doing GUI calls, save all
189 // messages for later processing - we can't process them right now because
190 // it will lead to recursive library calls (and we're not reentrant)
191 if ( !wxGuiOwnedByMainThread() )
192 {
3754265e 193 s_hadGuiLock = false;
3808e191
JS
194
195 // leave out WM_COMMAND messages: too dangerous, sometimes
196 // the message will be processed twice
6c0e8b4e 197 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 198 {
9af08eb8
VZ
199 MSG* pMsg = new MSG(msg);
200 s_aSavedMessages.Append(pMsg);
3808e191
JS
201 }
202
3754265e 203 return true;
3808e191
JS
204 }
205 else
206 {
207 // have we just regained the GUI lock? if so, post all of the saved
208 // messages
209 //
210 // FIXME of course, it's not _exactly_ the same as processing the
211 // messages normally - expect some things to break...
212 if ( !s_hadGuiLock )
213 {
3754265e 214 s_hadGuiLock = true;
3808e191 215
9af08eb8
VZ
216 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
217 while (node)
3808e191 218 {
9af08eb8
VZ
219 MSG* pMsg = node->GetData();
220 s_aSavedMessages.Erase(node);
221
222 ProcessMessage(pMsg);
223 delete pMsg;
3808e191 224
9af08eb8
VZ
225 node = s_aSavedMessages.GetFirst();
226 }
3808e191
JS
227 }
228 }
229#endif // wxUSE_THREADS
230
3754265e 231 ProcessMessage(&msg);
3808e191 232
3754265e 233 return true;
3808e191
JS
234}
235
9af42efd
VZ
236int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
237{
238 MSG msg;
239 int rc = GetNextMessageTimeout(&msg, timeout);
240 if ( rc != 1 )
241 return rc;
242
243 ProcessMessage(&msg);
244
245 return 1;
246}
247
81df6af3
VZ
248void wxGUIEventLoop::OnNextIteration()
249{
250#if wxUSE_THREADS
251 wxMutexGuiLeaveOrEnter();
252#endif // wxUSE_THREADS
253}
254
255void wxGUIEventLoop::WakeUp()
256{
257 ::PostMessage(NULL, WM_NULL, 0, 0);
258}
259
dde19c21
FM
260
261// ----------------------------------------------------------------------------
262// Yield to incoming messages
263// ----------------------------------------------------------------------------
264
265#include <wx/arrimpl.cpp>
266WX_DEFINE_OBJARRAY(wxMSGArray);
267
268bool wxGUIEventLoop::YieldFor(long eventsToProcess)
269{
270 // set the flag and don't forget to reset it before returning
271 m_isInsideYield = true;
272 m_eventsToProcessInsideYield = eventsToProcess;
273
274 wxON_BLOCK_EXIT_SET(m_isInsideYield, false);
275
276#if wxUSE_LOG
277 // disable log flushing from here because a call to wxYield() shouldn't
278 // normally result in message boxes popping up &c
279 wxLog::Suspend();
280
281 // ensure the logs will be flashed again when we exit
282 wxON_BLOCK_EXIT0(wxLog::Resume);
283#endif // wxUSE_LOG
284
285 // we don't want to process WM_QUIT from here - it should be processed in
286 // the main event loop in order to stop it
287 MSG msg;
9d2e29d2 288 int nPaintsReceived = 0;
dde19c21
FM
289 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
290 msg.message != WM_QUIT )
291 {
292#if wxUSE_THREADS
293 wxMutexGuiLeaveOrEnter();
294#endif // wxUSE_THREADS
295
296 if (msg.message == WM_PAINT)
297 {
9d2e29d2
FM
298 // NOTE: WM_PAINTs are categorized as wxEVT_CATEGORY_UI
299 if ((eventsToProcess & wxEVT_CATEGORY_UI) == 0)
300 {
301 // this msg is not going to be dispatched...
03647350 302 // however WM_PAINT is special: until there are damaged
9d2e29d2
FM
303 // windows, Windows will keep sending it forever!
304 if (nPaintsReceived > 10)
305 {
306 // we got 10 WM_PAINT consecutive messages...
307 // we must have reached the tail of the message queue:
308 // we're now getting _only_ WM_PAINT events and this will
309 // continue forever (since we don't dispatch them
310 // because of the user-specified eventsToProcess mask)...
311 // break out of this loop!
312 break;
313 }
314 else
315 nPaintsReceived++;
316 }
317 //else: we're going to dispatch it below,
318 // so we don't need to take any special action
319 }
320 else
321 {
322 // reset the counter of consecutive WM_PAINT messages received:
323 nPaintsReceived = 0;
dde19c21
FM
324 }
325
326 // choose a wxEventCategory for this Windows message
4934653e 327 bool processNow;
dde19c21
FM
328 switch (msg.message)
329 {
dd07a4ce 330#if !defined(__WXWINCE__)
03647350 331 case WM_NCMOUSEMOVE:
dd07a4ce 332
03647350 333 case WM_NCLBUTTONDOWN:
dde19c21
FM
334 case WM_NCLBUTTONUP:
335 case WM_NCLBUTTONDBLCLK:
336 case WM_NCRBUTTONDOWN:
337 case WM_NCRBUTTONUP:
338 case WM_NCRBUTTONDBLCLK:
339 case WM_NCMBUTTONDOWN:
340 case WM_NCMBUTTONUP:
341 case WM_NCMBUTTONDBLCLK:
dd07a4ce 342#endif
dde19c21
FM
343
344 case WM_KEYDOWN:
345 case WM_KEYUP:
346 case WM_CHAR:
347 case WM_DEADCHAR:
348 case WM_SYSKEYDOWN:
349 case WM_SYSKEYUP:
350 case WM_SYSCHAR:
351 case WM_SYSDEADCHAR:
352#ifdef WM_UNICHAR
353 case WM_UNICHAR:
354#endif
355 case WM_HOTKEY:
356 case WM_IME_STARTCOMPOSITION:
357 case WM_IME_ENDCOMPOSITION:
358 case WM_IME_COMPOSITION:
359 case WM_COMMAND:
360 case WM_SYSCOMMAND:
361
362 case WM_IME_SETCONTEXT:
363 case WM_IME_NOTIFY:
364 case WM_IME_CONTROL:
365 case WM_IME_COMPOSITIONFULL:
366 case WM_IME_SELECT:
367 case WM_IME_CHAR:
368 case WM_IME_KEYDOWN:
369 case WM_IME_KEYUP:
370
dd07a4ce 371#if !defined(__WXWINCE__)
dde19c21 372 case WM_MOUSEHOVER:
dd07a4ce
FM
373 case WM_MOUSELEAVE:
374#endif
dde19c21
FM
375#ifdef WM_NCMOUSELEAVE
376 case WM_NCMOUSELEAVE:
377#endif
dde19c21
FM
378
379 case WM_CUT:
380 case WM_COPY:
381 case WM_PASTE:
382 case WM_CLEAR:
383 case WM_UNDO:
384
385 case WM_MOUSEMOVE:
386 case WM_LBUTTONDOWN:
387 case WM_LBUTTONUP:
388 case WM_LBUTTONDBLCLK:
389 case WM_RBUTTONDOWN:
390 case WM_RBUTTONUP:
391 case WM_RBUTTONDBLCLK:
392 case WM_MBUTTONDOWN:
393 case WM_MBUTTONUP:
394 case WM_MBUTTONDBLCLK:
395 case WM_MOUSEWHEEL:
4934653e 396 processNow = (eventsToProcess & wxEVT_CATEGORY_USER_INPUT) != 0;
dde19c21
FM
397 break;
398
399 case WM_TIMER:
4934653e 400 processNow = (eventsToProcess & wxEVT_CATEGORY_TIMER) != 0;
dde19c21
FM
401 break;
402
403 default:
404 if (msg.message < WM_USER)
405 {
406 // 0;WM_USER-1 is the range of message IDs reserved for use
407 // by the system.
dde19c21
FM
408 // there are too many of these types of messages to handle
409 // them in this switch
4934653e 410 processNow = (eventsToProcess & wxEVT_CATEGORY_UI) != 0;
dde19c21
FM
411 }
412 else
4934653e
VZ
413 {
414 // Process all the unknown messages. We must do it because
415 // failure to process some of them can be fatal, e.g. if we
416 // don't dispatch WM_APP+2 then embedded IE ActiveX
417 // controls don't work any more, see #14027. And there may
418 // be more examples like this, so dispatch all unknown
419 // messages immediately to be safe.
420 processNow = true;
421 }
dde19c21
FM
422 }
423
424 // should we process this event now?
4934653e 425 if ( processNow )
dde19c21
FM
426 {
427 if ( !wxTheApp->Dispatch() )
428 break;
429 }
430 else
431 {
432 // remove the message and store it
433 ::GetMessage(&msg, NULL, 0, 0);
434 m_arrMSG.Add(msg);
435 }
436 }
437
438 // if there are pending events, we must process them.
834fcdd9
FM
439 if (wxTheApp)
440 wxTheApp->ProcessPendingEvents();
dde19c21
FM
441
442 // put back unprocessed events in the queue
443 DWORD id = GetCurrentThreadId();
444 for (size_t i=0; i<m_arrMSG.GetCount(); i++)
445 {
446 PostThreadMessage(id, m_arrMSG[i].message,
447 m_arrMSG[i].wParam, m_arrMSG[i].lParam);
448 }
449
450 m_arrMSG.Clear();
451
452 return true;
453}