]>
Commit | Line | Data |
---|---|---|
251b80c4 | 1 | /////////////////////////////////////////////////////////////////////////////// |
e4db172a | 2 | // Name: src/os2/evtloop.cpp |
251b80c4 KB |
3 | // Purpose: implements wxEventLoop 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> | |
65571936 | 9 | // License: wxWindows licence |
251b80c4 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
251b80c4 KB |
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" | |
598dc9b7 | 30 | #include "wx/timer.h" |
e4db172a | 31 | #include "wx/log.h" |
251b80c4 KB |
32 | #endif //WX_PRECOMP |
33 | ||
34 | #include "wx/evtloop.h" | |
35 | #include "wx/tooltip.h" | |
598dc9b7 | 36 | #include "wx/ptr_scpd.h" |
251b80c4 KB |
37 | |
38 | #include "wx/os2/private.h" | |
55bfbcb9 | 39 | #include "wx/os2/private/timer.h" // for wxTimerProc |
251b80c4 KB |
40 | |
41 | #if wxUSE_THREADS | |
42 | // define the array of QMSG strutures | |
43 | WX_DECLARE_OBJARRAY(QMSG, wxMsgArray); | |
598dc9b7 SN |
44 | |
45 | #include "wx/arrimpl.cpp" | |
46 | ||
47 | WX_DEFINE_OBJARRAY(wxMsgArray); | |
251b80c4 KB |
48 | #endif |
49 | ||
598dc9b7 | 50 | extern HAB vHabmain; |
251b80c4 KB |
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 | ||
598dc9b7 SN |
81 | // ---------------------------------------------------------------------------- |
82 | // helper class | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl); | |
86 | ||
251b80c4 KB |
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 | |
598dc9b7 | 101 | ::WinDispatchMsg(vHabmain, msg); |
251b80c4 KB |
102 | } |
103 | } | |
104 | ||
598dc9b7 | 105 | bool wxEventLoopImpl::PreProcessMessage(QMSG *pMsg) |
251b80c4 | 106 | { |
598dc9b7 SN |
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)) | |
251b80c4 | 127 | { |
598dc9b7 | 128 | return FALSE; |
251b80c4 | 129 | } |
251b80c4 | 130 | |
598dc9b7 SN |
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) | |
251b80c4 | 137 | { |
598dc9b7 SN |
138 | hWnd = ::WinQueryWindow(hWnd, QW_PARENT); |
139 | pWndThis = wxFindWinFromHandle((WXHWND)hWnd); | |
251b80c4 KB |
140 | } |
141 | ||
598dc9b7 SN |
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) | |
251b80c4 | 150 | { |
598dc9b7 SN |
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) | |
e7d70ef0 | 167 | break; |
e4db172a WS |
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; | |
598dc9b7 SN |
173 | } |
174 | ||
175 | if(!bRc) // untranslated, should restore original value | |
176 | CHARMSG(pChmsg)->chr = uSch; | |
177 | } | |
251b80c4 | 178 | } |
598dc9b7 SN |
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 | // } | |
251b80c4 KB |
187 | return FALSE; |
188 | } | |
189 | ||
190 | // ---------------------------------------------------------------------------- | |
191 | // wxEventLoopImpl idle event processing | |
192 | // ---------------------------------------------------------------------------- | |
193 | ||
194 | bool wxEventLoopImpl::SendIdleMessage() | |
195 | { | |
e39af974 | 196 | return wxTheApp->ProcessIdle() ; |
251b80c4 KB |
197 | } |
198 | ||
199 | // ============================================================================ | |
200 | // wxEventLoop implementation | |
201 | // ============================================================================ | |
202 | ||
251b80c4 KB |
203 | // ---------------------------------------------------------------------------- |
204 | // wxEventLoop running and exiting | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | wxEventLoop::~wxEventLoop() | |
208 | { | |
209 | wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") ); | |
210 | } | |
211 | ||
598dc9b7 SN |
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 (wxEventLoop::*FuncType)(); | |
231 | ||
232 | CallEventLoopMethod(wxEventLoop *evtLoop, FuncType fn) | |
233 | : m_evtLoop(evtLoop), m_fn(fn) { } | |
234 | ~CallEventLoopMethod() { (m_evtLoop->*m_fn)(); } | |
235 | ||
236 | private: | |
237 | wxEventLoop *m_evtLoop; | |
238 | FuncType m_fn; | |
239 | }; | |
240 | ||
251b80c4 KB |
241 | int wxEventLoop::Run() |
242 | { | |
243 | // event loops are not recursive, you need to create another loop! | |
244 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
245 | ||
598dc9b7 SN |
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 | |
77fb1a02 | 249 | wxEventLoopActivator activate(this); |
598dc9b7 | 250 | wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl); |
251b80c4 | 251 | |
598dc9b7 | 252 | CallEventLoopMethod callOnExit(this, &wxEventLoop::OnExit); |
251b80c4 KB |
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() ) | |
e4db172a WS |
263 | { |
264 | wxTheApp->HandleSockets(); | |
265 | wxMilliSleep(10); | |
266 | } | |
598dc9b7 SN |
267 | |
268 | wxTheApp->HandleSockets(); | |
269 | if (Pending()) | |
e4db172a WS |
270 | { |
271 | if ( !Dispatch() ) | |
272 | { | |
273 | // we got WM_QUIT | |
274 | break; | |
275 | } | |
276 | } | |
598dc9b7 | 277 | else |
d3a919bd | 278 | wxMilliSleep(10); |
251b80c4 KB |
279 | } |
280 | ||
16d17da6 VZ |
281 | OnExit(); |
282 | ||
598dc9b7 | 283 | return m_impl->GetExitCode(); |
251b80c4 KB |
284 | } |
285 | ||
286 | void wxEventLoop::Exit(int rc) | |
287 | { | |
288 | wxCHECK_RET( IsRunning(), _T("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 | // wxEventLoop message processing dispatching | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | bool wxEventLoop::Pending() const | |
300 | { | |
301 | QMSG msg; | |
598dc9b7 | 302 | return ::WinPeekMsg(vHabmain, &msg, 0, 0, 0, PM_NOREMOVE) != 0; |
251b80c4 KB |
303 | } |
304 | ||
305 | bool wxEventLoop::Dispatch() | |
306 | { | |
e4db172a | 307 | wxCHECK_MSG( IsRunning(), false, _T("can't call Dispatch() if not running") ); |
251b80c4 KB |
308 | |
309 | QMSG msg; | |
598dc9b7 | 310 | BOOL bRc = ::WinGetMsg(vHabmain, &msg, (HWND) NULL, 0, 0); |
251b80c4 | 311 | |
598dc9b7 | 312 | if ( bRc == 0 ) |
251b80c4 KB |
313 | { |
314 | // got WM_QUIT | |
e4db172a | 315 | return false; |
251b80c4 KB |
316 | } |
317 | ||
251b80c4 KB |
318 | #if wxUSE_THREADS |
319 | wxASSERT_MSG( wxThread::IsMain(), | |
320 | wxT("only the main thread can process Windows messages") ); | |
321 | ||
e4db172a | 322 | static bool s_hadGuiLock = true; |
251b80c4 KB |
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 | { | |
e4db172a | 330 | s_hadGuiLock = false; |
251b80c4 KB |
331 | |
332 | // leave out WM_COMMAND messages: too dangerous, sometimes | |
333 | // the message will be processed twice | |
598dc9b7 | 334 | if ( !wxIsWaitingForThread() || msg.msg != WM_COMMAND ) |
251b80c4 KB |
335 | { |
336 | s_aSavedMessages.Add(msg); | |
337 | } | |
338 | ||
e4db172a | 339 | return true; |
251b80c4 KB |
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 | { | |
e4db172a | 350 | s_hadGuiLock = true; |
251b80c4 KB |
351 | |
352 | size_t count = s_aSavedMessages.Count(); | |
353 | for ( size_t n = 0; n < count; n++ ) | |
354 | { | |
598dc9b7 | 355 | QMSG& msg = s_aSavedMessages[n]; |
251b80c4 KB |
356 | m_impl->ProcessMessage(&msg); |
357 | } | |
358 | ||
359 | s_aSavedMessages.Empty(); | |
360 | } | |
361 | } | |
362 | #endif // wxUSE_THREADS | |
363 | ||
364 | m_impl->ProcessMessage(&msg); | |
365 | ||
e4db172a | 366 | return true; |
251b80c4 | 367 | } |