]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
rename wxDECLARE_LOCAL_EVENT to just wxDECLARE_EVENT
[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
JS
37#include "wx/msw/private.h"
38
81df6af3
VZ
39#if wxUSE_GUI
40 #include "wx/tooltip.h"
41 #if wxUSE_THREADS
81df6af3
VZ
42 // define the list of MSG strutures
43 WX_DECLARE_LIST(MSG, wxMsgList);
4300caa7 44
81df6af3 45 #include "wx/listimpl.cpp"
4300caa7 46
81df6af3
VZ
47 WX_DEFINE_LIST(wxMsgList)
48 #endif // wxUSE_THREADS
49#endif //wxUSE_GUI
50
51#if wxUSE_BASE
0cd7d9b2 52
3808e191 53// ============================================================================
81df6af3 54// wxMSWEventLoopBase implementation
3808e191
JS
55// ============================================================================
56
57// ----------------------------------------------------------------------------
3754265e 58// ctor/dtor
3808e191
JS
59// ----------------------------------------------------------------------------
60
81df6af3 61wxMSWEventLoopBase::wxMSWEventLoopBase()
3754265e
VZ
62{
63 m_shouldExit = false;
64 m_exitcode = 0;
65}
66
67// ----------------------------------------------------------------------------
81df6af3 68// wxEventLoop message processing dispatching
3754265e
VZ
69// ----------------------------------------------------------------------------
70
81df6af3 71bool wxMSWEventLoopBase::Pending() const
3808e191 72{
81df6af3
VZ
73 MSG msg;
74 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
75}
76
77bool wxMSWEventLoopBase::GetNextMessage(WXMSG* msg)
78{
79 wxCHECK_MSG( IsRunning(), false, _T("can't get messages if not running") );
80
81 const BOOL rc = ::GetMessage(msg, NULL, 0, 0);
82
83 if ( rc == 0 )
3808e191 84 {
81df6af3
VZ
85 // got WM_QUIT
86 return false;
3808e191 87 }
81df6af3
VZ
88
89 if ( rc == -1 )
90 {
91 // should never happen, but let's test for it nevertheless
92 wxLogLastError(wxT("GetMessage"));
93
94 // still break from the loop
95 return false;
96 }
97
98 return true;
3808e191
JS
99}
100
9af42efd
VZ
101int wxMSWEventLoopBase::GetNextMessageTimeout(WXMSG *msg, unsigned long timeout)
102{
103 // MsgWaitForMultipleObjects() won't notice any input which was already
104 // examined (e.g. using PeekMessage()) but not yet removed from the queue
105 // so we need to remove any immediately messages manually
106 //
107 // NB: using MsgWaitForMultipleObjectsEx() could simplify the code here but
108 // it is not available in very old Windows versions
109 if ( !::PeekMessage(msg, 0, 0, 0, PM_REMOVE) )
110 {
111 // we use this function just in order to not block longer than the
112 // given timeout, so we don't pass any handles to it at all
ac16633f
VZ
113 DWORD rc = ::MsgWaitForMultipleObjects
114 (
115 0, NULL,
116 FALSE,
117 timeout,
118 QS_ALLINPUT
119 );
120
121 switch ( rc )
9af42efd 122 {
ac16633f
VZ
123 default:
124 wxLogDebug("unexpected MsgWaitForMultipleObjects() return "
125 "value %lu", rc);
126 // fall through
127
128 case WAIT_TIMEOUT:
129 return -1;
130
131 case WAIT_OBJECT_0:
132 if ( !::PeekMessage(msg, 0, 0, 0, PM_REMOVE) )
133 {
134 // somehow it may happen that MsgWaitForMultipleObjects()
135 // returns true but there are no messages -- just treat it
136 // the same as timeout then
137 return -1;
138 }
139 break;
9af42efd
VZ
140 }
141 }
142
143 return msg->message != WM_QUIT;
144}
145
146
81df6af3
VZ
147#endif // wxUSE_BASE
148
149#if wxUSE_GUI
150
151// ============================================================================
152// GUI wxEventLoop implementation
153// ============================================================================
154
155wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL;
156
157bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
a7b7500c
VZ
158{
159 while ( win )
160 {
161 if ( win == ms_winCritical )
162 return true;
163
164 win = win->GetParent();
165 }
166
167 return false;
168}
169
81df6af3 170bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg)
3808e191 171{
ac8d0c11 172 HWND hwnd = msg->hwnd;
89c83180 173 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
a7b7500c 174 wxWindow *wnd;
ac8d0c11 175
18f5234f
DS
176 // this might happen if we're in a modeless dialog, or if a wx control has
177 // children which themselves were not created by wx (i.e. wxActiveX control children)
ac8d0c11
VZ
178 if ( !wndThis )
179 {
18f5234f 180 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
ac8d0c11
VZ
181 {
182 hwnd = ::GetParent(hwnd);
89c83180 183
18f5234f
DS
184 // If the control has a wx parent, break and give the parent a chance
185 // to process the window message
186 wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
187 if (wndThis != NULL)
188 break;
ac8d0c11
VZ
189 }
190
18f5234f
DS
191 if ( !wndThis )
192 {
193 // this may happen if the event occurred in a standard modeless dialog (the
194 // only example of which I know of is the find/replace dialog) - then call
195 // IsDialogMessage() to make TAB navigation in it work
196
197 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
198 // them) if we call it for the control itself
199 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
200 }
ac8d0c11 201 }
3808e191 202
a7b7500c
VZ
203 if ( !AllowProcessing(wndThis) )
204 {
205 // not a child of critical window, so we eat the event but take care to
206 // stop an endless stream of WM_PAINTs which would have resulted if we
207 // didn't validate the invalidated part of the window
208 if ( msg->message == WM_PAINT )
209 ::ValidateRect(hwnd, NULL);
210
211 return true;
212 }
213
3808e191
JS
214#if wxUSE_TOOLTIPS
215 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
216 // popup the tooltip bubbles
ac8d0c11 217 if ( msg->message == WM_MOUSEMOVE )
3808e191 218 {
c009bf3e
VZ
219 // we should do it if one of window children has an associated tooltip
220 // (and not just if the window has a tooltip itself)
221 if ( wndThis->HasToolTips() )
222 wxToolTip::RelayEvent((WXMSG *)msg);
3808e191
JS
223 }
224#endif // wxUSE_TOOLTIPS
225
ac8d0c11
VZ
226 // allow the window to prevent certain messages from being
227 // translated/processed (this is currently used by wxTextCtrl to always
228 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
229 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
230 {
3754265e 231 return false;
ac8d0c11
VZ
232 }
233
234 // try translations first: the accelerators override everything
3808e191
JS
235 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
236 {
ac8d0c11 237 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3754265e 238 return true;
ac8d0c11
VZ
239
240 // stop at first top level window, i.e. don't try to process the key
241 // strokes originating in a dialog using the accelerators of the parent
242 // frame - this doesn't make much sense
243 if ( wnd->IsTopLevel() )
244 break;
3808e191
JS
245 }
246
22cfea03
JS
247 // now try the other hooks (kbd navigation is handled here)
248 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
3808e191 249 {
f6388667
VZ
250 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
251 return true;
c8026dea 252
f6388667
VZ
253 // also stop at first top level window here, just as above because
254 // if we don't do this, pressing ESC on a modal dialog shown as child
255 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
256 // be closed, for example
257 if ( wnd->IsTopLevel() )
22cfea03 258 break;
3808e191
JS
259 }
260
ac8d0c11 261 // no special preprocessing for this message, dispatch it normally
3754265e 262 return false;
cea62f9c
JS
263}
264
81df6af3 265void wxGUIEventLoop::ProcessMessage(WXMSG *msg)
3808e191 266{
81df6af3
VZ
267 // give us the chance to preprocess the message first
268 if ( !PreProcessMessage(msg) )
269 {
270 // if it wasn't done, dispatch it to the corresponding window
271 ::TranslateMessage(msg);
272 ::DispatchMessage(msg);
273 }
3808e191
JS
274}
275
81df6af3 276bool wxGUIEventLoop::Dispatch()
3808e191 277{
3808e191 278 MSG msg;
81df6af3 279 if ( !GetNextMessage(&msg) )
3754265e 280 return false;
3808e191
JS
281
282#if wxUSE_THREADS
283 wxASSERT_MSG( wxThread::IsMain(),
284 wxT("only the main thread can process Windows messages") );
285
3754265e 286 static bool s_hadGuiLock = true;
9af08eb8 287 static wxMsgList s_aSavedMessages;
3808e191
JS
288
289 // if a secondary thread owning the mutex is doing GUI calls, save all
290 // messages for later processing - we can't process them right now because
291 // it will lead to recursive library calls (and we're not reentrant)
292 if ( !wxGuiOwnedByMainThread() )
293 {
3754265e 294 s_hadGuiLock = false;
3808e191
JS
295
296 // leave out WM_COMMAND messages: too dangerous, sometimes
297 // the message will be processed twice
6c0e8b4e 298 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 299 {
9af08eb8
VZ
300 MSG* pMsg = new MSG(msg);
301 s_aSavedMessages.Append(pMsg);
3808e191
JS
302 }
303
3754265e 304 return true;
3808e191
JS
305 }
306 else
307 {
308 // have we just regained the GUI lock? if so, post all of the saved
309 // messages
310 //
311 // FIXME of course, it's not _exactly_ the same as processing the
312 // messages normally - expect some things to break...
313 if ( !s_hadGuiLock )
314 {
3754265e 315 s_hadGuiLock = true;
3808e191 316
9af08eb8
VZ
317 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
318 while (node)
3808e191 319 {
9af08eb8
VZ
320 MSG* pMsg = node->GetData();
321 s_aSavedMessages.Erase(node);
322
323 ProcessMessage(pMsg);
324 delete pMsg;
3808e191 325
9af08eb8
VZ
326 node = s_aSavedMessages.GetFirst();
327 }
3808e191
JS
328 }
329 }
330#endif // wxUSE_THREADS
331
3754265e 332 ProcessMessage(&msg);
3808e191 333
3754265e 334 return true;
3808e191
JS
335}
336
9af42efd
VZ
337int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
338{
339 MSG msg;
340 int rc = GetNextMessageTimeout(&msg, timeout);
341 if ( rc != 1 )
342 return rc;
343
344 ProcessMessage(&msg);
345
346 return 1;
347}
348
81df6af3
VZ
349void wxGUIEventLoop::OnNextIteration()
350{
351#if wxUSE_THREADS
352 wxMutexGuiLeaveOrEnter();
353#endif // wxUSE_THREADS
354}
355
356void wxGUIEventLoop::WakeUp()
357{
358 ::PostMessage(NULL, WM_NULL, 0, 0);
359}
360
361#else // !wxUSE_GUI
362
d48b06bd
FM
363
364// ============================================================================
365// wxConsoleEventLoop implementation
366// ============================================================================
367
61478ea0
VZ
368#if wxUSE_CONSOLE_EVENTLOOP
369
81df6af3
VZ
370void wxConsoleEventLoop::WakeUp()
371{
372#if wxUSE_THREADS
373 wxWakeUpMainThread();
374#endif
375}
376
9af42efd 377void wxConsoleEventLoop::ProcessMessage(WXMSG *msg)
81df6af3 378{
9af42efd 379 if ( msg->message == WM_TIMER )
81df6af3 380 {
9af42efd 381 TIMERPROC proc = (TIMERPROC)msg->lParam;
81df6af3 382 if ( proc )
9af42efd 383 (*proc)(NULL, 0, msg->wParam, 0);
81df6af3
VZ
384 }
385 else
386 {
9af42efd 387 ::DispatchMessage(msg);
81df6af3 388 }
9af42efd
VZ
389}
390
391bool wxConsoleEventLoop::Dispatch()
392{
393 MSG msg;
394 if ( !GetNextMessage(&msg) )
395 return false;
396
397 ProcessMessage(&msg);
398
399 return !m_shouldExit;
400}
401
402int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
403{
404 MSG msg;
405 int rc = GetNextMessageTimeout(&msg, timeout);
406 if ( rc != 1 )
407 return rc;
408
409 ProcessMessage(&msg);
81df6af3
VZ
410
411 return !m_shouldExit;
412}
413
61478ea0
VZ
414#endif // wxUSE_CONSOLE_EVENTLOOP
415
81df6af3 416#endif //wxUSE_GUI