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