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
* const wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
130 // this may happen if the event occurred in a standard modeless dialog (the
131 // only example of which I know of is the find/replace dialog) - then call
132 // IsDialogMessage() to make TAB navigation in it work
135 // we need to find the dialog containing this control as
136 // IsDialogMessage() just eats all the messages (i.e. returns true for
137 // them) if we call it for the control itself
138 while ( hwnd
&& ::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
)
140 hwnd
= ::GetParent(hwnd
);
143 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
146 if ( !AllowProcessing(wndThis
) )
148 // not a child of critical window, so we eat the event but take care to
149 // stop an endless stream of WM_PAINTs which would have resulted if we
150 // didn't validate the invalidated part of the window
151 if ( msg
->message
== WM_PAINT
)
152 ::ValidateRect(hwnd
, NULL
);
158 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
159 // popup the tooltip bubbles
160 if ( msg
->message
== WM_MOUSEMOVE
)
162 wxToolTip
*tt
= wndThis
->GetToolTip();
165 tt
->RelayEvent((WXMSG
*)msg
);
168 #endif // wxUSE_TOOLTIPS
170 // allow the window to prevent certain messages from being
171 // translated/processed (this is currently used by wxTextCtrl to always
172 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
173 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
178 // try translations first: the accelerators override everything
179 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
181 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
184 // stop at first top level window, i.e. don't try to process the key
185 // strokes originating in a dialog using the accelerators of the parent
186 // frame - this doesn't make much sense
187 if ( wnd
->IsTopLevel() )
191 // now try the other hooks (kbd navigation is handled here)
192 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
194 if (wnd
!= wndThis
) // Skip the first since wndThis->MSWProcessMessage() was called above
196 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
200 // Stop at first top level window (as per comment above).
201 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
202 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
203 if (wnd
->IsTopLevel())
207 // no special preprocessing for this message, dispatch it normally
211 // ----------------------------------------------------------------------------
212 // wxEventLoop running and exiting
213 // ----------------------------------------------------------------------------
215 bool wxEventLoop::IsRunning() const
217 return ms_activeLoop
== this;
220 int wxEventLoop::Run()
222 // event loops are not recursive, you need to create another loop!
223 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
225 // ProcessIdle() and Dispatch() below may throw so the code here should
226 // be exception-safe, hence we must use local objects for all actions we
228 wxEventLoopActivator
activate(&ms_activeLoop
, this);
230 // we must ensure that OnExit() is called even if an exception is thrown
231 // from inside Dispatch() but we must call it from Exit() in normal
232 // situations because it is supposed to be called synchronously,
233 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
234 // something similar here)
240 #endif // wxUSE_EXCEPTIONS
242 // this is the event loop itself
246 wxMutexGuiLeaveOrEnter();
247 #endif // wxUSE_THREADS
249 // generate and process idle events for as long as we don't
250 // have anything else to do
251 while ( !Pending() && (wxTheApp
&& wxTheApp
->ProcessIdle()) )
254 // if the "should exit" flag is set, the loop should terminate
255 // but not before processing any remaining messages so while
256 // Pending() returns true, do process them
265 // a message came or no more idle processing to do, sit in
266 // Dispatch() waiting for the next message
275 // exit the outer loop as well
282 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
287 //else: continue running the event loop
291 // OnException() throwed, possibly rethrowing the same
292 // exception again: very good, but we still need OnExit() to
299 #endif // wxUSE_EXCEPTIONS
304 void wxEventLoop::Exit(int rc
)
306 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
313 // all we have to do to exit from the loop is to (maybe) wake it up so that
314 // it can notice that Exit() had been called
316 // in particular, we do *not* use PostQuitMessage() here because we're not
317 // sure that WM_QUIT is going to be processed by the correct event loop: it
318 // is possible that another one is started before this one has a chance to
320 ::PostMessage(NULL
, WM_NULL
, 0, 0);
323 // ----------------------------------------------------------------------------
324 // wxEventLoop message processing dispatching
325 // ----------------------------------------------------------------------------
327 bool wxEventLoop::Pending() const
330 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
333 bool wxEventLoop::Dispatch()
335 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
338 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
348 // should never happen, but let's test for it nevertheless
349 wxLogLastError(wxT("GetMessage"));
351 // still break from the loop
356 wxASSERT_MSG( wxThread::IsMain(),
357 wxT("only the main thread can process Windows messages") );
359 static bool s_hadGuiLock
= true;
360 static wxMsgList s_aSavedMessages
;
362 // if a secondary thread owning the mutex is doing GUI calls, save all
363 // messages for later processing - we can't process them right now because
364 // it will lead to recursive library calls (and we're not reentrant)
365 if ( !wxGuiOwnedByMainThread() )
367 s_hadGuiLock
= false;
369 // leave out WM_COMMAND messages: too dangerous, sometimes
370 // the message will be processed twice
371 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
373 MSG
* pMsg
= new MSG(msg
);
374 s_aSavedMessages
.Append(pMsg
);
381 // have we just regained the GUI lock? if so, post all of the saved
384 // FIXME of course, it's not _exactly_ the same as processing the
385 // messages normally - expect some things to break...
390 wxMsgList::compatibility_iterator node
= s_aSavedMessages
.GetFirst();
393 MSG
* pMsg
= node
->GetData();
394 s_aSavedMessages
.Erase(node
);
396 ProcessMessage(pMsg
);
399 node
= s_aSavedMessages
.GetFirst();
403 #endif // wxUSE_THREADS
405 ProcessMessage(&msg
);