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() { SetExitCode(0); }
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 SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
73 int GetExitCode() const { return m_exitcode
; }
76 // preprocess a message, return TRUE if processed (i.e. no further
77 // dispatching required)
78 bool PreProcessMessage(MSG
*msg
);
80 // the exit code of the event loop
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl
);
90 // this object sets the wxEventLoop given to the ctor as the currently active
91 // one and unsets it in its dtor
92 class wxEventLoopActivator
95 wxEventLoopActivator(wxEventLoop
**pActive
,
99 m_evtLoopOld
= *pActive
;
103 ~wxEventLoopActivator()
105 // restore the previously active event loop
106 *m_pActive
= m_evtLoopOld
;
110 wxEventLoop
*m_evtLoopOld
;
111 wxEventLoop
**m_pActive
;
114 // ============================================================================
115 // wxEventLoopImpl implementation
116 // ============================================================================
118 // ----------------------------------------------------------------------------
119 // wxEventLoopImpl message processing
120 // ----------------------------------------------------------------------------
122 void wxEventLoopImpl::ProcessMessage(MSG
*msg
)
124 // give us the chance to preprocess the message first
125 if ( !PreProcessMessage(msg
) )
127 // if it wasn't done, dispatch it to the corresponding window
128 ::TranslateMessage(msg
);
129 ::DispatchMessage(msg
);
133 bool wxEventLoopImpl::PreProcessMessage(MSG
*msg
)
135 HWND hwnd
= msg
->hwnd
;
136 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
138 // this may happen if the event occured in a standard modeless dialog (the
139 // only example of which I know of is the find/replace dialog) - then call
140 // IsDialogMessage() to make TAB navigation in it work
143 // we need to find the dialog containing this control as
144 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
145 // them) if we call it for the control itself
146 while ( hwnd
&& ::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
)
148 hwnd
= ::GetParent(hwnd
);
151 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
155 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
156 // popup the tooltip bubbles
157 if ( msg
->message
== WM_MOUSEMOVE
)
159 wxToolTip
*tt
= wndThis
->GetToolTip();
162 tt
->RelayEvent((WXMSG
*)msg
);
165 #endif // wxUSE_TOOLTIPS
167 // allow the window to prevent certain messages from being
168 // translated/processed (this is currently used by wxTextCtrl to always
169 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
170 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
175 // try translations first: the accelerators override everything
178 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
180 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
))
183 // stop at first top level window, i.e. don't try to process the key
184 // strokes originating in a dialog using the accelerators of the parent
185 // frame - this doesn't make much sense
186 if ( wnd
->IsTopLevel() )
190 // now try the other hooks (kbd navigation is handled here): we start from
191 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
193 for ( wnd
= wndThis
->GetParent(); wnd
; wnd
= wnd
->GetParent() )
195 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
199 // no special preprocessing for this message, dispatch it normally
203 // ----------------------------------------------------------------------------
204 // wxEventLoopImpl idle event processing
205 // ----------------------------------------------------------------------------
207 bool wxEventLoopImpl::SendIdleMessage()
209 return wxTheApp
->ProcessIdle();
212 // ============================================================================
213 // wxEventLoop implementation
214 // ============================================================================
216 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
218 // ----------------------------------------------------------------------------
219 // wxEventLoop running and exiting
220 // ----------------------------------------------------------------------------
222 wxEventLoop::~wxEventLoop()
224 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
227 bool wxEventLoop::IsRunning() const
229 return m_impl
!= NULL
;
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 // SendIdleMessage() 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);
241 wxEventLoopImplTiedPtr
impl(&m_impl
, new wxEventLoopImpl
);
243 // we must ensure that OnExit() is called even if an exception is thrown
244 // from inside Dispatch() but we must call it from Exit() in normal
245 // situations because it is supposed to be called synchronously,
246 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
247 // something similar here)
253 wxMutexGuiLeaveOrEnter();
254 #endif // wxUSE_THREADS
256 // generate and process idle events for as long as we don't have
257 // anything else to do
258 while ( !Pending() && m_impl
->SendIdleMessage() )
261 // a message came or no more idle processing to do, sit in
262 // Dispatch() waiting for the next message
270 wxCATCH_ALL( OnExit(); )
272 return m_impl
->GetExitCode();
275 void wxEventLoop::Exit(int rc
)
277 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
279 m_impl
->SetExitCode(rc
);
283 ::PostQuitMessage(rc
);
286 // ----------------------------------------------------------------------------
287 // wxEventLoop message processing dispatching
288 // ----------------------------------------------------------------------------
290 bool wxEventLoop::Pending() const
293 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
296 bool wxEventLoop::Dispatch()
298 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
301 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
311 // should never happen, but let's test for it nevertheless
312 wxLogLastError(wxT("GetMessage"));
314 // still break from the loop
319 wxASSERT_MSG( wxThread::IsMain(),
320 wxT("only the main thread can process Windows messages") );
322 static bool s_hadGuiLock
= TRUE
;
323 static wxMsgArray s_aSavedMessages
;
325 // if a secondary thread owning the mutex is doing GUI calls, save all
326 // messages for later processing - we can't process them right now because
327 // it will lead to recursive library calls (and we're not reentrant)
328 if ( !wxGuiOwnedByMainThread() )
330 s_hadGuiLock
= FALSE
;
332 // leave out WM_COMMAND messages: too dangerous, sometimes
333 // the message will be processed twice
334 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
336 s_aSavedMessages
.Add(msg
);
343 // have we just regained the GUI lock? if so, post all of the saved
346 // FIXME of course, it's not _exactly_ the same as processing the
347 // messages normally - expect some things to break...
352 size_t count
= s_aSavedMessages
.Count();
353 for ( size_t n
= 0; n
< count
; n
++ )
355 MSG
& msg
= s_aSavedMessages
[n
];
356 m_impl
->ProcessMessage(&msg
);
359 s_aSavedMessages
.Empty();
362 #endif // wxUSE_THREADS
364 m_impl
->ProcessMessage(&msg
);