]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/os2/evtloop.cpp | |
3 | // Purpose: implements wxGUIEventLoop for PM | |
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 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/window.h" | |
29 | #include "wx/app.h" | |
30 | #include "wx/timer.h" | |
31 | #include "wx/log.h" | |
32 | #endif //WX_PRECOMP | |
33 | ||
34 | #include "wx/evtloop.h" | |
35 | #include "wx/tooltip.h" | |
36 | #include "wx/scopedptr.h" | |
37 | ||
38 | #include "wx/os2/private.h" | |
39 | #include "wx/os2/private/timer.h" // for wxTimerProc | |
40 | ||
41 | #if wxUSE_THREADS | |
42 | // define the array of QMSG strutures | |
43 | WX_DECLARE_OBJARRAY(QMSG, wxMsgArray); | |
44 | ||
45 | #include "wx/arrimpl.cpp" | |
46 | ||
47 | WX_DEFINE_OBJARRAY(wxMsgArray); | |
48 | #endif | |
49 | ||
50 | extern HAB vHabmain; | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // wxEventLoopImpl | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | class WXDLLEXPORT wxEventLoopImpl | |
57 | { | |
58 | public: | |
59 | // ctor | |
60 | wxEventLoopImpl() { SetExitCode(0); } | |
61 | ||
62 | // process a message | |
63 | void ProcessMessage(QMSG *msg); | |
64 | ||
65 | // generate an idle message, return TRUE if more idle time requested | |
66 | bool SendIdleMessage(); | |
67 | ||
68 | // set/get the exit code | |
69 | void SetExitCode(int exitcode) { m_exitcode = exitcode; } | |
70 | int GetExitCode() const { return m_exitcode; } | |
71 | ||
72 | private: | |
73 | // preprocess a message, return TRUE if processed (i.e. no further | |
74 | // dispatching required) | |
75 | bool PreProcessMessage(QMSG *msg); | |
76 | ||
77 | // the exit code of the event loop | |
78 | int m_exitcode; | |
79 | }; | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // helper class | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl); | |
86 | ||
87 | // ============================================================================ | |
88 | // wxEventLoopImpl implementation | |
89 | // ============================================================================ | |
90 | ||
91 | // ---------------------------------------------------------------------------- | |
92 | // wxEventLoopImpl message processing | |
93 | // ---------------------------------------------------------------------------- | |
94 | ||
95 | void wxEventLoopImpl::ProcessMessage(QMSG *msg) | |
96 | { | |
97 | // give us the chance to preprocess the message first | |
98 | if ( !PreProcessMessage(msg) ) | |
99 | { | |
100 | // if it wasn't done, dispatch it to the corresponding window | |
101 | ::WinDispatchMsg(vHabmain, msg); | |
102 | } | |
103 | } | |
104 | ||
105 | bool wxEventLoopImpl::PreProcessMessage(QMSG *pMsg) | |
106 | { | |
107 | HWND hWnd = pMsg->hwnd; | |
108 | wxWindow *pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
109 | wxWindow *pWnd; | |
110 | ||
111 | // | |
112 | // Pass non-system timer messages to the wxTimerProc | |
113 | // | |
114 | if (pMsg->msg == WM_TIMER && | |
115 | (SHORT1FROMMP(pMsg->mp1) != TID_CURSOR && | |
116 | SHORT1FROMMP(pMsg->mp1) != TID_FLASHWINDOW && | |
117 | SHORT1FROMMP(pMsg->mp1) != TID_SCROLL && | |
118 | SHORT1FROMMP(pMsg->mp1) != 0x0000 | |
119 | )) | |
120 | wxTimerProc(NULL, 0, (int)pMsg->mp1, 0); | |
121 | ||
122 | // Allow the window to prevent certain messages from being | |
123 | // translated/processed (this is currently used by wxTextCtrl to always | |
124 | // grab Ctrl-C/V/X, even if they are also accelerators in some parent) | |
125 | // | |
126 | if (pWndThis && !pWndThis->OS2ShouldPreProcessMessage((WXMSG*)pMsg)) | |
127 | { | |
128 | return FALSE; | |
129 | } | |
130 | ||
131 | // | |
132 | // For some composite controls (like a combobox), wndThis might be NULL | |
133 | // because the subcontrol is not a wxWindow, but only the control itself | |
134 | // is - try to catch this case | |
135 | // | |
136 | while (hWnd && !pWndThis) | |
137 | { | |
138 | hWnd = ::WinQueryWindow(hWnd, QW_PARENT); | |
139 | pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
140 | } | |
141 | ||
142 | ||
143 | // | |
144 | // Try translations first; find the youngest window with | |
145 | // a translation table. OS/2 has case sensiive accels, so | |
146 | // this block, coded by BK, removes that and helps make them | |
147 | // case insensitive. | |
148 | // | |
149 | if(pMsg->msg == WM_CHAR) | |
150 | { | |
151 | PBYTE pChmsg = (PBYTE)&(pMsg->msg); | |
152 | USHORT uSch = CHARMSG(pChmsg)->chr; | |
153 | bool bRc = FALSE; | |
154 | ||
155 | // | |
156 | // Do not process keyup events | |
157 | // | |
158 | if(!(CHARMSG(pChmsg)->fs & KC_KEYUP)) | |
159 | { | |
160 | if((CHARMSG(pChmsg)->fs & (KC_ALT | KC_CTRL)) && CHARMSG(pChmsg)->chr != 0) | |
161 | CHARMSG(pChmsg)->chr = (USHORT)wxToupper((UCHAR)uSch); | |
162 | ||
163 | ||
164 | for(pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent() ) | |
165 | { | |
166 | if((bRc = pWnd->OS2TranslateMessage((WXMSG*)pMsg)) == TRUE) | |
167 | break; | |
168 | // stop at first top level window, i.e. don't try to process the | |
169 | // key strokes originating in a dialog using the accelerators of | |
170 | // the parent frame - this doesn't make much sense | |
171 | if ( pWnd->IsTopLevel() ) | |
172 | break; | |
173 | } | |
174 | ||
175 | if(!bRc) // untranslated, should restore original value | |
176 | CHARMSG(pChmsg)->chr = uSch; | |
177 | } | |
178 | } | |
179 | // | |
180 | // Anyone for a non-translation message? Try youngest descendants first. | |
181 | // | |
182 | // for (pWnd = pWndThis->GetParent(); pWnd; pWnd = pWnd->GetParent()) | |
183 | // { | |
184 | // if (pWnd->OS2ProcessMessage(pWxmsg)) | |
185 | // return TRUE; | |
186 | // } | |
187 | return FALSE; | |
188 | } | |
189 | ||
190 | // ---------------------------------------------------------------------------- | |
191 | // wxEventLoopImpl idle event processing | |
192 | // ---------------------------------------------------------------------------- | |
193 | ||
194 | bool wxEventLoopImpl::SendIdleMessage() | |
195 | { | |
196 | return wxTheApp->ProcessIdle() ; | |
197 | } | |
198 | ||
199 | // ============================================================================ | |
200 | // wxGUIEventLoop implementation | |
201 | // ============================================================================ | |
202 | ||
203 | // ---------------------------------------------------------------------------- | |
204 | // wxGUIEventLoop running and exiting | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | wxGUIEventLoop::~wxGUIEventLoop() | |
208 | { | |
209 | wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") ); | |
210 | } | |
211 | ||
212 | ////////////////////////////////////////////////////////////////////////////// | |
213 | // | |
214 | // Keep trying to process messages until WM_QUIT | |
215 | // received. | |
216 | // | |
217 | // If there are messages to be processed, they will all be | |
218 | // processed and OnIdle will not be called. | |
219 | // When there are no more messages, OnIdle is called. | |
220 | // If OnIdle requests more time, | |
221 | // it will be repeatedly called so long as there are no pending messages. | |
222 | // A 'feature' of this is that once OnIdle has decided that no more processing | |
223 | // is required, then it won't get processing time until further messages | |
224 | // are processed (it'll sit in Dispatch). | |
225 | // | |
226 | ////////////////////////////////////////////////////////////////////////////// | |
227 | class CallEventLoopMethod | |
228 | { | |
229 | public: | |
230 | typedef void (wxGUIEventLoop::*FuncType)(); | |
231 | ||
232 | CallEventLoopMethod(wxGUIEventLoop *evtLoop, FuncType fn) | |
233 | : m_evtLoop(evtLoop), m_fn(fn) { } | |
234 | ~CallEventLoopMethod() { (m_evtLoop->*m_fn)(); } | |
235 | ||
236 | private: | |
237 | wxGUIEventLoop *m_evtLoop; | |
238 | FuncType m_fn; | |
239 | }; | |
240 | ||
241 | int wxGUIEventLoop::Run() | |
242 | { | |
243 | // event loops are not recursive, you need to create another loop! | |
244 | wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") ); | |
245 | ||
246 | // SendIdleMessage() and Dispatch() below may throw so the code here should | |
247 | // be exception-safe, hence we must use local objects for all actions we | |
248 | // should undo | |
249 | wxEventLoopActivator activate(this); | |
250 | wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl); | |
251 | ||
252 | CallEventLoopMethod callOnExit(this, &wxGUIEventLoop::OnExit); | |
253 | ||
254 | for ( ;; ) | |
255 | { | |
256 | #if wxUSE_THREADS | |
257 | wxMutexGuiLeaveOrEnter(); | |
258 | #endif // wxUSE_THREADS | |
259 | ||
260 | // generate and process idle events for as long as we don't have | |
261 | // anything else to do | |
262 | while ( !Pending() && m_impl->SendIdleMessage() ) | |
263 | { | |
264 | wxTheApp->HandleSockets(); | |
265 | wxMilliSleep(10); | |
266 | } | |
267 | ||
268 | wxTheApp->HandleSockets(); | |
269 | if (Pending()) | |
270 | { | |
271 | if ( !Dispatch() ) | |
272 | { | |
273 | // we got WM_QUIT | |
274 | break; | |
275 | } | |
276 | } | |
277 | else | |
278 | wxMilliSleep(10); | |
279 | } | |
280 | ||
281 | OnExit(); | |
282 | ||
283 | return m_impl->GetExitCode(); | |
284 | } | |
285 | ||
286 | void wxGUIEventLoop::Exit(int rc) | |
287 | { | |
288 | wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") ); | |
289 | ||
290 | m_impl->SetExitCode(rc); | |
291 | ||
292 | ::WinPostMsg(NULL, WM_QUIT, 0, 0); | |
293 | } | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // wxGUIEventLoop message processing dispatching | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | bool wxGUIEventLoop::Pending() const | |
300 | { | |
301 | QMSG msg; | |
302 | return ::WinPeekMsg(vHabmain, &msg, 0, 0, 0, PM_NOREMOVE) != 0; | |
303 | } | |
304 | ||
305 | bool wxGUIEventLoop::Dispatch() | |
306 | { | |
307 | wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") ); | |
308 | ||
309 | QMSG msg; | |
310 | BOOL bRc = ::WinGetMsg(vHabmain, &msg, (HWND) NULL, 0, 0); | |
311 | ||
312 | if ( bRc == 0 ) | |
313 | { | |
314 | // got WM_QUIT | |
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 | |
334 | if ( !wxIsWaitingForThread() || msg.msg != WM_COMMAND ) | |
335 | { | |
336 | s_aSavedMessages.Add(msg); | |
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 | QMSG& 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 | ||
369 | // | |
370 | // Yield to incoming messages | |
371 | // | |
372 | bool wxGUIEventLoop::YieldFor(long eventsToProcess) | |
373 | { | |
374 | HAB vHab = 0; | |
375 | QMSG vMsg; | |
376 | ||
377 | // | |
378 | // Disable log flushing from here because a call to wxYield() shouldn't | |
379 | // normally result in message boxes popping up &c | |
380 | // | |
381 | wxLog::Suspend(); | |
382 | ||
383 | m_isInsideYield = true; | |
384 | m_eventsToProcessInsideYield = eventsToProcess; | |
385 | ||
386 | // | |
387 | // We want to go back to the main message loop | |
388 | // if we see a WM_QUIT. (?) | |
389 | // | |
390 | while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT) | |
391 | { | |
392 | // TODO: implement event filtering using the eventsToProcess mask | |
393 | ||
394 | #if wxUSE_THREADS | |
395 | wxMutexGuiLeaveOrEnter(); | |
396 | #endif // wxUSE_THREADS | |
397 | if (!wxTheApp->Dispatch()) | |
398 | break; | |
399 | } | |
400 | ||
401 | // | |
402 | // If they are pending events, we must process them. | |
403 | // | |
404 | if (wxTheApp) | |
405 | { | |
406 | wxTheApp->ProcessPendingEvents(); | |
407 | wxTheApp->HandleSockets(); | |
408 | } | |
409 | ||
410 | // | |
411 | // Let the logs be flashed again | |
412 | // | |
413 | wxLog::Resume(); | |
414 | m_isInsideYield = false; | |
415 | ||
416 | return true; | |
417 | } // end of wxYield |