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