1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/evtloop.cpp
3 // Purpose: implements wxEventLoop for wxMSW port
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/evtloop.h"
29 #include "wx/window.h"
34 #include "wx/thread.h"
35 #include "wx/except.h"
36 #include "wx/msw/private.h"
37 #include "wx/scopeguard.h"
39 #include "wx/tooltip.h"
41 // define the list of MSG strutures
42 WX_DECLARE_LIST(MSG
, wxMsgList
);
44 #include "wx/listimpl.cpp"
46 WX_DEFINE_LIST(wxMsgList
)
47 #endif // wxUSE_THREADS
49 // ============================================================================
50 // GUI wxEventLoop implementation
51 // ============================================================================
53 wxWindowMSW
*wxGUIEventLoop::ms_winCritical
= NULL
;
55 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
59 if ( win
== ms_winCritical
)
62 win
= win
->GetParent();
68 bool wxGUIEventLoop::PreProcessMessage(WXMSG
*msg
)
70 HWND hwnd
= msg
->hwnd
;
71 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
74 // this might happen if we're in a modeless dialog, or if a wx control has
75 // children which themselves were not created by wx (i.e. wxActiveX control children)
78 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
80 hwnd
= ::GetParent(hwnd
);
82 // If the control has a wx parent, break and give the parent a chance
83 // to process the window message
84 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
91 // this may happen if the event occurred in a standard modeless dialog (the
92 // only example of which I know of is the find/replace dialog) - then call
93 // IsDialogMessage() to make TAB navigation in it work
95 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
96 // them) if we call it for the control itself
97 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
101 if ( !AllowProcessing(wndThis
) )
103 // not a child of critical window, so we eat the event but take care to
104 // stop an endless stream of WM_PAINTs which would have resulted if we
105 // didn't validate the invalidated part of the window
106 if ( msg
->message
== WM_PAINT
)
107 ::ValidateRect(hwnd
, NULL
);
113 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
114 // popup the tooltip bubbles
115 if ( msg
->message
== WM_MOUSEMOVE
)
117 // we should do it if one of window children has an associated tooltip
118 // (and not just if the window has a tooltip itself)
119 if ( wndThis
->HasToolTips() )
120 wxToolTip::RelayEvent((WXMSG
*)msg
);
122 #endif // wxUSE_TOOLTIPS
124 // allow the window to prevent certain messages from being
125 // translated/processed (this is currently used by wxTextCtrl to always
126 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
127 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
132 // try translations first: the accelerators override everything
133 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
135 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
138 // stop at first top level window, i.e. don't try to process the key
139 // strokes originating in a dialog using the accelerators of the parent
140 // frame - this doesn't make much sense
141 if ( wnd
->IsTopLevel() )
145 // now try the other hooks (kbd navigation is handled here)
146 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
148 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
151 // also stop at first top level window here, just as above because
152 // if we don't do this, pressing ESC on a modal dialog shown as child
153 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
154 // be closed, for example
155 if ( wnd
->IsTopLevel() )
159 // no special preprocessing for this message, dispatch it normally
163 void wxGUIEventLoop::ProcessMessage(WXMSG
*msg
)
165 // give us the chance to preprocess the message first
166 if ( !PreProcessMessage(msg
) )
168 // if it wasn't done, dispatch it to the corresponding window
169 ::TranslateMessage(msg
);
170 ::DispatchMessage(msg
);
174 bool wxGUIEventLoop::Dispatch()
177 if ( !GetNextMessage(&msg
) )
181 wxASSERT_MSG( wxThread::IsMain(),
182 wxT("only the main thread can process Windows messages") );
184 static bool s_hadGuiLock
= true;
185 static wxMsgList s_aSavedMessages
;
187 // if a secondary thread owning the mutex is doing GUI calls, save all
188 // messages for later processing - we can't process them right now because
189 // it will lead to recursive library calls (and we're not reentrant)
190 if ( !wxGuiOwnedByMainThread() )
192 s_hadGuiLock
= false;
194 // leave out WM_COMMAND messages: too dangerous, sometimes
195 // the message will be processed twice
196 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
198 MSG
* pMsg
= new MSG(msg
);
199 s_aSavedMessages
.Append(pMsg
);
206 // have we just regained the GUI lock? if so, post all of the saved
209 // FIXME of course, it's not _exactly_ the same as processing the
210 // messages normally - expect some things to break...
215 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
218 MSG
* pMsg
= node
->GetData();
219 s_aSavedMessages
.Erase(node
);
221 ProcessMessage(pMsg
);
224 node
= s_aSavedMessages
.GetFirst();
228 #endif // wxUSE_THREADS
230 ProcessMessage(&msg
);
235 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout
)
238 int rc
= GetNextMessageTimeout(&msg
, timeout
);
242 ProcessMessage(&msg
);
247 void wxGUIEventLoop::OnNextIteration()
250 wxMutexGuiLeaveOrEnter();
251 #endif // wxUSE_THREADS
254 void wxGUIEventLoop::WakeUp()
256 ::PostMessage(NULL
, WM_NULL
, 0, 0);
260 // ----------------------------------------------------------------------------
261 // Yield to incoming messages
262 // ----------------------------------------------------------------------------
264 #include <wx/arrimpl.cpp>
265 WX_DEFINE_OBJARRAY(wxMSGArray
);
267 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
269 // set the flag and don't forget to reset it before returning
270 m_isInsideYield
= true;
271 m_eventsToProcessInsideYield
= eventsToProcess
;
273 wxON_BLOCK_EXIT_SET(m_isInsideYield
, false);
276 // disable log flushing from here because a call to wxYield() shouldn't
277 // normally result in message boxes popping up &c
280 // ensure the logs will be flashed again when we exit
281 wxON_BLOCK_EXIT0(wxLog::Resume
);
284 // we don't want to process WM_QUIT from here - it should be processed in
285 // the main event loop in order to stop it
287 int nPaintsReceived
= 0;
288 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
289 msg
.message
!= WM_QUIT
)
292 wxMutexGuiLeaveOrEnter();
293 #endif // wxUSE_THREADS
295 if (msg
.message
== WM_PAINT
)
297 // NOTE: WM_PAINTs are categorized as wxEVT_CATEGORY_UI
298 if ((eventsToProcess
& wxEVT_CATEGORY_UI
) == 0)
300 // this msg is not going to be dispatched...
301 // however WM_PAINT is special: until there are damaged
302 // windows, Windows will keep sending it forever!
303 if (nPaintsReceived
> 10)
305 // we got 10 WM_PAINT consecutive messages...
306 // we must have reached the tail of the message queue:
307 // we're now getting _only_ WM_PAINT events and this will
308 // continue forever (since we don't dispatch them
309 // because of the user-specified eventsToProcess mask)...
310 // break out of this loop!
316 //else: we're going to dispatch it below,
317 // so we don't need to take any special action
321 // reset the counter of consecutive WM_PAINT messages received:
325 // choose a wxEventCategory for this Windows message
329 #if !defined(__WXWINCE__)
332 case WM_NCLBUTTONDOWN
:
334 case WM_NCLBUTTONDBLCLK
:
335 case WM_NCRBUTTONDOWN
:
337 case WM_NCRBUTTONDBLCLK
:
338 case WM_NCMBUTTONDOWN
:
340 case WM_NCMBUTTONDBLCLK
:
355 case WM_IME_STARTCOMPOSITION
:
356 case WM_IME_ENDCOMPOSITION
:
357 case WM_IME_COMPOSITION
:
361 case WM_IME_SETCONTEXT
:
364 case WM_IME_COMPOSITIONFULL
:
370 #if !defined(__WXWINCE__)
374 #ifdef WM_NCMOUSELEAVE
375 case WM_NCMOUSELEAVE
:
387 case WM_LBUTTONDBLCLK
:
390 case WM_RBUTTONDBLCLK
:
393 case WM_MBUTTONDBLCLK
:
395 processNow
= (eventsToProcess
& wxEVT_CATEGORY_USER_INPUT
) != 0;
399 processNow
= (eventsToProcess
& wxEVT_CATEGORY_TIMER
) != 0;
403 if (msg
.message
< WM_USER
)
405 // 0;WM_USER-1 is the range of message IDs reserved for use
407 // there are too many of these types of messages to handle
408 // them in this switch
409 processNow
= (eventsToProcess
& wxEVT_CATEGORY_UI
) != 0;
413 // Process all the unknown messages. We must do it because
414 // failure to process some of them can be fatal, e.g. if we
415 // don't dispatch WM_APP+2 then embedded IE ActiveX
416 // controls don't work any more, see #14027. And there may
417 // be more examples like this, so dispatch all unknown
418 // messages immediately to be safe.
423 // should we process this event now?
426 if ( !wxTheApp
->Dispatch() )
431 // remove the message and store it
432 ::GetMessage(&msg
, NULL
, 0, 0);
437 // if there are pending events, we must process them.
439 wxTheApp
->ProcessPendingEvents();
441 // put back unprocessed events in the queue
442 DWORD id
= GetCurrentThreadId();
443 for (size_t i
=0; i
<m_arrMSG
.GetCount(); i
++)
445 PostThreadMessage(id
, m_arrMSG
[i
].message
,
446 m_arrMSG
[i
].wParam
, m_arrMSG
[i
].lParam
);