]> git.saurik.com Git - wxWidgets.git/blame - src/motif/evtloop.cpp
background drawing uncommented again
[wxWidgets.git] / src / motif / evtloop.cpp
CommitLineData
7e1bcfa8
MB
1///////////////////////////////////////////////////////////////////////////////
2// Name: motif/evtloop.cpp
3// Purpose: implements wxEventLoop for Motif
4// Author: Mattia Barbon
5// Modified by:
6// Created: 01.11.02
7// RCS-ID: $Id$
8// Copyright: (c) 2002 Mattia Barbon
6aa89a22 9// License: wxWindows licence
7e1bcfa8
MB
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
7e1bcfa8
MB
21 #pragma implementation "evtloop.h"
22#endif
23
24#ifdef __VMS
25#define XtParent XTPARENT
26#define XtDisplay XTDISPLAY
27#endif
28
29// For compilers that support precompilation, includes "wx.h".
30#include "wx/wxprec.h"
31
32#ifndef WX_PRECOMP
33#endif //WX_PRECOMP
34
35#include "wx/evtloop.h"
36#include "wx/event.h"
37#include "wx/app.h"
d8d18184 38#include "wx/window.h"
7e1bcfa8
MB
39
40#ifdef __VMS__
41#pragma message disable nosimpint
42#endif
43#include <Xm/Xm.h>
44#include <X11/Xlib.h>
45#ifdef __VMS__
46#pragma message enable nosimpint
47#endif
48
49#include "wx/motif/private.h"
50
51static bool CheckForKeyUp(XEvent* event);
52static bool CheckForKeyDown(XEvent* event);
53static bool CheckForAccelerator(XEvent* event);
54static void ProcessXEvent(XEvent* event);
55static void wxBreakDispatch();
56
57// ----------------------------------------------------------------------------
58// wxEventLoopImpl
59// ----------------------------------------------------------------------------
60
61class WXDLLEXPORT wxEventLoopImpl
62{
63public:
64 // ctor
65 wxEventLoopImpl() { SetExitCode(0); }
66
67 // set/get the exit code
68 void SetExitCode(int exitcode) { m_exitcode = exitcode; }
69 int GetExitCode() const { return m_exitcode; }
70
71 bool SendIdleMessage();
72 bool GetKeepGoing() const { return m_keepGoing; }
73 void SetKeepGoing(bool keepGoing) { m_keepGoing = keepGoing; }
74private:
75 // the exit code of the event loop
76 int m_exitcode;
77 bool m_keepGoing;
78};
79
80// ----------------------------------------------------------------------------
81// wxEventLoopImpl idle event processing
82// ----------------------------------------------------------------------------
83
84static bool SendIdleMessage()
85{
df7f0a04 86 return wxTheApp->ProcessIdle();
7e1bcfa8
MB
87}
88
89bool wxEventLoopImpl::SendIdleMessage()
90{
91 return ::SendIdleMessage();
92}
93
94// ============================================================================
95// wxEventLoop implementation
96// ============================================================================
97
98// ----------------------------------------------------------------------------
99// wxEventLoop running and exiting
100// ----------------------------------------------------------------------------
101
102wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
103
104wxEventLoop::~wxEventLoop()
105{
106 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
107}
108
109bool wxEventLoop::IsRunning() const
110{
111 return m_impl != NULL;
112}
113
114int wxEventLoop::Run()
115{
116 // event loops are not recursive, you need to create another loop!
117 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
118
119 wxEventLoop *oldLoop = ms_activeLoop;
120 ms_activeLoop = this;
121
122 m_impl = new wxEventLoopImpl;
123 m_impl->SetKeepGoing( true );
124
125 for( ;; )
126 {
127 if( !wxDoEventLoopIteration( *this ) )
128 break;
129 }
130
131 int exitcode = m_impl->GetExitCode();
132 delete m_impl;
133 m_impl = NULL;
134
135 ms_activeLoop = oldLoop;
136
137 return exitcode;
138}
139
140void wxEventLoop::Exit(int rc)
141{
142 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
143
144 m_impl->SetExitCode(rc);
145 m_impl->SetKeepGoing( false );
146
147 ::wxBreakDispatch();
148}
149
150// ----------------------------------------------------------------------------
151// wxEventLoop message processing dispatching
152// ----------------------------------------------------------------------------
153
154bool wxEventLoop::Pending() const
155{
156 return XtAppPending( (XtAppContext)wxTheApp->GetAppContext() ) != 0;
157}
158
159bool wxEventLoop::Dispatch()
160{
161 XEvent event;
162 XtAppContext context = (XtAppContext)wxTheApp->GetAppContext();
163
164 if( XtAppPeekEvent( context, &event ) != 0 )
165 {
166 XtAppNextEvent( context, &event );
167 ProcessXEvent( &event );
168 }
169 else
bf91e652
JJ
170#ifdef __VMS
171 XtAppProcessEvent( context, XtIMTimer|XtIMAlternateInput );
172#else
173 XtAppProcessEvent( context, XtIMTimer|XtIMAlternateInput|XtIMSignal );
174#endif
369da724 175
7e1bcfa8
MB
176 return m_impl ? m_impl->GetKeepGoing() : true;
177}
178
179// ----------------------------------------------------------------------------
180// Static functions (originally in app.cpp)
181// ----------------------------------------------------------------------------
182
183void ProcessXEvent(XEvent* event)
184{
185 if (event->type == KeyPress)
186 {
187 if (CheckForAccelerator(event))
188 {
189 // Do nothing! We intercepted and processed the event as an
190 // accelerator.
191 return;
192 }
193 // It seemed before that this hack was redundant and
194 // key down events were being generated by wxCanvasInputEvent.
195 // But no longer - why ???
196 //
197 else if (CheckForKeyDown(event))
198 {
199 // We intercepted and processed the key down event
200 return;
201 }
202 else
203 {
204 XtDispatchEvent(event);
205 return;
206 }
207 }
208 else if (event->type == KeyRelease)
209 {
210 // TODO: work out why we still need this ! -michael
211 //
212 if (::CheckForKeyUp(event))
213 {
214 // We intercepted and processed the key up event
215 return;
216 }
217 else
218 {
219 XtDispatchEvent(event);
220 return;
221 }
222 }
223 else if (event->type == PropertyNotify)
224 {
225 wxTheApp->HandlePropertyChange(event);
226 return;
227 }
228 else if (event->type == ResizeRequest)
229 {
230 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
231 * If resize event, don't resize until the last resize event for this
232 * window is recieved. Prevents flicker as windows are resized.
233 */
234
eb6fa4b4 235 Display *disp = event->xany.display;
7e1bcfa8
MB
236 Window win = event->xany.window;
237 XEvent report;
238
239 // to avoid flicker
240 report = * event;
241 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
242
243 // TODO: when implementing refresh optimization, we can use
244 // XtAddExposureToRegion to expand the window's paint region.
245
246 XtDispatchEvent(event);
247 }
248 else
249 {
250 XtDispatchEvent(event);
251 }
252}
253
254// Returns true if an accelerator has been processed
255bool CheckForAccelerator(XEvent* event)
256{
257 if (event->xany.type == KeyPress)
258 {
259 // Find a wxWindow for this window
260 // TODO: should get display for the window, not the current display
eb6fa4b4 261 Widget widget = XtWindowToWidget(event->xany.display,
7e1bcfa8
MB
262 event->xany.window);
263 wxWindow* win = NULL;
264
265 // Find the first wxWindow that corresponds to this event window
266 while (widget && !(win = wxGetWindowFromTable(widget)))
267 widget = XtParent(widget);
268
269 if (!widget || !win)
270 return false;
271
272 wxKeyEvent keyEvent(wxEVT_CHAR);
273 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
274
275 // Now we have a wxKeyEvent and we have a wxWindow.
276 // Go up the hierarchy until we find a matching accelerator,
277 // or we get to the top.
278 while (win)
279 {
280 if (win->ProcessAccelerator(keyEvent))
281 return true;
282 win = win->GetParent();
283 }
284 return false;
285 }
286 return false;
287}
288
289// bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
290
291bool CheckForKeyDown(XEvent* event)
292{
293 if (event->xany.type == KeyPress)
294 {
eb6fa4b4
MB
295 Widget widget = XtWindowToWidget(event->xany.display,
296 event->xany.window);
7e1bcfa8
MB
297 wxWindow* win = NULL;
298
299 // Find the first wxWindow that corresponds to this event window
300 while (widget && !(win = wxGetWindowFromTable(widget)))
301 widget = XtParent(widget);
302
303 if (!widget || !win)
304 return false;
305
306 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
307 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
308
44f5b995 309 return win->GetEventHandler()->ProcessEvent( keyEvent );
7e1bcfa8
MB
310 }
311
312 return false;
313}
314
315// bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
316
317bool CheckForKeyUp(XEvent* event)
318{
319 if (event->xany.type == KeyRelease)
320 {
eb6fa4b4
MB
321 Widget widget = XtWindowToWidget(event->xany.display,
322 event->xany.window);
7e1bcfa8
MB
323 wxWindow* win = NULL;
324
325 // Find the first wxWindow that corresponds to this event window
326 while (widget && !(win = wxGetWindowFromTable(widget)))
327 widget = XtParent(widget);
328
329 if (!widget || !win)
330 return false;
331
332 wxKeyEvent keyEvent(wxEVT_KEY_UP);
333 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
334
44f5b995 335 return win->GetEventHandler()->ProcessEvent( keyEvent );
7e1bcfa8
MB
336 }
337
338 return false;
339}
340
341// ----------------------------------------------------------------------------
342// executes one main loop iteration (declared in include/wx/motif/private.h)
343// ----------------------------------------------------------------------------
344
345bool wxDoEventLoopIteration( wxEventLoop& evtLoop )
346{
369da724
MB
347 bool moreRequested, pendingEvents;
348
349 for(;;)
350 {
351 pendingEvents = evtLoop.Pending();
352 if( pendingEvents ) break;
353 moreRequested = ::SendIdleMessage();
354 if( !moreRequested ) break;
355 }
356
7e1bcfa8 357#if wxUSE_THREADS
369da724
MB
358 if( !pendingEvents && !moreRequested )
359 {
360 // leave the main loop to give other threads a chance to
361 // perform their GUI work
362 wxMutexGuiLeave();
363 wxUsleep(20);
364 wxMutexGuiEnter();
365 }
7e1bcfa8
MB
366#endif
367
7e1bcfa8
MB
368 if( !evtLoop.Dispatch() )
369 return false;
370
371 return true;
372}
373
374// ----------------------------------------------------------------------------
375// ::wxWakeUpIdle implementation
376// ----------------------------------------------------------------------------
377
378// As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
379// writing a single byte to the pipe will cause wxEventLoop::Pending
380// to return true, and wxEventLoop::Dispatch to dispatch an input handler
381// that simply removes the byte(s), and to return, which will cause
382// an idle event to be sent
383
384// also wxEventLoop::Exit is implemented that way, so that exiting an
385// event loop won't require an event being in the queue
386
387#include "wx/module.h"
388
389#include <sys/types.h>
390#include <sys/time.h>
391#include <unistd.h>
392
393static XtInputId inputId;
394static int idleFds[2] = { -1, -1 };
395
396class wxIdlePipeModule : public wxModule
397{
398public:
399 wxIdlePipeModule() {};
400
401 virtual bool OnInit()
402 {
74039d33
JS
403 // Must be done before modules are initialized
404#if 0
7e1bcfa8
MB
405 if( pipe(idleFds) != 0 )
406 return false;
74039d33 407#endif
7e1bcfa8
MB
408 return true;
409 }
410
411 virtual void OnExit()
412 {
413 close( idleFds[0] );
414 close( idleFds[1] );
415 }
416private:
417 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule);
418};
419
420IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule, wxModule);
421
422static void wxInputCallback( XtPointer, int* fd, XtInputId* )
423{
424 char buffer[128];
425
426 // wxWakeUpIdle may have been called more than once
427 for(;;)
428 {
429 fd_set in;
430 struct timeval timeout;
431
432 timeout.tv_sec = 0;
433 timeout.tv_usec = 0;
434
435 FD_ZERO( &in );
436 FD_SET( *fd, &in );
437
438 if( select( *fd + 1, &in, NULL, NULL, &timeout ) <= 0 )
439 break;
440 if( read( *fd, buffer, sizeof(buffer) - 1 ) != sizeof(buffer) - 1 )
441 break;
442 }
443}
444
445static void wxBreakDispatch()
446{
d8d18184 447 char dummy = 0; // for valgrind
7e1bcfa8
MB
448
449 // check if wxWakeUpIdle has already been called
450 fd_set in;
451 struct timeval timeout;
452
453 timeout.tv_sec = 0;
454 timeout.tv_usec = 0;
455
456 FD_ZERO( &in );
457 FD_SET( idleFds[0], &in );
458
459 if( select( idleFds[0] + 1, &in, NULL, NULL, &timeout ) > 0 )
460 return;
461
462 // write a single byte
463 write( idleFds[1], &dummy, 1 );
464}
465
e2478fde 466void wxApp::WakeUpIdle()
7e1bcfa8
MB
467{
468 ::wxBreakDispatch();
469}
470
74039d33
JS
471bool wxInitIdleFds()
472{
473 if( pipe(idleFds) != 0 )
474 return false;
475 return true;
476}
477
7e1bcfa8
MB
478bool wxAddIdleCallback()
479{
74039d33
JS
480 if (!wxInitIdleFds())
481 return false;
482
7e1bcfa8
MB
483 // install input handler for wxWakeUpIdle
484 inputId = XtAppAddInput( (XtAppContext) wxTheApp->GetAppContext(),
485 idleFds[0],
486 (XtPointer)XtInputReadMask,
487 wxInputCallback,
488 NULL );
489
490 return true;
491}
492