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 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // this object sets the wxEventLoop given to the ctor as the currently active
56 // one and unsets it in its dtor
57 class wxEventLoopActivator
60 wxEventLoopActivator(wxEventLoop
**pActive
,
64 m_evtLoopOld
= *pActive
;
68 ~wxEventLoopActivator()
70 // restore the previously active event loop
71 *m_pActive
= m_evtLoopOld
;
75 wxEventLoop
*m_evtLoopOld
;
76 wxEventLoop
**m_pActive
;
79 // ============================================================================
80 // wxEventLoop implementation
81 // ============================================================================
83 wxEventLoop
*wxEventLoopBase
::ms_activeLoop
= NULL
;
84 wxWindowMSW
*wxEventLoop
::ms_winCritical
= NULL
;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 wxEventLoop
::wxEventLoop()
96 // ----------------------------------------------------------------------------
97 // wxEventLoop message processing
98 // ----------------------------------------------------------------------------
100 void wxEventLoop
::ProcessMessage(WXMSG
*msg
)
102 // give us the chance to preprocess the message first
103 if ( !PreProcessMessage(msg
) )
105 // if it wasn't done, dispatch it to the corresponding window
106 ::TranslateMessage(msg
);
107 ::DispatchMessage(msg
);
111 bool wxEventLoop
::IsChildOfCriticalWindow(wxWindowMSW
*win
)
115 if ( win
== ms_winCritical
)
118 win
= win
->GetParent();
124 bool wxEventLoop
::PreProcessMessage(WXMSG
*msg
)
126 HWND hwnd
= msg
->hwnd
;
127 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
130 wxWindow
* parent
= NULL
;
132 // this might happen if we're in a modeless dialog, or if a wx control has
133 // children which themselves were not created by wx (i.e. wxActiveX control children)
136 while ( hwnd
&& ::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
)
138 hwnd
= ::GetParent(hwnd
);
140 // If the control has a wx parent, break and give the parent a chance
141 // to process the window message
142 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 wxToolTip
*tt
= wndThis
->GetToolTip();
177 tt
->RelayEvent((WXMSG
*)msg
);
180 #endif // wxUSE_TOOLTIPS
182 // allow the window to prevent certain messages from being
183 // translated/processed (this is currently used by wxTextCtrl to always
184 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
185 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
190 // try translations first: the accelerators override everything
191 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
193 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
196 // stop at first top level window, i.e. don't try to process the key
197 // strokes originating in a dialog using the accelerators of the parent
198 // frame - this doesn't make much sense
199 if ( wnd
->IsTopLevel() )
203 // now try the other hooks (kbd navigation is handled here)
204 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
206 if (wnd
!= wndThis
) // Skip the first since wndThis->MSWProcessMessage() was called above
208 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
212 // Stop at first top level window (as per comment above).
213 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
214 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
215 if (wnd
->IsTopLevel())
219 // no special preprocessing for this message, dispatch it normally
223 // ----------------------------------------------------------------------------
224 // wxEventLoop running and exiting
225 // ----------------------------------------------------------------------------
227 bool wxEventLoop
::IsRunning() const
229 return ms_activeLoop
== this;
232 int wxEventLoop
::Run()
234 // event loops are not recursive, you need to create another loop!
235 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
237 // ProcessIdle() and Dispatch() below may throw so the code here should
238 // be exception-safe, hence we must use local objects for all actions we
240 wxEventLoopActivator
activate(&ms_activeLoop
, this);
242 // we must ensure that OnExit() is called even if an exception is thrown
243 // from inside Dispatch() but we must call it from Exit() in normal
244 // situations because it is supposed to be called synchronously,
245 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
246 // something similar here)
252 #endif // wxUSE_EXCEPTIONS
254 // this is the event loop itself
258 wxMutexGuiLeaveOrEnter();
259 #endif // wxUSE_THREADS
261 // generate and process idle events for as long as we don't
262 // have anything else to do
263 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
266 // if the "should exit" flag is set, the loop should terminate
267 // but not before processing any remaining messages so while
268 // Pending() returns true, do process them
277 // a message came or no more idle processing to do, sit in
278 // Dispatch() waiting for the next message
287 // exit the outer loop as well
294 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
299 //else: continue running the event loop
303 // OnException() throwed, possibly rethrowing the same
304 // exception again: very good, but we still need OnExit() to
311 #endif // wxUSE_EXCEPTIONS
316 void wxEventLoop
::Exit(int rc
)
318 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
325 // all we have to do to exit from the loop is to (maybe) wake it up so that
326 // it can notice that Exit() had been called
328 // in particular, we do *not* use PostQuitMessage() here because we're not
329 // sure that WM_QUIT is going to be processed by the correct event loop: it
330 // is possible that another one is started before this one has a chance to
332 ::PostMessage(NULL
, WM_NULL
, 0, 0);
335 // ----------------------------------------------------------------------------
336 // wxEventLoop message processing dispatching
337 // ----------------------------------------------------------------------------
339 bool wxEventLoop
::Pending() const
342 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
345 bool wxEventLoop
::Dispatch()
347 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
350 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
360 // should never happen, but let's test for it nevertheless
361 wxLogLastError(wxT("GetMessage"));
363 // still break from the loop
368 wxASSERT_MSG( wxThread
::IsMain(),
369 wxT("only the main thread can process Windows messages") );
371 static bool s_hadGuiLock
= true;
372 static wxMsgList s_aSavedMessages
;
374 // if a secondary thread owning the mutex is doing GUI calls, save all
375 // messages for later processing - we can't process them right now because
376 // it will lead to recursive library calls (and we're not reentrant)
377 if ( !wxGuiOwnedByMainThread() )
379 s_hadGuiLock
= false;
381 // leave out WM_COMMAND messages: too dangerous, sometimes
382 // the message will be processed twice
383 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
385 MSG
* pMsg
= new MSG(msg
);
386 s_aSavedMessages
.Append(pMsg
);
393 // have we just regained the GUI lock? if so, post all of the saved
396 // FIXME of course, it's not _exactly_ the same as processing the
397 // messages normally - expect some things to break...
402 wxMsgList
::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
405 MSG
* pMsg
= node
->GetData();
406 s_aSavedMessages
.Erase(node
);
408 ProcessMessage(pMsg
);
411 node
= s_aSavedMessages
.GetFirst();
415 #endif // wxUSE_THREADS
417 ProcessMessage(&msg
);