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