]> git.saurik.com Git - wxWidgets.git/blob - src/msw/evtloop.cpp
Mac compilation fix after const patch
[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 * const wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
128 wxWindow *wnd;
129
130 // this may happen if the event occurred in a standard modeless dialog (the
131 // only example of which I know of is the find/replace dialog) - then call
132 // IsDialogMessage() to make TAB navigation in it work
133 if ( !wndThis )
134 {
135 // we need to find the dialog containing this control as
136 // IsDialogMessage() just eats all the messages (i.e. returns true for
137 // them) if we call it for the control itself
138 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
139 {
140 hwnd = ::GetParent(hwnd);
141 }
142
143 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
144 }
145
146 if ( !AllowProcessing(wndThis) )
147 {
148 // not a child of critical window, so we eat the event but take care to
149 // stop an endless stream of WM_PAINTs which would have resulted if we
150 // didn't validate the invalidated part of the window
151 if ( msg->message == WM_PAINT )
152 ::ValidateRect(hwnd, NULL);
153
154 return true;
155 }
156
157 #if wxUSE_TOOLTIPS
158 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
159 // popup the tooltip bubbles
160 if ( msg->message == WM_MOUSEMOVE )
161 {
162 wxToolTip *tt = wndThis->GetToolTip();
163 if ( tt )
164 {
165 tt->RelayEvent((WXMSG *)msg);
166 }
167 }
168 #endif // wxUSE_TOOLTIPS
169
170 // allow the window to prevent certain messages from being
171 // translated/processed (this is currently used by wxTextCtrl to always
172 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
173 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
174 {
175 return false;
176 }
177
178 // try translations first: the accelerators override everything
179 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
180 {
181 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
182 return true;
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;
189 }
190
191 // now try the other hooks (kbd navigation is handled here)
192 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
193 {
194 if (wnd != wndThis) // Skip the first since wndThis->MSWProcessMessage() was called above
195 {
196 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
197 return true;
198 }
199
200 // Stop at first top level window (as per comment above).
201 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
202 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
203 if (wnd->IsTopLevel())
204 break;
205 }
206
207 // no special preprocessing for this message, dispatch it normally
208 return false;
209 }
210
211 // ----------------------------------------------------------------------------
212 // wxEventLoop running and exiting
213 // ----------------------------------------------------------------------------
214
215 bool wxEventLoop::IsRunning() const
216 {
217 return ms_activeLoop == this;
218 }
219
220 int wxEventLoop::Run()
221 {
222 // event loops are not recursive, you need to create another loop!
223 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
224
225 // ProcessIdle() and Dispatch() below may throw so the code here should
226 // be exception-safe, hence we must use local objects for all actions we
227 // should undo
228 wxEventLoopActivator activate(&ms_activeLoop, this);
229
230 // we must ensure that OnExit() is called even if an exception is thrown
231 // from inside Dispatch() but we must call it from Exit() in normal
232 // situations because it is supposed to be called synchronously,
233 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
234 // something similar here)
235 #if wxUSE_EXCEPTIONS
236 for ( ;; )
237 {
238 try
239 {
240 #endif // wxUSE_EXCEPTIONS
241
242 // this is the event loop itself
243 for ( ;; )
244 {
245 #if wxUSE_THREADS
246 wxMutexGuiLeaveOrEnter();
247 #endif // wxUSE_THREADS
248
249 // generate and process idle events for as long as we don't
250 // have anything else to do
251 while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) )
252 ;
253
254 // if the "should exit" flag is set, the loop should terminate
255 // but not before processing any remaining messages so while
256 // Pending() returns true, do process them
257 if ( m_shouldExit )
258 {
259 while ( Pending() )
260 Dispatch();
261
262 break;
263 }
264
265 // a message came or no more idle processing to do, sit in
266 // Dispatch() waiting for the next message
267 if ( !Dispatch() )
268 {
269 // we got WM_QUIT
270 break;
271 }
272 }
273
274 #if wxUSE_EXCEPTIONS
275 // exit the outer loop as well
276 break;
277 }
278 catch ( ... )
279 {
280 try
281 {
282 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
283 {
284 OnExit();
285 break;
286 }
287 //else: continue running the event loop
288 }
289 catch ( ... )
290 {
291 // OnException() throwed, possibly rethrowing the same
292 // exception again: very good, but we still need OnExit() to
293 // be called
294 OnExit();
295 throw;
296 }
297 }
298 }
299 #endif // wxUSE_EXCEPTIONS
300
301 return m_exitcode;
302 }
303
304 void wxEventLoop::Exit(int rc)
305 {
306 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
307
308 m_exitcode = rc;
309 m_shouldExit = true;
310
311 OnExit();
312
313 // all we have to do to exit from the loop is to (maybe) wake it up so that
314 // it can notice that Exit() had been called
315 //
316 // in particular, we do *not* use PostQuitMessage() here because we're not
317 // sure that WM_QUIT is going to be processed by the correct event loop: it
318 // is possible that another one is started before this one has a chance to
319 // process WM_QUIT
320 ::PostMessage(NULL, WM_NULL, 0, 0);
321 }
322
323 // ----------------------------------------------------------------------------
324 // wxEventLoop message processing dispatching
325 // ----------------------------------------------------------------------------
326
327 bool wxEventLoop::Pending() const
328 {
329 MSG msg;
330 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
331 }
332
333 bool wxEventLoop::Dispatch()
334 {
335 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
336
337 MSG msg;
338 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
339
340 if ( rc == 0 )
341 {
342 // got WM_QUIT
343 return false;
344 }
345
346 if ( rc == -1 )
347 {
348 // should never happen, but let's test for it nevertheless
349 wxLogLastError(wxT("GetMessage"));
350
351 // still break from the loop
352 return false;
353 }
354
355 #if wxUSE_THREADS
356 wxASSERT_MSG( wxThread::IsMain(),
357 wxT("only the main thread can process Windows messages") );
358
359 static bool s_hadGuiLock = true;
360 static wxMsgList s_aSavedMessages;
361
362 // if a secondary thread owning the mutex is doing GUI calls, save all
363 // messages for later processing - we can't process them right now because
364 // it will lead to recursive library calls (and we're not reentrant)
365 if ( !wxGuiOwnedByMainThread() )
366 {
367 s_hadGuiLock = false;
368
369 // leave out WM_COMMAND messages: too dangerous, sometimes
370 // the message will be processed twice
371 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
372 {
373 MSG* pMsg = new MSG(msg);
374 s_aSavedMessages.Append(pMsg);
375 }
376
377 return true;
378 }
379 else
380 {
381 // have we just regained the GUI lock? if so, post all of the saved
382 // messages
383 //
384 // FIXME of course, it's not _exactly_ the same as processing the
385 // messages normally - expect some things to break...
386 if ( !s_hadGuiLock )
387 {
388 s_hadGuiLock = true;
389
390 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
391 while (node)
392 {
393 MSG* pMsg = node->GetData();
394 s_aSavedMessages.Erase(node);
395
396 ProcessMessage(pMsg);
397 delete pMsg;
398
399 node = s_aSavedMessages.GetFirst();
400 }
401 }
402 }
403 #endif // wxUSE_THREADS
404
405 ProcessMessage(&msg);
406
407 return true;
408 }
409