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"
29 #include "wx/window.h"
34 #include "wx/evtloop.h"
35 #include "wx/thread.h"
36 #include "wx/except.h"
37 #include "wx/msw/private.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
53 // ============================================================================
54 // wxMSWEventLoopBase implementation
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 wxMSWEventLoopBase::wxMSWEventLoopBase()
67 // ----------------------------------------------------------------------------
68 // wxEventLoop message processing dispatching
69 // ----------------------------------------------------------------------------
71 bool wxMSWEventLoopBase::Pending() const
74 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
77 bool wxMSWEventLoopBase::GetNextMessage(WXMSG
* msg
)
79 wxCHECK_MSG( IsRunning(), false, _T("can't get messages if not running") );
81 const BOOL rc
= ::GetMessage(msg
, NULL
, 0, 0);
91 // should never happen, but let's test for it nevertheless
92 wxLogLastError(wxT("GetMessage"));
94 // still break from the loop
101 int wxMSWEventLoopBase::GetNextMessageTimeout(WXMSG
*msg
, unsigned long timeout
)
103 // MsgWaitForMultipleObjects() won't notice any input which was already
104 // examined (e.g. using PeekMessage()) but not yet removed from the queue
105 // so we need to remove any immediately messages manually
107 // NB: using MsgWaitForMultipleObjectsEx() could simplify the code here but
108 // it is not available in very old Windows versions
109 if ( !::PeekMessage(msg
, 0, 0, 0, PM_REMOVE
) )
111 // we use this function just in order to not block longer than the
112 // given timeout, so we don't pass any handles to it at all
113 DWORD rc
= ::MsgWaitForMultipleObjects
124 wxLogDebug("unexpected MsgWaitForMultipleObjects() return "
132 if ( !::PeekMessage(msg
, 0, 0, 0, PM_REMOVE
) )
134 // somehow it may happen that MsgWaitForMultipleObjects()
135 // returns true but there are no messages -- just treat it
136 // the same as timeout then
143 return msg
->message
!= WM_QUIT
;
151 // ============================================================================
152 // GUI wxEventLoop implementation
153 // ============================================================================
155 wxWindowMSW
*wxGUIEventLoop::ms_winCritical
= NULL
;
157 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
161 if ( win
== ms_winCritical
)
164 win
= win
->GetParent();
170 bool wxGUIEventLoop::PreProcessMessage(WXMSG
*msg
)
172 HWND hwnd
= msg
->hwnd
;
173 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
176 // this might happen if we're in a modeless dialog, or if a wx control has
177 // children which themselves were not created by wx (i.e. wxActiveX control children)
180 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
182 hwnd
= ::GetParent(hwnd
);
184 // If the control has a wx parent, break and give the parent a chance
185 // to process the window message
186 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
193 // this may happen if the event occurred in a standard modeless dialog (the
194 // only example of which I know of is the find/replace dialog) - then call
195 // IsDialogMessage() to make TAB navigation in it work
197 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
198 // them) if we call it for the control itself
199 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
203 if ( !AllowProcessing(wndThis
) )
205 // not a child of critical window, so we eat the event but take care to
206 // stop an endless stream of WM_PAINTs which would have resulted if we
207 // didn't validate the invalidated part of the window
208 if ( msg
->message
== WM_PAINT
)
209 ::ValidateRect(hwnd
, NULL
);
215 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
216 // popup the tooltip bubbles
217 if ( msg
->message
== WM_MOUSEMOVE
)
219 // we should do it if one of window children has an associated tooltip
220 // (and not just if the window has a tooltip itself)
221 if ( wndThis
->HasToolTips() )
222 wxToolTip::RelayEvent((WXMSG
*)msg
);
224 #endif // wxUSE_TOOLTIPS
226 // allow the window to prevent certain messages from being
227 // translated/processed (this is currently used by wxTextCtrl to always
228 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
229 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
234 // try translations first: the accelerators override everything
235 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
237 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
240 // stop at first top level window, i.e. don't try to process the key
241 // strokes originating in a dialog using the accelerators of the parent
242 // frame - this doesn't make much sense
243 if ( wnd
->IsTopLevel() )
247 // now try the other hooks (kbd navigation is handled here)
248 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
250 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
253 // also stop at first top level window here, just as above because
254 // if we don't do this, pressing ESC on a modal dialog shown as child
255 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
256 // be closed, for example
257 if ( wnd
->IsTopLevel() )
261 // no special preprocessing for this message, dispatch it normally
265 void wxGUIEventLoop::ProcessMessage(WXMSG
*msg
)
267 // give us the chance to preprocess the message first
268 if ( !PreProcessMessage(msg
) )
270 // if it wasn't done, dispatch it to the corresponding window
271 ::TranslateMessage(msg
);
272 ::DispatchMessage(msg
);
276 bool wxGUIEventLoop::Dispatch()
279 if ( !GetNextMessage(&msg
) )
283 wxASSERT_MSG( wxThread::IsMain(),
284 wxT("only the main thread can process Windows messages") );
286 static bool s_hadGuiLock
= true;
287 static wxMsgList s_aSavedMessages
;
289 // if a secondary thread owning the mutex is doing GUI calls, save all
290 // messages for later processing - we can't process them right now because
291 // it will lead to recursive library calls (and we're not reentrant)
292 if ( !wxGuiOwnedByMainThread() )
294 s_hadGuiLock
= false;
296 // leave out WM_COMMAND messages: too dangerous, sometimes
297 // the message will be processed twice
298 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
300 MSG
* pMsg
= new MSG(msg
);
301 s_aSavedMessages
.Append(pMsg
);
308 // have we just regained the GUI lock? if so, post all of the saved
311 // FIXME of course, it's not _exactly_ the same as processing the
312 // messages normally - expect some things to break...
317 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
320 MSG
* pMsg
= node
->GetData();
321 s_aSavedMessages
.Erase(node
);
323 ProcessMessage(pMsg
);
326 node
= s_aSavedMessages
.GetFirst();
330 #endif // wxUSE_THREADS
332 ProcessMessage(&msg
);
337 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout
)
340 int rc
= GetNextMessageTimeout(&msg
, timeout
);
344 ProcessMessage(&msg
);
349 void wxGUIEventLoop::OnNextIteration()
352 wxMutexGuiLeaveOrEnter();
353 #endif // wxUSE_THREADS
356 void wxGUIEventLoop::WakeUp()
358 ::PostMessage(NULL
, WM_NULL
, 0, 0);
364 // ============================================================================
365 // wxConsoleEventLoop implementation
366 // ============================================================================
368 #if wxUSE_CONSOLE_EVENTLOOP
370 void wxConsoleEventLoop::WakeUp()
373 wxWakeUpMainThread();
377 void wxConsoleEventLoop::ProcessMessage(WXMSG
*msg
)
379 if ( msg
->message
== WM_TIMER
)
381 TIMERPROC proc
= (TIMERPROC
)msg
->lParam
;
383 (*proc
)(NULL
, 0, msg
->wParam
, 0);
387 ::DispatchMessage(msg
);
391 bool wxConsoleEventLoop::Dispatch()
394 if ( !GetNextMessage(&msg
) )
397 ProcessMessage(&msg
);
399 return !m_shouldExit
;
402 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout
)
405 int rc
= GetNextMessageTimeout(&msg
, timeout
);
409 ProcessMessage(&msg
);
411 return !m_shouldExit
;
414 #endif // wxUSE_CONSOLE_EVENTLOOP