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