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"
41 #include "wx/scopeguard.h"
43 #include "wx/msw/private.h"
46 #include "wx/thread.h"
48 // define the array of MSG strutures
49 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
51 #include "wx/arrimpl.cpp"
53 WX_DEFINE_OBJARRAY(wxMsgArray
);
54 #endif // wxUSE_THREADS
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 class WXDLLEXPORT wxEventLoopImpl
64 wxEventLoopImpl() { SetExitCode(0); }
67 void ProcessMessage(MSG
*msg
);
69 // generate an idle message, return TRUE if more idle time requested
70 bool SendIdleMessage();
72 // set/get the exit code
73 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
74 int GetExitCode() const { return m_exitcode
; }
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
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl
);
91 // this object sets the wxEventLoop given to the ctor as the currently active
92 // one and unsets it in its dtor
93 class wxEventLoopActivator
96 wxEventLoopActivator(wxEventLoop
**pActive
,
100 m_evtLoopOld
= *pActive
;
104 ~wxEventLoopActivator()
106 // restore the previously active event loop
107 *m_pActive
= m_evtLoopOld
;
111 wxEventLoop
*m_evtLoopOld
;
112 wxEventLoop
**m_pActive
;
115 // ============================================================================
116 // wxEventLoopImpl implementation
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // wxEventLoopImpl message processing
121 // ----------------------------------------------------------------------------
123 void wxEventLoopImpl::ProcessMessage(MSG
*msg
)
125 // give us the chance to preprocess the message first
126 if ( !PreProcessMessage(msg
) )
128 // if it wasn't done, dispatch it to the corresponding window
129 ::TranslateMessage(msg
);
130 ::DispatchMessage(msg
);
134 bool wxEventLoopImpl::PreProcessMessage(MSG
*msg
)
136 HWND hwnd
= msg
->hwnd
;
137 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hwnd
);
139 // this may happen if the event occured in a standard modeless dialog (the
140 // only example of which I know of is the find/replace dialog) - then call
141 // IsDialogMessage() to make TAB navigation in it work
144 // we need to find the dialog containing this control as
145 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
146 // them) if we call it for the control itself
147 while ( hwnd
&& ::GetWindowLong(hwnd
, GWL_STYLE
) & WS_CHILD
)
149 hwnd
= ::GetParent(hwnd
);
152 return hwnd
&& ::IsDialogMessage(hwnd
, msg
) != 0;
156 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
157 // popup the tooltip bubbles
158 if ( msg
->message
== WM_MOUSEMOVE
)
160 wxToolTip
*tt
= wndThis
->GetToolTip();
163 tt
->RelayEvent((WXMSG
*)msg
);
166 #endif // wxUSE_TOOLTIPS
168 // allow the window to prevent certain messages from being
169 // translated/processed (this is currently used by wxTextCtrl to always
170 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
171 if ( !wndThis
->MSWShouldPreProcessMessage((WXMSG
*)msg
) )
176 // 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): we start from
192 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
194 for ( wnd
= wndThis
->GetParent(); wnd
; wnd
= wnd
->GetParent() )
196 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
200 // no special preprocessing for this message, dispatch it normally
204 // ----------------------------------------------------------------------------
205 // wxEventLoopImpl idle event processing
206 // ----------------------------------------------------------------------------
208 bool wxEventLoopImpl::SendIdleMessage()
210 return wxTheApp
->ProcessIdle();
213 // ============================================================================
214 // wxEventLoop implementation
215 // ============================================================================
217 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
219 // ----------------------------------------------------------------------------
220 // wxEventLoop running and exiting
221 // ----------------------------------------------------------------------------
223 wxEventLoop::~wxEventLoop()
225 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
228 bool wxEventLoop::IsRunning() const
230 return m_impl
!= NULL
;
233 int wxEventLoop::Run()
235 // event loops are not recursive, you need to create another loop!
236 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
238 // SendIdleMessage() and Dispatch() below may throw so the code here should
239 // be exception-safe, hence we must use local objects for all actions we
241 wxEventLoopActivator
activate(&ms_activeLoop
, this);
242 wxEventLoopImplTiedPtr
impl(&m_impl
, new wxEventLoopImpl
);
244 wxON_BLOCK_EXIT_OBJ0(*this, &wxEventLoop::OnExit
);
249 wxMutexGuiLeaveOrEnter();
250 #endif // wxUSE_THREADS
252 // generate and process idle events for as long as we don't have
253 // anything else to do
254 while ( !Pending() && m_impl
->SendIdleMessage() )
257 // a message came or no more idle processing to do, sit in
258 // Dispatch() waiting for the next message
266 return m_impl
->GetExitCode();
269 void wxEventLoop::Exit(int rc
)
271 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
273 m_impl
->SetExitCode(rc
);
275 ::PostQuitMessage(rc
);
278 // ----------------------------------------------------------------------------
279 // wxEventLoop message processing dispatching
280 // ----------------------------------------------------------------------------
282 bool wxEventLoop::Pending() const
285 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
288 bool wxEventLoop::Dispatch()
290 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
293 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
303 // should never happen, but let's test for it nevertheless
304 wxLogLastError(wxT("GetMessage"));
306 // still break from the loop
311 wxASSERT_MSG( wxThread::IsMain(),
312 wxT("only the main thread can process Windows messages") );
314 static bool s_hadGuiLock
= TRUE
;
315 static wxMsgArray s_aSavedMessages
;
317 // if a secondary thread owning the mutex is doing GUI calls, save all
318 // messages for later processing - we can't process them right now because
319 // it will lead to recursive library calls (and we're not reentrant)
320 if ( !wxGuiOwnedByMainThread() )
322 s_hadGuiLock
= FALSE
;
324 // leave out WM_COMMAND messages: too dangerous, sometimes
325 // the message will be processed twice
326 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
328 s_aSavedMessages
.Add(msg
);
335 // have we just regained the GUI lock? if so, post all of the saved
338 // FIXME of course, it's not _exactly_ the same as processing the
339 // messages normally - expect some things to break...
344 size_t count
= s_aSavedMessages
.Count();
345 for ( size_t n
= 0; n
< count
; n
++ )
347 MSG
& msg
= s_aSavedMessages
[n
];
348 m_impl
->ProcessMessage(&msg
);
351 s_aSavedMessages
.Empty();
354 #endif // wxUSE_THREADS
356 m_impl
->ProcessMessage(&msg
);