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