]> git.saurik.com Git - wxWidgets.git/blame - src/os2/evtloop.cpp
Add missing c_str() call to fix wxGTK ANSI+STL build.
[wxWidgets.git] / src / os2 / evtloop.cpp
CommitLineData
251b80c4 1///////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/os2/evtloop.cpp
b46b1d59 3// Purpose: implements wxGUIEventLoop for PM
251b80c4
KB
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 01.06.01
251b80c4 7// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
526954c5 8// Licence: wxWindows licence
251b80c4
KB
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
251b80c4
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27 #include "wx/window.h"
28 #include "wx/app.h"
598dc9b7 29 #include "wx/timer.h"
e4db172a 30 #include "wx/log.h"
251b80c4
KB
31#endif //WX_PRECOMP
32
33#include "wx/evtloop.h"
34#include "wx/tooltip.h"
664e1314 35#include "wx/scopedptr.h"
251b80c4
KB
36
37#include "wx/os2/private.h"
55bfbcb9 38#include "wx/os2/private/timer.h" // for wxTimerProc
251b80c4
KB
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
251b80c4
KB
86// ============================================================================
87// wxEventLoopImpl implementation
88// ============================================================================
89
90// ----------------------------------------------------------------------------
91// wxEventLoopImpl message processing
92// ----------------------------------------------------------------------------
93
94void wxEventLoopImpl::ProcessMessage(QMSG *msg)
95{
96 // give us the chance to preprocess the message first
97 if ( !PreProcessMessage(msg) )
98 {
99 // if it wasn't done, dispatch it to the corresponding window
598dc9b7 100 ::WinDispatchMsg(vHabmain, msg);
251b80c4
KB
101 }
102}
103
598dc9b7 104bool wxEventLoopImpl::PreProcessMessage(QMSG *pMsg)
251b80c4 105{
598dc9b7
SN
106 HWND hWnd = pMsg->hwnd;
107 wxWindow *pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
108 wxWindow *pWnd;
109
110 //
111 // Pass non-system timer messages to the wxTimerProc
112 //
113 if (pMsg->msg == WM_TIMER &&
114 (SHORT1FROMMP(pMsg->mp1) != TID_CURSOR &&
115 SHORT1FROMMP(pMsg->mp1) != TID_FLASHWINDOW &&
116 SHORT1FROMMP(pMsg->mp1) != TID_SCROLL &&
117 SHORT1FROMMP(pMsg->mp1) != 0x0000
118 ))
119 wxTimerProc(NULL, 0, (int)pMsg->mp1, 0);
120
121 // Allow the window to prevent certain messages from being
122 // translated/processed (this is currently used by wxTextCtrl to always
123 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
124 //
125 if (pWndThis && !pWndThis->OS2ShouldPreProcessMessage((WXMSG*)pMsg))
251b80c4 126 {
598dc9b7 127 return FALSE;
251b80c4 128 }
251b80c4 129
598dc9b7
SN
130 //
131 // For some composite controls (like a combobox), wndThis might be NULL
132 // because the subcontrol is not a wxWindow, but only the control itself
133 // is - try to catch this case
134 //
135 while (hWnd && !pWndThis)
251b80c4 136 {
598dc9b7
SN
137 hWnd = ::WinQueryWindow(hWnd, QW_PARENT);
138 pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
251b80c4
KB
139 }
140
598dc9b7
SN
141
142 //
143 // Try translations first; find the youngest window with
144 // a translation table. OS/2 has case sensiive accels, so
145 // this block, coded by BK, removes that and helps make them
146 // case insensitive.
147 //
148 if(pMsg->msg == WM_CHAR)
251b80c4 149 {
598dc9b7
SN
150 PBYTE pChmsg = (PBYTE)&(pMsg->msg);
151 USHORT uSch = CHARMSG(pChmsg)->chr;
152 bool bRc = FALSE;
153
154 //
155 // Do not process keyup events
156 //
157 if(!(CHARMSG(pChmsg)->fs & KC_KEYUP))
158 {
159 if((CHARMSG(pChmsg)->fs & (KC_ALT | KC_CTRL)) && CHARMSG(pChmsg)->chr != 0)
160 CHARMSG(pChmsg)->chr = (USHORT)wxToupper((UCHAR)uSch);
161
162
163 for(pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent() )
164 {
165 if((bRc = pWnd->OS2TranslateMessage((WXMSG*)pMsg)) == TRUE)
e7d70ef0 166 break;
e4db172a
WS
167 // stop at first top level window, i.e. don't try to process the
168 // key strokes originating in a dialog using the accelerators of
169 // the parent frame - this doesn't make much sense
170 if ( pWnd->IsTopLevel() )
171 break;
598dc9b7
SN
172 }
173
174 if(!bRc) // untranslated, should restore original value
175 CHARMSG(pChmsg)->chr = uSch;
176 }
251b80c4 177 }
598dc9b7
SN
178 //
179 // Anyone for a non-translation message? Try youngest descendants first.
180 //
181// for (pWnd = pWndThis->GetParent(); pWnd; pWnd = pWnd->GetParent())
182// {
183// if (pWnd->OS2ProcessMessage(pWxmsg))
184// return TRUE;
185// }
251b80c4
KB
186 return FALSE;
187}
188
189// ----------------------------------------------------------------------------
190// wxEventLoopImpl idle event processing
191// ----------------------------------------------------------------------------
192
193bool wxEventLoopImpl::SendIdleMessage()
194{
e39af974 195 return wxTheApp->ProcessIdle() ;
251b80c4
KB
196}
197
198// ============================================================================
b46b1d59 199// wxGUIEventLoop implementation
251b80c4
KB
200// ============================================================================
201
251b80c4 202// ----------------------------------------------------------------------------
b46b1d59 203// wxGUIEventLoop running and exiting
251b80c4
KB
204// ----------------------------------------------------------------------------
205
b46b1d59 206wxGUIEventLoop::~wxGUIEventLoop()
251b80c4 207{
9a83f860 208 wxASSERT_MSG( !m_impl, wxT("should have been deleted in Run()") );
251b80c4
KB
209}
210
598dc9b7
SN
211//////////////////////////////////////////////////////////////////////////////
212//
213// Keep trying to process messages until WM_QUIT
214// received.
215//
216// If there are messages to be processed, they will all be
217// processed and OnIdle will not be called.
218// When there are no more messages, OnIdle is called.
219// If OnIdle requests more time,
220// it will be repeatedly called so long as there are no pending messages.
221// A 'feature' of this is that once OnIdle has decided that no more processing
222// is required, then it won't get processing time until further messages
223// are processed (it'll sit in Dispatch).
224//
225//////////////////////////////////////////////////////////////////////////////
226class CallEventLoopMethod
227{
228public:
b46b1d59 229 typedef void (wxGUIEventLoop::*FuncType)();
598dc9b7 230
b46b1d59 231 CallEventLoopMethod(wxGUIEventLoop *evtLoop, FuncType fn)
598dc9b7
SN
232 : m_evtLoop(evtLoop), m_fn(fn) { }
233 ~CallEventLoopMethod() { (m_evtLoop->*m_fn)(); }
234
235private:
b46b1d59 236 wxGUIEventLoop *m_evtLoop;
598dc9b7
SN
237 FuncType m_fn;
238};
239
b46b1d59 240int wxGUIEventLoop::Run()
251b80c4
KB
241{
242 // event loops are not recursive, you need to create another loop!
9a83f860 243 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
251b80c4 244
598dc9b7
SN
245 // SendIdleMessage() and Dispatch() below may throw so the code here should
246 // be exception-safe, hence we must use local objects for all actions we
247 // should undo
77fb1a02 248 wxEventLoopActivator activate(this);
598dc9b7 249 wxEventLoopImplTiedPtr impl(&m_impl, new wxEventLoopImpl);
251b80c4 250
b46b1d59 251 CallEventLoopMethod callOnExit(this, &wxGUIEventLoop::OnExit);
251b80c4
KB
252
253 for ( ;; )
254 {
255#if wxUSE_THREADS
256 wxMutexGuiLeaveOrEnter();
257#endif // wxUSE_THREADS
258
259 // generate and process idle events for as long as we don't have
260 // anything else to do
261 while ( !Pending() && m_impl->SendIdleMessage() )
e4db172a
WS
262 {
263 wxTheApp->HandleSockets();
264 wxMilliSleep(10);
265 }
598dc9b7
SN
266
267 wxTheApp->HandleSockets();
268 if (Pending())
e4db172a
WS
269 {
270 if ( !Dispatch() )
271 {
272 // we got WM_QUIT
273 break;
274 }
275 }
598dc9b7 276 else
d3a919bd 277 wxMilliSleep(10);
251b80c4
KB
278 }
279
16d17da6
VZ
280 OnExit();
281
598dc9b7 282 return m_impl->GetExitCode();
251b80c4
KB
283}
284
b46b1d59 285void wxGUIEventLoop::Exit(int rc)
251b80c4 286{
9a83f860 287 wxCHECK_RET( IsRunning(), wxT("can't call Exit() if not running") );
251b80c4
KB
288
289 m_impl->SetExitCode(rc);
290
291 ::WinPostMsg(NULL, WM_QUIT, 0, 0);
292}
293
294// ----------------------------------------------------------------------------
b46b1d59 295// wxGUIEventLoop message processing dispatching
251b80c4
KB
296// ----------------------------------------------------------------------------
297
b46b1d59 298bool wxGUIEventLoop::Pending() const
251b80c4
KB
299{
300 QMSG msg;
598dc9b7 301 return ::WinPeekMsg(vHabmain, &msg, 0, 0, 0, PM_NOREMOVE) != 0;
251b80c4
KB
302}
303
b46b1d59 304bool wxGUIEventLoop::Dispatch()
251b80c4 305{
9a83f860 306 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
251b80c4
KB
307
308 QMSG msg;
598dc9b7 309 BOOL bRc = ::WinGetMsg(vHabmain, &msg, (HWND) NULL, 0, 0);
251b80c4 310
598dc9b7 311 if ( bRc == 0 )
251b80c4
KB
312 {
313 // got WM_QUIT
e4db172a 314 return false;
251b80c4
KB
315 }
316
251b80c4
KB
317#if wxUSE_THREADS
318 wxASSERT_MSG( wxThread::IsMain(),
319 wxT("only the main thread can process Windows messages") );
320
e4db172a 321 static bool s_hadGuiLock = true;
251b80c4
KB
322 static wxMsgArray s_aSavedMessages;
323
324 // if a secondary thread owning the mutex is doing GUI calls, save all
325 // messages for later processing - we can't process them right now because
326 // it will lead to recursive library calls (and we're not reentrant)
327 if ( !wxGuiOwnedByMainThread() )
328 {
e4db172a 329 s_hadGuiLock = false;
251b80c4
KB
330
331 // leave out WM_COMMAND messages: too dangerous, sometimes
332 // the message will be processed twice
598dc9b7 333 if ( !wxIsWaitingForThread() || msg.msg != WM_COMMAND )
251b80c4
KB
334 {
335 s_aSavedMessages.Add(msg);
336 }
337
e4db172a 338 return true;
251b80c4
KB
339 }
340 else
341 {
342 // have we just regained the GUI lock? if so, post all of the saved
343 // messages
344 //
345 // FIXME of course, it's not _exactly_ the same as processing the
346 // messages normally - expect some things to break...
347 if ( !s_hadGuiLock )
348 {
e4db172a 349 s_hadGuiLock = true;
251b80c4
KB
350
351 size_t count = s_aSavedMessages.Count();
352 for ( size_t n = 0; n < count; n++ )
353 {
598dc9b7 354 QMSG& msg = s_aSavedMessages[n];
251b80c4
KB
355 m_impl->ProcessMessage(&msg);
356 }
357
358 s_aSavedMessages.Empty();
359 }
360 }
361#endif // wxUSE_THREADS
362
363 m_impl->ProcessMessage(&msg);
364
e4db172a 365 return true;
251b80c4 366}
dde19c21
FM
367
368//
369// Yield to incoming messages
370//
371bool wxGUIEventLoop::YieldFor(long eventsToProcess)
372{
373 HAB vHab = 0;
374 QMSG vMsg;
375
376 //
377 // Disable log flushing from here because a call to wxYield() shouldn't
378 // normally result in message boxes popping up &c
379 //
380 wxLog::Suspend();
381
382 m_isInsideYield = true;
383 m_eventsToProcessInsideYield = eventsToProcess;
384
385 //
386 // We want to go back to the main message loop
387 // if we see a WM_QUIT. (?)
388 //
389 while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT)
390 {
391 // TODO: implement event filtering using the eventsToProcess mask
392
393#if wxUSE_THREADS
394 wxMutexGuiLeaveOrEnter();
395#endif // wxUSE_THREADS
396 if (!wxTheApp->Dispatch())
397 break;
398 }
399
400 //
401 // If they are pending events, we must process them.
402 //
403 if (wxTheApp)
e39b5fa4 404 {
dde19c21 405 wxTheApp->ProcessPendingEvents();
03647350 406 wxTheApp->HandleSockets();
e39b5fa4 407 }
dde19c21
FM
408
409 //
410 // Let the logs be flashed again
411 //
412 wxLog::Resume();
413 m_isInsideYield = false;
414
415 return true;
416} // end of wxYield