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