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