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