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"
37 #include "wx/except.h"
38 #include "wx/ptr_scpd.h"
40 #include "wx/msw/private.h"
43 #include "wx/tooltip.h"
45 #include "wx/thread.h"
47 // define the list of MSG strutures
48 WX_DECLARE_LIST(MSG
, wxMsgList
);
50 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST(wxMsgList
)
53 #endif // wxUSE_THREADS
58 // ============================================================================
59 // wxMSWEventLoopBase implementation
60 // ============================================================================
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 wxMSWEventLoopBase::wxMSWEventLoopBase()
72 // ----------------------------------------------------------------------------
73 // wxEventLoop message processing dispatching
74 // ----------------------------------------------------------------------------
76 bool wxMSWEventLoopBase::Pending() const
79 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
82 bool wxMSWEventLoopBase::GetNextMessage(WXMSG
* msg
)
84 wxCHECK_MSG( IsRunning(), false, _T("can't get messages if not running") );
86 const BOOL rc
= ::GetMessage(msg
, NULL
, 0, 0);
96 // should never happen, but let's test for it nevertheless
97 wxLogLastError(wxT("GetMessage"));
99 // still break from the loop
110 // ============================================================================
111 // GUI wxEventLoop implementation
112 // ============================================================================
114 wxWindowMSW
*wxGUIEventLoop::ms_winCritical
= NULL
;
116 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW
*win
)
120 if ( win
== ms_winCritical
)
123 win
= win
->GetParent();
129 bool wxGUIEventLoop::PreProcessMessage(WXMSG
*msg
)
131 HWND hwnd
= msg
->hwnd
;
132 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
135 // this might happen if we're in a modeless dialog, or if a wx control has
136 // children which themselves were not created by wx (i.e. wxActiveX control children)
139 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
141 hwnd
= ::GetParent(hwnd
);
143 // If the control has a wx parent, break and give the parent a chance
144 // to process the window message
145 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
152 // this may happen if the event occurred in a standard modeless dialog (the
153 // only example of which I know of is the find/replace dialog) - then call
154 // IsDialogMessage() to make TAB navigation in it work
156 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
157 // them) if we call it for the control itself
158 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
162 if ( !AllowProcessing(wndThis
) )
164 // not a child of critical window, so we eat the event but take care to
165 // stop an endless stream of WM_PAINTs which would have resulted if we
166 // didn't validate the invalidated part of the window
167 if ( msg
->message
== WM_PAINT
)
168 ::ValidateRect(hwnd
, NULL
);
174 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
175 // popup the tooltip bubbles
176 if ( msg
->message
== WM_MOUSEMOVE
)
178 // we should do it if one of window children has an associated tooltip
179 // (and not just if the window has a tooltip itself)
180 if ( wndThis
->HasToolTips() )
181 wxToolTip::RelayEvent((WXMSG
*)msg
);
183 #endif // wxUSE_TOOLTIPS
185 // allow the window to prevent certain messages from being
186 // translated/processed (this is currently used by wxTextCtrl to always
187 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
188 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
193 // try translations first: the accelerators override everything
194 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
196 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
199 // stop at first top level window, i.e. don't try to process the key
200 // strokes originating in a dialog using the accelerators of the parent
201 // frame - this doesn't make much sense
202 if ( wnd
->IsTopLevel() )
206 // now try the other hooks (kbd navigation is handled here)
207 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
209 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
212 // also stop at first top level window here, just as above because
213 // if we don't do this, pressing ESC on a modal dialog shown as child
214 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
215 // be closed, for example
216 if ( wnd
->IsTopLevel() )
220 // no special preprocessing for this message, dispatch it normally
224 void wxGUIEventLoop::ProcessMessage(WXMSG
*msg
)
226 // give us the chance to preprocess the message first
227 if ( !PreProcessMessage(msg
) )
229 // if it wasn't done, dispatch it to the corresponding window
230 ::TranslateMessage(msg
);
231 ::DispatchMessage(msg
);
235 bool wxGUIEventLoop::Dispatch()
238 if ( !GetNextMessage(&msg
) )
242 wxASSERT_MSG( wxThread::IsMain(),
243 wxT("only the main thread can process Windows messages") );
245 static bool s_hadGuiLock
= true;
246 static wxMsgList s_aSavedMessages
;
248 // if a secondary thread owning the mutex is doing GUI calls, save all
249 // messages for later processing - we can't process them right now because
250 // it will lead to recursive library calls (and we're not reentrant)
251 if ( !wxGuiOwnedByMainThread() )
253 s_hadGuiLock
= false;
255 // leave out WM_COMMAND messages: too dangerous, sometimes
256 // the message will be processed twice
257 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
259 MSG
* pMsg
= new MSG(msg
);
260 s_aSavedMessages
.Append(pMsg
);
267 // have we just regained the GUI lock? if so, post all of the saved
270 // FIXME of course, it's not _exactly_ the same as processing the
271 // messages normally - expect some things to break...
276 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
279 MSG
* pMsg
= node
->GetData();
280 s_aSavedMessages
.Erase(node
);
282 ProcessMessage(pMsg
);
285 node
= s_aSavedMessages
.GetFirst();
289 #endif // wxUSE_THREADS
291 ProcessMessage(&msg
);
296 void wxGUIEventLoop::OnNextIteration()
299 wxMutexGuiLeaveOrEnter();
300 #endif // wxUSE_THREADS
303 void wxGUIEventLoop::WakeUp()
305 ::PostMessage(NULL
, WM_NULL
, 0, 0);
310 #if wxUSE_CONSOLE_EVENTLOOP
312 void wxConsoleEventLoop::OnNextIteration()
315 wxTheApp
->ProcessPendingEvents();
318 void wxConsoleEventLoop::WakeUp()
321 wxWakeUpMainThread();
325 bool wxConsoleEventLoop::Dispatch()
328 if ( !GetNextMessage(&msg
) )
331 if ( msg
.message
== WM_TIMER
)
333 TIMERPROC proc
= (TIMERPROC
)msg
.lParam
;
335 (*proc
)(NULL
, 0, msg
.wParam
, 0);
339 wxLogDebug(_T("Ignoring unexpected message %d"), msg
.message
);
342 return !m_shouldExit
;
345 #endif // wxUSE_CONSOLE_EVENTLOOP