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 // this might happen if we're in a modeless dialog, or if a wx control has
131 // children which themselves were not created by wx (i.e. wxActiveX control children)
134 while ( hwnd
&& (::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
))
136 hwnd
= ::GetParent(hwnd
);
138 // If the control has a wx parent, break and give the parent a chance
139 // to process the window message
140 wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
147 // this may happen if the event occurred in a standard modeless dialog (the
148 // only example of which I know of is the find/replace dialog) - then call
149 // IsDialogMessage() to make TAB navigation in it work
151 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
152 // them) if we call it for the control itself
153 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
157 if ( !AllowProcessing(wndThis
) )
159 // not a child of critical window, so we eat the event but take care to
160 // stop an endless stream of WM_PAINTs which would have resulted if we
161 // didn't validate the invalidated part of the window
162 if ( msg
->message
== WM_PAINT
)
163 ::ValidateRect(hwnd
, NULL
);
169 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
170 // popup the tooltip bubbles
171 if ( msg
->message
== WM_MOUSEMOVE
)
173 wxToolTip
*tt
= wndThis
->GetToolTip();
176 tt
->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
!= wndThis
) // Skip the first since wndThis->MSWProcessMessage() was called above
207 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
211 // Stop at first top level window (as per comment above).
212 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
213 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
214 if (wnd
->IsTopLevel())
218 // no special preprocessing for this message, dispatch it normally
222 // ----------------------------------------------------------------------------
223 // wxEventLoop running and exiting
224 // ----------------------------------------------------------------------------
226 bool wxEventLoop::IsRunning() const
228 return ms_activeLoop
== this;
231 int wxEventLoop::Run()
233 // event loops are not recursive, you need to create another loop!
234 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
236 // ProcessIdle() and Dispatch() below may throw so the code here should
237 // be exception-safe, hence we must use local objects for all actions we
239 wxEventLoopActivator
activate(&ms_activeLoop
, this);
241 // we must ensure that OnExit() is called even if an exception is thrown
242 // from inside Dispatch() but we must call it from Exit() in normal
243 // situations because it is supposed to be called synchronously,
244 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
245 // something similar here)
251 #endif // wxUSE_EXCEPTIONS
253 // this is the event loop itself
257 wxMutexGuiLeaveOrEnter();
258 #endif // wxUSE_THREADS
260 // generate and process idle events for as long as we don't
261 // have anything else to do
262 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
265 // if the "should exit" flag is set, the loop should terminate
266 // but not before processing any remaining messages so while
267 // Pending() returns true, do process them
276 // a message came or no more idle processing to do, sit in
277 // Dispatch() waiting for the next message
286 // exit the outer loop as well
293 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
298 //else: continue running the event loop
302 // OnException() throwed, possibly rethrowing the same
303 // exception again: very good, but we still need OnExit() to
310 #endif // wxUSE_EXCEPTIONS
315 void wxEventLoop::Exit(int rc
)
317 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
324 // all we have to do to exit from the loop is to (maybe) wake it up so that
325 // it can notice that Exit() had been called
327 // in particular, we do *not* use PostQuitMessage() here because we're not
328 // sure that WM_QUIT is going to be processed by the correct event loop: it
329 // is possible that another one is started before this one has a chance to
331 ::PostMessage(NULL
, WM_NULL
, 0, 0);
334 // ----------------------------------------------------------------------------
335 // wxEventLoop message processing dispatching
336 // ----------------------------------------------------------------------------
338 bool wxEventLoop::Pending() const
341 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
344 bool wxEventLoop::Dispatch()
346 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
349 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
359 // should never happen, but let's test for it nevertheless
360 wxLogLastError(wxT("GetMessage"));
362 // still break from the loop
367 wxASSERT_MSG( wxThread::IsMain(),
368 wxT("only the main thread can process Windows messages") );
370 static bool s_hadGuiLock
= true;
371 static wxMsgList s_aSavedMessages
;
373 // if a secondary thread owning the mutex is doing GUI calls, save all
374 // messages for later processing - we can't process them right now because
375 // it will lead to recursive library calls (and we're not reentrant)
376 if ( !wxGuiOwnedByMainThread() )
378 s_hadGuiLock
= false;
380 // leave out WM_COMMAND messages: too dangerous, sometimes
381 // the message will be processed twice
382 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
384 MSG
* pMsg
= new MSG(msg
);
385 s_aSavedMessages
.Append(pMsg
);
392 // have we just regained the GUI lock? if so, post all of the saved
395 // FIXME of course, it's not _exactly_ the same as processing the
396 // messages normally - expect some things to break...
401 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
404 MSG
* pMsg
= node
->GetData();
405 s_aSavedMessages
.Erase(node
);
407 ProcessMessage(pMsg
);
410 node
= s_aSavedMessages
.GetFirst();
414 #endif // wxUSE_THREADS
416 ProcessMessage(&msg
);