]> git.saurik.com Git - wxWidgets.git/blame - src/msw/evtloop.cpp
don't hang forever in Dispatch() if there is no event loop
[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>
6c9a19aa 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"
0cd7d9b2 37#include "wx/tooltip.h"
3808e191
JS
38
39#include "wx/msw/private.h"
40
0cd7d9b2
VS
41#if wxUSE_THREADS
42 // define the array of MSG strutures
43 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
44 // VS: this is a bit dirty - it duplicates same declaration in app.cpp
45 // (and there's no WX_DEFINE_OBJARRAY for that reason - it is already
6c0e8b4e 46 // defined in app.cpp).
0cd7d9b2
VS
47#endif
48
3808e191
JS
49// ----------------------------------------------------------------------------
50// wxEventLoopImpl
51// ----------------------------------------------------------------------------
52
53class WXDLLEXPORT wxEventLoopImpl
54{
55public:
56 // ctor
57 wxEventLoopImpl() { SetExitCode(0); }
58
59 // process a message
60 void ProcessMessage(MSG *msg);
61
62 // generate an idle message, return TRUE if more idle time requested
63 bool SendIdleMessage();
64
65 // set/get the exit code
66 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
67 int GetExitCode() const { return m_exitcode; }
68
69private:
70 // preprocess a message, return TRUE if processed (i.e. no further
71 // dispatching required)
72 bool PreProcessMessage(MSG *msg);
73
74 // the exit code of the event loop
75 int m_exitcode;
76};
77
78// ============================================================================
79// wxEventLoopImpl implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
83// wxEventLoopImpl message processing
84// ----------------------------------------------------------------------------
85
86void wxEventLoopImpl::ProcessMessage(MSG *msg)
87{
88 // give us the chance to preprocess the message first
89 if ( !PreProcessMessage(msg) )
90 {
91 // if it wasn't done, dispatch it to the corresponding window
92 ::TranslateMessage(msg);
93 ::DispatchMessage(msg);
94 }
95}
96
97bool wxEventLoopImpl::PreProcessMessage(MSG *msg)
98{
ac8d0c11
VZ
99 HWND hwnd = msg->hwnd;
100 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
101
102 // this may happen if the event occured in a standard modeless dialog (the
103 // only example of which I know of is the find/replace dialog) - then call
104 // IsDialogMessage() to make TAB navigation in it work
105 if ( !wndThis )
106 {
107 // we need to find the dialog containing this control as
108 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
109 // them) if we call it for the control itself
110 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
111 {
112 hwnd = ::GetParent(hwnd);
113 }
114
115 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
116 }
3808e191
JS
117
118#if wxUSE_TOOLTIPS
119 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
120 // popup the tooltip bubbles
ac8d0c11 121 if ( msg->message == WM_MOUSEMOVE )
3808e191
JS
122 {
123 wxToolTip *tt = wndThis->GetToolTip();
124 if ( tt )
125 {
126 tt->RelayEvent((WXMSG *)msg);
127 }
128 }
129#endif // wxUSE_TOOLTIPS
130
ac8d0c11
VZ
131 // allow the window to prevent certain messages from being
132 // translated/processed (this is currently used by wxTextCtrl to always
133 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
134 if ( !wndThis->MSWShouldPreProcessMessage((WXMSG *)msg) )
135 {
136 return FALSE;
137 }
138
139 // try translations first: the accelerators override everything
3808e191 140 wxWindow *wnd;
ac8d0c11 141
3808e191
JS
142 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
143 {
ac8d0c11 144 if ( wnd->MSWTranslateMessage((WXMSG *)msg))
3808e191 145 return TRUE;
ac8d0c11
VZ
146
147 // stop at first top level window, i.e. don't try to process the key
148 // strokes originating in a dialog using the accelerators of the parent
149 // frame - this doesn't make much sense
150 if ( wnd->IsTopLevel() )
151 break;
3808e191
JS
152 }
153
ac8d0c11
VZ
154 // now try the other hooks (kbd navigation is handled here): we start from
155 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
156 // called above
157 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
3808e191
JS
158 {
159 if ( wnd->MSWProcessMessage((WXMSG *)msg) )
160 return TRUE;
161 }
162
ac8d0c11 163 // no special preprocessing for this message, dispatch it normally
3808e191
JS
164 return FALSE;
165}
166
167// ----------------------------------------------------------------------------
168// wxEventLoopImpl idle event processing
169// ----------------------------------------------------------------------------
170
171bool wxEventLoopImpl::SendIdleMessage()
172{
e39af974 173 return wxTheApp->ProcessIdle();
3808e191
JS
174}
175
176// ============================================================================
177// wxEventLoop implementation
178// ============================================================================
179
b9f246f7
VS
180wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
181
3808e191
JS
182// ----------------------------------------------------------------------------
183// wxEventLoop running and exiting
184// ----------------------------------------------------------------------------
185
186wxEventLoop::~wxEventLoop()
187{
188 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
189}
190
191bool wxEventLoop::IsRunning() const
192{
193 return m_impl != NULL;
194}
195
196int wxEventLoop::Run()
197{
198 // event loops are not recursive, you need to create another loop!
199 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
200
201 m_impl = new wxEventLoopImpl;
b9f246f7
VS
202
203 wxEventLoop *oldLoop = ms_activeLoop;
204 ms_activeLoop = this;
3808e191
JS
205
206 for ( ;; )
207 {
208#if wxUSE_THREADS
209 wxMutexGuiLeaveOrEnter();
210#endif // wxUSE_THREADS
211
212 // generate and process idle events for as long as we don't have
213 // anything else to do
214 while ( !Pending() && m_impl->SendIdleMessage() )
215 ;
216
217 // a message came or no more idle processing to do, sit in Dispatch()
218 // waiting for the next message
219 if ( !Dispatch() )
220 {
221 // we got WM_QUIT
222 break;
223 }
224 }
225
226 int exitcode = m_impl->GetExitCode();
227 delete m_impl;
228 m_impl = NULL;
229
b9f246f7
VS
230 ms_activeLoop = oldLoop;
231
3808e191
JS
232 return exitcode;
233}
234
235void wxEventLoop::Exit(int rc)
236{
237 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
238
239 m_impl->SetExitCode(rc);
240
241 ::PostQuitMessage(rc);
242}
243
244// ----------------------------------------------------------------------------
245// wxEventLoop message processing dispatching
246// ----------------------------------------------------------------------------
247
248bool wxEventLoop::Pending() const
249{
250 MSG msg;
251 return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0;
252}
253
254bool wxEventLoop::Dispatch()
255{
256 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
257
258 MSG msg;
259 BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0);
260
261 if ( rc == 0 )
262 {
263 // got WM_QUIT
264 return FALSE;
265 }
266
267 if ( rc == -1 )
268 {
269 // should never happen, but let's test for it nevertheless
270 wxLogLastError(wxT("GetMessage"));
271
272 // still break from the loop
273 return FALSE;
274 }
275
276#if wxUSE_THREADS
277 wxASSERT_MSG( wxThread::IsMain(),
278 wxT("only the main thread can process Windows messages") );
279
280 static bool s_hadGuiLock = TRUE;
281 static wxMsgArray s_aSavedMessages;
282
283 // if a secondary thread owning the mutex is doing GUI calls, save all
284 // messages for later processing - we can't process them right now because
285 // it will lead to recursive library calls (and we're not reentrant)
286 if ( !wxGuiOwnedByMainThread() )
287 {
288 s_hadGuiLock = FALSE;
289
290 // leave out WM_COMMAND messages: too dangerous, sometimes
291 // the message will be processed twice
6c0e8b4e 292 if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND )
3808e191 293 {
0cd7d9b2 294 s_aSavedMessages.Add(msg);
3808e191
JS
295 }
296
297 return TRUE;
298 }
299 else
300 {
301 // have we just regained the GUI lock? if so, post all of the saved
302 // messages
303 //
304 // FIXME of course, it's not _exactly_ the same as processing the
305 // messages normally - expect some things to break...
306 if ( !s_hadGuiLock )
307 {
308 s_hadGuiLock = TRUE;
309
310 size_t count = s_aSavedMessages.Count();
311 for ( size_t n = 0; n < count; n++ )
312 {
313 MSG& msg = s_aSavedMessages[n];
314 m_impl->ProcessMessage(&msg);
315 }
316
317 s_aSavedMessages.Empty();
318 }
319 }
320#endif // wxUSE_THREADS
321
322 m_impl->ProcessMessage(&msg);
323
324 return TRUE;
325}
326