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/ptr_scpd.h"
38 #include "wx/msw/private.h"
41 #include "wx/tooltip.h"
43 // define the list of MSG strutures
44 WX_DECLARE_LIST(MSG
, wxMsgList
);
46 #include "wx/listimpl.cpp"
48 WX_DEFINE_LIST(wxMsgList
)
49 #endif // wxUSE_THREADS
54 // ============================================================================
55 // wxMSWEventLoopBase implementation
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 wxMSWEventLoopBase::wxMSWEventLoopBase()
68 // ----------------------------------------------------------------------------
69 // wxEventLoop message processing dispatching
70 // ----------------------------------------------------------------------------
72 bool wxMSWEventLoopBase::Pending() const
75 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
78 bool wxMSWEventLoopBase::GetNextMessage(WXMSG
* msg
)
80 wxCHECK_MSG( IsRunning(), false, _T("can't get messages if not running") );
82 const BOOL rc
= ::GetMessage(msg
, NULL
, 0, 0);
92 // should never happen, but let's test for it nevertheless
93 wxLogLastError(wxT("GetMessage"));
95 // still break from the loop
102 int wxMSWEventLoopBase::GetNextMessageTimeout(WXMSG
*msg
, unsigned long timeout
)
104 // MsgWaitForMultipleObjects() won't notice any input which was already
105 // examined (e.g. using PeekMessage()) but not yet removed from the queue
106 // so we need to remove any immediately messages manually
108 // NB: using MsgWaitForMultipleObjectsEx() could simplify the code here but
109 // it is not available in very old Windows versions
110 if ( !::PeekMessage(msg
, 0, 0, 0, PM_REMOVE
) )
112 // we use this function just in order to not block longer than the
113 // given timeout, so we don't pass any handles to it at all
114 if ( ::MsgWaitForMultipleObjects
125 if ( !::PeekMessage(msg
, 0, 0, 0, PM_REMOVE
) )
127 wxFAIL_MSG( _T("PeekMessage() should have succeeded") );
133 return msg
->message
!= WM_QUIT
;
141 // ============================================================================
142 // GUI wxEventLoop implementation
143 // ============================================================================
145 wxWindowMSW
*wxGUIEventLoop::ms_winCritical
= NULL
;
147 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
151 if ( win
== ms_winCritical
)
154 win
= win
->GetParent();
160 bool wxGUIEventLoop::PreProcessMessage(WXMSG
*msg
)
162 HWND hwnd
= msg
->hwnd
;
163 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
166 // this might happen if we're in a modeless dialog, or if a wx control has
167 // children which themselves were not created by wx (i.e. wxActiveX control children)
170 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
172 hwnd
= ::GetParent(hwnd
);
174 // If the control has a wx parent, break and give the parent a chance
175 // to process the window message
176 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
183 // this may happen if the event occurred in a standard modeless dialog (the
184 // only example of which I know of is the find/replace dialog) - then call
185 // IsDialogMessage() to make TAB navigation in it work
187 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
188 // them) if we call it for the control itself
189 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
193 if ( !AllowProcessing(wndThis
) )
195 // not a child of critical window, so we eat the event but take care to
196 // stop an endless stream of WM_PAINTs which would have resulted if we
197 // didn't validate the invalidated part of the window
198 if ( msg
->message
== WM_PAINT
)
199 ::ValidateRect(hwnd
, NULL
);
205 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
206 // popup the tooltip bubbles
207 if ( msg
->message
== WM_MOUSEMOVE
)
209 // we should do it if one of window children has an associated tooltip
210 // (and not just if the window has a tooltip itself)
211 if ( wndThis
->HasToolTips() )
212 wxToolTip::RelayEvent((WXMSG
*)msg
);
214 #endif // wxUSE_TOOLTIPS
216 // allow the window to prevent certain messages from being
217 // translated/processed (this is currently used by wxTextCtrl to always
218 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
219 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
224 // try translations first: the accelerators override everything
225 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
227 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
230 // stop at first top level window, i.e. don't try to process the key
231 // strokes originating in a dialog using the accelerators of the parent
232 // frame - this doesn't make much sense
233 if ( wnd
->IsTopLevel() )
237 // now try the other hooks (kbd navigation is handled here)
238 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
240 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
243 // also stop at first top level window here, just as above because
244 // if we don't do this, pressing ESC on a modal dialog shown as child
245 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
246 // be closed, for example
247 if ( wnd
->IsTopLevel() )
251 // no special preprocessing for this message, dispatch it normally
255 void wxGUIEventLoop::ProcessMessage(WXMSG
*msg
)
257 // give us the chance to preprocess the message first
258 if ( !PreProcessMessage(msg
) )
260 // if it wasn't done, dispatch it to the corresponding window
261 ::TranslateMessage(msg
);
262 ::DispatchMessage(msg
);
266 bool wxGUIEventLoop::Dispatch()
269 if ( !GetNextMessage(&msg
) )
273 wxASSERT_MSG( wxThread::IsMain(),
274 wxT("only the main thread can process Windows messages") );
276 static bool s_hadGuiLock
= true;
277 static wxMsgList s_aSavedMessages
;
279 // if a secondary thread owning the mutex is doing GUI calls, save all
280 // messages for later processing - we can't process them right now because
281 // it will lead to recursive library calls (and we're not reentrant)
282 if ( !wxGuiOwnedByMainThread() )
284 s_hadGuiLock
= false;
286 // leave out WM_COMMAND messages: too dangerous, sometimes
287 // the message will be processed twice
288 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
290 MSG
* pMsg
= new MSG(msg
);
291 s_aSavedMessages
.Append(pMsg
);
298 // have we just regained the GUI lock? if so, post all of the saved
301 // FIXME of course, it's not _exactly_ the same as processing the
302 // messages normally - expect some things to break...
307 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
310 MSG
* pMsg
= node
->GetData();
311 s_aSavedMessages
.Erase(node
);
313 ProcessMessage(pMsg
);
316 node
= s_aSavedMessages
.GetFirst();
320 #endif // wxUSE_THREADS
322 ProcessMessage(&msg
);
327 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout
)
330 int rc
= GetNextMessageTimeout(&msg
, timeout
);
334 ProcessMessage(&msg
);
339 void wxGUIEventLoop::OnNextIteration()
342 wxMutexGuiLeaveOrEnter();
343 #endif // wxUSE_THREADS
346 void wxGUIEventLoop::WakeUp()
348 ::PostMessage(NULL
, WM_NULL
, 0, 0);
353 #if wxUSE_CONSOLE_EVENTLOOP
355 void wxConsoleEventLoop::OnNextIteration()
358 wxTheApp
->ProcessPendingEvents();
361 void wxConsoleEventLoop::WakeUp()
364 wxWakeUpMainThread();
368 void wxConsoleEventLoop::ProcessMessage(WXMSG
*msg
)
370 if ( msg
->message
== WM_TIMER
)
372 TIMERPROC proc
= (TIMERPROC
)msg
->lParam
;
374 (*proc
)(NULL
, 0, msg
->wParam
, 0);
378 ::DispatchMessage(msg
);
382 bool wxConsoleEventLoop::Dispatch()
385 if ( !GetNextMessage(&msg
) )
388 ProcessMessage(&msg
);
390 return !m_shouldExit
;
393 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout
)
396 int rc
= GetNextMessageTimeout(&msg
, timeout
);
400 ProcessMessage(&msg
);
402 return !m_shouldExit
;
405 #endif // wxUSE_CONSOLE_EVENTLOOP