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"
32 #include "wx/window.h"
36 #include "wx/evtloop.h"
37 #include "wx/tooltip.h"
39 #include "wx/msw/private.h"
42 // define the array of MSG strutures
43 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
44 // VS: this is a bit dirty - it duplicates same declaration in app.cpp
45 // (and there's no WX_DEFINE_OBJARRAY for that reason - it is already
46 // defined in app.cpp).
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 class WXDLLEXPORT wxEventLoopImpl
57 wxEventLoopImpl() { SetExitCode(0); }
60 void ProcessMessage(MSG
*msg
);
62 // generate an idle message, return TRUE if more idle time requested
63 bool SendIdleMessage();
65 // set/get the exit code
66 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
67 int GetExitCode() const { return m_exitcode
; }
70 // preprocess a message, return TRUE if processed (i.e. no further
71 // dispatching required)
72 bool PreProcessMessage(MSG
*msg
);
74 // the exit code of the event loop
78 // ============================================================================
79 // wxEventLoopImpl implementation
80 // ============================================================================
82 // ----------------------------------------------------------------------------
83 // wxEventLoopImpl message processing
84 // ----------------------------------------------------------------------------
86 void wxEventLoopImpl::ProcessMessage(MSG
*msg
)
88 // give us the chance to preprocess the message first
89 if ( !PreProcessMessage(msg
) )
91 // if it wasn't done, dispatch it to the corresponding window
92 ::TranslateMessage(msg
);
93 ::DispatchMessage(msg
);
97 bool wxEventLoopImpl::PreProcessMessage(MSG
*msg
)
99 HWND hWnd
= msg
->hwnd
;
100 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
103 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
104 // popup the tooltip bubbles
105 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
107 wxToolTip
*tt
= wndThis
->GetToolTip();
110 tt
->RelayEvent((WXMSG
*)msg
);
113 #endif // wxUSE_TOOLTIPS
115 // try translations first; find the youngest window with a translation
118 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
120 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
124 // Anyone for a non-translation message? Try youngest descendants first.
125 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
127 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
134 // ----------------------------------------------------------------------------
135 // wxEventLoopImpl idle event processing
136 // ----------------------------------------------------------------------------
138 bool wxEventLoopImpl::SendIdleMessage()
142 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
145 // ============================================================================
146 // wxEventLoop implementation
147 // ============================================================================
149 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
151 // ----------------------------------------------------------------------------
152 // wxEventLoop running and exiting
153 // ----------------------------------------------------------------------------
155 wxEventLoop::~wxEventLoop()
157 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
160 bool wxEventLoop::IsRunning() const
162 return m_impl
!= NULL
;
165 int wxEventLoop::Run()
167 // event loops are not recursive, you need to create another loop!
168 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
170 m_impl
= new wxEventLoopImpl
;
172 wxEventLoop
*oldLoop
= ms_activeLoop
;
173 ms_activeLoop
= this;
178 wxMutexGuiLeaveOrEnter();
179 #endif // wxUSE_THREADS
181 // generate and process idle events for as long as we don't have
182 // anything else to do
183 while ( !Pending() && m_impl
->SendIdleMessage() )
186 // a message came or no more idle processing to do, sit in Dispatch()
187 // waiting for the next message
195 int exitcode
= m_impl
->GetExitCode();
199 ms_activeLoop
= oldLoop
;
204 void wxEventLoop::Exit(int rc
)
206 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
208 m_impl
->SetExitCode(rc
);
210 ::PostQuitMessage(rc
);
213 // ----------------------------------------------------------------------------
214 // wxEventLoop message processing dispatching
215 // ----------------------------------------------------------------------------
217 bool wxEventLoop::Pending() const
220 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
223 bool wxEventLoop::Dispatch()
225 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
228 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
238 // should never happen, but let's test for it nevertheless
239 wxLogLastError(wxT("GetMessage"));
241 // still break from the loop
246 wxASSERT_MSG( wxThread::IsMain(),
247 wxT("only the main thread can process Windows messages") );
249 static bool s_hadGuiLock
= TRUE
;
250 static wxMsgArray s_aSavedMessages
;
252 // if a secondary thread owning the mutex is doing GUI calls, save all
253 // messages for later processing - we can't process them right now because
254 // it will lead to recursive library calls (and we're not reentrant)
255 if ( !wxGuiOwnedByMainThread() )
257 s_hadGuiLock
= FALSE
;
259 // leave out WM_COMMAND messages: too dangerous, sometimes
260 // the message will be processed twice
261 if ( !wxIsWaitingForThread() || msg
.message
!= WM_COMMAND
)
263 s_aSavedMessages
.Add(msg
);
270 // have we just regained the GUI lock? if so, post all of the saved
273 // FIXME of course, it's not _exactly_ the same as processing the
274 // messages normally - expect some things to break...
279 size_t count
= s_aSavedMessages
.Count();
280 for ( size_t n
= 0; n
< count
; n
++ )
282 MSG
& msg
= s_aSavedMessages
[n
];
283 m_impl
->ProcessMessage(&msg
);
286 s_aSavedMessages
.Empty();
289 #endif // wxUSE_THREADS
291 m_impl
->ProcessMessage(&msg
);