]> git.saurik.com Git - wxWidgets.git/blob - src/msw/evtloop.cpp
fixed test failures under wxMSW when using our wxVsnprintf implementation (remains...
[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 #include "wx/evtloop.h"
28
29 #ifndef WX_PRECOMP
30 #if wxUSE_GUI
31 #include "wx/window.h"
32 #endif
33 #include "wx/app.h"
34 #include "wx/log.h"
35 #endif //WX_PRECOMP
36
37 #include "wx/thread.h"
38 #include "wx/except.h"
39 #include "wx/msw/private.h"
40 #include "wx/scopeguard.h"
41
42 #if wxUSE_GUI
43 #include "wx/tooltip.h"
44 #if wxUSE_THREADS
45 // define the list of MSG strutures
46 WX_DECLARE_LIST(MSG, wxMsgList);
47
48 #include "wx/listimpl.cpp"
49
50 WX_DEFINE_LIST(wxMsgList)
51 #endif // wxUSE_THREADS
52 #endif //wxUSE_GUI
53
54 #if wxUSE_BASE
55
56 // ============================================================================
57 // wxMSWEventLoopBase implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // ctor/dtor
62 // ----------------------------------------------------------------------------
63
64 wxMSWEventLoopBase::wxMSWEventLoopBase()
65 {
66 m_shouldExit = false;
67 m_exitcode = 0;
68 }
69
70 // ----------------------------------------------------------------------------
71 // wxEventLoop message processing dispatching
72 // ----------------------------------------------------------------------------
73
74 bool wxMSWEventLoopBase::Pending() const
75 {
76 MSG msg;
77 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
78 }
79
80 bool wxMSWEventLoopBase::GetNextMessage(WXMSG* msg)
81 {
82 wxCHECK_MSG( IsRunning(), false, _T("can't get messages if not running") );
83
84 const BOOL rc = ::GetMessage(msg, NULL, 0, 0);
85
86 if ( rc == 0 )
87 {
88 // got WM_QUIT
89 return false;
90 }
91
92 if ( rc == -1 )
93 {
94 // should never happen, but let's test for it nevertheless
95 wxLogLastError(wxT("GetMessage"));
96
97 // still break from the loop
98 return false;
99 }
100
101 return true;
102 }
103
104 int wxMSWEventLoopBase::GetNextMessageTimeout(WXMSG *msg, unsigned long timeout)
105 {
106 // MsgWaitForMultipleObjects() won't notice any input which was already
107 // examined (e.g. using PeekMessage()) but not yet removed from the queue
108 // so we need to remove any immediately messages manually
109 //
110 // NB: using MsgWaitForMultipleObjectsEx() could simplify the code here but
111 // it is not available in very old Windows versions
112 if ( !::PeekMessage(msg, 0, 0, 0, PM_REMOVE) )
113 {
114 // we use this function just in order to not block longer than the
115 // given timeout, so we don't pass any handles to it at all
116 DWORD rc = ::MsgWaitForMultipleObjects
117 (
118 0, NULL,
119 FALSE,
120 timeout,
121 QS_ALLINPUT
122 );
123
124 switch ( rc )
125 {
126 default:
127 wxLogDebug("unexpected MsgWaitForMultipleObjects() return "
128 "value %lu", rc);
129 // fall through
130
131 case WAIT_TIMEOUT:
132 return -1;
133
134 case WAIT_OBJECT_0:
135 if ( !::PeekMessage(msg, 0, 0, 0, PM_REMOVE) )
136 {
137 // somehow it may happen that MsgWaitForMultipleObjects()
138 // returns true but there are no messages -- just treat it
139 // the same as timeout then
140 return -1;
141 }
142 break;
143 }
144 }
145
146 return msg->message != WM_QUIT;
147 }
148
149
150 #endif // wxUSE_BASE
151
152 #if wxUSE_GUI
153
154 // ============================================================================
155 // GUI wxEventLoop implementation
156 // ============================================================================
157
158 wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL;
159
160 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
161 {
162 while ( win )
163 {
164 if ( win == ms_winCritical )
165 return true;
166
167 win = win->GetParent();
168 }
169
170 return false;
171 }
172
173 bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg)
174 {
175 HWND hwnd = msg->hwnd;
176 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
177 wxWindow *wnd;
178
179 // this might happen if we're in a modeless dialog, or if a wx control has
180 // children which themselves were not created by wx (i.e. wxActiveX control children)
181 if ( !wndThis )
182 {
183 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
184 {
185 hwnd = ::GetParent(hwnd);
186
187 // If the control has a wx parent, break and give the parent a chance
188 // to process the window message
189 wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
190 if (wndThis != NULL)
191 break;
192 }
193
194 if ( !wndThis )
195 {
196 // this may happen if the event occurred in a standard modeless dialog (the
197 // only example of which I know of is the find/replace dialog) - then call
198 // IsDialogMessage() to make TAB navigation in it work
199
200 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
201 // them) if we call it for the control itself
202 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
203 }
204 }
205
206 if ( !AllowProcessing(wndThis) )
207 {
208 // not a child of critical window, so we eat the event but take care to
209 // stop an endless stream of WM_PAINTs which would have resulted if we
210 // didn't validate the invalidated part of the window
211 if ( msg->message == WM_PAINT )
212 ::ValidateRect(hwnd, NULL);
213
214 return true;
215 }
216
217 #if wxUSE_TOOLTIPS
218 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
219 // popup the tooltip bubbles
220 if ( msg->message == WM_MOUSEMOVE )
221 {
222 // we should do it if one of window children has an associated tooltip
223 // (and not just if the window has a tooltip itself)
224 if ( wndThis->HasToolTips() )
225 wxToolTip::RelayEvent((WXMSG *)msg);
226 }
227 #endif // wxUSE_TOOLTIPS
228
229 // allow the window to prevent certain messages from being
230 // translated/processed (this is currently used by wxTextCtrl to always
231 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
232 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
233 {
234 return false;
235 }
236
237 // try translations first: the accelerators override everything
238 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
239 {
240 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
241 return true;
242
243 // stop at first top level window, i.e. don't try to process the key
244 // strokes originating in a dialog using the accelerators of the parent
245 // frame - this doesn't make much sense
246 if ( wnd->IsTopLevel() )
247 break;
248 }
249
250 // now try the other hooks (kbd navigation is handled here)
251 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
252 {
253 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
254 return true;
255
256 // also stop at first top level window here, just as above because
257 // if we don't do this, pressing ESC on a modal dialog shown as child
258 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
259 // be closed, for example
260 if ( wnd->IsTopLevel() )
261 break;
262 }
263
264 // no special preprocessing for this message, dispatch it normally
265 return false;
266 }
267
268 void wxGUIEventLoop::ProcessMessage(WXMSG *msg)
269 {
270 // give us the chance to preprocess the message first
271 if ( !PreProcessMessage(msg) )
272 {
273 // if it wasn't done, dispatch it to the corresponding window
274 ::TranslateMessage(msg);
275 ::DispatchMessage(msg);
276 }
277 }
278
279 bool wxGUIEventLoop::Dispatch()
280 {
281 MSG msg;
282 if ( !GetNextMessage(&msg) )
283 return false;
284
285 #if wxUSE_THREADS
286 wxASSERT_MSG( wxThread::IsMain(),
287 wxT("only the main thread can process Windows messages") );
288
289 static bool s_hadGuiLock = true;
290 static wxMsgList s_aSavedMessages;
291
292 // if a secondary thread owning the mutex is doing GUI calls, save all
293 // messages for later processing - we can't process them right now because
294 // it will lead to recursive library calls (and we're not reentrant)
295 if ( !wxGuiOwnedByMainThread() )
296 {
297 s_hadGuiLock = false;
298
299 // leave out WM_COMMAND messages: too dangerous, sometimes
300 // the message will be processed twice
301 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
302 {
303 MSG* pMsg = new MSG(msg);
304 s_aSavedMessages.Append(pMsg);
305 }
306
307 return true;
308 }
309 else
310 {
311 // have we just regained the GUI lock? if so, post all of the saved
312 // messages
313 //
314 // FIXME of course, it's not _exactly_ the same as processing the
315 // messages normally - expect some things to break...
316 if ( !s_hadGuiLock )
317 {
318 s_hadGuiLock = true;
319
320 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
321 while (node)
322 {
323 MSG* pMsg = node->GetData();
324 s_aSavedMessages.Erase(node);
325
326 ProcessMessage(pMsg);
327 delete pMsg;
328
329 node = s_aSavedMessages.GetFirst();
330 }
331 }
332 }
333 #endif // wxUSE_THREADS
334
335 ProcessMessage(&msg);
336
337 return true;
338 }
339
340 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
341 {
342 MSG msg;
343 int rc = GetNextMessageTimeout(&msg, timeout);
344 if ( rc != 1 )
345 return rc;
346
347 ProcessMessage(&msg);
348
349 return 1;
350 }
351
352 void wxGUIEventLoop::OnNextIteration()
353 {
354 #if wxUSE_THREADS
355 wxMutexGuiLeaveOrEnter();
356 #endif // wxUSE_THREADS
357 }
358
359 void wxGUIEventLoop::WakeUp()
360 {
361 ::PostMessage(NULL, WM_NULL, 0, 0);
362 }
363
364
365 // ----------------------------------------------------------------------------
366 // Yield to incoming messages
367 // ----------------------------------------------------------------------------
368
369 #include <wx/arrimpl.cpp>
370 WX_DEFINE_OBJARRAY(wxMSGArray);
371
372 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
373 {
374 // set the flag and don't forget to reset it before returning
375 m_isInsideYield = true;
376 m_eventsToProcessInsideYield = eventsToProcess;
377
378 wxON_BLOCK_EXIT_SET(m_isInsideYield, false);
379
380 #if wxUSE_LOG
381 // disable log flushing from here because a call to wxYield() shouldn't
382 // normally result in message boxes popping up &c
383 wxLog::Suspend();
384
385 // ensure the logs will be flashed again when we exit
386 wxON_BLOCK_EXIT0(wxLog::Resume);
387 #endif // wxUSE_LOG
388
389 // we don't want to process WM_QUIT from here - it should be processed in
390 // the main event loop in order to stop it
391 MSG msg;
392 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
393 msg.message != WM_QUIT )
394 {
395 #if wxUSE_THREADS
396 wxMutexGuiLeaveOrEnter();
397 #endif // wxUSE_THREADS
398
399 if (msg.message == WM_PAINT)
400 {
401 // WM_PAINT messages are the last ones of the queue...
402 break;
403 }
404
405 // choose a wxEventCategory for this Windows message
406 wxEventCategory cat;
407 switch (msg.message)
408 {
409 case WM_NCMOUSEMOVE:
410 case WM_NCLBUTTONDOWN:
411 case WM_NCLBUTTONUP:
412 case WM_NCLBUTTONDBLCLK:
413 case WM_NCRBUTTONDOWN:
414 case WM_NCRBUTTONUP:
415 case WM_NCRBUTTONDBLCLK:
416 case WM_NCMBUTTONDOWN:
417 case WM_NCMBUTTONUP:
418 case WM_NCMBUTTONDBLCLK:
419
420 case WM_KEYDOWN:
421 case WM_KEYUP:
422 case WM_CHAR:
423 case WM_DEADCHAR:
424 case WM_SYSKEYDOWN:
425 case WM_SYSKEYUP:
426 case WM_SYSCHAR:
427 case WM_SYSDEADCHAR:
428 #ifdef WM_UNICHAR
429 case WM_UNICHAR:
430 #endif
431 case WM_HOTKEY:
432 case WM_IME_STARTCOMPOSITION:
433 case WM_IME_ENDCOMPOSITION:
434 case WM_IME_COMPOSITION:
435 case WM_COMMAND:
436 case WM_SYSCOMMAND:
437
438 case WM_IME_SETCONTEXT:
439 case WM_IME_NOTIFY:
440 case WM_IME_CONTROL:
441 case WM_IME_COMPOSITIONFULL:
442 case WM_IME_SELECT:
443 case WM_IME_CHAR:
444 case WM_IME_KEYDOWN:
445 case WM_IME_KEYUP:
446
447 case WM_MOUSEHOVER:
448 #ifdef WM_NCMOUSELEAVE
449 case WM_NCMOUSELEAVE:
450 #endif
451 case WM_MOUSELEAVE:
452
453 case WM_CUT:
454 case WM_COPY:
455 case WM_PASTE:
456 case WM_CLEAR:
457 case WM_UNDO:
458
459 case WM_MOUSEMOVE:
460 case WM_LBUTTONDOWN:
461 case WM_LBUTTONUP:
462 case WM_LBUTTONDBLCLK:
463 case WM_RBUTTONDOWN:
464 case WM_RBUTTONUP:
465 case WM_RBUTTONDBLCLK:
466 case WM_MBUTTONDOWN:
467 case WM_MBUTTONUP:
468 case WM_MBUTTONDBLCLK:
469 case WM_MOUSEWHEEL:
470 cat = wxEVT_CATEGORY_USER_INPUT;
471 break;
472
473 case WM_TIMER:
474 cat = wxEVT_CATEGORY_TIMER;
475 break;
476
477 default:
478 if (msg.message < WM_USER)
479 {
480 // 0;WM_USER-1 is the range of message IDs reserved for use
481 // by the system.
482
483 // there are too many of these types of messages to handle
484 // them in this switch
485 cat = wxEVT_CATEGORY_UI;
486 }
487 else
488 cat = wxEVT_CATEGORY_UNKNOWN;
489 }
490
491 // should we process this event now?
492 if (cat & eventsToProcess)
493 {
494 if ( !wxTheApp->Dispatch() )
495 break;
496 }
497 else
498 {
499 // remove the message and store it
500 ::GetMessage(&msg, NULL, 0, 0);
501 m_arrMSG.Add(msg);
502 }
503 }
504
505 // if there are pending events, we must process them.
506 ProcessPendingEvents();
507
508 // put back unprocessed events in the queue
509 DWORD id = GetCurrentThreadId();
510 for (size_t i=0; i<m_arrMSG.GetCount(); i++)
511 {
512 PostThreadMessage(id, m_arrMSG[i].message,
513 m_arrMSG[i].wParam, m_arrMSG[i].lParam);
514 }
515
516 m_arrMSG.Clear();
517
518 return true;
519 }
520
521
522 #else // !wxUSE_GUI
523
524
525 // ============================================================================
526 // wxConsoleEventLoop implementation
527 // ============================================================================
528
529 #if wxUSE_CONSOLE_EVENTLOOP
530
531 void wxConsoleEventLoop::WakeUp()
532 {
533 #if wxUSE_THREADS
534 wxWakeUpMainThread();
535 #endif
536 }
537
538 void wxConsoleEventLoop::ProcessMessage(WXMSG *msg)
539 {
540 if ( msg->message == WM_TIMER )
541 {
542 TIMERPROC proc = (TIMERPROC)msg->lParam;
543 if ( proc )
544 (*proc)(NULL, 0, msg->wParam, 0);
545 }
546 else
547 {
548 ::DispatchMessage(msg);
549 }
550 }
551
552 bool wxConsoleEventLoop::Dispatch()
553 {
554 MSG msg;
555 if ( !GetNextMessage(&msg) )
556 return false;
557
558 ProcessMessage(&msg);
559
560 return !m_shouldExit;
561 }
562
563 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
564 {
565 MSG msg;
566 int rc = GetNextMessageTimeout(&msg, timeout);
567 if ( rc != 1 )
568 return rc;
569
570 ProcessMessage(&msg);
571
572 return !m_shouldExit;
573 }
574
575 #endif // wxUSE_CONSOLE_EVENTLOOP
576
577 #endif //wxUSE_GUI