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