]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
Reverted fragment
[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
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3808e191
JS
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"
4300caa7 37
0cd7d9b2 38#include "wx/tooltip.h"
4300caa7
VZ
39#include "wx/except.h"
40#include "wx/ptr_scpd.h"
41#include "wx/scopeguard.h"
3808e191
JS
42
43#include "wx/msw/private.h"
44
0cd7d9b2 45#if wxUSE_THREADS
4300caa7
VZ
46 #include "wx/thread.h"
47
0cd7d9b2
VS
48 // define the array of MSG strutures
49 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
4300caa7
VZ
50
51 #include "wx/arrimpl.cpp"
52
53 WX_DEFINE_OBJARRAY(wxMsgArray);
54#endif // wxUSE_THREADS
0cd7d9b2 55
3808e191
JS
56// ----------------------------------------------------------------------------
57// wxEventLoopImpl
58// ----------------------------------------------------------------------------
59
60class WXDLLEXPORT wxEventLoopImpl
61{
62public:
63 // ctor
64 wxEventLoopImpl() { SetExitCode(0); }
65
66 // process a message
67 void ProcessMessage(MSG *msg);
68
69 // generate an idle message, return TRUE if more idle time requested
70 bool SendIdleMessage();
71
72 // set/get the exit code
73 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
74 int GetExitCode() const { return m_exitcode; }
75
76private:
77 // preprocess a message, return TRUE if processed (i.e. no further
78 // dispatching required)
79 bool PreProcessMessage(MSG *msg);
80
81 // the exit code of the event loop
82 int m_exitcode;
83};
84
4300caa7
VZ
85// ----------------------------------------------------------------------------
86// helper class
87// ----------------------------------------------------------------------------
88
89wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl);
90
91// this object sets the wxEventLoop given to the ctor as the currently active
92// one and unsets it in its dtor
93class wxEventLoopActivator
94{
95public:
96 wxEventLoopActivator(wxEventLoop **pActive,
97 wxEventLoop *evtLoop)
98 {
99 m_pActive = pActive;
100 m_evtLoopOld = *pActive;
101 *pActive = evtLoop;
102 }
103
104 ~wxEventLoopActivator()
105 {
106 // restore the previously active event loop
107 *m_pActive = m_evtLoopOld;
108 }
109
110private:
111 wxEventLoop *m_evtLoopOld;
112 wxEventLoop **m_pActive;
113};
114
3808e191
JS
115// ============================================================================
116// wxEventLoopImpl implementation
117// ============================================================================
118
119// ----------------------------------------------------------------------------
120// wxEventLoopImpl message processing
121// ----------------------------------------------------------------------------
122
123void wxEventLoopImpl::ProcessMessage(MSG *msg)
124{
125 // give us the chance to preprocess the message first
126 if ( !PreProcessMessage(msg) )
127 {
128 // if it wasn't done, dispatch it to the corresponding window
129 ::TranslateMessage(msg);
130 ::DispatchMessage(msg);
131 }
132}
133
134bool wxEventLoopImpl::PreProcessMessage(MSG *msg)
135{
ac8d0c11
VZ
136 HWND hwnd = msg->hwnd;
137 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
138
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
142 if ( !wndThis )
143 {
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 )
148 {
149 hwnd = ::GetParent(hwnd);
150 }
151
152 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
153 }
3808e191
JS
154
155#if wxUSE_TOOLTIPS
156 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
157 // popup the tooltip bubbles
ac8d0c11 158 if ( msg->message == WM_MOUSEMOVE )
3808e191
JS
159 {
160 wxToolTip *tt = wndThis->GetToolTip();
161 if ( tt )
162 {
163 tt->RelayEvent((WXMSG *)msg);
164 }
165 }
166#endif // wxUSE_TOOLTIPS
167
ac8d0c11
VZ
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) )
172 {
173 return FALSE;
174 }
175
176 // try translations first: the accelerators override everything
3808e191 177 wxWindow *wnd;
ac8d0c11 178
3808e191
JS
179 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
180 {
ac8d0c11 181 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3808e191 182 return TRUE;
ac8d0c11
VZ
183
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() )
188 break;
3808e191
JS
189 }
190
ac8d0c11
VZ
191 // now try the other hooks (kbd navigation is handled here): we start from
192 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
193 // called above
194 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
3808e191
JS
195 {
196 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
197 return TRUE;
198 }
199
ac8d0c11 200 // no special preprocessing for this message, dispatch it normally
3808e191
JS
201 return FALSE;
202}
203
204// ----------------------------------------------------------------------------
205// wxEventLoopImpl idle event processing
206// ----------------------------------------------------------------------------
207
208bool wxEventLoopImpl::SendIdleMessage()
209{
e39af974 210 return wxTheApp->ProcessIdle();
3808e191
JS
211}
212
213// ============================================================================
214// wxEventLoop implementation
215// ============================================================================
216
b9f246f7
VS
217wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
218
3808e191
JS
219// ----------------------------------------------------------------------------
220// wxEventLoop running and exiting
221// ----------------------------------------------------------------------------
222
223wxEventLoop::~wxEventLoop()
224{
225 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
226}
227
228bool wxEventLoop::IsRunning() const
229{
230 return m_impl != NULL;
231}
232
233int wxEventLoop::Run()
234{
235 // event loops are not recursive, you need to create another loop!
236 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
237
4300caa7
VZ
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
240 // should undo
241 wxEventLoopActivator activate(&ms_activeLoop, this);
242 wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl);
243
244 wxON_BLOCK_EXIT_OBJ0(*this, &wxEventLoop::OnExit);
3808e191
JS
245
246 for ( ;; )
247 {
248#if wxUSE_THREADS
249 wxMutexGuiLeaveOrEnter();
250#endif // wxUSE_THREADS
251
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() )
255 ;
256
4300caa7
VZ
257 // a message came or no more idle processing to do, sit in
258 // Dispatch() waiting for the next message
3808e191
JS
259 if ( !Dispatch() )
260 {
261 // we got WM_QUIT
262 break;
263 }
264 }
265
4300caa7 266 return m_impl->GetExitCode();
3808e191
JS
267}
268
269void wxEventLoop::Exit(int rc)
270{
271 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
272
273 m_impl->SetExitCode(rc);
274
275 ::PostQuitMessage(rc);
276}
277
278// ----------------------------------------------------------------------------
279// wxEventLoop message processing dispatching
280// ----------------------------------------------------------------------------
281
282bool wxEventLoop::Pending() const
283{
284 MSG msg;
285 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
286}
287
288bool wxEventLoop::Dispatch()
289{
290 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
291
292 MSG msg;
293 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
294
295 if ( rc == 0 )
296 {
297 // got WM_QUIT
298 return FALSE;
299 }
300
301 if ( rc == -1 )
302 {
303 // should never happen, but let's test for it nevertheless
304 wxLogLastError(wxT("GetMessage"));
305
306 // still break from the loop
307 return FALSE;
308 }
309
310#if wxUSE_THREADS
311 wxASSERT_MSG( wxThread::IsMain(),
312 wxT("only the main thread can process Windows messages") );
313
314 static bool s_hadGuiLock = TRUE;
315 static wxMsgArray s_aSavedMessages;
316
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() )
321 {
322 s_hadGuiLock = FALSE;
323
324 // leave out WM_COMMAND messages: too dangerous, sometimes
325 // the message will be processed twice
6c0e8b4e 326 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 327 {
0cd7d9b2 328 s_aSavedMessages.Add(msg);
3808e191
JS
329 }
330
331 return TRUE;
332 }
333 else
334 {
335 // have we just regained the GUI lock? if so, post all of the saved
336 // messages
337 //
338 // FIXME of course, it's not _exactly_ the same as processing the
339 // messages normally - expect some things to break...
340 if ( !s_hadGuiLock )
341 {
342 s_hadGuiLock = TRUE;
343
344 size_t count = s_aSavedMessages.Count();
345 for ( size_t n = 0; n < count; n++ )
346 {
347 MSG& msg = s_aSavedMessages[n];
348 m_impl->ProcessMessage(&msg);
349 }
350
351 s_aSavedMessages.Empty();
352 }
353 }
354#endif // wxUSE_THREADS
355
356 m_impl->ProcessMessage(&msg);
357
358 return TRUE;
359}
360