]>
Commit | Line | Data |
---|---|---|
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> | |
9 | // License: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
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 | |
32 | #include "wx/window.h" | |
33 | #include "wx/app.h" | |
34 | #endif //WX_PRECOMP | |
35 | ||
36 | #include "wx/evtloop.h" | |
37 | ||
38 | #include "wx/tooltip.h" | |
39 | #include "wx/except.h" | |
40 | #include "wx/ptr_scpd.h" | |
41 | ||
42 | #include "wx/msw/private.h" | |
43 | ||
44 | #if wxUSE_THREADS | |
45 | #include "wx/thread.h" | |
46 | ||
47 | // define the array of MSG strutures | |
48 | WX_DECLARE_OBJARRAY(MSG, wxMsgArray); | |
49 | ||
50 | #include "wx/arrimpl.cpp" | |
51 | ||
52 | WX_DEFINE_OBJARRAY(wxMsgArray); | |
53 | #endif // wxUSE_THREADS | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // helper class | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // this object sets the wxEventLoop given to the ctor as the currently active | |
60 | // one and unsets it in its dtor | |
61 | class wxEventLoopActivator | |
62 | { | |
63 | public: | |
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 | ||
78 | private: | |
79 | wxEventLoop *m_evtLoopOld; | |
80 | wxEventLoop **m_pActive; | |
81 | }; | |
82 | ||
83 | // ============================================================================ | |
84 | // wxEventLoop implementation | |
85 | // ============================================================================ | |
86 | ||
87 | wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL; | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // ctor/dtor | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | wxEventLoop::wxEventLoop() | |
94 | { | |
95 | m_shouldExit = false; | |
96 | m_exitcode = 0; | |
97 | } | |
98 | ||
99 | // ---------------------------------------------------------------------------- | |
100 | // wxEventLoop message processing | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
103 | void wxEventLoop::ProcessMessage(WXMSG *msg) | |
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 | ||
114 | bool wxEventLoop::PreProcessMessage(WXMSG *msg) | |
115 | { | |
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 | |
125 | // IsDialogMessage() just eats all the messages (i.e. returns true for | |
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 | } | |
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 | |
138 | if ( msg->message == WM_MOUSEMOVE ) | |
139 | { | |
140 | wxToolTip *tt = wndThis->GetToolTip(); | |
141 | if ( tt ) | |
142 | { | |
143 | tt->RelayEvent((WXMSG *)msg); | |
144 | } | |
145 | } | |
146 | #endif // wxUSE_TOOLTIPS | |
147 | ||
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 | { | |
153 | return false; | |
154 | } | |
155 | ||
156 | // try translations first: the accelerators override everything | |
157 | wxWindow *wnd; | |
158 | ||
159 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
160 | { | |
161 | if ( wnd->MSWTranslateMessage((WXMSG *)msg)) | |
162 | return true; | |
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; | |
169 | } | |
170 | ||
171 | // now try the other hooks (kbd navigation is handled here): we start from | |
172 | // wndThis->GetParent() because wndThis->MSWProcessMessage() was already | |
173 | // called above | |
174 | for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() ) | |
175 | { | |
176 | if ( wnd->MSWProcessMessage((WXMSG *)msg) ) | |
177 | return true; | |
178 | } | |
179 | ||
180 | // no special preprocessing for this message, dispatch it normally | |
181 | return false; | |
182 | } | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // wxEventLoop running and exiting | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | bool wxEventLoop::IsRunning() const | |
189 | { | |
190 | return ms_activeLoop == this; | |
191 | } | |
192 | ||
193 | int wxEventLoop::Run() | |
194 | { | |
195 | // event loops are not recursive, you need to create another loop! | |
196 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
197 | ||
198 | // ProcessIdle() and Dispatch() below may throw so the code here should | |
199 | // be exception-safe, hence we must use local objects for all actions we | |
200 | // should undo | |
201 | wxEventLoopActivator activate(&ms_activeLoop, this); | |
202 | ||
203 | // we must ensure that OnExit() is called even if an exception is thrown | |
204 | // from inside Dispatch() but we must call it from Exit() in normal | |
205 | // situations because it is supposed to be called synchronously, | |
206 | // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or | |
207 | // something similar here) | |
208 | #if wxUSE_EXCEPTIONS | |
209 | for ( ;; ) | |
210 | { | |
211 | try | |
212 | { | |
213 | #endif // wxUSE_EXCEPTIONS | |
214 | ||
215 | // this is the event loop itself | |
216 | for ( ;; ) | |
217 | { | |
218 | #if wxUSE_THREADS | |
219 | wxMutexGuiLeaveOrEnter(); | |
220 | #endif // wxUSE_THREADS | |
221 | ||
222 | // generate and process idle events for as long as we don't | |
223 | // have anything else to do | |
224 | while ( !Pending() && (wxTheApp && wxTheApp->ProcessIdle()) ) | |
225 | ; | |
226 | ||
227 | // if the "should exit" flag is set, the loop should terminate | |
228 | // but not before processing any remaining messages so while | |
229 | // Pending() returns true, do process them | |
230 | if ( m_shouldExit ) | |
231 | { | |
232 | while ( Pending() ) | |
233 | Dispatch(); | |
234 | ||
235 | break; | |
236 | } | |
237 | ||
238 | // a message came or no more idle processing to do, sit in | |
239 | // Dispatch() waiting for the next message | |
240 | if ( !Dispatch() ) | |
241 | { | |
242 | // we got WM_QUIT | |
243 | break; | |
244 | } | |
245 | } | |
246 | ||
247 | #if wxUSE_EXCEPTIONS | |
248 | // exit the outer loop as well | |
249 | break; | |
250 | } | |
251 | catch ( ... ) | |
252 | { | |
253 | try | |
254 | { | |
255 | if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() ) | |
256 | { | |
257 | OnExit(); | |
258 | break; | |
259 | } | |
260 | //else: continue running the event loop | |
261 | } | |
262 | catch ( ... ) | |
263 | { | |
264 | // OnException() throwed, possibly rethrowing the same | |
265 | // exception again: very good, but we still need OnExit() to | |
266 | // be called | |
267 | OnExit(); | |
268 | throw; | |
269 | } | |
270 | } | |
271 | } | |
272 | #endif // wxUSE_EXCEPTIONS | |
273 | ||
274 | return m_exitcode; | |
275 | } | |
276 | ||
277 | void wxEventLoop::Exit(int rc) | |
278 | { | |
279 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
280 | ||
281 | m_exitcode = rc; | |
282 | m_shouldExit = true; | |
283 | ||
284 | OnExit(); | |
285 | ||
286 | // all we have to do to exit from the loop is to (maybe) wake it up so that | |
287 | // it can notice that Exit() had been called | |
288 | // | |
289 | // in particular, we do *not* use PostQuitMessage() here because we're not | |
290 | // sure that WM_QUIT is going to be processed by the correct event loop: it | |
291 | // is possible that another one is started before this one has a chance to | |
292 | // process WM_QUIT | |
293 | ::PostMessage(NULL, WM_NULL, 0, 0); | |
294 | } | |
295 | ||
296 | // ---------------------------------------------------------------------------- | |
297 | // wxEventLoop message processing dispatching | |
298 | // ---------------------------------------------------------------------------- | |
299 | ||
300 | bool wxEventLoop::Pending() const | |
301 | { | |
302 | MSG msg; | |
303 | return ::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE) != 0; | |
304 | } | |
305 | ||
306 | bool wxEventLoop::Dispatch() | |
307 | { | |
308 | wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") ); | |
309 | ||
310 | MSG msg; | |
311 | BOOL rc = ::GetMessage(&msg, (HWND) NULL, 0, 0); | |
312 | ||
313 | if ( rc == 0 ) | |
314 | { | |
315 | // got WM_QUIT | |
316 | return false; | |
317 | } | |
318 | ||
319 | if ( rc == -1 ) | |
320 | { | |
321 | // should never happen, but let's test for it nevertheless | |
322 | wxLogLastError(wxT("GetMessage")); | |
323 | ||
324 | // still break from the loop | |
325 | return false; | |
326 | } | |
327 | ||
328 | #if wxUSE_THREADS | |
329 | wxASSERT_MSG( wxThread::IsMain(), | |
330 | wxT("only the main thread can process Windows messages") ); | |
331 | ||
332 | static bool s_hadGuiLock = true; | |
333 | static wxMsgArray s_aSavedMessages; | |
334 | ||
335 | // if a secondary thread owning the mutex is doing GUI calls, save all | |
336 | // messages for later processing - we can't process them right now because | |
337 | // it will lead to recursive library calls (and we're not reentrant) | |
338 | if ( !wxGuiOwnedByMainThread() ) | |
339 | { | |
340 | s_hadGuiLock = false; | |
341 | ||
342 | // leave out WM_COMMAND messages: too dangerous, sometimes | |
343 | // the message will be processed twice | |
344 | if ( !wxIsWaitingForThread() || msg.message != WM_COMMAND ) | |
345 | { | |
346 | s_aSavedMessages.Add(msg); | |
347 | } | |
348 | ||
349 | return true; | |
350 | } | |
351 | else | |
352 | { | |
353 | // have we just regained the GUI lock? if so, post all of the saved | |
354 | // messages | |
355 | // | |
356 | // FIXME of course, it's not _exactly_ the same as processing the | |
357 | // messages normally - expect some things to break... | |
358 | if ( !s_hadGuiLock ) | |
359 | { | |
360 | s_hadGuiLock = true; | |
361 | ||
362 | size_t count = s_aSavedMessages.Count(); | |
363 | for ( size_t n = 0; n < count; n++ ) | |
364 | { | |
365 | MSG& msg = s_aSavedMessages[n]; | |
366 | ProcessMessage(&msg); | |
367 | } | |
368 | ||
369 | s_aSavedMessages.Empty(); | |
370 | } | |
371 | } | |
372 | #endif // wxUSE_THREADS | |
373 | ||
374 | ProcessMessage(&msg); | |
375 | ||
376 | return true; | |
377 | } | |
378 |