]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
do take the toolbar into account for Windows CE, otherwise the menus overlap with...
[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"
3808e191
JS
41
42#include "wx/msw/private.h"
43
0cd7d9b2 44#if wxUSE_THREADS
4300caa7
VZ
45 #include "wx/thread.h"
46
0cd7d9b2
VS
47 // define the array of MSG strutures
48 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
4300caa7
VZ
49
50 #include "wx/arrimpl.cpp"
51
52 WX_DEFINE_OBJARRAY(wxMsgArray);
53#endif // wxUSE_THREADS
0cd7d9b2 54
3808e191
JS
55// ----------------------------------------------------------------------------
56// wxEventLoopImpl
57// ----------------------------------------------------------------------------
58
59class WXDLLEXPORT wxEventLoopImpl
60{
61public:
62 // ctor
f7550d73 63 wxEventLoopImpl() { m_exitcode = 0; m_shouldExit = false; }
3808e191
JS
64
65 // process a message
66 void ProcessMessage(MSG *msg);
67
68 // generate an idle message, return TRUE if more idle time requested
69 bool SendIdleMessage();
70
71 // set/get the exit code
f7550d73 72 void Exit(int exitcode) { m_exitcode = exitcode; m_shouldExit = true; }
3808e191 73 int GetExitCode() const { return m_exitcode; }
f7550d73 74 bool ShouldExit() const { return m_shouldExit; }
3808e191
JS
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;
f7550d73
VZ
83
84 // true if we were asked to terminate
85 bool m_shouldExit;
3808e191
JS
86};
87
4300caa7
VZ
88// ----------------------------------------------------------------------------
89// helper class
90// ----------------------------------------------------------------------------
91
92wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl);
93
94// this object sets the wxEventLoop given to the ctor as the currently active
95// one and unsets it in its dtor
96class wxEventLoopActivator
97{
98public:
99 wxEventLoopActivator(wxEventLoop **pActive,
100 wxEventLoop *evtLoop)
101 {
102 m_pActive = pActive;
103 m_evtLoopOld = *pActive;
104 *pActive = evtLoop;
105 }
106
107 ~wxEventLoopActivator()
108 {
109 // restore the previously active event loop
110 *m_pActive = m_evtLoopOld;
111 }
112
113private:
114 wxEventLoop *m_evtLoopOld;
115 wxEventLoop **m_pActive;
116};
117
3808e191
JS
118// ============================================================================
119// wxEventLoopImpl implementation
120// ============================================================================
121
122// ----------------------------------------------------------------------------
123// wxEventLoopImpl message processing
124// ----------------------------------------------------------------------------
125
126void wxEventLoopImpl::ProcessMessage(MSG *msg)
127{
128 // give us the chance to preprocess the message first
129 if ( !PreProcessMessage(msg) )
130 {
131 // if it wasn't done, dispatch it to the corresponding window
132 ::TranslateMessage(msg);
133 ::DispatchMessage(msg);
134 }
135}
136
137bool wxEventLoopImpl::PreProcessMessage(MSG *msg)
138{
ac8d0c11
VZ
139 HWND hwnd = msg->hwnd;
140 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
141
142 // this may happen if the event occured in a standard modeless dialog (the
143 // only example of which I know of is the find/replace dialog) - then call
144 // IsDialogMessage() to make TAB navigation in it work
145 if ( !wndThis )
146 {
147 // we need to find the dialog containing this control as
148 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
149 // them) if we call it for the control itself
150 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
151 {
152 hwnd = ::GetParent(hwnd);
153 }
154
155 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
156 }
3808e191
JS
157
158#if wxUSE_TOOLTIPS
159 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
160 // popup the tooltip bubbles
ac8d0c11 161 if ( msg->message == WM_MOUSEMOVE )
3808e191
JS
162 {
163 wxToolTip *tt = wndThis->GetToolTip();
164 if ( tt )
165 {
166 tt->RelayEvent((WXMSG *)msg);
167 }
168 }
169#endif // wxUSE_TOOLTIPS
170
ac8d0c11
VZ
171 // allow the window to prevent certain messages from being
172 // translated/processed (this is currently used by wxTextCtrl to always
173 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
174 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
175 {
176 return FALSE;
177 }
178
179 // try translations first: the accelerators override everything
3808e191 180 wxWindow *wnd;
ac8d0c11 181
3808e191
JS
182 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
183 {
ac8d0c11 184 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3808e191 185 return TRUE;
ac8d0c11
VZ
186
187 // stop at first top level window, i.e. don't try to process the key
188 // strokes originating in a dialog using the accelerators of the parent
189 // frame - this doesn't make much sense
190 if ( wnd->IsTopLevel() )
191 break;
3808e191
JS
192 }
193
ac8d0c11
VZ
194 // now try the other hooks (kbd navigation is handled here): we start from
195 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
196 // called above
197 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
3808e191
JS
198 {
199 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
200 return TRUE;
201 }
202
ac8d0c11 203 // no special preprocessing for this message, dispatch it normally
3808e191
JS
204 return FALSE;
205}
206
207// ----------------------------------------------------------------------------
208// wxEventLoopImpl idle event processing
209// ----------------------------------------------------------------------------
210
211bool wxEventLoopImpl::SendIdleMessage()
212{
d284c075 213 return wxTheApp && wxTheApp->ProcessIdle();
cea62f9c
JS
214}
215
3808e191
JS
216// ============================================================================
217// wxEventLoop implementation
218// ============================================================================
219
b9f246f7
VS
220wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
221
3808e191
JS
222// ----------------------------------------------------------------------------
223// wxEventLoop running and exiting
224// ----------------------------------------------------------------------------
225
226wxEventLoop::~wxEventLoop()
227{
228 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
229}
230
231bool wxEventLoop::IsRunning() const
232{
233 return m_impl != NULL;
234}
235
236int wxEventLoop::Run()
237{
238 // event loops are not recursive, you need to create another loop!
239 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
240
4300caa7
VZ
241 // SendIdleMessage() and Dispatch() below may throw so the code here should
242 // be exception-safe, hence we must use local objects for all actions we
243 // should undo
244 wxEventLoopActivator activate(&ms_activeLoop, this);
245 wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl);
246
2a8f35c3
VZ
247 // we must ensure that OnExit() is called even if an exception is thrown
248 // from inside Dispatch() but we must call it from Exit() in normal
249 // situations because it is supposed to be called synchronously,
250 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
251 // something similar here)
cea62f9c 252#if wxUSE_EXCEPTIONS
d284c075 253 for ( ;; )
3808e191 254 {
d284c075 255 try
2a8f35c3 256 {
d284c075
VZ
257#endif // wxUSE_EXCEPTIONS
258
259 // this is the event loop itself
260 for ( ;; )
f7550d73 261 {
d284c075
VZ
262 #if wxUSE_THREADS
263 wxMutexGuiLeaveOrEnter();
264 #endif // wxUSE_THREADS
265
266 // generate and process idle events for as long as we don't
267 // have anything else to do
268 while ( !Pending() && m_impl->SendIdleMessage() )
269 ;
270
271 // if the "should exit" flag is set, the loop should terminate
272 // but not before processing any remaining messages so while
273 // Pending() returns true, do process them
274 if ( m_impl->ShouldExit() )
275 {
276 while ( Pending() )
277 Dispatch();
f7550d73 278
d284c075
VZ
279 break;
280 }
f7550d73 281
d284c075
VZ
282 // a message came or no more idle processing to do, sit in
283 // Dispatch() waiting for the next message
284 if ( !Dispatch() )
285 {
286 // we got WM_QUIT
287 break;
288 }
2a8f35c3 289 }
d284c075
VZ
290
291#if wxUSE_EXCEPTIONS
292 // exit the outer loop as well
293 break;
3808e191 294 }
d284c075
VZ
295 catch ( ... )
296 {
297 try
298 {
299 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
300 {
cea62f9c 301 OnExit();
cea62f9c 302 break;
d284c075
VZ
303 }
304 //else: continue running the event loop
305 }
306 catch ( ... )
307 {
308 // OnException() throwed, possibly rethrowing the same
309 // exception again: very good, but we still need OnExit() to
310 // be called
311 OnExit();
312 throw;
313 }
314 }
3808e191 315 }
d284c075 316#endif // wxUSE_EXCEPTIONS
3808e191 317
4300caa7 318 return m_impl->GetExitCode();
3808e191
JS
319}
320
321void wxEventLoop::Exit(int rc)
322{
323 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
324
f7550d73 325 m_impl->Exit(rc);
3808e191 326
2a8f35c3
VZ
327 OnExit();
328
f7550d73
VZ
329 // all we have to do to exit from the loop is to (maybe) wake it up so that
330 // it can notice that Exit() had been called
331 //
332 // in particular, we do *not* use PostQuitMessage() here because we're not
333 // sure that WM_QUIT is going to be processed by the correct event loop: it
334 // is possible that another one is started before this one has a chance to
335 // process WM_QUIT
336 ::PostMessage(NULL, WM_NULL, 0, 0);
3808e191
JS
337}
338
339// ----------------------------------------------------------------------------
340// wxEventLoop message processing dispatching
341// ----------------------------------------------------------------------------
342
343bool wxEventLoop::Pending() const
344{
345 MSG msg;
346 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
347}
348
349bool wxEventLoop::Dispatch()
350{
351 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
352
353 MSG msg;
354 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
355
356 if ( rc == 0 )
357 {
358 // got WM_QUIT
359 return FALSE;
360 }
361
362 if ( rc == -1 )
363 {
364 // should never happen, but let's test for it nevertheless
365 wxLogLastError(wxT("GetMessage"));
366
367 // still break from the loop
368 return FALSE;
369 }
370
371#if wxUSE_THREADS
372 wxASSERT_MSG( wxThread::IsMain(),
373 wxT("only the main thread can process Windows messages") );
374
375 static bool s_hadGuiLock = TRUE;
376 static wxMsgArray s_aSavedMessages;
377
378 // if a secondary thread owning the mutex is doing GUI calls, save all
379 // messages for later processing - we can't process them right now because
380 // it will lead to recursive library calls (and we're not reentrant)
381 if ( !wxGuiOwnedByMainThread() )
382 {
383 s_hadGuiLock = FALSE;
384
385 // leave out WM_COMMAND messages: too dangerous, sometimes
386 // the message will be processed twice
6c0e8b4e 387 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 388 {
0cd7d9b2 389 s_aSavedMessages.Add(msg);
3808e191
JS
390 }
391
392 return TRUE;
393 }
394 else
395 {
396 // have we just regained the GUI lock? if so, post all of the saved
397 // messages
398 //
399 // FIXME of course, it's not _exactly_ the same as processing the
400 // messages normally - expect some things to break...
401 if ( !s_hadGuiLock )
402 {
403 s_hadGuiLock = TRUE;
404
405 size_t count = s_aSavedMessages.Count();
406 for ( size_t n = 0; n < count; n++ )
407 {
408 MSG& msg = s_aSavedMessages[n];
409 m_impl->ProcessMessage(&msg);
410 }
411
412 s_aSavedMessages.Empty();
413 }
414 }
415#endif // wxUSE_THREADS
416
417 m_impl->ProcessMessage(&msg);
418
419 return TRUE;
420}
421