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