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"
36 #include "wx/msw/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxEventLoopImpl
46 wxEventLoopImpl() { SetExitCode(0); }
49 void ProcessMessage(MSG
*msg
);
51 // generate an idle message, return TRUE if more idle time requested
52 bool SendIdleMessage();
54 // set/get the exit code
55 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
56 int GetExitCode() const { return m_exitcode
; }
59 // preprocess a message, return TRUE if processed (i.e. no further
60 // dispatching required)
61 bool PreProcessMessage(MSG
*msg
);
63 // the exit code of the event loop
67 // ============================================================================
68 // wxEventLoopImpl implementation
69 // ============================================================================
71 // ----------------------------------------------------------------------------
72 // wxEventLoopImpl message processing
73 // ----------------------------------------------------------------------------
75 void wxEventLoopImpl::ProcessMessage(MSG
*msg
)
77 // give us the chance to preprocess the message first
78 if ( !PreProcessMessage(msg
) )
80 // if it wasn't done, dispatch it to the corresponding window
81 ::TranslateMessage(msg
);
82 ::DispatchMessage(msg
);
86 bool wxEventLoopImpl::PreProcessMessage(MSG
*msg
)
88 HWND hWnd
= msg
->hwnd
;
89 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
92 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
93 // popup the tooltip bubbles
94 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
96 wxToolTip
*tt
= wndThis
->GetToolTip();
99 tt
->RelayEvent((WXMSG
*)msg
);
102 #endif // wxUSE_TOOLTIPS
104 // try translations first; find the youngest window with a translation
107 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
109 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
113 // Anyone for a non-translation message? Try youngest descendants first.
114 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
116 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
123 // ----------------------------------------------------------------------------
124 // wxEventLoopImpl idle event processing
125 // ----------------------------------------------------------------------------
127 bool wxEventLoopImpl::SendIdleMessage()
131 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
134 // ============================================================================
135 // wxEventLoop implementation
136 // ============================================================================
138 // ----------------------------------------------------------------------------
139 // wxEventLoop running and exiting
140 // ----------------------------------------------------------------------------
142 wxEventLoop::~wxEventLoop()
144 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
147 bool wxEventLoop::IsRunning() const
149 return m_impl
!= NULL
;
152 int wxEventLoop::Run()
154 // event loops are not recursive, you need to create another loop!
155 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
157 m_impl
= new wxEventLoopImpl
;
162 wxMutexGuiLeaveOrEnter();
163 #endif // wxUSE_THREADS
165 // generate and process idle events for as long as we don't have
166 // anything else to do
167 while ( !Pending() && m_impl
->SendIdleMessage() )
170 // a message came or no more idle processing to do, sit in Dispatch()
171 // waiting for the next message
179 int exitcode
= m_impl
->GetExitCode();
186 void wxEventLoop::Exit(int rc
)
188 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
190 m_impl
->SetExitCode(rc
);
192 ::PostQuitMessage(rc
);
195 // ----------------------------------------------------------------------------
196 // wxEventLoop message processing dispatching
197 // ----------------------------------------------------------------------------
199 bool wxEventLoop::Pending() const
202 return ::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
205 bool wxEventLoop::Dispatch()
207 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
210 BOOL rc
= ::GetMessage(&msg
, (HWND
) NULL
, 0, 0);
220 // should never happen, but let's test for it nevertheless
221 wxLogLastError(wxT("GetMessage"));
223 // still break from the loop
228 wxASSERT_MSG( wxThread::IsMain(),
229 wxT("only the main thread can process Windows messages") );
231 static bool s_hadGuiLock
= TRUE
;
232 static wxMsgArray s_aSavedMessages
;
234 // if a secondary thread owning the mutex is doing GUI calls, save all
235 // messages for later processing - we can't process them right now because
236 // it will lead to recursive library calls (and we're not reentrant)
237 if ( !wxGuiOwnedByMainThread() )
239 s_hadGuiLock
= FALSE
;
241 // leave out WM_COMMAND messages: too dangerous, sometimes
242 // the message will be processed twice
243 if ( !wxIsWaitingForThread() ||
244 s_currentMsg
.message
!= WM_COMMAND
)
246 s_aSavedMessages
.Add(s_currentMsg
);
253 // have we just regained the GUI lock? if so, post all of the saved
256 // FIXME of course, it's not _exactly_ the same as processing the
257 // messages normally - expect some things to break...
262 size_t count
= s_aSavedMessages
.Count();
263 for ( size_t n
= 0; n
< count
; n
++ )
265 MSG
& msg
= s_aSavedMessages
[n
];
266 m_impl
->ProcessMessage(&msg
);
269 s_aSavedMessages
.Empty();
272 #endif // wxUSE_THREADS
274 m_impl
->ProcessMessage(&msg
);