]> git.saurik.com Git - wxWidgets.git/blame - src/os2/evtloop.cpp
just added a comment
[wxWidgets.git] / src / os2 / evtloop.cpp
CommitLineData
251b80c4
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: os2/evtloop.cpp
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"
251b80c4
KB
31#endif //WX_PRECOMP
32
33#include "wx/evtloop.h"
598dc9b7 34#include "wx/log.h"
251b80c4 35#include "wx/tooltip.h"
598dc9b7 36#include "wx/ptr_scpd.h"
251b80c4
KB
37
38#include "wx/os2/private.h"
39
40#if wxUSE_THREADS
41 // define the array of QMSG strutures
42 WX_DECLARE_OBJARRAY(QMSG, wxMsgArray);
598dc9b7
SN
43
44 #include "wx/arrimpl.cpp"
45
46 WX_DEFINE_OBJARRAY(wxMsgArray);
251b80c4
KB
47#endif
48
598dc9b7 49extern HAB vHabmain;
251b80c4
KB
50
51// ----------------------------------------------------------------------------
52// wxEventLoopImpl
53// ----------------------------------------------------------------------------
54
55class WXDLLEXPORT wxEventLoopImpl
56{
57public:
58 // ctor
59 wxEventLoopImpl() { SetExitCode(0); }
60
61 // process a message
62 void ProcessMessage(QMSG *msg);
63
64 // generate an idle message, return TRUE if more idle time requested
65 bool SendIdleMessage();
66
67 // set/get the exit code
68 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
69 int GetExitCode() const { return m_exitcode; }
70
71private:
72 // preprocess a message, return TRUE if processed (i.e. no further
73 // dispatching required)
74 bool PreProcessMessage(QMSG *msg);
75
76 // the exit code of the event loop
77 int m_exitcode;
78};
79
598dc9b7
SN
80// ----------------------------------------------------------------------------
81// helper class
82// ----------------------------------------------------------------------------
83
84wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl);
85
86// this object sets the wxEventLoop given to the ctor as the currently active
87// one and unsets it in its dtor
88class wxEventLoopActivator
89{
90public:
91 wxEventLoopActivator(wxEventLoop **pActive,
92 wxEventLoop *evtLoop)
93 {
94 m_pActive = pActive;
95 m_evtLoopOld = *pActive;
96 *pActive = evtLoop;
97 }
98
99 ~wxEventLoopActivator()
100 {
101 // restore the previously active event loop
102 *m_pActive = m_evtLoopOld;
103 }
104
105private:
106 wxEventLoop *m_evtLoopOld;
107 wxEventLoop **m_pActive;
108};
109
251b80c4
KB
110// ============================================================================
111// wxEventLoopImpl implementation
112// ============================================================================
113
114// ----------------------------------------------------------------------------
115// wxEventLoopImpl message processing
116// ----------------------------------------------------------------------------
117
118void wxEventLoopImpl::ProcessMessage(QMSG *msg)
119{
120 // give us the chance to preprocess the message first
121 if ( !PreProcessMessage(msg) )
122 {
123 // if it wasn't done, dispatch it to the corresponding window
598dc9b7 124 ::WinDispatchMsg(vHabmain, msg);
251b80c4
KB
125 }
126}
127
598dc9b7 128bool wxEventLoopImpl::PreProcessMessage(QMSG *pMsg)
251b80c4 129{
598dc9b7
SN
130 HWND hWnd = pMsg->hwnd;
131 wxWindow *pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
132 wxWindow *pWnd;
133
134 //
135 // Pass non-system timer messages to the wxTimerProc
136 //
137 if (pMsg->msg == WM_TIMER &&
138 (SHORT1FROMMP(pMsg->mp1) != TID_CURSOR &&
139 SHORT1FROMMP(pMsg->mp1) != TID_FLASHWINDOW &&
140 SHORT1FROMMP(pMsg->mp1) != TID_SCROLL &&
141 SHORT1FROMMP(pMsg->mp1) != 0x0000
142 ))
143 wxTimerProc(NULL, 0, (int)pMsg->mp1, 0);
144
145 // Allow the window to prevent certain messages from being
146 // translated/processed (this is currently used by wxTextCtrl to always
147 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
148 //
149 if (pWndThis && !pWndThis->OS2ShouldPreProcessMessage((WXMSG*)pMsg))
251b80c4 150 {
598dc9b7 151 return FALSE;
251b80c4 152 }
251b80c4 153
598dc9b7
SN
154 //
155 // For some composite controls (like a combobox), wndThis might be NULL
156 // because the subcontrol is not a wxWindow, but only the control itself
157 // is - try to catch this case
158 //
159 while (hWnd && !pWndThis)
251b80c4 160 {
598dc9b7
SN
161 hWnd = ::WinQueryWindow(hWnd, QW_PARENT);
162 pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
251b80c4
KB
163 }
164
598dc9b7
SN
165
166 //
167 // Try translations first; find the youngest window with
168 // a translation table. OS/2 has case sensiive accels, so
169 // this block, coded by BK, removes that and helps make them
170 // case insensitive.
171 //
172 if(pMsg->msg == WM_CHAR)
251b80c4 173 {
598dc9b7
SN
174 PBYTE pChmsg = (PBYTE)&(pMsg->msg);
175 USHORT uSch = CHARMSG(pChmsg)->chr;
176 bool bRc = FALSE;
177
178 //
179 // Do not process keyup events
180 //
181 if(!(CHARMSG(pChmsg)->fs & KC_KEYUP))
182 {
183 if((CHARMSG(pChmsg)->fs & (KC_ALT | KC_CTRL)) && CHARMSG(pChmsg)->chr != 0)
184 CHARMSG(pChmsg)->chr = (USHORT)wxToupper((UCHAR)uSch);
185
186
187 for(pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent() )
188 {
189 if((bRc = pWnd->OS2TranslateMessage((WXMSG*)pMsg)) == TRUE)
e7d70ef0 190 break;
598dc9b7
SN
191 // stop at first top level window, i.e. don't try to process the
192 // key strokes originating in a dialog using the accelerators of
193 // the parent frame - this doesn't make much sense
194 if ( pWnd->IsTopLevel() )
195 break;
196 }
197
198 if(!bRc) // untranslated, should restore original value
199 CHARMSG(pChmsg)->chr = uSch;
200 }
251b80c4 201 }
598dc9b7
SN
202 //
203 // Anyone for a non-translation message? Try youngest descendants first.
204 //
205// for (pWnd = pWndThis->GetParent(); pWnd; pWnd = pWnd->GetParent())
206// {
207// if (pWnd->OS2ProcessMessage(pWxmsg))
208// return TRUE;
209// }
251b80c4
KB
210 return FALSE;
211}
212
213// ----------------------------------------------------------------------------
214// wxEventLoopImpl idle event processing
215// ----------------------------------------------------------------------------
216
217bool wxEventLoopImpl::SendIdleMessage()
218{
e39af974 219 return wxTheApp->ProcessIdle() ;
251b80c4
KB
220}
221
222// ============================================================================
223// wxEventLoop implementation
224// ============================================================================
225
3754265e 226wxEventLoop *wxEventLoopBase::ms_activeLoop = NULL;
251b80c4
KB
227
228// ----------------------------------------------------------------------------
229// wxEventLoop running and exiting
230// ----------------------------------------------------------------------------
231
232wxEventLoop::~wxEventLoop()
233{
234 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
235}
236
598dc9b7
SN
237//////////////////////////////////////////////////////////////////////////////
238//
239// Keep trying to process messages until WM_QUIT
240// received.
241//
242// If there are messages to be processed, they will all be
243// processed and OnIdle will not be called.
244// When there are no more messages, OnIdle is called.
245// If OnIdle requests more time,
246// it will be repeatedly called so long as there are no pending messages.
247// A 'feature' of this is that once OnIdle has decided that no more processing
248// is required, then it won't get processing time until further messages
249// are processed (it'll sit in Dispatch).
250//
251//////////////////////////////////////////////////////////////////////////////
252class CallEventLoopMethod
253{
254public:
255 typedef void (wxEventLoop::*FuncType)();
256
257 CallEventLoopMethod(wxEventLoop *evtLoop, FuncType fn)
258 : m_evtLoop(evtLoop), m_fn(fn) { }
259 ~CallEventLoopMethod() { (m_evtLoop->*m_fn)(); }
260
261private:
262 wxEventLoop *m_evtLoop;
263 FuncType m_fn;
264};
265
251b80c4
KB
266int wxEventLoop::Run()
267{
268 // event loops are not recursive, you need to create another loop!
269 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
270
598dc9b7
SN
271 // SendIdleMessage() and Dispatch() below may throw so the code here should
272 // be exception-safe, hence we must use local objects for all actions we
273 // should undo
274 wxEventLoopActivator activate(&ms_activeLoop, this);
275 wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl);
251b80c4 276
598dc9b7 277 CallEventLoopMethod callOnExit(this, &wxEventLoop::OnExit);
251b80c4
KB
278
279 for ( ;; )
280 {
281#if wxUSE_THREADS
282 wxMutexGuiLeaveOrEnter();
283#endif // wxUSE_THREADS
284
285 // generate and process idle events for as long as we don't have
286 // anything else to do
287 while ( !Pending() && m_impl->SendIdleMessage() )
598dc9b7
SN
288 {
289 wxTheApp->HandleSockets();
d3a919bd 290 wxMilliSleep(10);
598dc9b7
SN
291 }
292
293 wxTheApp->HandleSockets();
294 if (Pending())
295 {
296 if ( !Dispatch() )
297 {
298 // we got WM_QUIT
299 break;
300 }
301 }
302 else
d3a919bd 303 wxMilliSleep(10);
251b80c4
KB
304 }
305
598dc9b7 306 return m_impl->GetExitCode();
251b80c4
KB
307}
308
309void wxEventLoop::Exit(int rc)
310{
311 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
312
313 m_impl->SetExitCode(rc);
314
315 ::WinPostMsg(NULL, WM_QUIT, 0, 0);
316}
317
318// ----------------------------------------------------------------------------
319// wxEventLoop message processing dispatching
320// ----------------------------------------------------------------------------
321
322bool wxEventLoop::Pending() const
323{
324 QMSG msg;
598dc9b7 325 return ::WinPeekMsg(vHabmain, &msg, 0, 0, 0, PM_NOREMOVE) != 0;
251b80c4
KB
326}
327
328bool wxEventLoop::Dispatch()
329{
330 wxCHECK_MSG( IsRunning(), FALSE, _T("can't call Dispatch() if not running") );
331
332 QMSG msg;
598dc9b7 333 BOOL bRc = ::WinGetMsg(vHabmain, &msg, (HWND) NULL, 0, 0);
251b80c4 334
598dc9b7 335 if ( bRc == 0 )
251b80c4
KB
336 {
337 // got WM_QUIT
338 return FALSE;
339 }
340
251b80c4
KB
341#if wxUSE_THREADS
342 wxASSERT_MSG( wxThread::IsMain(),
343 wxT("only the main thread can process Windows messages") );
344
345 static bool s_hadGuiLock = TRUE;
346 static wxMsgArray s_aSavedMessages;
347
348 // if a secondary thread owning the mutex is doing GUI calls, save all
349 // messages for later processing - we can't process them right now because
350 // it will lead to recursive library calls (and we're not reentrant)
351 if ( !wxGuiOwnedByMainThread() )
352 {
353 s_hadGuiLock = FALSE;
354
355 // leave out WM_COMMAND messages: too dangerous, sometimes
356 // the message will be processed twice
598dc9b7 357 if ( !wxIsWaitingForThread() || msg.msg != WM_COMMAND )
251b80c4
KB
358 {
359 s_aSavedMessages.Add(msg);
360 }
361
362 return TRUE;
363 }
364 else
365 {
366 // have we just regained the GUI lock? if so, post all of the saved
367 // messages
368 //
369 // FIXME of course, it's not _exactly_ the same as processing the
370 // messages normally - expect some things to break...
371 if ( !s_hadGuiLock )
372 {
373 s_hadGuiLock = TRUE;
374
375 size_t count = s_aSavedMessages.Count();
376 for ( size_t n = 0; n < count; n++ )
377 {
598dc9b7 378 QMSG& msg = s_aSavedMessages[n];
251b80c4
KB
379 m_impl->ProcessMessage(&msg);
380 }
381
382 s_aSavedMessages.Empty();
383 }
384 }
385#endif // wxUSE_THREADS
386
387 m_impl->ProcessMessage(&msg);
388
389 return TRUE;
390}
391