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
106 // ============================================================================
107 // GUI wxEventLoop implementation
108 // ============================================================================
110 wxWindowMSW
*wxGUIEventLoop::ms_winCritical
= NULL
;
112 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
116 if ( win
== ms_winCritical
)
119 win
= win
->GetParent();
125 bool wxGUIEventLoop::PreProcessMessage(WXMSG
*msg
)
127 HWND hwnd
= msg
->hwnd
;
128 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
131 // this might happen if we're in a modeless dialog, or if a wx control has
132 // children which themselves were not created by wx (i.e. wxActiveX control children)
135 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
137 hwnd
= ::GetParent(hwnd
);
139 // If the control has a wx parent, break and give the parent a chance
140 // to process the window message
141 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
148 // this may happen if the event occurred in a standard modeless dialog (the
149 // only example of which I know of is the find/replace dialog) - then call
150 // IsDialogMessage() to make TAB navigation in it work
152 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
153 // them) if we call it for the control itself
154 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
158 if ( !AllowProcessing(wndThis
) )
160 // not a child of critical window, so we eat the event but take care to
161 // stop an endless stream of WM_PAINTs which would have resulted if we
162 // didn't validate the invalidated part of the window
163 if ( msg
->message
== WM_PAINT
)
164 ::ValidateRect(hwnd
, NULL
);
170 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
171 // popup the tooltip bubbles
172 if ( msg
->message
== WM_MOUSEMOVE
)
174 // we should do it if one of window children has an associated tooltip
175 // (and not just if the window has a tooltip itself)
176 if ( wndThis
->HasToolTips() )
177 wxToolTip::RelayEvent((WXMSG
*)msg
);
179 #endif // wxUSE_TOOLTIPS
181 // allow the window to prevent certain messages from being
182 // translated/processed (this is currently used by wxTextCtrl to always
183 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
184 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
189 // try translations first: the accelerators override everything
190 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
192 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
195 // stop at first top level window, i.e. don't try to process the key
196 // strokes originating in a dialog using the accelerators of the parent
197 // frame - this doesn't make much sense
198 if ( wnd
->IsTopLevel() )
202 // now try the other hooks (kbd navigation is handled here)
203 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
205 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
208 // also stop at first top level window here, just as above because
209 // if we don't do this, pressing ESC on a modal dialog shown as child
210 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
211 // be closed, for example
212 if ( wnd
->IsTopLevel() )
216 // no special preprocessing for this message, dispatch it normally
220 void wxGUIEventLoop::ProcessMessage(WXMSG
*msg
)
222 // give us the chance to preprocess the message first
223 if ( !PreProcessMessage(msg
) )
225 // if it wasn't done, dispatch it to the corresponding window
226 ::TranslateMessage(msg
);
227 ::DispatchMessage(msg
);
231 bool wxGUIEventLoop::Dispatch()
234 if ( !GetNextMessage(&msg
) )
238 wxASSERT_MSG( wxThread::IsMain(),
239 wxT("only the main thread can process Windows messages") );
241 static bool s_hadGuiLock
= true;
242 static wxMsgList s_aSavedMessages
;
244 // if a secondary thread owning the mutex is doing GUI calls, save all
245 // messages for later processing - we can't process them right now because
246 // it will lead to recursive library calls (and we're not reentrant)
247 if ( !wxGuiOwnedByMainThread() )
249 s_hadGuiLock
= false;
251 // leave out WM_COMMAND messages: too dangerous, sometimes
252 // the message will be processed twice
253 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
255 MSG
* pMsg
= new MSG(msg
);
256 s_aSavedMessages
.Append(pMsg
);
263 // have we just regained the GUI lock? if so, post all of the saved
266 // FIXME of course, it's not _exactly_ the same as processing the
267 // messages normally - expect some things to break...
272 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
275 MSG
* pMsg
= node
->GetData();
276 s_aSavedMessages
.Erase(node
);
278 ProcessMessage(pMsg
);
281 node
= s_aSavedMessages
.GetFirst();
285 #endif // wxUSE_THREADS
287 ProcessMessage(&msg
);
292 void wxGUIEventLoop::OnNextIteration()
295 wxMutexGuiLeaveOrEnter();
296 #endif // wxUSE_THREADS
299 void wxGUIEventLoop::WakeUp()
301 ::PostMessage(NULL
, WM_NULL
, 0, 0);
306 #if wxUSE_CONSOLE_EVENTLOOP
308 void wxConsoleEventLoop::OnNextIteration()
311 wxTheApp
->ProcessPendingEvents();
314 void wxConsoleEventLoop::WakeUp()
317 wxWakeUpMainThread();
321 bool wxConsoleEventLoop::Dispatch()
324 if ( !GetNextMessage(&msg
) )
327 if ( msg
.message
== WM_TIMER
)
329 TIMERPROC proc
= (TIMERPROC
)msg
.lParam
;
331 (*proc
)(NULL
, 0, msg
.wParam
, 0);
335 ::DispatchMessage(&msg
);
338 return !m_shouldExit
;
341 #endif // wxUSE_CONSOLE_EVENTLOOP