]> git.saurik.com Git - wxWidgets.git/blob - src/msw/evtloop.cpp
78d4ee212e3f026fa314a7d87f72a2a6439d01a3
[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/msw/private.h"
38
39 #if wxUSE_GUI
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 #endif //wxUSE_GUI
50
51 #if wxUSE_BASE
52
53 // ============================================================================
54 // wxMSWEventLoopBase implementation
55 // ============================================================================
56
57 // ----------------------------------------------------------------------------
58 // ctor/dtor
59 // ----------------------------------------------------------------------------
60
61 wxMSWEventLoopBase::wxMSWEventLoopBase()
62 {
63 m_shouldExit = false;
64 m_exitcode = 0;
65 }
66
67 // ----------------------------------------------------------------------------
68 // wxEventLoop message processing dispatching
69 // ----------------------------------------------------------------------------
70
71 bool wxMSWEventLoopBase::Pending() const
72 {
73 MSG msg;
74 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
75 }
76
77 bool 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 )
84 {
85 // got WM_QUIT
86 return false;
87 }
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;
99 }
100
101 int 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
113 DWORD rc = ::MsgWaitForMultipleObjects
114 (
115 0, NULL,
116 FALSE,
117 timeout,
118 QS_ALLINPUT
119 );
120
121 switch ( rc )
122 {
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;
140 }
141 }
142
143 return msg->message != WM_QUIT;
144 }
145
146
147 #endif // wxUSE_BASE
148
149 #if wxUSE_GUI
150
151 // ============================================================================
152 // GUI wxEventLoop implementation
153 // ============================================================================
154
155 wxWindowMSW *wxGUIEventLoop::ms_winCritical = NULL;
156
157 bool wxGUIEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
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
170 bool wxGUIEventLoop::PreProcessMessage(WXMSG *msg)
171 {
172 HWND hwnd = msg->hwnd;
173 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
174 wxWindow *wnd;
175
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)
178 if ( !wndThis )
179 {
180 while ( hwnd && (::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ))
181 {
182 hwnd = ::GetParent(hwnd);
183
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;
189 }
190
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 }
201 }
202
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
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
217 if ( msg->message == WM_MOUSEMOVE )
218 {
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);
223 }
224 #endif // wxUSE_TOOLTIPS
225
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 {
231 return false;
232 }
233
234 // try translations first: the accelerators override everything
235 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
236 {
237 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
238 return true;
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;
245 }
246
247 // now try the other hooks (kbd navigation is handled here)
248 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
249 {
250 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
251 return true;
252
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() )
258 break;
259 }
260
261 // no special preprocessing for this message, dispatch it normally
262 return false;
263 }
264
265 void wxGUIEventLoop::ProcessMessage(WXMSG *msg)
266 {
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 }
274 }
275
276 bool wxGUIEventLoop::Dispatch()
277 {
278 MSG msg;
279 if ( !GetNextMessage(&msg) )
280 return false;
281
282 #if wxUSE_THREADS
283 wxASSERT_MSG( wxThread::IsMain(),
284 wxT("only the main thread can process Windows messages") );
285
286 static bool s_hadGuiLock = true;
287 static wxMsgList s_aSavedMessages;
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 {
294 s_hadGuiLock = false;
295
296 // leave out WM_COMMAND messages: too dangerous, sometimes
297 // the message will be processed twice
298 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
299 {
300 MSG* pMsg = new MSG(msg);
301 s_aSavedMessages.Append(pMsg);
302 }
303
304 return true;
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 {
315 s_hadGuiLock = true;
316
317 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
318 while (node)
319 {
320 MSG* pMsg = node->GetData();
321 s_aSavedMessages.Erase(node);
322
323 ProcessMessage(pMsg);
324 delete pMsg;
325
326 node = s_aSavedMessages.GetFirst();
327 }
328 }
329 }
330 #endif // wxUSE_THREADS
331
332 ProcessMessage(&msg);
333
334 return true;
335 }
336
337 int 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
349 void wxGUIEventLoop::OnNextIteration()
350 {
351 #if wxUSE_THREADS
352 wxMutexGuiLeaveOrEnter();
353 #endif // wxUSE_THREADS
354 }
355
356 void wxGUIEventLoop::WakeUp()
357 {
358 ::PostMessage(NULL, WM_NULL, 0, 0);
359 }
360
361 #else // !wxUSE_GUI
362
363 #if wxUSE_CONSOLE_EVENTLOOP
364
365 void wxConsoleEventLoop::WakeUp()
366 {
367 #if wxUSE_THREADS
368 wxWakeUpMainThread();
369 #endif
370 }
371
372 void wxConsoleEventLoop::ProcessMessage(WXMSG *msg)
373 {
374 if ( msg->message == WM_TIMER )
375 {
376 TIMERPROC proc = (TIMERPROC)msg->lParam;
377 if ( proc )
378 (*proc)(NULL, 0, msg->wParam, 0);
379 }
380 else
381 {
382 ::DispatchMessage(msg);
383 }
384 }
385
386 bool wxConsoleEventLoop::Dispatch()
387 {
388 MSG msg;
389 if ( !GetNextMessage(&msg) )
390 return false;
391
392 ProcessMessage(&msg);
393
394 return !m_shouldExit;
395 }
396
397 int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
398 {
399 MSG msg;
400 int rc = GetNextMessageTimeout(&msg, timeout);
401 if ( rc != 1 )
402 return rc;
403
404 ProcessMessage(&msg);
405
406 return !m_shouldExit;
407 }
408
409 #endif // wxUSE_CONSOLE_EVENTLOOP
410
411 #endif //wxUSE_GUI