1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/evtloop.cpp
3 // Purpose: implements wxEventLoop for MSW
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: 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"
31 #include "wx/window.h"
37 #include "wx/thread.h"
38 #include "wx/except.h"
39 #include "wx/msw/private.h"
40 #include "wx/scopeguard.h"
43 #include "wx/tooltip.h"
45 // define the list of MSG strutures
46 WX_DECLARE_LIST(MSG
, wxMsgList
);
48 #include "wx/listimpl.cpp"
50 WX_DEFINE_LIST(wxMsgList
)
51 #endif // wxUSE_THREADS
56 // ============================================================================
57 // wxMSWEventLoopBase implementation
58 // ============================================================================
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 wxMSWEventLoopBase::wxMSWEventLoopBase()
70 // ----------------------------------------------------------------------------
71 // wxEventLoop message processing dispatching
72 // ----------------------------------------------------------------------------
74 bool wxMSWEventLoopBase::Pending() const
77 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
80 bool wxMSWEventLoopBase::GetNextMessage(WXMSG
* msg
)
82 wxCHECK_MSG( IsRunning(), false, _T("can't get messages if not running") );
84 const BOOL rc
= ::GetMessage(msg
, NULL
, 0, 0);
94 // should never happen, but let's test for it nevertheless
95 wxLogLastError(wxT("GetMessage"));
97 // still break from the loop
104 int wxMSWEventLoopBase::GetNextMessageTimeout(WXMSG
*msg
, unsigned long timeout
)
106 // MsgWaitForMultipleObjects() won't notice any input which was already
107 // examined (e.g. using PeekMessage()) but not yet removed from the queue
108 // so we need to remove any immediately messages manually
110 // NB: using MsgWaitForMultipleObjectsEx() could simplify the code here but
111 // it is not available in very old Windows versions
112 if ( !::PeekMessage(msg
, 0, 0, 0, PM_REMOVE
) )
114 // we use this function just in order to not block longer than the
115 // given timeout, so we don't pass any handles to it at all
116 DWORD rc
= ::MsgWaitForMultipleObjects
127 wxLogDebug("unexpected MsgWaitForMultipleObjects() return "
135 if ( !::PeekMessage(msg
, 0, 0, 0, PM_REMOVE
) )
137 // somehow it may happen that MsgWaitForMultipleObjects()
138 // returns true but there are no messages -- just treat it
139 // the same as timeout then
146 return msg
->message
!= WM_QUIT
;
154 // ============================================================================
155 // GUI wxEventLoop implementation
156 // ============================================================================
158 wxWindowMSW
*wxGUIEventLoop::ms_winCritical
= NULL
;
160 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
164 if ( win
== ms_winCritical
)
167 win
= win
->GetParent();
173 bool wxGUIEventLoop::PreProcessMessage(WXMSG
*msg
)
175 HWND hwnd
= msg
->hwnd
;
176 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
179 // this might happen if we're in a modeless dialog, or if a wx control has
180 // children which themselves were not created by wx (i.e. wxActiveX control children)
183 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
185 hwnd
= ::GetParent(hwnd
);
187 // If the control has a wx parent, break and give the parent a chance
188 // to process the window message
189 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
196 // this may happen if the event occurred in a standard modeless dialog (the
197 // only example of which I know of is the find/replace dialog) - then call
198 // IsDialogMessage() to make TAB navigation in it work
200 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
201 // them) if we call it for the control itself
202 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
206 if ( !AllowProcessing(wndThis
) )
208 // not a child of critical window, so we eat the event but take care to
209 // stop an endless stream of WM_PAINTs which would have resulted if we
210 // didn't validate the invalidated part of the window
211 if ( msg
->message
== WM_PAINT
)
212 ::ValidateRect(hwnd
, NULL
);
218 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
219 // popup the tooltip bubbles
220 if ( msg
->message
== WM_MOUSEMOVE
)
222 // we should do it if one of window children has an associated tooltip
223 // (and not just if the window has a tooltip itself)
224 if ( wndThis
->HasToolTips() )
225 wxToolTip::RelayEvent((WXMSG
*)msg
);
227 #endif // wxUSE_TOOLTIPS
229 // allow the window to prevent certain messages from being
230 // translated/processed (this is currently used by wxTextCtrl to always
231 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
232 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
237 // try translations first: the accelerators override everything
238 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
240 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
243 // stop at first top level window, i.e. don't try to process the key
244 // strokes originating in a dialog using the accelerators of the parent
245 // frame - this doesn't make much sense
246 if ( wnd
->IsTopLevel() )
250 // now try the other hooks (kbd navigation is handled here)
251 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
253 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
256 // also stop at first top level window here, just as above because
257 // if we don't do this, pressing ESC on a modal dialog shown as child
258 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
259 // be closed, for example
260 if ( wnd
->IsTopLevel() )
264 // no special preprocessing for this message, dispatch it normally
268 void wxGUIEventLoop::ProcessMessage(WXMSG
*msg
)
270 // give us the chance to preprocess the message first
271 if ( !PreProcessMessage(msg
) )
273 // if it wasn't done, dispatch it to the corresponding window
274 ::TranslateMessage(msg
);
275 ::DispatchMessage(msg
);
279 bool wxGUIEventLoop::Dispatch()
282 if ( !GetNextMessage(&msg
) )
286 wxASSERT_MSG( wxThread::IsMain(),
287 wxT("only the main thread can process Windows messages") );
289 static bool s_hadGuiLock
= true;
290 static wxMsgList s_aSavedMessages
;
292 // if a secondary thread owning the mutex is doing GUI calls, save all
293 // messages for later processing - we can't process them right now because
294 // it will lead to recursive library calls (and we're not reentrant)
295 if ( !wxGuiOwnedByMainThread() )
297 s_hadGuiLock
= false;
299 // leave out WM_COMMAND messages: too dangerous, sometimes
300 // the message will be processed twice
301 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
303 MSG
* pMsg
= new MSG(msg
);
304 s_aSavedMessages
.Append(pMsg
);
311 // have we just regained the GUI lock? if so, post all of the saved
314 // FIXME of course, it's not _exactly_ the same as processing the
315 // messages normally - expect some things to break...
320 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
323 MSG
* pMsg
= node
->GetData();
324 s_aSavedMessages
.Erase(node
);
326 ProcessMessage(pMsg
);
329 node
= s_aSavedMessages
.GetFirst();
333 #endif // wxUSE_THREADS
335 ProcessMessage(&msg
);
340 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout
)
343 int rc
= GetNextMessageTimeout(&msg
, timeout
);
347 ProcessMessage(&msg
);
352 void wxGUIEventLoop::OnNextIteration()
355 wxMutexGuiLeaveOrEnter();
356 #endif // wxUSE_THREADS
359 void wxGUIEventLoop::WakeUp()
361 ::PostMessage(NULL
, WM_NULL
, 0, 0);
365 // ----------------------------------------------------------------------------
366 // Yield to incoming messages
367 // ----------------------------------------------------------------------------
369 #include <wx/arrimpl.cpp>
370 WX_DEFINE_OBJARRAY(wxMSGArray
);
372 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
374 // set the flag and don't forget to reset it before returning
375 m_isInsideYield
= true;
376 m_eventsToProcessInsideYield
= eventsToProcess
;
378 wxON_BLOCK_EXIT_SET(m_isInsideYield
, false);
381 // disable log flushing from here because a call to wxYield() shouldn't
382 // normally result in message boxes popping up &c
385 // ensure the logs will be flashed again when we exit
386 wxON_BLOCK_EXIT0(wxLog::Resume
);
389 // we don't want to process WM_QUIT from here - it should be processed in
390 // the main event loop in order to stop it
392 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
393 msg
.message
!= WM_QUIT
)
396 wxMutexGuiLeaveOrEnter();
397 #endif // wxUSE_THREADS
399 if (msg
.message
== WM_PAINT
)
401 // WM_PAINT messages are the last ones of the queue...
405 // choose a wxEventCategory for this Windows message
410 case WM_NCLBUTTONDOWN
:
412 case WM_NCLBUTTONDBLCLK
:
413 case WM_NCRBUTTONDOWN
:
415 case WM_NCRBUTTONDBLCLK
:
416 case WM_NCMBUTTONDOWN
:
418 case WM_NCMBUTTONDBLCLK
:
432 case WM_IME_STARTCOMPOSITION
:
433 case WM_IME_ENDCOMPOSITION
:
434 case WM_IME_COMPOSITION
:
438 case WM_IME_SETCONTEXT
:
441 case WM_IME_COMPOSITIONFULL
:
448 #ifdef WM_NCMOUSELEAVE
449 case WM_NCMOUSELEAVE
:
462 case WM_LBUTTONDBLCLK
:
465 case WM_RBUTTONDBLCLK
:
468 case WM_MBUTTONDBLCLK
:
470 cat
= wxEVT_CATEGORY_USER_INPUT
;
474 cat
= wxEVT_CATEGORY_TIMER
;
478 if (msg
.message
< WM_USER
)
480 // 0;WM_USER-1 is the range of message IDs reserved for use
483 // there are too many of these types of messages to handle
484 // them in this switch
485 cat
= wxEVT_CATEGORY_UI
;
488 cat
= wxEVT_CATEGORY_UNKNOWN
;
491 // should we process this event now?
492 if (cat
& eventsToProcess
)
494 if ( !wxTheApp
->Dispatch() )
499 // remove the message and store it
500 ::GetMessage(&msg
, NULL
, 0, 0);
505 // if there are pending events, we must process them.
506 ProcessPendingEvents();
508 // put back unprocessed events in the queue
509 DWORD id
= GetCurrentThreadId();
510 for (size_t i
=0; i
<m_arrMSG
.GetCount(); i
++)
512 PostThreadMessage(id
, m_arrMSG
[i
].message
,
513 m_arrMSG
[i
].wParam
, m_arrMSG
[i
].lParam
);
525 // ============================================================================
526 // wxConsoleEventLoop implementation
527 // ============================================================================
529 #if wxUSE_CONSOLE_EVENTLOOP
531 void wxConsoleEventLoop::WakeUp()
534 wxWakeUpMainThread();
538 void wxConsoleEventLoop::ProcessMessage(WXMSG
*msg
)
540 if ( msg
->message
== WM_TIMER
)
542 TIMERPROC proc
= (TIMERPROC
)msg
->lParam
;
544 (*proc
)(NULL
, 0, msg
->wParam
, 0);
548 ::DispatchMessage(msg
);
552 bool wxConsoleEventLoop::Dispatch()
555 if ( !GetNextMessage(&msg
) )
558 ProcessMessage(&msg
);
560 return !m_shouldExit
;
563 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout
)
566 int rc
= GetNextMessageTimeout(&msg
, timeout
);
570 ProcessMessage(&msg
);
572 return !m_shouldExit
;
575 #endif // wxUSE_CONSOLE_EVENTLOOP