]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
Allow wxStaticText to have a custom fg without a custom bg
[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
VZ
87wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
88
3808e191 89// ----------------------------------------------------------------------------
3754265e 90// ctor/dtor
3808e191
JS
91// ----------------------------------------------------------------------------
92
3754265e
VZ
93wxEventLoop::wxEventLoop()
94{
95 m_shouldExit = false;
96 m_exitcode = 0;
97}
98
99// ----------------------------------------------------------------------------
100// wxEventLoop message processing
101// ----------------------------------------------------------------------------
102
103void wxEventLoop::ProcessMessage(WXMSG *msg)
3808e191
JS
104{
105 // give us the chance to preprocess the message first
106 if ( !PreProcessMessage(msg) )
107 {
108 // if it wasn't done, dispatch it to the corresponding window
109 ::TranslateMessage(msg);
110 ::DispatchMessage(msg);
111 }
112}
113
3754265e 114bool wxEventLoop::PreProcessMessage(WXMSG *msg)
3808e191 115{
ac8d0c11
VZ
116 HWND hwnd = msg->hwnd;
117 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
118
119 // this may happen if the event occured in a standard modeless dialog (the
120 // only example of which I know of is the find/replace dialog) - then call
121 // IsDialogMessage() to make TAB navigation in it work
122 if ( !wndThis )
123 {
124 // we need to find the dialog containing this control as
3754265e 125 // IsDialogMessage() just eats all the messages (i.e. returns true for
ac8d0c11
VZ
126 // them) if we call it for the control itself
127 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
128 {
129 hwnd = ::GetParent(hwnd);
130 }
131
132 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
133 }
3808e191
JS
134
135#if wxUSE_TOOLTIPS
136 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
137 // popup the tooltip bubbles
ac8d0c11 138 if ( msg->message == WM_MOUSEMOVE )
3808e191
JS
139 {
140 wxToolTip *tt = wndThis->GetToolTip();
141 if ( tt )
142 {
143 tt->RelayEvent((WXMSG *)msg);
144 }
145 }
146#endif // wxUSE_TOOLTIPS
147
ac8d0c11
VZ
148 // allow the window to prevent certain messages from being
149 // translated/processed (this is currently used by wxTextCtrl to always
150 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
151 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
152 {
3754265e 153 return false;
ac8d0c11
VZ
154 }
155
156 // try translations first: the accelerators override everything
3808e191 157 wxWindow *wnd;
ac8d0c11 158
3808e191
JS
159 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
160 {
ac8d0c11 161 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3754265e 162 return true;
ac8d0c11
VZ
163
164 // stop at first top level window, i.e. don't try to process the key
165 // strokes originating in a dialog using the accelerators of the parent
166 // frame - this doesn't make much sense
167 if ( wnd->IsTopLevel() )
168 break;
3808e191
JS
169 }
170
22cfea03
JS
171 // now try the other hooks (kbd navigation is handled here)
172 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
3808e191 173 {
22cfea03
JS
174 if (wnd != wndThis) // Skip the first since wndThis->MSWProcessMessage() was called above
175 {
176 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
177 return true;
178 }
179
180 // Stop at first top level window (as per comment above).
181 // If we don't do this, pressing ESC on a modal dialog shown as child of a modal
182 // dialog with wxID_CANCEL will cause the parent dialog to be closed, for example
183 if (wnd->IsTopLevel())
184 break;
3808e191
JS
185 }
186
ac8d0c11 187 // no special preprocessing for this message, dispatch it normally
3754265e 188 return false;
cea62f9c
JS
189}
190
3808e191
JS
191// ----------------------------------------------------------------------------
192// wxEventLoop running and exiting
193// ----------------------------------------------------------------------------
194
3808e191
JS
195bool wxEventLoop::IsRunning() const
196{
3754265e 197 return ms_activeLoop == this;
3808e191
JS
198}
199
200int wxEventLoop::Run()
201{
202 // event loops are not recursive, you need to create another loop!
203 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
204
3754265e 205 // ProcessIdle() and Dispatch() below may throw so the code here should
4300caa7
VZ
206 // be exception-safe, hence we must use local objects for all actions we
207 // should undo
208 wxEventLoopActivator activate(&ms_activeLoop, this);
4300caa7 209
2a8f35c3
VZ
210 // we must ensure that OnExit() is called even if an exception is thrown
211 // from inside Dispatch() but we must call it from Exit() in normal
212 // situations because it is supposed to be called synchronously,
213 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
214 // something similar here)
cea62f9c 215#if wxUSE_EXCEPTIONS
d284c075 216 for ( ;; )
3808e191 217 {
d284c075 218 try
2a8f35c3 219 {
d284c075
VZ
220#endif // wxUSE_EXCEPTIONS
221
222 // this is the event loop itself
223 for ( ;; )
f7550d73 224 {
d284c075
VZ
225 #if wxUSE_THREADS
226 wxMutexGuiLeaveOrEnter();
227 #endif // wxUSE_THREADS
228
229 // generate and process idle events for as long as we don't
230 // have anything else to do
3754265e 231 while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) )
d284c075
VZ
232 ;
233
234 // if the "should exit" flag is set, the loop should terminate
235 // but not before processing any remaining messages so while
236 // Pending() returns true, do process them
3754265e 237 if ( m_shouldExit )
d284c075
VZ
238 {
239 while ( Pending() )
240 Dispatch();
f7550d73 241
d284c075
VZ
242 break;
243 }
f7550d73 244
d284c075
VZ
245 // a message came or no more idle processing to do, sit in
246 // Dispatch() waiting for the next message
247 if ( !Dispatch() )
248 {
249 // we got WM_QUIT
250 break;
251 }
2a8f35c3 252 }
d284c075
VZ
253
254#if wxUSE_EXCEPTIONS
255 // exit the outer loop as well
256 break;
3808e191 257 }
d284c075
VZ
258 catch ( ... )
259 {
260 try
261 {
262 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
263 {
cea62f9c 264 OnExit();
cea62f9c 265 break;
d284c075
VZ
266 }
267 //else: continue running the event loop
268 }
269 catch ( ... )
270 {
271 // OnException() throwed, possibly rethrowing the same
272 // exception again: very good, but we still need OnExit() to
273 // be called
274 OnExit();
275 throw;
276 }
277 }
3808e191 278 }
d284c075 279#endif // wxUSE_EXCEPTIONS
3808e191 280
3754265e 281 return m_exitcode;
3808e191
JS
282}
283
284void wxEventLoop::Exit(int rc)
285{
286 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
287
3754265e
VZ
288 m_exitcode = rc;
289 m_shouldExit = true;
3808e191 290
2a8f35c3
VZ
291 OnExit();
292
f7550d73
VZ
293 // all we have to do to exit from the loop is to (maybe) wake it up so that
294 // it can notice that Exit() had been called
295 //
296 // in particular, we do *not* use PostQuitMessage() here because we're not
297 // sure that WM_QUIT is going to be processed by the correct event loop: it
298 // is possible that another one is started before this one has a chance to
299 // process WM_QUIT
300 ::PostMessage(NULL, WM_NULL, 0, 0);
3808e191
JS
301}
302
303// ----------------------------------------------------------------------------
304// wxEventLoop message processing dispatching
305// ----------------------------------------------------------------------------
306
307bool wxEventLoop::Pending() const
308{
309 MSG msg;
310 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
311}
312
313bool wxEventLoop::Dispatch()
314{
3754265e 315 wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") );
3808e191
JS
316
317 MSG msg;
318 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
319
320 if ( rc == 0 )
321 {
322 // got WM_QUIT
3754265e 323 return false;
3808e191
JS
324 }
325
326 if ( rc == -1 )
327 {
328 // should never happen, but let's test for it nevertheless
329 wxLogLastError(wxT("GetMessage"));
330
331 // still break from the loop
3754265e 332 return false;
3808e191
JS
333 }
334
335#if wxUSE_THREADS
336 wxASSERT_MSG( wxThread::IsMain(),
337 wxT("only the main thread can process Windows messages") );
338
3754265e 339 static bool s_hadGuiLock = true;
9af08eb8 340 static wxMsgList s_aSavedMessages;
3808e191
JS
341
342 // if a secondary thread owning the mutex is doing GUI calls, save all
343 // messages for later processing - we can't process them right now because
344 // it will lead to recursive library calls (and we're not reentrant)
345 if ( !wxGuiOwnedByMainThread() )
346 {
3754265e 347 s_hadGuiLock = false;
3808e191
JS
348
349 // leave out WM_COMMAND messages: too dangerous, sometimes
350 // the message will be processed twice
6c0e8b4e 351 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 352 {
9af08eb8
VZ
353 MSG* pMsg = new MSG(msg);
354 s_aSavedMessages.Append(pMsg);
3808e191
JS
355 }
356
3754265e 357 return true;
3808e191
JS
358 }
359 else
360 {
361 // have we just regained the GUI lock? if so, post all of the saved
362 // messages
363 //
364 // FIXME of course, it's not _exactly_ the same as processing the
365 // messages normally - expect some things to break...
366 if ( !s_hadGuiLock )
367 {
3754265e 368 s_hadGuiLock = true;
3808e191 369
9af08eb8
VZ
370 wxMsgList::compatibility_iterator node = s_aSavedMessages.GetFirst();
371 while (node)
3808e191 372 {
9af08eb8
VZ
373 MSG* pMsg = node->GetData();
374 s_aSavedMessages.Erase(node);
375
376 ProcessMessage(pMsg);
377 delete pMsg;
3808e191 378
9af08eb8
VZ
379 node = s_aSavedMessages.GetFirst();
380 }
3808e191
JS
381 }
382 }
383#endif // wxUSE_THREADS
384
3754265e 385 ProcessMessage(&msg);
3808e191 386
3754265e 387 return true;
3808e191
JS
388}
389