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