]>
Commit | Line | Data |
---|---|---|
3808e191 | 1 | /////////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/msw/evtloop.cpp |
e27f5540 | 3 | // Purpose: implements wxEventLoop for wxMSW port |
3808e191 JS |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 01.06.01 | |
3808e191 | 7 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
526954c5 | 8 | // Licence: wxWindows licence |
3808e191 JS |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
3808e191 JS |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
1e04d2bf PC |
26 | #include "wx/evtloop.h" |
27 | ||
3808e191 | 28 | #ifndef WX_PRECOMP |
e27f5540 | 29 | #include "wx/window.h" |
6c0e8b4e | 30 | #include "wx/app.h" |
1e04d2bf | 31 | #include "wx/log.h" |
3808e191 JS |
32 | #endif //WX_PRECOMP |
33 | ||
204abcd4 | 34 | #include "wx/thread.h" |
4300caa7 | 35 | #include "wx/except.h" |
3808e191 | 36 | #include "wx/msw/private.h" |
dde19c21 | 37 | #include "wx/scopeguard.h" |
3808e191 | 38 | |
e27f5540 VZ |
39 | #include "wx/tooltip.h" |
40 | #if wxUSE_THREADS | |
41 | // define the list of MSG strutures | |
42 | WX_DECLARE_LIST(MSG, wxMsgList); | |
9af42efd | 43 | |
e27f5540 | 44 | #include "wx/listimpl.cpp" |
81df6af3 | 45 | |
e27f5540 VZ |
46 | WX_DEFINE_LIST(wxMsgList) |
47 | #endif // wxUSE_THREADS | |
81df6af3 VZ |
48 | |
49 | // ============================================================================ | |
50 | // GUI wxEventLoop implementation | |
51 | // ============================================================================ | |
52 | ||
53 | wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL; | |
54 | ||
55 | bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win) | |
a7b7500c VZ |
56 | { |
57 | while ( win ) | |
58 | { | |
59 | if ( win == ms_winCritical ) | |
60 | return true; | |
61 | ||
62 | win = win->GetParent(); | |
63 | } | |
64 | ||
65 | return false; | |
66 | } | |
67 | ||
81df6af3 | 68 | bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg) |
3808e191 | 69 | { |
ac8d0c11 | 70 | HWND hwnd = msg->hwnd; |
89c83180 | 71 | wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd); |
a7b7500c | 72 | wxWindow *wnd; |
ac8d0c11 | 73 | |
18f5234f DS |
74 | // this might happen if we're in a modeless dialog, or if a wx control has |
75 | // children which themselves were not created by wx (i.e. wxActiveX control children) | |
ac8d0c11 VZ |
76 | if ( !wndThis ) |
77 | { | |
18f5234f | 78 | while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )) |
ac8d0c11 VZ |
79 | { |
80 | hwnd = ::GetParent(hwnd); | |
89c83180 | 81 | |
18f5234f DS |
82 | // If the control has a wx parent, break and give the parent a chance |
83 | // to process the window message | |
84 | wndThis = wxGetWindowFromHWND((WXHWND)hwnd); | |
85 | if (wndThis != NULL) | |
86 | break; | |
ac8d0c11 VZ |
87 | } |
88 | ||
18f5234f DS |
89 | if ( !wndThis ) |
90 | { | |
91 | // this may happen if the event occurred in a standard modeless dialog (the | |
92 | // only example of which I know of is the find/replace dialog) - then call | |
93 | // IsDialogMessage() to make TAB navigation in it work | |
94 | ||
95 | // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for | |
96 | // them) if we call it for the control itself | |
97 | return hwnd && ::IsDialogMessage(hwnd, msg) != 0; | |
98 | } | |
ac8d0c11 | 99 | } |
3808e191 | 100 | |
a7b7500c VZ |
101 | if ( !AllowProcessing(wndThis) ) |
102 | { | |
103 | // not a child of critical window, so we eat the event but take care to | |
104 | // stop an endless stream of WM_PAINTs which would have resulted if we | |
105 | // didn't validate the invalidated part of the window | |
106 | if ( msg->message == WM_PAINT ) | |
107 | ::ValidateRect(hwnd, NULL); | |
108 | ||
109 | return true; | |
110 | } | |
111 | ||
3808e191 JS |
112 | #if wxUSE_TOOLTIPS |
113 | // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to | |
114 | // popup the tooltip bubbles | |
ac8d0c11 | 115 | if ( msg->message == WM_MOUSEMOVE ) |
3808e191 | 116 | { |
c009bf3e VZ |
117 | // we should do it if one of window children has an associated tooltip |
118 | // (and not just if the window has a tooltip itself) | |
119 | if ( wndThis->HasToolTips() ) | |
120 | wxToolTip::RelayEvent((WXMSG *)msg); | |
3808e191 JS |
121 | } |
122 | #endif // wxUSE_TOOLTIPS | |
123 | ||
ac8d0c11 VZ |
124 | // allow the window to prevent certain messages from being |
125 | // translated/processed (this is currently used by wxTextCtrl to always | |
126 | // grab Ctrl-C/V/X, even if they are also accelerators in some parent) | |
127 | if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) ) | |
128 | { | |
3754265e | 129 | return false; |
ac8d0c11 VZ |
130 | } |
131 | ||
132 | // try translations first: the accelerators override everything | |
3808e191 JS |
133 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) |
134 | { | |
ac8d0c11 | 135 | if ( wnd->MSWTranslateMessage((WXMSG *)msg)) |
3754265e | 136 | return true; |
ac8d0c11 VZ |
137 | |
138 | // stop at first top level window, i.e. don't try to process the key | |
139 | // strokes originating in a dialog using the accelerators of the parent | |
140 | // frame - this doesn't make much sense | |
141 | if ( wnd->IsTopLevel() ) | |
142 | break; | |
3808e191 JS |
143 | } |
144 | ||
22cfea03 JS |
145 | // now try the other hooks (kbd navigation is handled here) |
146 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
3808e191 | 147 | { |
f6388667 VZ |
148 | if ( wnd->MSWProcessMessage((WXMSG *)msg) ) |
149 | return true; | |
c8026dea | 150 | |
f6388667 VZ |
151 | // also stop at first top level window here, just as above because |
152 | // if we don't do this, pressing ESC on a modal dialog shown as child | |
153 | // of a modal dialog with wxID_CANCEL will cause the parent dialog to | |
154 | // be closed, for example | |
155 | if ( wnd->IsTopLevel() ) | |
22cfea03 | 156 | break; |
3808e191 JS |
157 | } |
158 | ||
ac8d0c11 | 159 | // no special preprocessing for this message, dispatch it normally |
3754265e | 160 | return false; |
cea62f9c JS |
161 | } |
162 | ||
81df6af3 | 163 | void wxGUIEventLoop::ProcessMessage(WXMSG *msg) |
3808e191 | 164 | { |
81df6af3 VZ |
165 | // give us the chance to preprocess the message first |
166 | if ( !PreProcessMessage(msg) ) | |
167 | { | |
168 | // if it wasn't done, dispatch it to the corresponding window | |
169 | ::TranslateMessage(msg); | |
170 | ::DispatchMessage(msg); | |
171 | } | |
3808e191 JS |
172 | } |
173 | ||
81df6af3 | 174 | bool wxGUIEventLoop::Dispatch() |
3808e191 | 175 | { |
3808e191 | 176 | MSG msg; |
81df6af3 | 177 | if ( !GetNextMessage(&msg) ) |
3754265e | 178 | return false; |
3808e191 JS |
179 | |
180 | #if wxUSE_THREADS | |
181 | wxASSERT_MSG( wxThread::IsMain(), | |
182 | wxT("only the main thread can process Windows messages") ); | |
183 | ||
3754265e | 184 | static bool s_hadGuiLock = true; |
9af08eb8 | 185 | static wxMsgList s_aSavedMessages; |
3808e191 JS |
186 | |
187 | // if a secondary thread owning the mutex is doing GUI calls, save all | |
188 | // messages for later processing - we can't process them right now because | |
189 | // it will lead to recursive library calls (and we're not reentrant) | |
190 | if ( !wxGuiOwnedByMainThread() ) | |
191 | { | |
3754265e | 192 | s_hadGuiLock = false; |
3808e191 JS |
193 | |
194 | // leave out WM_COMMAND messages: too dangerous, sometimes | |
195 | // the message will be processed twice | |
6c0e8b4e | 196 | if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND ) |
3808e191 | 197 | { |
9af08eb8 VZ |
198 | MSG* pMsg = new MSG(msg); |
199 | s_aSavedMessages.Append(pMsg); | |
3808e191 JS |
200 | } |
201 | ||
3754265e | 202 | return true; |
3808e191 JS |
203 | } |
204 | else | |
205 | { | |
206 | // have we just regained the GUI lock? if so, post all of the saved | |
207 | // messages | |
208 | // | |
209 | // FIXME of course, it's not _exactly_ the same as processing the | |
210 | // messages normally - expect some things to break... | |
211 | if ( !s_hadGuiLock ) | |
212 | { | |
3754265e | 213 | s_hadGuiLock = true; |
3808e191 | 214 | |
9af08eb8 VZ |
215 | wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst(); |
216 | while (node) | |
3808e191 | 217 | { |
9af08eb8 VZ |
218 | MSG* pMsg = node->GetData(); |
219 | s_aSavedMessages.Erase(node); | |
220 | ||
221 | ProcessMessage(pMsg); | |
222 | delete pMsg; | |
3808e191 | 223 | |
9af08eb8 VZ |
224 | node = s_aSavedMessages.GetFirst(); |
225 | } | |
3808e191 JS |
226 | } |
227 | } | |
228 | #endif // wxUSE_THREADS | |
229 | ||
3754265e | 230 | ProcessMessage(&msg); |
3808e191 | 231 | |
3754265e | 232 | return true; |
3808e191 JS |
233 | } |
234 | ||
9af42efd VZ |
235 | int wxGUIEventLoop::DispatchTimeout(unsigned long timeout) |
236 | { | |
237 | MSG msg; | |
238 | int rc = GetNextMessageTimeout(&msg, timeout); | |
239 | if ( rc != 1 ) | |
240 | return rc; | |
241 | ||
242 | ProcessMessage(&msg); | |
243 | ||
244 | return 1; | |
245 | } | |
246 | ||
81df6af3 VZ |
247 | void wxGUIEventLoop::OnNextIteration() |
248 | { | |
249 | #if wxUSE_THREADS | |
250 | wxMutexGuiLeaveOrEnter(); | |
251 | #endif // wxUSE_THREADS | |
252 | } | |
253 | ||
254 | void wxGUIEventLoop::WakeUp() | |
255 | { | |
256 | ::PostMessage(NULL, WM_NULL, 0, 0); | |
257 | } | |
258 | ||
dde19c21 FM |
259 | |
260 | // ---------------------------------------------------------------------------- | |
261 | // Yield to incoming messages | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
264 | #include <wx/arrimpl.cpp> | |
265 | WX_DEFINE_OBJARRAY(wxMSGArray); | |
266 | ||
267 | bool wxGUIEventLoop::YieldFor(long eventsToProcess) | |
268 | { | |
269 | // set the flag and don't forget to reset it before returning | |
270 | m_isInsideYield = true; | |
271 | m_eventsToProcessInsideYield = eventsToProcess; | |
272 | ||
273 | wxON_BLOCK_EXIT_SET(m_isInsideYield, false); | |
274 | ||
275 | #if wxUSE_LOG | |
276 | // disable log flushing from here because a call to wxYield() shouldn't | |
277 | // normally result in message boxes popping up &c | |
278 | wxLog::Suspend(); | |
279 | ||
280 | // ensure the logs will be flashed again when we exit | |
281 | wxON_BLOCK_EXIT0(wxLog::Resume); | |
282 | #endif // wxUSE_LOG | |
283 | ||
284 | // we don't want to process WM_QUIT from here - it should be processed in | |
285 | // the main event loop in order to stop it | |
286 | MSG msg; | |
9d2e29d2 | 287 | int nPaintsReceived = 0; |
dde19c21 FM |
288 | while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) && |
289 | msg.message != WM_QUIT ) | |
290 | { | |
291 | #if wxUSE_THREADS | |
292 | wxMutexGuiLeaveOrEnter(); | |
293 | #endif // wxUSE_THREADS | |
294 | ||
295 | if (msg.message == WM_PAINT) | |
296 | { | |
9d2e29d2 FM |
297 | // NOTE: WM_PAINTs are categorized as wxEVT_CATEGORY_UI |
298 | if ((eventsToProcess & wxEVT_CATEGORY_UI) == 0) | |
299 | { | |
300 | // this msg is not going to be dispatched... | |
03647350 | 301 | // however WM_PAINT is special: until there are damaged |
9d2e29d2 FM |
302 | // windows, Windows will keep sending it forever! |
303 | if (nPaintsReceived > 10) | |
304 | { | |
305 | // we got 10 WM_PAINT consecutive messages... | |
306 | // we must have reached the tail of the message queue: | |
307 | // we're now getting _only_ WM_PAINT events and this will | |
308 | // continue forever (since we don't dispatch them | |
309 | // because of the user-specified eventsToProcess mask)... | |
310 | // break out of this loop! | |
311 | break; | |
312 | } | |
313 | else | |
314 | nPaintsReceived++; | |
315 | } | |
316 | //else: we're going to dispatch it below, | |
317 | // so we don't need to take any special action | |
318 | } | |
319 | else | |
320 | { | |
321 | // reset the counter of consecutive WM_PAINT messages received: | |
322 | nPaintsReceived = 0; | |
dde19c21 FM |
323 | } |
324 | ||
325 | // choose a wxEventCategory for this Windows message | |
4934653e | 326 | bool processNow; |
dde19c21 FM |
327 | switch (msg.message) |
328 | { | |
dd07a4ce | 329 | #if !defined(__WXWINCE__) |
03647350 | 330 | case WM_NCMOUSEMOVE: |
dd07a4ce | 331 | |
03647350 | 332 | case WM_NCLBUTTONDOWN: |
dde19c21 FM |
333 | case WM_NCLBUTTONUP: |
334 | case WM_NCLBUTTONDBLCLK: | |
335 | case WM_NCRBUTTONDOWN: | |
336 | case WM_NCRBUTTONUP: | |
337 | case WM_NCRBUTTONDBLCLK: | |
338 | case WM_NCMBUTTONDOWN: | |
339 | case WM_NCMBUTTONUP: | |
340 | case WM_NCMBUTTONDBLCLK: | |
dd07a4ce | 341 | #endif |
dde19c21 FM |
342 | |
343 | case WM_KEYDOWN: | |
344 | case WM_KEYUP: | |
345 | case WM_CHAR: | |
346 | case WM_DEADCHAR: | |
347 | case WM_SYSKEYDOWN: | |
348 | case WM_SYSKEYUP: | |
349 | case WM_SYSCHAR: | |
350 | case WM_SYSDEADCHAR: | |
351 | #ifdef WM_UNICHAR | |
352 | case WM_UNICHAR: | |
353 | #endif | |
354 | case WM_HOTKEY: | |
355 | case WM_IME_STARTCOMPOSITION: | |
356 | case WM_IME_ENDCOMPOSITION: | |
357 | case WM_IME_COMPOSITION: | |
358 | case WM_COMMAND: | |
359 | case WM_SYSCOMMAND: | |
360 | ||
361 | case WM_IME_SETCONTEXT: | |
362 | case WM_IME_NOTIFY: | |
363 | case WM_IME_CONTROL: | |
364 | case WM_IME_COMPOSITIONFULL: | |
365 | case WM_IME_SELECT: | |
366 | case WM_IME_CHAR: | |
367 | case WM_IME_KEYDOWN: | |
368 | case WM_IME_KEYUP: | |
369 | ||
dd07a4ce | 370 | #if !defined(__WXWINCE__) |
dde19c21 | 371 | case WM_MOUSEHOVER: |
dd07a4ce FM |
372 | case WM_MOUSELEAVE: |
373 | #endif | |
dde19c21 FM |
374 | #ifdef WM_NCMOUSELEAVE |
375 | case WM_NCMOUSELEAVE: | |
376 | #endif | |
dde19c21 FM |
377 | |
378 | case WM_CUT: | |
379 | case WM_COPY: | |
380 | case WM_PASTE: | |
381 | case WM_CLEAR: | |
382 | case WM_UNDO: | |
383 | ||
384 | case WM_MOUSEMOVE: | |
385 | case WM_LBUTTONDOWN: | |
386 | case WM_LBUTTONUP: | |
387 | case WM_LBUTTONDBLCLK: | |
388 | case WM_RBUTTONDOWN: | |
389 | case WM_RBUTTONUP: | |
390 | case WM_RBUTTONDBLCLK: | |
391 | case WM_MBUTTONDOWN: | |
392 | case WM_MBUTTONUP: | |
393 | case WM_MBUTTONDBLCLK: | |
394 | case WM_MOUSEWHEEL: | |
4934653e | 395 | processNow = (eventsToProcess & wxEVT_CATEGORY_USER_INPUT) != 0; |
dde19c21 FM |
396 | break; |
397 | ||
398 | case WM_TIMER: | |
4934653e | 399 | processNow = (eventsToProcess & wxEVT_CATEGORY_TIMER) != 0; |
dde19c21 FM |
400 | break; |
401 | ||
402 | default: | |
403 | if (msg.message < WM_USER) | |
404 | { | |
405 | // 0;WM_USER-1 is the range of message IDs reserved for use | |
406 | // by the system. | |
dde19c21 FM |
407 | // there are too many of these types of messages to handle |
408 | // them in this switch | |
4934653e | 409 | processNow = (eventsToProcess & wxEVT_CATEGORY_UI) != 0; |
dde19c21 FM |
410 | } |
411 | else | |
4934653e VZ |
412 | { |
413 | // Process all the unknown messages. We must do it because | |
414 | // failure to process some of them can be fatal, e.g. if we | |
415 | // don't dispatch WM_APP+2 then embedded IE ActiveX | |
416 | // controls don't work any more, see #14027. And there may | |
417 | // be more examples like this, so dispatch all unknown | |
418 | // messages immediately to be safe. | |
419 | processNow = true; | |
420 | } | |
dde19c21 FM |
421 | } |
422 | ||
423 | // should we process this event now? | |
4934653e | 424 | if ( processNow ) |
dde19c21 FM |
425 | { |
426 | if ( !wxTheApp->Dispatch() ) | |
427 | break; | |
428 | } | |
429 | else | |
430 | { | |
431 | // remove the message and store it | |
432 | ::GetMessage(&msg, NULL, 0, 0); | |
433 | m_arrMSG.Add(msg); | |
434 | } | |
435 | } | |
436 | ||
437 | // if there are pending events, we must process them. | |
834fcdd9 FM |
438 | if (wxTheApp) |
439 | wxTheApp->ProcessPendingEvents(); | |
dde19c21 FM |
440 | |
441 | // put back unprocessed events in the queue | |
442 | DWORD id = GetCurrentThreadId(); | |
443 | for (size_t i=0; i<m_arrMSG.GetCount(); i++) | |
444 | { | |
445 | PostThreadMessage(id, m_arrMSG[i].message, | |
446 | m_arrMSG[i].wParam, m_arrMSG[i].lParam); | |
447 | } | |
448 | ||
449 | m_arrMSG.Clear(); | |
450 | ||
451 | return true; | |
452 | } |