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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "evtloop.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/window.h"
36 #include "wx/evtloop.h"
38 #include "wx/tooltip.h"
39 #include "wx/except.h"
40 #include "wx/ptr_scpd.h"
42 #include "wx/msw/private.h"
45 #include "wx/thread.h"
47 // define the array of MSG strutures
48 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
50 #include "wx/arrimpl.cpp"
52 WX_DEFINE_OBJARRAY(wxMsgArray
);
53 #endif // wxUSE_THREADS
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 class WXDLLEXPORT wxEventLoopImpl
63 wxEventLoopImpl() { m_exitcode
= 0; m_shouldExit
= false; }
66 void ProcessMessage(MSG
*msg
);
68 // generate an idle message, return TRUE if more idle time requested
69 bool SendIdleMessage();
71 // set/get the exit code
72 void Exit(int exitcode
) { m_exitcode
= exitcode
; m_shouldExit
= true; }
73 int GetExitCode() const { return m_exitcode
; }
74 bool ShouldExit() const { return m_shouldExit
; }
77 // preprocess a message, return TRUE if processed (i.e. no further
78 // dispatching required)
79 bool PreProcessMessage(MSG
*msg
);
81 // the exit code of the event loop
84 // true if we were asked to terminate
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl
);
94 // this object sets the wxEventLoop given to the ctor as the currently active
95 // one and unsets it in its dtor
96 class wxEventLoopActivator
99 wxEventLoopActivator(wxEventLoop
**pActive
,
100 wxEventLoop
*evtLoop
)
103 m_evtLoopOld
= *pActive
;
107 ~wxEventLoopActivator()
109 // restore the previously active event loop
110 *m_pActive
= m_evtLoopOld
;
114 wxEventLoop
*m_evtLoopOld
;
115 wxEventLoop
**m_pActive
;
118 // ============================================================================
119 // wxEventLoopImpl implementation
120 // ============================================================================
122 // ----------------------------------------------------------------------------
123 // wxEventLoopImpl message processing
124 // ----------------------------------------------------------------------------
126 void wxEventLoopImpl::ProcessMessage(MSG
*msg
)
128 // give us the chance to preprocess the message first
129 if ( !PreProcessMessage(msg
) )
131 // if it wasn't done, dispatch it to the corresponding window
132 ::TranslateMessage(msg
);
133 ::DispatchMessage(msg
);
137 bool wxEventLoopImpl::PreProcessMessage(MSG
*msg
)
139 HWND hwnd
= msg
->hwnd
;
140 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
142 // this may happen if the event occured in a standard modeless dialog (the
143 // only example of which I know of is the find/replace dialog) - then call
144 // IsDialogMessage() to make TAB navigation in it work
147 // we need to find the dialog containing this control as
148 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
149 // them) if we call it for the control itself
150 while ( hwnd
&& ::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
)
152 hwnd
= ::GetParent(hwnd
);
155 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
159 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
160 // popup the tooltip bubbles
161 if ( msg
->message
== WM_MOUSEMOVE
)
163 wxToolTip
*tt
= wndThis
->GetToolTip();
166 tt
->RelayEvent((WXMSG
*)msg
);
169 #endif // wxUSE_TOOLTIPS
171 // allow the window to prevent certain messages from being
172 // translated/processed (this is currently used by wxTextCtrl to always
173 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
174 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
179 // try translations first: the accelerators override everything
182 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
184 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
187 // stop at first top level window, i.e. don't try to process the key
188 // strokes originating in a dialog using the accelerators of the parent
189 // frame - this doesn't make much sense
190 if ( wnd
->IsTopLevel() )
194 // now try the other hooks (kbd navigation is handled here): we start from
195 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
197 for ( wnd
= wndThis
->GetParent(); wnd
; wnd
= wnd
->GetParent() )
199 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
203 // no special preprocessing for this message, dispatch it normally
207 // ----------------------------------------------------------------------------
208 // wxEventLoopImpl idle event processing
209 // ----------------------------------------------------------------------------
211 bool wxEventLoopImpl::SendIdleMessage()
213 return wxTheApp
->ProcessIdle();
216 // ============================================================================
217 // wxEventLoop implementation
218 // ============================================================================
220 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
222 // ----------------------------------------------------------------------------
223 // wxEventLoop running and exiting
224 // ----------------------------------------------------------------------------
226 wxEventLoop::~wxEventLoop()
228 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
231 bool wxEventLoop::IsRunning() const
233 return m_impl
!= NULL
;
236 int wxEventLoop::Run()
238 // event loops are not recursive, you need to create another loop!
239 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
241 // SendIdleMessage() and Dispatch() below may throw so the code here should
242 // be exception-safe, hence we must use local objects for all actions we
244 wxEventLoopActivator
activate(&ms_activeLoop
, this);
245 wxEventLoopImplTiedPtr
impl(&m_impl
, new wxEventLoopImpl
);
247 // we must ensure that OnExit() is called even if an exception is thrown
248 // from inside Dispatch() but we must call it from Exit() in normal
249 // situations because it is supposed to be called synchronously,
250 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
251 // something similar here)
257 wxMutexGuiLeaveOrEnter();
258 #endif // wxUSE_THREADS
260 // generate and process idle events for as long as we don't have
261 // anything else to do
262 while ( !Pending() && m_impl
->SendIdleMessage() )
265 // if the "should exit" flag is set, the loop should terminate but
266 // not before processing any remaining messages so while Pending()
267 // returns true, do process them
268 if ( m_impl
->ShouldExit() )
276 // a message came or no more idle processing to do, sit in
277 // Dispatch() waiting for the next message
285 wxCATCH_ALL( OnExit(); )
287 return m_impl
->GetExitCode();
290 void wxEventLoop::Exit(int rc
)
292 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
298 // all we have to do to exit from the loop is to (maybe) wake it up so that
299 // it can notice that Exit() had been called
301 // in particular, we do *not* use PostQuitMessage() here because we're not
302 // sure that WM_QUIT is going to be processed by the correct event loop: it
303 // is possible that another one is started before this one has a chance to
305 ::PostMessage(NULL
, WM_NULL
, 0, 0);
308 // ----------------------------------------------------------------------------
309 // wxEventLoop message processing dispatching
310 // ----------------------------------------------------------------------------
312 bool wxEventLoop::Pending() const
315 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
318 bool wxEventLoop::Dispatch()
320 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
323 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
333 // should never happen, but let's test for it nevertheless
334 wxLogLastError(wxT("GetMessage"));
336 // still break from the loop
341 wxASSERT_MSG( wxThread::IsMain(),
342 wxT("only the main thread can process Windows messages") );
344 static bool s_hadGuiLock
= TRUE
;
345 static wxMsgArray s_aSavedMessages
;
347 // if a secondary thread owning the mutex is doing GUI calls, save all
348 // messages for later processing - we can't process them right now because
349 // it will lead to recursive library calls (and we're not reentrant)
350 if ( !wxGuiOwnedByMainThread() )
352 s_hadGuiLock
= FALSE
;
354 // leave out WM_COMMAND messages: too dangerous, sometimes
355 // the message will be processed twice
356 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
358 s_aSavedMessages
.Add(msg
);
365 // have we just regained the GUI lock? if so, post all of the saved
368 // FIXME of course, it's not _exactly_ the same as processing the
369 // messages normally - expect some things to break...
374 size_t count
= s_aSavedMessages
.Count();
375 for ( size_t n
= 0; n
< count
; n
++ )
377 MSG
& msg
= s_aSavedMessages
[n
];
378 m_impl
->ProcessMessage(&msg
);
381 s_aSavedMessages
.Empty();
384 #endif // wxUSE_THREADS
386 m_impl
->ProcessMessage(&msg
);