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