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