1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/evtloop.cpp
3 // Purpose: implements wxEventLoop for wxMSW port
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/evtloop.h"
30 #include "wx/window.h"
35 #include "wx/thread.h"
36 #include "wx/except.h"
37 #include "wx/msw/private.h"
38 #include "wx/scopeguard.h"
40 #include "wx/tooltip.h"
42 // define the list of MSG strutures
43 WX_DECLARE_LIST(MSG
, wxMsgList
);
45 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxMsgList
)
48 #endif // wxUSE_THREADS
50 // ============================================================================
51 // GUI wxEventLoop implementation
52 // ============================================================================
54 wxWindowMSW
*wxGUIEventLoop::ms_winCritical
= NULL
;
56 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
60 if ( win
== ms_winCritical
)
63 win
= win
->GetParent();
69 bool wxGUIEventLoop::PreProcessMessage(WXMSG
*msg
)
71 HWND hwnd
= msg
->hwnd
;
72 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
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)
79 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
81 hwnd
= ::GetParent(hwnd
);
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
);
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
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;
102 if ( !AllowProcessing(wndThis
) )
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
);
114 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
115 // popup the tooltip bubbles
116 if ( msg
->message
== WM_MOUSEMOVE
)
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
);
123 #endif // wxUSE_TOOLTIPS
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
) )
133 // try translations first: the accelerators override everything
134 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
136 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
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() )
146 // now try the other hooks (kbd navigation is handled here)
147 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
149 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
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() )
160 // no special preprocessing for this message, dispatch it normally
164 void wxGUIEventLoop::ProcessMessage(WXMSG
*msg
)
166 // give us the chance to preprocess the message first
167 if ( !PreProcessMessage(msg
) )
169 // if it wasn't done, dispatch it to the corresponding window
170 ::TranslateMessage(msg
);
171 ::DispatchMessage(msg
);
175 bool wxGUIEventLoop::Dispatch()
178 if ( !GetNextMessage(&msg
) )
182 wxASSERT_MSG( wxThread::IsMain(),
183 wxT("only the main thread can process Windows messages") );
185 static bool s_hadGuiLock
= true;
186 static wxMsgList s_aSavedMessages
;
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() )
193 s_hadGuiLock
= false;
195 // leave out WM_COMMAND messages: too dangerous, sometimes
196 // the message will be processed twice
197 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
199 MSG
* pMsg
= new MSG(msg
);
200 s_aSavedMessages
.Append(pMsg
);
207 // have we just regained the GUI lock? if so, post all of the saved
210 // FIXME of course, it's not _exactly_ the same as processing the
211 // messages normally - expect some things to break...
216 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
219 MSG
* pMsg
= node
->GetData();
220 s_aSavedMessages
.Erase(node
);
222 ProcessMessage(pMsg
);
225 node
= s_aSavedMessages
.GetFirst();
229 #endif // wxUSE_THREADS
231 ProcessMessage(&msg
);
236 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout
)
239 int rc
= GetNextMessageTimeout(&msg
, timeout
);
243 ProcessMessage(&msg
);
248 void wxGUIEventLoop::OnNextIteration()
251 wxMutexGuiLeaveOrEnter();
252 #endif // wxUSE_THREADS
255 void wxGUIEventLoop::WakeUp()
257 ::PostMessage(NULL
, WM_NULL
, 0, 0);
261 // ----------------------------------------------------------------------------
262 // Yield to incoming messages
263 // ----------------------------------------------------------------------------
265 #include <wx/arrimpl.cpp>
266 WX_DEFINE_OBJARRAY(wxMSGArray
);
268 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
270 // set the flag and don't forget to reset it before returning
271 m_isInsideYield
= true;
272 m_eventsToProcessInsideYield
= eventsToProcess
;
274 wxON_BLOCK_EXIT_SET(m_isInsideYield
, false);
277 // disable log flushing from here because a call to wxYield() shouldn't
278 // normally result in message boxes popping up &c
281 // ensure the logs will be flashed again when we exit
282 wxON_BLOCK_EXIT0(wxLog::Resume
);
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
288 int nPaintsReceived
= 0;
289 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
290 msg
.message
!= WM_QUIT
)
293 wxMutexGuiLeaveOrEnter();
294 #endif // wxUSE_THREADS
296 if (msg
.message
== WM_PAINT
)
298 // NOTE: WM_PAINTs are categorized as wxEVT_CATEGORY_UI
299 if ((eventsToProcess
& wxEVT_CATEGORY_UI
) == 0)
301 // this msg is not going to be dispatched...
302 // however WM_PAINT is special: until there are damaged
303 // windows, Windows will keep sending it forever!
304 if (nPaintsReceived
> 10)
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!
317 //else: we're going to dispatch it below,
318 // so we don't need to take any special action
322 // reset the counter of consecutive WM_PAINT messages received:
326 // choose a wxEventCategory for this Windows message
330 #if !defined(__WXWINCE__)
333 case WM_NCLBUTTONDOWN
:
335 case WM_NCLBUTTONDBLCLK
:
336 case WM_NCRBUTTONDOWN
:
338 case WM_NCRBUTTONDBLCLK
:
339 case WM_NCMBUTTONDOWN
:
341 case WM_NCMBUTTONDBLCLK
:
356 case WM_IME_STARTCOMPOSITION
:
357 case WM_IME_ENDCOMPOSITION
:
358 case WM_IME_COMPOSITION
:
362 case WM_IME_SETCONTEXT
:
365 case WM_IME_COMPOSITIONFULL
:
371 #if !defined(__WXWINCE__)
375 #ifdef WM_NCMOUSELEAVE
376 case WM_NCMOUSELEAVE
:
388 case WM_LBUTTONDBLCLK
:
391 case WM_RBUTTONDBLCLK
:
394 case WM_MBUTTONDBLCLK
:
396 processNow
= (eventsToProcess
& wxEVT_CATEGORY_USER_INPUT
) != 0;
400 processNow
= (eventsToProcess
& wxEVT_CATEGORY_TIMER
) != 0;
404 if (msg
.message
< WM_USER
)
406 // 0;WM_USER-1 is the range of message IDs reserved for use
408 // there are too many of these types of messages to handle
409 // them in this switch
410 processNow
= (eventsToProcess
& wxEVT_CATEGORY_UI
) != 0;
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.
424 // should we process this event now?
427 if ( !wxTheApp
->Dispatch() )
432 // remove the message and store it
433 ::GetMessage(&msg
, NULL
, 0, 0);
438 // if there are pending events, we must process them.
440 wxTheApp
->ProcessPendingEvents();
442 // put back unprocessed events in the queue
443 DWORD id
= GetCurrentThreadId();
444 for (size_t i
=0; i
<m_arrMSG
.GetCount(); i
++)
446 PostThreadMessage(id
, m_arrMSG
[i
].message
,
447 m_arrMSG
[i
].wParam
, m_arrMSG
[i
].lParam
);