1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: os2/evtloop.cpp
3 // Purpose: implements wxEventLoop for PM
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/window.h"
33 #include "wx/evtloop.h"
35 #include "wx/tooltip.h"
36 #include "wx/ptr_scpd.h"
38 #include "wx/os2/private.h"
41 // define the array of QMSG strutures
42 WX_DECLARE_OBJARRAY(QMSG
, wxMsgArray
);
44 #include "wx/arrimpl.cpp"
46 WX_DEFINE_OBJARRAY(wxMsgArray
);
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxEventLoopImpl
59 wxEventLoopImpl() { SetExitCode(0); }
62 void ProcessMessage(QMSG
*msg
);
64 // generate an idle message, return TRUE if more idle time requested
65 bool SendIdleMessage();
67 // set/get the exit code
68 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
69 int GetExitCode() const { return m_exitcode
; }
72 // preprocess a message, return TRUE if processed (i.e. no further
73 // dispatching required)
74 bool PreProcessMessage(QMSG
*msg
);
76 // the exit code of the event loop
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopImpl
);
86 // this object sets the wxEventLoop given to the ctor as the currently active
87 // one and unsets it in its dtor
88 class wxEventLoopActivator
91 wxEventLoopActivator(wxEventLoop
**pActive
,
95 m_evtLoopOld
= *pActive
;
99 ~wxEventLoopActivator()
101 // restore the previously active event loop
102 *m_pActive
= m_evtLoopOld
;
106 wxEventLoop
*m_evtLoopOld
;
107 wxEventLoop
**m_pActive
;
110 // ============================================================================
111 // wxEventLoopImpl implementation
112 // ============================================================================
114 // ----------------------------------------------------------------------------
115 // wxEventLoopImpl message processing
116 // ----------------------------------------------------------------------------
118 void wxEventLoopImpl::ProcessMessage(QMSG
*msg
)
120 // give us the chance to preprocess the message first
121 if ( !PreProcessMessage(msg
) )
123 // if it wasn't done, dispatch it to the corresponding window
124 ::WinDispatchMsg(vHabmain
, msg
);
128 bool wxEventLoopImpl::PreProcessMessage(QMSG
*pMsg
)
130 HWND hWnd
= pMsg
->hwnd
;
131 wxWindow
*pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
135 // Pass non-system timer messages to the wxTimerProc
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
143 wxTimerProc(NULL
, 0, (int)pMsg
->mp1
, 0);
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)
149 if (pWndThis
&& !pWndThis
->OS2ShouldPreProcessMessage((WXMSG
*)pMsg
))
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
159 while (hWnd
&& !pWndThis
)
161 hWnd
= ::WinQueryWindow(hWnd
, QW_PARENT
);
162 pWndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
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
172 if(pMsg
->msg
== WM_CHAR
)
174 PBYTE pChmsg
= (PBYTE
)&(pMsg
->msg
);
175 USHORT uSch
= CHARMSG(pChmsg
)->chr
;
179 // Do not process keyup events
181 if(!(CHARMSG(pChmsg
)->fs
& KC_KEYUP
))
183 if((CHARMSG(pChmsg
)->fs
& (KC_ALT
| KC_CTRL
)) && CHARMSG(pChmsg
)->chr
!= 0)
184 CHARMSG(pChmsg
)->chr
= (USHORT
)wxToupper((UCHAR
)uSch
);
187 for(pWnd
= pWndThis
; pWnd
; pWnd
= pWnd
->GetParent() )
189 if((bRc
= pWnd
->OS2TranslateMessage((WXMSG
*)pMsg
)) == TRUE
)
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() )
198 if(!bRc
) // untranslated, should restore original value
199 CHARMSG(pChmsg
)->chr
= uSch
;
203 // Anyone for a non-translation message? Try youngest descendants first.
205 // for (pWnd = pWndThis->GetParent(); pWnd; pWnd = pWnd->GetParent())
207 // if (pWnd->OS2ProcessMessage(pWxmsg))
213 // ----------------------------------------------------------------------------
214 // wxEventLoopImpl idle event processing
215 // ----------------------------------------------------------------------------
217 bool wxEventLoopImpl::SendIdleMessage()
219 return wxTheApp
->ProcessIdle() ;
222 // ============================================================================
223 // wxEventLoop implementation
224 // ============================================================================
226 wxEventLoop
*wxEventLoopBase::ms_activeLoop
= NULL
;
228 // ----------------------------------------------------------------------------
229 // wxEventLoop running and exiting
230 // ----------------------------------------------------------------------------
232 wxEventLoop::~wxEventLoop()
234 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
237 //////////////////////////////////////////////////////////////////////////////
239 // Keep trying to process messages until WM_QUIT
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).
251 //////////////////////////////////////////////////////////////////////////////
252 class CallEventLoopMethod
255 typedef void (wxEventLoop::*FuncType
)();
257 CallEventLoopMethod(wxEventLoop
*evtLoop
, FuncType fn
)
258 : m_evtLoop(evtLoop
), m_fn(fn
) { }
259 ~CallEventLoopMethod() { (m_evtLoop
->*m_fn
)(); }
262 wxEventLoop
*m_evtLoop
;
266 int wxEventLoop::Run()
268 // event loops are not recursive, you need to create another loop!
269 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
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
274 wxEventLoopActivator
activate(&ms_activeLoop
, this);
275 wxEventLoopImplTiedPtr
impl(&m_impl
, new wxEventLoopImpl
);
277 CallEventLoopMethod
callOnExit(this, &wxEventLoop::OnExit
);
282 wxMutexGuiLeaveOrEnter();
283 #endif // wxUSE_THREADS
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() )
289 wxTheApp
->HandleSockets();
293 wxTheApp
->HandleSockets();
306 return m_impl
->GetExitCode();
309 void wxEventLoop::Exit(int rc
)
311 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
313 m_impl
->SetExitCode(rc
);
315 ::WinPostMsg(NULL
, WM_QUIT
, 0, 0);
318 // ----------------------------------------------------------------------------
319 // wxEventLoop message processing dispatching
320 // ----------------------------------------------------------------------------
322 bool wxEventLoop::Pending() const
325 return ::WinPeekMsg(vHabmain
, &msg
, 0, 0, 0, PM_NOREMOVE
) != 0;
328 bool wxEventLoop::Dispatch()
330 wxCHECK_MSG( IsRunning(), FALSE
, _T("can't call Dispatch() if not running") );
333 BOOL bRc
= ::WinGetMsg(vHabmain
, &msg
, (HWND
) NULL
, 0, 0);
342 wxASSERT_MSG( wxThread::IsMain(),
343 wxT("only the main thread can process Windows messages") );
345 static bool s_hadGuiLock
= TRUE
;
346 static wxMsgArray s_aSavedMessages
;
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() )
353 s_hadGuiLock
= FALSE
;
355 // leave out WM_COMMAND messages: too dangerous, sometimes
356 // the message will be processed twice
357 if ( !wxIsWaitingForThread() || msg
.msg
!= WM_COMMAND
)
359 s_aSavedMessages
.Add(msg
);
366 // have we just regained the GUI lock? if so, post all of the saved
369 // FIXME of course, it's not _exactly_ the same as processing the
370 // messages normally - expect some things to break...
375 size_t count
= s_aSavedMessages
.Count();
376 for ( size_t n
= 0; n
< count
; n
++ )
378 QMSG
& msg
= s_aSavedMessages
[n
];
379 m_impl
->ProcessMessage(&msg
);
382 s_aSavedMessages
.Empty();
385 #endif // wxUSE_THREADS
387 m_impl
->ProcessMessage(&msg
);