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