]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
extracted duplicated code from src/*/data.cpp in a common file
[wxWidgets.git] / src / msw / evtloop.cpp
CommitLineData
3808e191
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/evtloop.cpp
3// Purpose: implements wxEventLoop for MSW
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 01.06.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
6c9a19aa 9// License: wxWindows licence
3808e191
JS
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "evtloop.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
6c0e8b4e
VZ
32 #include "wx/window.h"
33 #include "wx/app.h"
3808e191
JS
34#endif //WX_PRECOMP
35
36#include "wx/evtloop.h"
0cd7d9b2 37#include "wx/tooltip.h"
3808e191
JS
38
39#include "wx/msw/private.h"
40
0cd7d9b2
VS
41#if wxUSE_THREADS
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
6c0e8b4e 46 // defined in app.cpp).
0cd7d9b2
VS
47#endif
48
3808e191
JS
49// ----------------------------------------------------------------------------
50// wxEventLoopImpl
51// ----------------------------------------------------------------------------
52
53class WXDLLEXPORT wxEventLoopImpl
54{
55public:
56 // ctor
57 wxEventLoopImpl() { SetExitCode(0); }
58
59 // process a message
60 void ProcessMessage(MSG *msg);
61
62 // generate an idle message, return TRUE if more idle time requested
63 bool SendIdleMessage();
64
65 // set/get the exit code
66 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
67 int GetExitCode() const { return m_exitcode; }
68
69private:
70 // preprocess a message, return TRUE if processed (i.e. no further
71 // dispatching required)
72 bool PreProcessMessage(MSG *msg);
73
74 // the exit code of the event loop
75 int m_exitcode;
76};
77
78// ============================================================================
79// wxEventLoopImpl implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
83// wxEventLoopImpl message processing
84// ----------------------------------------------------------------------------
85
86void wxEventLoopImpl::ProcessMessage(MSG *msg)
87{
88 // give us the chance to preprocess the message first
89 if ( !PreProcessMessage(msg) )
90 {
91 // if it wasn't done, dispatch it to the corresponding window
92 ::TranslateMessage(msg);
93 ::DispatchMessage(msg);
94 }
95}
96
97bool wxEventLoopImpl::PreProcessMessage(MSG *msg)
98{
99 HWND hWnd = msg->hwnd;
100 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd);
101
102#if wxUSE_TOOLTIPS
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) )
106 {
107 wxToolTip *tt = wndThis->GetToolTip();
108 if ( tt )
109 {
110 tt->RelayEvent((WXMSG *)msg);
111 }
112 }
113#endif // wxUSE_TOOLTIPS
114
115 // try translations first; find the youngest window with a translation
116 // table.
117 wxWindow *wnd;
118 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
119 {
120 if ( wnd->MSWTranslateMessage((WXMSG *)msg) )
121 return TRUE;
122 }
123
124 // Anyone for a non-translation message? Try youngest descendants first.
125 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
126 {
127 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
128 return TRUE;
129 }
130
131 return FALSE;
132}
133
134// ----------------------------------------------------------------------------
135// wxEventLoopImpl idle event processing
136// ----------------------------------------------------------------------------
137
138bool wxEventLoopImpl::SendIdleMessage()
139{
140 wxIdleEvent event;
141
142 return wxTheApp->ProcessEvent(event) && event.MoreRequested();
143}
144
145// ============================================================================
146// wxEventLoop implementation
147// ============================================================================
148
b9f246f7
VS
149wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
150
3808e191
JS
151// ----------------------------------------------------------------------------
152// wxEventLoop running and exiting
153// ----------------------------------------------------------------------------
154
155wxEventLoop::~wxEventLoop()
156{
157 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
158}
159
160bool wxEventLoop::IsRunning() const
161{
162 return m_impl != NULL;
163}
164
165int wxEventLoop::Run()
166{
167 // event loops are not recursive, you need to create another loop!
168 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
169
170 m_impl = new wxEventLoopImpl;
b9f246f7
VS
171
172 wxEventLoop *oldLoop = ms_activeLoop;
173 ms_activeLoop = this;
3808e191
JS
174
175 for ( ;; )
176 {
177#if wxUSE_THREADS
178 wxMutexGuiLeaveOrEnter();
179#endif // wxUSE_THREADS
180
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() )
184 ;
185
186 // a message came or no more idle processing to do, sit in Dispatch()
187 // waiting for the next message
188 if ( !Dispatch() )
189 {
190 // we got WM_QUIT
191 break;
192 }
193 }
194
195 int exitcode = m_impl->GetExitCode();
196 delete m_impl;
197 m_impl = NULL;
198
b9f246f7
VS
199 ms_activeLoop = oldLoop;
200
3808e191
JS
201 return exitcode;
202}
203
204void wxEventLoop::Exit(int rc)
205{
206 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
207
208 m_impl->SetExitCode(rc);
209
210 ::PostQuitMessage(rc);
211}
212
213// ----------------------------------------------------------------------------
214// wxEventLoop message processing dispatching
215// ----------------------------------------------------------------------------
216
217bool wxEventLoop::Pending() const
218{
219 MSG msg;
220 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
221}
222
223bool wxEventLoop::Dispatch()
224{
225 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
226
227 MSG msg;
228 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
229
230 if ( rc == 0 )
231 {
232 // got WM_QUIT
233 return FALSE;
234 }
235
236 if ( rc == -1 )
237 {
238 // should never happen, but let's test for it nevertheless
239 wxLogLastError(wxT("GetMessage"));
240
241 // still break from the loop
242 return FALSE;
243 }
244
245#if wxUSE_THREADS
246 wxASSERT_MSG( wxThread::IsMain(),
247 wxT("only the main thread can process Windows messages") );
248
249 static bool s_hadGuiLock = TRUE;
250 static wxMsgArray s_aSavedMessages;
251
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() )
256 {
257 s_hadGuiLock = FALSE;
258
259 // leave out WM_COMMAND messages: too dangerous, sometimes
260 // the message will be processed twice
6c0e8b4e 261 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 262 {
0cd7d9b2 263 s_aSavedMessages.Add(msg);
3808e191
JS
264 }
265
266 return TRUE;
267 }
268 else
269 {
270 // have we just regained the GUI lock? if so, post all of the saved
271 // messages
272 //
273 // FIXME of course, it's not _exactly_ the same as processing the
274 // messages normally - expect some things to break...
275 if ( !s_hadGuiLock )
276 {
277 s_hadGuiLock = TRUE;
278
279 size_t count = s_aSavedMessages.Count();
280 for ( size_t n = 0; n < count; n++ )
281 {
282 MSG& msg = s_aSavedMessages[n];
283 m_impl->ProcessMessage(&msg);
284 }
285
286 s_aSavedMessages.Empty();
287 }
288 }
289#endif // wxUSE_THREADS
290
291 m_impl->ProcessMessage(&msg);
292
293 return TRUE;
294}
295