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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "evtloop.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/evtloop.h"
35 #include "wx/window.h"
38 #include "wx/msw/private.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxEventLoopImpl
48 wxEventLoopImpl() { SetExitCode(0); }
51 void ProcessMessage(MSG
*msg
);
53 // generate an idle message, return TRUE if more idle time requested
54 bool SendIdleMessage();
56 // set/get the exit code
57 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
58 int GetExitCode() const { return m_exitcode
; }
61 // preprocess a message, return TRUE if processed (i.e. no further
62 // dispatching required)
63 bool PreProcessMessage(MSG
*msg
);
65 // the exit code of the event loop
69 // ============================================================================
70 // wxEventLoopImpl implementation
71 // ============================================================================
73 // ----------------------------------------------------------------------------
74 // wxEventLoopImpl message processing
75 // ----------------------------------------------------------------------------
77 void wxEventLoopImpl::ProcessMessage(MSG
*msg
)
79 // give us the chance to preprocess the message first
80 if ( !PreProcessMessage(msg
) )
82 // if it wasn't done, dispatch it to the corresponding window
83 ::TranslateMessage(msg
);
84 ::DispatchMessage(msg
);
88 bool wxEventLoopImpl::PreProcessMessage(MSG
*msg
)
90 HWND hWnd
= msg
->hwnd
;
91 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
94 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
95 // popup the tooltip bubbles
96 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
98 wxToolTip
*tt
= wndThis
->GetToolTip();
101 tt
->RelayEvent((WXMSG
*)msg
);
104 #endif // wxUSE_TOOLTIPS
106 // try translations first; find the youngest window with a translation
109 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
111 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
115 // Anyone for a non-translation message? Try youngest descendants first.
116 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
118 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
125 // ----------------------------------------------------------------------------
126 // wxEventLoopImpl idle event processing
127 // ----------------------------------------------------------------------------
129 bool wxEventLoopImpl::SendIdleMessage()
133 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
136 // ============================================================================
137 // wxEventLoop implementation
138 // ============================================================================
140 // ----------------------------------------------------------------------------
141 // wxEventLoop running and exiting
142 // ----------------------------------------------------------------------------
144 wxEventLoop::~wxEventLoop()
146 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
149 bool wxEventLoop::IsRunning() const
151 return m_impl
!= NULL
;
154 int wxEventLoop::Run()
156 // event loops are not recursive, you need to create another loop!
157 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
159 m_impl
= new wxEventLoopImpl
;
164 wxMutexGuiLeaveOrEnter();
165 #endif // wxUSE_THREADS
167 // generate and process idle events for as long as we don't have
168 // anything else to do
169 while ( !Pending() && m_impl
->SendIdleMessage() )
172 // a message came or no more idle processing to do, sit in Dispatch()
173 // waiting for the next message
181 int exitcode
= m_impl
->GetExitCode();
188 void wxEventLoop::Exit(int rc
)
190 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
192 m_impl
->SetExitCode(rc
);
194 ::PostQuitMessage(rc
);
197 // ----------------------------------------------------------------------------
198 // wxEventLoop message processing dispatching
199 // ----------------------------------------------------------------------------
201 bool wxEventLoop::Pending() const
204 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
207 bool wxEventLoop::Dispatch()
209 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
212 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
222 // should never happen, but let's test for it nevertheless
223 wxLogLastError(wxT("GetMessage"));
225 // still break from the loop
230 wxASSERT_MSG( wxThread::IsMain(),
231 wxT("only the main thread can process Windows messages") );
233 static bool s_hadGuiLock
= TRUE
;
234 static wxMsgArray s_aSavedMessages
;
236 // if a secondary thread owning the mutex is doing GUI calls, save all
237 // messages for later processing - we can't process them right now because
238 // it will lead to recursive library calls (and we're not reentrant)
239 if ( !wxGuiOwnedByMainThread() )
241 s_hadGuiLock
= FALSE
;
243 // leave out WM_COMMAND messages: too dangerous, sometimes
244 // the message will be processed twice
245 if ( !wxIsWaitingForThread() ||
246 s_currentMsg
.message
!= WM_COMMAND
)
248 s_aSavedMessages
.Add(s_currentMsg
);
255 // have we just regained the GUI lock? if so, post all of the saved
258 // FIXME of course, it's not _exactly_ the same as processing the
259 // messages normally - expect some things to break...
264 size_t count
= s_aSavedMessages
.Count();
265 for ( size_t n
= 0; n
< count
; n
++ )
267 MSG
& msg
= s_aSavedMessages
[n
];
268 m_impl
->ProcessMessage(&msg
);
271 s_aSavedMessages
.Empty();
274 #endif // wxUSE_THREADS
276 m_impl
->ProcessMessage(&msg
);