]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
add accessors for sockaddr to wxSockAddress (closes #10511)
[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
1e04d2bf
PC
27#include "wx/evtloop.h"
28
3808e191 29#ifndef WX_PRECOMP
81df6af3
VZ
30 #if wxUSE_GUI
31 #include "wx/window.h"
32 #endif
6c0e8b4e 33 #include "wx/app.h"
1e04d2bf 34 #include "wx/log.h"
3808e191
JS
35#endif //WX_PRECOMP
36
204abcd4 37#include "wx/thread.h"
4300caa7 38#include "wx/except.h"
3808e191 39#include "wx/msw/private.h"
dde19c21 40#include "wx/scopeguard.h"
3808e191 41
81df6af3
VZ
42#if wxUSE_GUI
43 #include "wx/tooltip.h"
44 #if wxUSE_THREADS
81df6af3
VZ
45 // define the list of MSG strutures
46 WX_DECLARE_LIST(MSG, wxMsgList);
4300caa7 47
81df6af3 48 #include "wx/listimpl.cpp"
4300caa7 49
81df6af3
VZ
50 WX_DEFINE_LIST(wxMsgList)
51 #endif // wxUSE_THREADS
52#endif //wxUSE_GUI
53
54#if wxUSE_BASE
0cd7d9b2 55
3808e191 56// ============================================================================
81df6af3 57// wxMSWEventLoopBase implementation
3808e191
JS
58// ============================================================================
59
60// ----------------------------------------------------------------------------
3754265e 61// ctor/dtor
3808e191
JS
62// ----------------------------------------------------------------------------
63
81df6af3 64wxMSWEventLoopBase::wxMSWEventLoopBase()
3754265e
VZ
65{
66 m_shouldExit = false;
67 m_exitcode = 0;
68}
69
70// ----------------------------------------------------------------------------
81df6af3 71// wxEventLoop message processing dispatching
3754265e
VZ
72// ----------------------------------------------------------------------------
73
81df6af3 74bool wxMSWEventLoopBase::Pending() const
3808e191 75{
81df6af3
VZ
76 MSG msg;
77 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
78}
79
80bool 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 )
3808e191 87 {
81df6af3
VZ
88 // got WM_QUIT
89 return false;
3808e191 90 }
81df6af3
VZ
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;
3808e191
JS
102}
103
9af42efd
VZ
104int 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
ac16633f
VZ
116 DWORD rc = ::MsgWaitForMultipleObjects
117 (
118 0, NULL,
119 FALSE,
120 timeout,
121 QS_ALLINPUT
122 );
123
124 switch ( rc )
9af42efd 125 {
ac16633f
VZ
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;
9af42efd
VZ
143 }
144 }
145
146 return msg->message != WM_QUIT;
147}
148
149
81df6af3
VZ
150#endif // wxUSE_BASE
151
152#if wxUSE_GUI
153
154// ============================================================================
155// GUI wxEventLoop implementation
156// ============================================================================
157
158wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL;
159
160bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
a7b7500c
VZ
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
81df6af3 173bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg)
3808e191 174{
ac8d0c11 175 HWND hwnd = msg->hwnd;
89c83180 176 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
a7b7500c 177 wxWindow *wnd;
ac8d0c11 178
18f5234f
DS
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)
ac8d0c11
VZ
181 if ( !wndThis )
182 {
18f5234f 183 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
ac8d0c11
VZ
184 {
185 hwnd = ::GetParent(hwnd);
89c83180 186
18f5234f
DS
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;
ac8d0c11
VZ
192 }
193
18f5234f
DS
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 }
ac8d0c11 204 }
3808e191 205
a7b7500c
VZ
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
3808e191
JS
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
ac8d0c11 220 if ( msg->message == WM_MOUSEMOVE )
3808e191 221 {
c009bf3e
VZ
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);
3808e191
JS
226 }
227#endif // wxUSE_TOOLTIPS
228
ac8d0c11
VZ
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 {
3754265e 234 return false;
ac8d0c11
VZ
235 }
236
237 // try translations first: the accelerators override everything
3808e191
JS
238 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
239 {
ac8d0c11 240 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3754265e 241 return true;
ac8d0c11
VZ
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;
3808e191
JS
248 }
249
22cfea03
JS
250 // now try the other hooks (kbd navigation is handled here)
251 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
3808e191 252 {
f6388667
VZ
253 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
254 return true;
c8026dea 255
f6388667
VZ
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() )
22cfea03 261 break;
3808e191
JS
262 }
263
ac8d0c11 264 // no special preprocessing for this message, dispatch it normally
3754265e 265 return false;
cea62f9c
JS
266}
267
81df6af3 268void wxGUIEventLoop::ProcessMessage(WXMSG *msg)
3808e191 269{
81df6af3
VZ
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 }
3808e191
JS
277}
278
81df6af3 279bool wxGUIEventLoop::Dispatch()
3808e191 280{
3808e191 281 MSG msg;
81df6af3 282 if ( !GetNextMessage(&msg) )
3754265e 283 return false;
3808e191
JS
284
285#if wxUSE_THREADS
286 wxASSERT_MSG( wxThread::IsMain(),
287 wxT("only the main thread can process Windows messages") );
288
3754265e 289 static bool s_hadGuiLock = true;
9af08eb8 290 static wxMsgList s_aSavedMessages;
3808e191
JS
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 {
3754265e 297 s_hadGuiLock = false;
3808e191
JS
298
299 // leave out WM_COMMAND messages: too dangerous, sometimes
300 // the message will be processed twice
6c0e8b4e 301 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 302 {
9af08eb8
VZ
303 MSG* pMsg = new MSG(msg);
304 s_aSavedMessages.Append(pMsg);
3808e191
JS
305 }
306
3754265e 307 return true;
3808e191
JS
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 {
3754265e 318 s_hadGuiLock = true;
3808e191 319
9af08eb8
VZ
320 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
321 while (node)
3808e191 322 {
9af08eb8
VZ
323 MSG* pMsg = node->GetData();
324 s_aSavedMessages.Erase(node);
325
326 ProcessMessage(pMsg);
327 delete pMsg;
3808e191 328
9af08eb8
VZ
329 node = s_aSavedMessages.GetFirst();
330 }
3808e191
JS
331 }
332 }
333#endif // wxUSE_THREADS
334
3754265e 335 ProcessMessage(&msg);
3808e191 336
3754265e 337 return true;
3808e191
JS
338}
339
9af42efd
VZ
340int 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
81df6af3
VZ
352void wxGUIEventLoop::OnNextIteration()
353{
354#if wxUSE_THREADS
355 wxMutexGuiLeaveOrEnter();
356#endif // wxUSE_THREADS
357}
358
359void wxGUIEventLoop::WakeUp()
360{
361 ::PostMessage(NULL, WM_NULL, 0, 0);
362}
363
dde19c21
FM
364
365// ----------------------------------------------------------------------------
366// Yield to incoming messages
367// ----------------------------------------------------------------------------
368
369#include <wx/arrimpl.cpp>
370WX_DEFINE_OBJARRAY(wxMSGArray);
371
372bool 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
81df6af3
VZ
522#else // !wxUSE_GUI
523
d48b06bd
FM
524
525// ============================================================================
526// wxConsoleEventLoop implementation
527// ============================================================================
528
61478ea0
VZ
529#if wxUSE_CONSOLE_EVENTLOOP
530
81df6af3
VZ
531void wxConsoleEventLoop::WakeUp()
532{
533#if wxUSE_THREADS
534 wxWakeUpMainThread();
535#endif
536}
537
9af42efd 538void wxConsoleEventLoop::ProcessMessage(WXMSG *msg)
81df6af3 539{
9af42efd 540 if ( msg->message == WM_TIMER )
81df6af3 541 {
9af42efd 542 TIMERPROC proc = (TIMERPROC)msg->lParam;
81df6af3 543 if ( proc )
9af42efd 544 (*proc)(NULL, 0, msg->wParam, 0);
81df6af3
VZ
545 }
546 else
547 {
9af42efd 548 ::DispatchMessage(msg);
81df6af3 549 }
9af42efd
VZ
550}
551
552bool wxConsoleEventLoop::Dispatch()
553{
554 MSG msg;
555 if ( !GetNextMessage(&msg) )
556 return false;
557
558 ProcessMessage(&msg);
559
560 return !m_shouldExit;
561}
562
563int 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);
81df6af3
VZ
571
572 return !m_shouldExit;
573}
574
61478ea0
VZ
575#endif // wxUSE_CONSOLE_EVENTLOOP
576
81df6af3 577#endif //wxUSE_GUI