]>
Commit | Line | Data |
---|---|---|
3808e191 JS |
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> | |
65571936 | 9 | // License: wxWindows licence |
3808e191 JS |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
3808e191 JS |
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 | |
6c0e8b4e VZ |
28 | #include "wx/window.h" |
29 | #include "wx/app.h" | |
3808e191 JS |
30 | #endif //WX_PRECOMP |
31 | ||
32 | #include "wx/evtloop.h" | |
4300caa7 | 33 | |
0cd7d9b2 | 34 | #include "wx/tooltip.h" |
4300caa7 VZ |
35 | #include "wx/except.h" |
36 | #include "wx/ptr_scpd.h" | |
3808e191 JS |
37 | |
38 | #include "wx/msw/private.h" | |
39 | ||
0cd7d9b2 | 40 | #if wxUSE_THREADS |
4300caa7 VZ |
41 | #include "wx/thread.h" |
42 | ||
9af08eb8 VZ |
43 | // define the list of MSG strutures |
44 | WX_DECLARE_LIST(MSG, wxMsgList); | |
4300caa7 | 45 | |
9af08eb8 | 46 | #include "wx/listimpl.cpp" |
4300caa7 | 47 | |
259c43f6 | 48 | WX_DEFINE_LIST(wxMsgList) |
4300caa7 | 49 | #endif // wxUSE_THREADS |
0cd7d9b2 | 50 | |
4300caa7 VZ |
51 | // ---------------------------------------------------------------------------- |
52 | // helper class | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
4300caa7 VZ |
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 | ||
3808e191 | 79 | // ============================================================================ |
3754265e | 80 | // wxEventLoop implementation |
3808e191 JS |
81 | // ============================================================================ |
82 | ||
3754265e | 83 | wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL; |
b4626104 | 84 | wxWindowMSW *wxEventLoop::ms_winCritical = NULL; |
3754265e | 85 | |
3808e191 | 86 | // ---------------------------------------------------------------------------- |
3754265e | 87 | // ctor/dtor |
3808e191 JS |
88 | // ---------------------------------------------------------------------------- |
89 | ||
3754265e VZ |
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) | |
3808e191 JS |
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 | ||
b4626104 | 111 | bool wxEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win) |
a7b7500c VZ |
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 | ||
3754265e | 124 | bool wxEventLoop::PreProcessMessage(WXMSG *msg) |
3808e191 | 125 | { |
ac8d0c11 | 126 | HWND hwnd = msg->hwnd; |
89c83180 | 127 | wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd); |
a7b7500c | 128 | wxWindow *wnd; |
ac8d0c11 | 129 | |
18f5234f DS |
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) | |
ac8d0c11 VZ |
132 | if ( !wndThis ) |
133 | { | |
18f5234f | 134 | while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )) |
ac8d0c11 VZ |
135 | { |
136 | hwnd = ::GetParent(hwnd); | |
89c83180 | 137 | |
18f5234f DS |
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; | |
ac8d0c11 VZ |
143 | } |
144 | ||
18f5234f DS |
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 | } | |
ac8d0c11 | 155 | } |
3808e191 | 156 | |
a7b7500c VZ |
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 | ||
3808e191 JS |
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 | |
ac8d0c11 | 171 | if ( msg->message == WM_MOUSEMOVE ) |
3808e191 JS |
172 | { |
173 | wxToolTip *tt = wndThis->GetToolTip(); | |
174 | if ( tt ) | |
175 | { | |
176 | tt->RelayEvent((WXMSG *)msg); | |
177 | } | |
178 | } | |
179 | #endif // wxUSE_TOOLTIPS | |
180 | ||
ac8d0c11 VZ |
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 | { | |
3754265e | 186 | return false; |
ac8d0c11 VZ |
187 | } |
188 | ||
189 | // try translations first: the accelerators override everything | |
3808e191 JS |
190 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) |
191 | { | |
ac8d0c11 | 192 | if ( wnd->MSWTranslateMessage((WXMSG *)msg)) |
3754265e | 193 | return true; |
ac8d0c11 VZ |
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; | |
3808e191 JS |
200 | } |
201 | ||
22cfea03 JS |
202 | // now try the other hooks (kbd navigation is handled here) |
203 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
3808e191 | 204 | { |
22cfea03 JS |
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; | |
3808e191 JS |
216 | } |
217 | ||
ac8d0c11 | 218 | // no special preprocessing for this message, dispatch it normally |
3754265e | 219 | return false; |
cea62f9c JS |
220 | } |
221 | ||
3808e191 JS |
222 | // ---------------------------------------------------------------------------- |
223 | // wxEventLoop running and exiting | |
224 | // ---------------------------------------------------------------------------- | |
225 | ||
3808e191 JS |
226 | bool wxEventLoop::IsRunning() const |
227 | { | |
3754265e | 228 | return ms_activeLoop == this; |
3808e191 JS |
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 | ||
3754265e | 236 | // ProcessIdle() and Dispatch() below may throw so the code here should |
4300caa7 VZ |
237 | // be exception-safe, hence we must use local objects for all actions we |
238 | // should undo | |
239 | wxEventLoopActivator activate(&ms_activeLoop, this); | |
4300caa7 | 240 | |
96d38c7e VS |
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) | |
cea62f9c | 246 | #if wxUSE_EXCEPTIONS |
96d38c7e | 247 | for ( ;; ) |
3808e191 | 248 | { |
96d38c7e | 249 | try |
2a8f35c3 | 250 | { |
96d38c7e | 251 | #endif // wxUSE_EXCEPTIONS |
d284c075 | 252 | |
96d38c7e VS |
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 | } | |
d284c075 | 283 | } |
924b84ab | 284 | |
96d38c7e VS |
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 ( ... ) | |
d284c075 | 301 | { |
96d38c7e VS |
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; | |
d284c075 VZ |
307 | } |
308 | } | |
3808e191 | 309 | } |
d284c075 | 310 | #endif // wxUSE_EXCEPTIONS |
3808e191 | 311 | |
3754265e | 312 | return m_exitcode; |
3808e191 JS |
313 | } |
314 | ||
315 | void wxEventLoop::Exit(int rc) | |
316 | { | |
317 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
318 | ||
3754265e VZ |
319 | m_exitcode = rc; |
320 | m_shouldExit = true; | |
3808e191 | 321 | |
2a8f35c3 VZ |
322 | OnExit(); |
323 | ||
f7550d73 VZ |
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); | |
3808e191 JS |
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 | { | |
3754265e | 346 | wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") ); |
3808e191 JS |
347 | |
348 | MSG msg; | |
349 | BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0); | |
350 | ||
351 | if ( rc == 0 ) | |
352 | { | |
353 | // got WM_QUIT | |
3754265e | 354 | return false; |
3808e191 JS |
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 | |
3754265e | 363 | return false; |
3808e191 JS |
364 | } |
365 | ||
366 | #if wxUSE_THREADS | |
367 | wxASSERT_MSG( wxThread::IsMain(), | |
368 | wxT("only the main thread can process Windows messages") ); | |
369 | ||
3754265e | 370 | static bool s_hadGuiLock = true; |
9af08eb8 | 371 | static wxMsgList s_aSavedMessages; |
3808e191 JS |
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 | { | |
3754265e | 378 | s_hadGuiLock = false; |
3808e191 JS |
379 | |
380 | // leave out WM_COMMAND messages: too dangerous, sometimes | |
381 | // the message will be processed twice | |
6c0e8b4e | 382 | if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND ) |
3808e191 | 383 | { |
9af08eb8 VZ |
384 | MSG* pMsg = new MSG(msg); |
385 | s_aSavedMessages.Append(pMsg); | |
3808e191 JS |
386 | } |
387 | ||
3754265e | 388 | return true; |
3808e191 JS |
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 | { | |
3754265e | 399 | s_hadGuiLock = true; |
3808e191 | 400 | |
9af08eb8 VZ |
401 | wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst(); |
402 | while (node) | |
3808e191 | 403 | { |
9af08eb8 VZ |
404 | MSG* pMsg = node->GetData(); |
405 | s_aSavedMessages.Erase(node); | |
406 | ||
407 | ProcessMessage(pMsg); | |
408 | delete pMsg; | |
3808e191 | 409 | |
9af08eb8 VZ |
410 | node = s_aSavedMessages.GetFirst(); |
411 | } | |
3808e191 JS |
412 | } |
413 | } | |
414 | #endif // wxUSE_THREADS | |
415 | ||
3754265e | 416 | ProcessMessage(&msg); |
3808e191 | 417 | |
3754265e | 418 | return true; |
3808e191 JS |
419 | } |
420 |