]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
Add wxListCtrl::Get/SetItemFont, patch 1236820 Francesco Montorsi
[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
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3808e191
JS
21 #pragma implementation "evtloop.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
6c0e8b4e
VZ
32 #include "wx/window.h"
33 #include "wx/app.h"
3808e191
JS
34#endif //WX_PRECOMP
35
36#include "wx/evtloop.h"
4300caa7 37
0cd7d9b2 38#include "wx/tooltip.h"
4300caa7
VZ
39#include "wx/except.h"
40#include "wx/ptr_scpd.h"
3808e191
JS
41
42#include "wx/msw/private.h"
43
0cd7d9b2 44#if wxUSE_THREADS
4300caa7
VZ
45 #include "wx/thread.h"
46
9af08eb8
VZ
47 // define the list of MSG strutures
48 WX_DECLARE_LIST(MSG, wxMsgList);
4300caa7 49
9af08eb8 50 #include "wx/listimpl.cpp"
4300caa7 51
9af08eb8 52 WX_DEFINE_LIST(wxMsgList);
4300caa7 53#endif // wxUSE_THREADS
0cd7d9b2 54
4300caa7
VZ
55// ----------------------------------------------------------------------------
56// helper class
57// ----------------------------------------------------------------------------
58
4300caa7
VZ
59// this object sets the wxEventLoop given to the ctor as the currently active
60// one and unsets it in its dtor
61class wxEventLoopActivator
62{
63public:
64 wxEventLoopActivator(wxEventLoop **pActive,
65 wxEventLoop *evtLoop)
66 {
67 m_pActive = pActive;
68 m_evtLoopOld = *pActive;
69 *pActive = evtLoop;
70 }
71
72 ~wxEventLoopActivator()
73 {
74 // restore the previously active event loop
75 *m_pActive = m_evtLoopOld;
76 }
77
78private:
79 wxEventLoop *m_evtLoopOld;
80 wxEventLoop **m_pActive;
81};
82
3808e191 83// ============================================================================
3754265e 84// wxEventLoop implementation
3808e191
JS
85// ============================================================================
86
3754265e 87wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
b4626104 88wxWindowMSW *wxEventLoop::ms_winCritical = NULL;
3754265e 89
3808e191 90// ----------------------------------------------------------------------------
3754265e 91// ctor/dtor
3808e191
JS
92// ----------------------------------------------------------------------------
93
3754265e
VZ
94wxEventLoop::wxEventLoop()
95{
96 m_shouldExit = false;
97 m_exitcode = 0;
98}
99
100// ----------------------------------------------------------------------------
101// wxEventLoop message processing
102// ----------------------------------------------------------------------------
103
104void wxEventLoop::ProcessMessage(WXMSG *msg)
3808e191
JS
105{
106 // give us the chance to preprocess the message first
107 if ( !PreProcessMessage(msg) )
108 {
109 // if it wasn't done, dispatch it to the corresponding window
110 ::TranslateMessage(msg);
111 ::DispatchMessage(msg);
112 }
113}
114
b4626104 115bool wxEventLoop::IsChildOfCriticalWindow(wxWindowMSW *win)
a7b7500c
VZ
116{
117 while ( win )
118 {
119 if ( win == ms_winCritical )
120 return true;
121
122 win = win->GetParent();
123 }
124
125 return false;
126}
127
3754265e 128bool wxEventLoop::PreProcessMessage(WXMSG *msg)
3808e191 129{
ac8d0c11 130 HWND hwnd = msg->hwnd;
a7b7500c
VZ
131 wxWindow * const wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
132 wxWindow *wnd;
ac8d0c11 133
3103e8a9 134 // this may happen if the event occurred in a standard modeless dialog (the
ac8d0c11
VZ
135 // only example of which I know of is the find/replace dialog) - then call
136 // IsDialogMessage() to make TAB navigation in it work
137 if ( !wndThis )
138 {
139 // we need to find the dialog containing this control as
3754265e 140 // IsDialogMessage() just eats all the messages (i.e. returns true for
ac8d0c11
VZ
141 // them) if we call it for the control itself
142 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
143 {
144 hwnd = ::GetParent(hwnd);
145 }
146
147 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
148 }
3808e191 149
a7b7500c
VZ
150 if ( !AllowProcessing(wndThis) )
151 {
152 // not a child of critical window, so we eat the event but take care to
153 // stop an endless stream of WM_PAINTs which would have resulted if we
154 // didn't validate the invalidated part of the window
155 if ( msg->message == WM_PAINT )
156 ::ValidateRect(hwnd, NULL);
157
158 return true;
159 }
160
3808e191
JS
161#if wxUSE_TOOLTIPS
162 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
163 // popup the tooltip bubbles
ac8d0c11 164 if ( msg->message == WM_MOUSEMOVE )
3808e191
JS
165 {
166 wxToolTip *tt = wndThis->GetToolTip();
167 if ( tt )
168 {
169 tt->RelayEvent((WXMSG *)msg);
170 }
171 }
172#endif // wxUSE_TOOLTIPS
173
ac8d0c11
VZ
174 // allow the window to prevent certain messages from being
175 // translated/processed (this is currently used by wxTextCtrl to always
176 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
177 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
178 {
3754265e 179 return false;
ac8d0c11
VZ
180 }
181
182 // try translations first: the accelerators override everything
3808e191
JS
183 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
184 {
ac8d0c11 185 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3754265e 186 return true;
ac8d0c11
VZ
187
188 // stop at first top level window, i.e. don't try to process the key
189 // strokes originating in a dialog using the accelerators of the parent
190 // frame - this doesn't make much sense
191 if ( wnd->IsTopLevel() )
192 break;
3808e191
JS
193 }
194
22cfea03
JS
195 // now try the other hooks (kbd navigation is handled here)
196 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
3808e191 197 {
22cfea03
JS
198 if (wnd != wndThis) // Skip the first since wndThis->MSWProcessMessage() was called above
199 {
200 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
201 return true;
202 }
203
204 // Stop at first top level window (as per comment above).
205 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
206 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
207 if (wnd->IsTopLevel())
208 break;
3808e191
JS
209 }
210
ac8d0c11 211 // no special preprocessing for this message, dispatch it normally
3754265e 212 return false;
cea62f9c
JS
213}
214
3808e191
JS
215// ----------------------------------------------------------------------------
216// wxEventLoop running and exiting
217// ----------------------------------------------------------------------------
218
3808e191
JS
219bool wxEventLoop::IsRunning() const
220{
3754265e 221 return ms_activeLoop == this;
3808e191
JS
222}
223
224int wxEventLoop::Run()
225{
226 // event loops are not recursive, you need to create another loop!
227 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
228
3754265e 229 // ProcessIdle() and Dispatch() below may throw so the code here should
4300caa7
VZ
230 // be exception-safe, hence we must use local objects for all actions we
231 // should undo
232 wxEventLoopActivator activate(&ms_activeLoop, this);
4300caa7 233
96d38c7e
VS
234 // we must ensure that OnExit() is called even if an exception is thrown
235 // from inside Dispatch() but we must call it from Exit() in normal
236 // situations because it is supposed to be called synchronously,
237 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
238 // something similar here)
cea62f9c 239#if wxUSE_EXCEPTIONS
96d38c7e 240 for ( ;; )
3808e191 241 {
96d38c7e 242 try
2a8f35c3 243 {
96d38c7e 244#endif // wxUSE_EXCEPTIONS
d284c075 245
96d38c7e
VS
246 // this is the event loop itself
247 for ( ;; )
248 {
249 #if wxUSE_THREADS
250 wxMutexGuiLeaveOrEnter();
251 #endif // wxUSE_THREADS
252
253 // generate and process idle events for as long as we don't
254 // have anything else to do
255 while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) )
256 ;
257
258 // if the "should exit" flag is set, the loop should terminate
259 // but not before processing any remaining messages so while
260 // Pending() returns true, do process them
261 if ( m_shouldExit )
262 {
263 while ( Pending() )
264 Dispatch();
265
266 break;
267 }
268
269 // a message came or no more idle processing to do, sit in
270 // Dispatch() waiting for the next message
271 if ( !Dispatch() )
272 {
273 // we got WM_QUIT
274 break;
275 }
d284c075 276 }
924b84ab 277
96d38c7e
VS
278#if wxUSE_EXCEPTIONS
279 // exit the outer loop as well
280 break;
281 }
282 catch ( ... )
283 {
284 try
285 {
286 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
287 {
288 OnExit();
289 break;
290 }
291 //else: continue running the event loop
292 }
293 catch ( ... )
d284c075 294 {
96d38c7e
VS
295 // OnException() throwed, possibly rethrowing the same
296 // exception again: very good, but we still need OnExit() to
297 // be called
298 OnExit();
299 throw;
d284c075
VZ
300 }
301 }
3808e191 302 }
d284c075 303#endif // wxUSE_EXCEPTIONS
3808e191 304
3754265e 305 return m_exitcode;
3808e191
JS
306}
307
308void wxEventLoop::Exit(int rc)
309{
310 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
311
3754265e
VZ
312 m_exitcode = rc;
313 m_shouldExit = true;
3808e191 314
2a8f35c3
VZ
315 OnExit();
316
f7550d73
VZ
317 // all we have to do to exit from the loop is to (maybe) wake it up so that
318 // it can notice that Exit() had been called
319 //
320 // in particular, we do *not* use PostQuitMessage() here because we're not
321 // sure that WM_QUIT is going to be processed by the correct event loop: it
322 // is possible that another one is started before this one has a chance to
323 // process WM_QUIT
324 ::PostMessage(NULL, WM_NULL, 0, 0);
3808e191
JS
325}
326
327// ----------------------------------------------------------------------------
328// wxEventLoop message processing dispatching
329// ----------------------------------------------------------------------------
330
331bool wxEventLoop::Pending() const
332{
333 MSG msg;
334 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
335}
336
337bool wxEventLoop::Dispatch()
338{
3754265e 339 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
3808e191
JS
340
341 MSG msg;
342 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
343
344 if ( rc == 0 )
345 {
346 // got WM_QUIT
3754265e 347 return false;
3808e191
JS
348 }
349
350 if ( rc == -1 )
351 {
352 // should never happen, but let's test for it nevertheless
353 wxLogLastError(wxT("GetMessage"));
354
355 // still break from the loop
3754265e 356 return false;
3808e191
JS
357 }
358
359#if wxUSE_THREADS
360 wxASSERT_MSG( wxThread::IsMain(),
361 wxT("only the main thread can process Windows messages") );
362
3754265e 363 static bool s_hadGuiLock = true;
9af08eb8 364 static wxMsgList s_aSavedMessages;
3808e191
JS
365
366 // if a secondary thread owning the mutex is doing GUI calls, save all
367 // messages for later processing - we can't process them right now because
368 // it will lead to recursive library calls (and we're not reentrant)
369 if ( !wxGuiOwnedByMainThread() )
370 {
3754265e 371 s_hadGuiLock = false;
3808e191
JS
372
373 // leave out WM_COMMAND messages: too dangerous, sometimes
374 // the message will be processed twice
6c0e8b4e 375 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 376 {
9af08eb8
VZ
377 MSG* pMsg = new MSG(msg);
378 s_aSavedMessages.Append(pMsg);
3808e191
JS
379 }
380
3754265e 381 return true;
3808e191
JS
382 }
383 else
384 {
385 // have we just regained the GUI lock? if so, post all of the saved
386 // messages
387 //
388 // FIXME of course, it's not _exactly_ the same as processing the
389 // messages normally - expect some things to break...
390 if ( !s_hadGuiLock )
391 {
3754265e 392 s_hadGuiLock = true;
3808e191 393
9af08eb8
VZ
394 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
395 while (node)
3808e191 396 {
9af08eb8
VZ
397 MSG* pMsg = node->GetData();
398 s_aSavedMessages.Erase(node);
399
400 ProcessMessage(pMsg);
401 delete pMsg;
3808e191 402
9af08eb8
VZ
403 node = s_aSavedMessages.GetFirst();
404 }
3808e191
JS
405 }
406 }
407#endif // wxUSE_THREADS
408
3754265e 409 ProcessMessage(&msg);
3808e191 410
3754265e 411 return true;
3808e191
JS
412}
413