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