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