]> git.saurik.com Git - wxWidgets.git/blob - src/msw/evtloop.cpp
Hack compilation using the latest MinGW release.
[wxWidgets.git] / src / msw / evtloop.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/evtloop.cpp
3 // Purpose: implements wxEventLoop for wxMSW port
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 01.06.01
7 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/evtloop.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/window.h"
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/thread.h"
35 #include "wx/except.h"
36 #include "wx/msw/private.h"
37 #include "wx/scopeguard.h"
38
39 #include "wx/tooltip.h"
40 #if wxUSE_THREADS
41 // define the list of MSG strutures
42 WX_DECLARE_LIST(MSG, wxMsgList);
43
44 #include "wx/listimpl.cpp"
45
46 WX_DEFINE_LIST(wxMsgList)
47 #endif // wxUSE_THREADS
48
49 // ============================================================================
50 // GUI wxEventLoop implementation
51 // ============================================================================
52
53 wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL;
54
55 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
56 {
57 while ( win )
58 {
59 if ( win == ms_winCritical )
60 return true;
61
62 win = win->GetParent();
63 }
64
65 return false;
66 }
67
68 bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg)
69 {
70 HWND hwnd = msg->hwnd;
71 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
72 wxWindow *wnd;
73
74 // this might happen if we're in a modeless dialog, or if a wx control has
75 // children which themselves were not created by wx (i.e. wxActiveX control children)
76 if ( !wndThis )
77 {
78 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
79 {
80 hwnd = ::GetParent(hwnd);
81
82 // If the control has a wx parent, break and give the parent a chance
83 // to process the window message
84 wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
85 if (wndThis != NULL)
86 break;
87 }
88
89 if ( !wndThis )
90 {
91 // this may happen if the event occurred in a standard modeless dialog (the
92 // only example of which I know of is the find/replace dialog) - then call
93 // IsDialogMessage() to make TAB navigation in it work
94
95 // NOTE: IsDialogMessage() just eats all the messages (i.e. returns true for
96 // them) if we call it for the control itself
97 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
98 }
99 }
100
101 if ( !AllowProcessing(wndThis) )
102 {
103 // not a child of critical window, so we eat the event but take care to
104 // stop an endless stream of WM_PAINTs which would have resulted if we
105 // didn't validate the invalidated part of the window
106 if ( msg->message == WM_PAINT )
107 ::ValidateRect(hwnd, NULL);
108
109 return true;
110 }
111
112 #if wxUSE_TOOLTIPS
113 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
114 // popup the tooltip bubbles
115 if ( msg->message == WM_MOUSEMOVE )
116 {
117 // we should do it if one of window children has an associated tooltip
118 // (and not just if the window has a tooltip itself)
119 if ( wndThis->HasToolTips() )
120 wxToolTip::RelayEvent((WXMSG *)msg);
121 }
122 #endif // wxUSE_TOOLTIPS
123
124 // allow the window to prevent certain messages from being
125 // translated/processed (this is currently used by wxTextCtrl to always
126 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
127 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
128 {
129 return false;
130 }
131
132 // try translations first: the accelerators override everything
133 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
134 {
135 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
136 return true;
137
138 // stop at first top level window, i.e. don't try to process the key
139 // strokes originating in a dialog using the accelerators of the parent
140 // frame - this doesn't make much sense
141 if ( wnd->IsTopLevel() )
142 break;
143 }
144
145 // now try the other hooks (kbd navigation is handled here)
146 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
147 {
148 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
149 return true;
150
151 // also stop at first top level window here, just as above because
152 // if we don't do this, pressing ESC on a modal dialog shown as child
153 // of a modal dialog with wxID_CANCEL will cause the parent dialog to
154 // be closed, for example
155 if ( wnd->IsTopLevel() )
156 break;
157 }
158
159 // no special preprocessing for this message, dispatch it normally
160 return false;
161 }
162
163 void wxGUIEventLoop::ProcessMessage(WXMSG *msg)
164 {
165 // give us the chance to preprocess the message first
166 if ( !PreProcessMessage(msg) )
167 {
168 // if it wasn't done, dispatch it to the corresponding window
169 ::TranslateMessage(msg);
170 ::DispatchMessage(msg);
171 }
172 }
173
174 bool wxGUIEventLoop::Dispatch()
175 {
176 MSG msg;
177 if ( !GetNextMessage(&msg) )
178 return false;
179
180 #if wxUSE_THREADS
181 wxASSERT_MSG( wxThread::IsMain(),
182 wxT("only the main thread can process Windows messages") );
183
184 static bool s_hadGuiLock = true;
185 static wxMsgList s_aSavedMessages;
186
187 // if a secondary thread owning the mutex is doing GUI calls, save all
188 // messages for later processing - we can't process them right now because
189 // it will lead to recursive library calls (and we're not reentrant)
190 if ( !wxGuiOwnedByMainThread() )
191 {
192 s_hadGuiLock = false;
193
194 // leave out WM_COMMAND messages: too dangerous, sometimes
195 // the message will be processed twice
196 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
197 {
198 MSG* pMsg = new MSG(msg);
199 s_aSavedMessages.Append(pMsg);
200 }
201
202 return true;
203 }
204 else
205 {
206 // have we just regained the GUI lock? if so, post all of the saved
207 // messages
208 //
209 // FIXME of course, it's not _exactly_ the same as processing the
210 // messages normally - expect some things to break...
211 if ( !s_hadGuiLock )
212 {
213 s_hadGuiLock = true;
214
215 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
216 while (node)
217 {
218 MSG* pMsg = node->GetData();
219 s_aSavedMessages.Erase(node);
220
221 ProcessMessage(pMsg);
222 delete pMsg;
223
224 node = s_aSavedMessages.GetFirst();
225 }
226 }
227 }
228 #endif // wxUSE_THREADS
229
230 ProcessMessage(&msg);
231
232 return true;
233 }
234
235 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
236 {
237 MSG msg;
238 int rc = GetNextMessageTimeout(&msg, timeout);
239 if ( rc != 1 )
240 return rc;
241
242 ProcessMessage(&msg);
243
244 return 1;
245 }
246
247 void wxGUIEventLoop::OnNextIteration()
248 {
249 #if wxUSE_THREADS
250 wxMutexGuiLeaveOrEnter();
251 #endif // wxUSE_THREADS
252 }
253
254 void wxGUIEventLoop::WakeUp()
255 {
256 ::PostMessage(NULL, WM_NULL, 0, 0);
257 }
258
259
260 // ----------------------------------------------------------------------------
261 // Yield to incoming messages
262 // ----------------------------------------------------------------------------
263
264 #include <wx/arrimpl.cpp>
265 WX_DEFINE_OBJARRAY(wxMSGArray);
266
267 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
268 {
269 // set the flag and don't forget to reset it before returning
270 m_isInsideYield = true;
271 m_eventsToProcessInsideYield = eventsToProcess;
272
273 wxON_BLOCK_EXIT_SET(m_isInsideYield, false);
274
275 #if wxUSE_LOG
276 // disable log flushing from here because a call to wxYield() shouldn't
277 // normally result in message boxes popping up &c
278 wxLog::Suspend();
279
280 // ensure the logs will be flashed again when we exit
281 wxON_BLOCK_EXIT0(wxLog::Resume);
282 #endif // wxUSE_LOG
283
284 // we don't want to process WM_QUIT from here - it should be processed in
285 // the main event loop in order to stop it
286 MSG msg;
287 int nPaintsReceived = 0;
288 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
289 msg.message != WM_QUIT )
290 {
291 #if wxUSE_THREADS
292 wxMutexGuiLeaveOrEnter();
293 #endif // wxUSE_THREADS
294
295 if (msg.message == WM_PAINT)
296 {
297 // NOTE: WM_PAINTs are categorized as wxEVT_CATEGORY_UI
298 if ((eventsToProcess & wxEVT_CATEGORY_UI) == 0)
299 {
300 // this msg is not going to be dispatched...
301 // however WM_PAINT is special: until there are damaged
302 // windows, Windows will keep sending it forever!
303 if (nPaintsReceived > 10)
304 {
305 // we got 10 WM_PAINT consecutive messages...
306 // we must have reached the tail of the message queue:
307 // we're now getting _only_ WM_PAINT events and this will
308 // continue forever (since we don't dispatch them
309 // because of the user-specified eventsToProcess mask)...
310 // break out of this loop!
311 break;
312 }
313 else
314 nPaintsReceived++;
315 }
316 //else: we're going to dispatch it below,
317 // so we don't need to take any special action
318 }
319 else
320 {
321 // reset the counter of consecutive WM_PAINT messages received:
322 nPaintsReceived = 0;
323 }
324
325 // choose a wxEventCategory for this Windows message
326 bool processNow;
327 switch (msg.message)
328 {
329 #if !defined(__WXWINCE__)
330 case WM_NCMOUSEMOVE:
331
332 case WM_NCLBUTTONDOWN:
333 case WM_NCLBUTTONUP:
334 case WM_NCLBUTTONDBLCLK:
335 case WM_NCRBUTTONDOWN:
336 case WM_NCRBUTTONUP:
337 case WM_NCRBUTTONDBLCLK:
338 case WM_NCMBUTTONDOWN:
339 case WM_NCMBUTTONUP:
340 case WM_NCMBUTTONDBLCLK:
341 #endif
342
343 case WM_KEYDOWN:
344 case WM_KEYUP:
345 case WM_CHAR:
346 case WM_DEADCHAR:
347 case WM_SYSKEYDOWN:
348 case WM_SYSKEYUP:
349 case WM_SYSCHAR:
350 case WM_SYSDEADCHAR:
351 #ifdef WM_UNICHAR
352 case WM_UNICHAR:
353 #endif
354 case WM_HOTKEY:
355 case WM_IME_STARTCOMPOSITION:
356 case WM_IME_ENDCOMPOSITION:
357 case WM_IME_COMPOSITION:
358 case WM_COMMAND:
359 case WM_SYSCOMMAND:
360
361 case WM_IME_SETCONTEXT:
362 case WM_IME_NOTIFY:
363 case WM_IME_CONTROL:
364 case WM_IME_COMPOSITIONFULL:
365 case WM_IME_SELECT:
366 case WM_IME_CHAR:
367 case WM_IME_KEYDOWN:
368 case WM_IME_KEYUP:
369
370 #if !defined(__WXWINCE__)
371 case WM_MOUSEHOVER:
372 case WM_MOUSELEAVE:
373 #endif
374 #ifdef WM_NCMOUSELEAVE
375 case WM_NCMOUSELEAVE:
376 #endif
377
378 case WM_CUT:
379 case WM_COPY:
380 case WM_PASTE:
381 case WM_CLEAR:
382 case WM_UNDO:
383
384 case WM_MOUSEMOVE:
385 case WM_LBUTTONDOWN:
386 case WM_LBUTTONUP:
387 case WM_LBUTTONDBLCLK:
388 case WM_RBUTTONDOWN:
389 case WM_RBUTTONUP:
390 case WM_RBUTTONDBLCLK:
391 case WM_MBUTTONDOWN:
392 case WM_MBUTTONUP:
393 case WM_MBUTTONDBLCLK:
394 case WM_MOUSEWHEEL:
395 processNow = (eventsToProcess & wxEVT_CATEGORY_USER_INPUT) != 0;
396 break;
397
398 case WM_TIMER:
399 processNow = (eventsToProcess & wxEVT_CATEGORY_TIMER) != 0;
400 break;
401
402 default:
403 if (msg.message < WM_USER)
404 {
405 // 0;WM_USER-1 is the range of message IDs reserved for use
406 // by the system.
407 // there are too many of these types of messages to handle
408 // them in this switch
409 processNow = (eventsToProcess & wxEVT_CATEGORY_UI) != 0;
410 }
411 else
412 {
413 // Process all the unknown messages. We must do it because
414 // failure to process some of them can be fatal, e.g. if we
415 // don't dispatch WM_APP+2 then embedded IE ActiveX
416 // controls don't work any more, see #14027. And there may
417 // be more examples like this, so dispatch all unknown
418 // messages immediately to be safe.
419 processNow = true;
420 }
421 }
422
423 // should we process this event now?
424 if ( processNow )
425 {
426 if ( !wxTheApp->Dispatch() )
427 break;
428 }
429 else
430 {
431 // remove the message and store it
432 ::GetMessage(&msg, NULL, 0, 0);
433 m_arrMSG.Add(msg);
434 }
435 }
436
437 // if there are pending events, we must process them.
438 if (wxTheApp)
439 wxTheApp->ProcessPendingEvents();
440
441 // put back unprocessed events in the queue
442 DWORD id = GetCurrentThreadId();
443 for (size_t i=0; i<m_arrMSG.GetCount(); i++)
444 {
445 PostThreadMessage(id, m_arrMSG[i].message,
446 m_arrMSG[i].wParam, m_arrMSG[i].lParam);
447 }
448
449 m_arrMSG.Clear();
450
451 return true;
452 }