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