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