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"
28 #include "wx/window.h"
32 #include "wx/evtloop.h"
34 #include "wx/tooltip.h"
35 #include "wx/except.h"
36 #include "wx/ptr_scpd.h"
38 #include "wx/msw/private.h"
41 #include "wx/thread.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
51 // ============================================================================
52 // wxEventLoop implementation
53 // ============================================================================
55 wxWindowMSW
*wxEventLoop::ms_winCritical
= NULL
;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 wxEventLoop::wxEventLoop()
67 // ----------------------------------------------------------------------------
68 // wxEventLoop message processing
69 // ----------------------------------------------------------------------------
71 void wxEventLoop::ProcessMessage(WXMSG
*msg
)
73 // give us the chance to preprocess the message first
74 if ( !PreProcessMessage(msg
) )
76 // if it wasn't done, dispatch it to the corresponding window
77 ::TranslateMessage(msg
);
78 ::DispatchMessage(msg
);
82 bool wxEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
86 if ( win
== ms_winCritical
)
89 win
= win
->GetParent();
95 bool wxEventLoop::PreProcessMessage(WXMSG
*msg
)
97 HWND hwnd
= msg
->hwnd
;
98 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
101 // this might happen if we're in a modeless dialog, or if a wx control has
102 // children which themselves were not created by wx (i.e. wxActiveX control children)
105 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
107 hwnd
= ::GetParent(hwnd
);
109 // If the control has a wx parent, break and give the parent a chance
110 // to process the window message
111 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
118 // this may happen if the event occurred in a standard modeless dialog (the
119 // only example of which I know of is the find/replace dialog) - then call
120 // IsDialogMessage() to make TAB navigation in it work
122 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
123 // them) if we call it for the control itself
124 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
128 if ( !AllowProcessing(wndThis
) )
130 // not a child of critical window, so we eat the event but take care to
131 // stop an endless stream of WM_PAINTs which would have resulted if we
132 // didn't validate the invalidated part of the window
133 if ( msg
->message
== WM_PAINT
)
134 ::ValidateRect(hwnd
, NULL
);
140 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
141 // popup the tooltip bubbles
142 if ( msg
->message
== WM_MOUSEMOVE
)
144 wxToolTip
*tt
= wndThis
->GetToolTip();
147 tt
->RelayEvent((WXMSG
*)msg
);
150 #endif // wxUSE_TOOLTIPS
152 // allow the window to prevent certain messages from being
153 // translated/processed (this is currently used by wxTextCtrl to always
154 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
155 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
160 // try translations first: the accelerators override everything
161 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
163 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
166 // stop at first top level window, i.e. don't try to process the key
167 // strokes originating in a dialog using the accelerators of the parent
168 // frame - this doesn't make much sense
169 if ( wnd
->IsTopLevel() )
173 // now try the other hooks (kbd navigation is handled here)
174 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
176 if (wnd
!= wndThis
) // Skip the first since wndThis->MSWProcessMessage() was called above
178 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
182 // Stop at first top level window (as per comment above).
183 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
184 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
185 if (wnd
->IsTopLevel())
189 // no special preprocessing for this message, dispatch it normally
193 // ----------------------------------------------------------------------------
194 // wxEventLoop running and exiting
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
198 // wxEventLoopManual customization
199 // ----------------------------------------------------------------------------
201 void wxEventLoop::OnNextIteration()
204 wxMutexGuiLeaveOrEnter();
205 #endif // wxUSE_THREADS
208 void wxEventLoop::WakeUp()
210 ::PostMessage(NULL
, WM_NULL
, 0, 0);
213 // ----------------------------------------------------------------------------
214 // wxEventLoop message processing dispatching
215 // ----------------------------------------------------------------------------
217 bool wxEventLoop::Pending() const
220 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
223 bool wxEventLoop::Dispatch()
225 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
228 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
238 // should never happen, but let's test for it nevertheless
239 wxLogLastError(wxT("GetMessage"));
241 // still break from the loop
246 wxASSERT_MSG( wxThread::IsMain(),
247 wxT("only the main thread can process Windows messages") );
249 static bool s_hadGuiLock
= true;
250 static wxMsgList s_aSavedMessages
;
252 // if a secondary thread owning the mutex is doing GUI calls, save all
253 // messages for later processing - we can't process them right now because
254 // it will lead to recursive library calls (and we're not reentrant)
255 if ( !wxGuiOwnedByMainThread() )
257 s_hadGuiLock
= false;
259 // leave out WM_COMMAND messages: too dangerous, sometimes
260 // the message will be processed twice
261 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
263 MSG
* pMsg
= new MSG(msg
);
264 s_aSavedMessages
.Append(pMsg
);
271 // have we just regained the GUI lock? if so, post all of the saved
274 // FIXME of course, it's not _exactly_ the same as processing the
275 // messages normally - expect some things to break...
280 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
283 MSG
* pMsg
= node
->GetData();
284 s_aSavedMessages
.Erase(node
);
286 ProcessMessage(pMsg
);
289 node
= s_aSavedMessages
.GetFirst();
293 #endif // wxUSE_THREADS
295 ProcessMessage(&msg
);