]> git.saurik.com Git - wxWidgets.git/blob - src/motif/evtloop.cpp
Added minimal file dialog for SmartPhone
[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 #ifdef __GNUG__
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 wxIdleEvent event;
87
88 return wxTheApp->ProcessEvent(event) && event.MoreRequested();
89 }
90
91 bool wxEventLoopImpl::SendIdleMessage()
92 {
93 return ::SendIdleMessage();
94 }
95
96 // ============================================================================
97 // wxEventLoop implementation
98 // ============================================================================
99
100 // ----------------------------------------------------------------------------
101 // wxEventLoop running and exiting
102 // ----------------------------------------------------------------------------
103
104 wxEventLoop *wxEventLoop::ms_activeLoop = NULL;
105
106 wxEventLoop::~wxEventLoop()
107 {
108 wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") );
109 }
110
111 bool wxEventLoop::IsRunning() const
112 {
113 return m_impl != NULL;
114 }
115
116 int wxEventLoop::Run()
117 {
118 // event loops are not recursive, you need to create another loop!
119 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
120
121 wxEventLoop *oldLoop = ms_activeLoop;
122 ms_activeLoop = this;
123
124 m_impl = new wxEventLoopImpl;
125 m_impl->SetKeepGoing( true );
126
127 for( ;; )
128 {
129 if( !wxDoEventLoopIteration( *this ) )
130 break;
131 }
132
133 int exitcode = m_impl->GetExitCode();
134 delete m_impl;
135 m_impl = NULL;
136
137 ms_activeLoop = oldLoop;
138
139 return exitcode;
140 }
141
142 void wxEventLoop::Exit(int rc)
143 {
144 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
145
146 m_impl->SetExitCode(rc);
147 m_impl->SetKeepGoing( false );
148
149 ::wxBreakDispatch();
150 }
151
152 // ----------------------------------------------------------------------------
153 // wxEventLoop message processing dispatching
154 // ----------------------------------------------------------------------------
155
156 bool wxEventLoop::Pending() const
157 {
158 return XtAppPending( (XtAppContext)wxTheApp->GetAppContext() ) != 0;
159 }
160
161 bool wxEventLoop::Dispatch()
162 {
163 XEvent event;
164 XtAppContext context = (XtAppContext)wxTheApp->GetAppContext();
165
166 if( XtAppPeekEvent( context, &event ) != 0 )
167 {
168 XtAppNextEvent( context, &event );
169 ProcessXEvent( &event );
170 }
171 else
172 #ifdef __VMS
173 XtAppProcessEvent( context, XtIMTimer|XtIMAlternateInput );
174 #else
175 XtAppProcessEvent( context, XtIMTimer|XtIMAlternateInput|XtIMSignal );
176 #endif
177
178 return m_impl ? m_impl->GetKeepGoing() : true;
179 }
180
181 // ----------------------------------------------------------------------------
182 // Static functions (originally in app.cpp)
183 // ----------------------------------------------------------------------------
184
185 void ProcessXEvent(XEvent* event)
186 {
187 if (event->type == KeyPress)
188 {
189 if (CheckForAccelerator(event))
190 {
191 // Do nothing! We intercepted and processed the event as an
192 // accelerator.
193 return;
194 }
195 // It seemed before that this hack was redundant and
196 // key down events were being generated by wxCanvasInputEvent.
197 // But no longer - why ???
198 //
199 else if (CheckForKeyDown(event))
200 {
201 // We intercepted and processed the key down event
202 return;
203 }
204 else
205 {
206 XtDispatchEvent(event);
207 return;
208 }
209 }
210 else if (event->type == KeyRelease)
211 {
212 // TODO: work out why we still need this ! -michael
213 //
214 if (::CheckForKeyUp(event))
215 {
216 // We intercepted and processed the key up event
217 return;
218 }
219 else
220 {
221 XtDispatchEvent(event);
222 return;
223 }
224 }
225 else if (event->type == PropertyNotify)
226 {
227 wxTheApp->HandlePropertyChange(event);
228 return;
229 }
230 else if (event->type == ResizeRequest)
231 {
232 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
233 * If resize event, don't resize until the last resize event for this
234 * window is recieved. Prevents flicker as windows are resized.
235 */
236
237 Display *disp = event->xany.display;
238 Window win = event->xany.window;
239 XEvent report;
240
241 // to avoid flicker
242 report = * event;
243 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
244
245 // TODO: when implementing refresh optimization, we can use
246 // XtAddExposureToRegion to expand the window's paint region.
247
248 XtDispatchEvent(event);
249 }
250 else
251 {
252 XtDispatchEvent(event);
253 }
254 }
255
256 // Returns true if an accelerator has been processed
257 bool CheckForAccelerator(XEvent* event)
258 {
259 if (event->xany.type == KeyPress)
260 {
261 // Find a wxWindow for this window
262 // TODO: should get display for the window, not the current display
263 Widget widget = XtWindowToWidget(event->xany.display,
264 event->xany.window);
265 wxWindow* win = NULL;
266
267 // Find the first wxWindow that corresponds to this event window
268 while (widget && !(win = wxGetWindowFromTable(widget)))
269 widget = XtParent(widget);
270
271 if (!widget || !win)
272 return false;
273
274 wxKeyEvent keyEvent(wxEVT_CHAR);
275 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
276
277 // Now we have a wxKeyEvent and we have a wxWindow.
278 // Go up the hierarchy until we find a matching accelerator,
279 // or we get to the top.
280 while (win)
281 {
282 if (win->ProcessAccelerator(keyEvent))
283 return true;
284 win = win->GetParent();
285 }
286 return false;
287 }
288 return false;
289 }
290
291 // bool wxApp::CheckForKeyDown(WXEvent* event) { wxFAIL; return false; }
292
293 bool CheckForKeyDown(XEvent* event)
294 {
295 if (event->xany.type == KeyPress)
296 {
297 Widget widget = XtWindowToWidget(event->xany.display,
298 event->xany.window);
299 wxWindow* win = NULL;
300
301 // Find the first wxWindow that corresponds to this event window
302 while (widget && !(win = wxGetWindowFromTable(widget)))
303 widget = XtParent(widget);
304
305 if (!widget || !win)
306 return false;
307
308 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
309 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
310
311 return win->GetEventHandler()->ProcessEvent( keyEvent );
312 }
313
314 return false;
315 }
316
317 // bool wxApp::CheckForKeyUp(WXEvent* event) { wxFAIL; return false; }
318
319 bool CheckForKeyUp(XEvent* event)
320 {
321 if (event->xany.type == KeyRelease)
322 {
323 Widget widget = XtWindowToWidget(event->xany.display,
324 event->xany.window);
325 wxWindow* win = NULL;
326
327 // Find the first wxWindow that corresponds to this event window
328 while (widget && !(win = wxGetWindowFromTable(widget)))
329 widget = XtParent(widget);
330
331 if (!widget || !win)
332 return false;
333
334 wxKeyEvent keyEvent(wxEVT_KEY_UP);
335 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, event);
336
337 return win->GetEventHandler()->ProcessEvent( keyEvent );
338 }
339
340 return false;
341 }
342
343 // ----------------------------------------------------------------------------
344 // executes one main loop iteration (declared in include/wx/motif/private.h)
345 // ----------------------------------------------------------------------------
346
347 bool wxDoEventLoopIteration( wxEventLoop& evtLoop )
348 {
349 bool moreRequested, pendingEvents;
350
351 for(;;)
352 {
353 pendingEvents = evtLoop.Pending();
354 if( pendingEvents ) break;
355 moreRequested = ::SendIdleMessage();
356 if( !moreRequested ) break;
357 }
358
359 #if wxUSE_THREADS
360 if( !pendingEvents && !moreRequested )
361 {
362 // leave the main loop to give other threads a chance to
363 // perform their GUI work
364 wxMutexGuiLeave();
365 wxUsleep(20);
366 wxMutexGuiEnter();
367 }
368 #endif
369
370 if( !evtLoop.Dispatch() )
371 return false;
372
373 return true;
374 }
375
376 // ----------------------------------------------------------------------------
377 // ::wxWakeUpIdle implementation
378 // ----------------------------------------------------------------------------
379
380 // As per Vadim's suggestion, we open a pipe, and XtAppAddInputSource it;
381 // writing a single byte to the pipe will cause wxEventLoop::Pending
382 // to return true, and wxEventLoop::Dispatch to dispatch an input handler
383 // that simply removes the byte(s), and to return, which will cause
384 // an idle event to be sent
385
386 // also wxEventLoop::Exit is implemented that way, so that exiting an
387 // event loop won't require an event being in the queue
388
389 #include "wx/module.h"
390
391 #include <sys/types.h>
392 #include <sys/time.h>
393 #include <unistd.h>
394
395 static XtInputId inputId;
396 static int idleFds[2] = { -1, -1 };
397
398 class wxIdlePipeModule : public wxModule
399 {
400 public:
401 wxIdlePipeModule() {};
402
403 virtual bool OnInit()
404 {
405 // Must be done before modules are initialized
406 #if 0
407 if( pipe(idleFds) != 0 )
408 return false;
409 #endif
410 return true;
411 }
412
413 virtual void OnExit()
414 {
415 close( idleFds[0] );
416 close( idleFds[1] );
417 }
418 private:
419 DECLARE_DYNAMIC_CLASS(wxIdlePipeModule);
420 };
421
422 IMPLEMENT_DYNAMIC_CLASS(wxIdlePipeModule, wxModule);
423
424 static 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
437 FD_ZERO( &in );
438 FD_SET( *fd, &in );
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
447 static void wxBreakDispatch()
448 {
449 char dummy = 0; // for valgrind
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
458 FD_ZERO( &in );
459 FD_SET( idleFds[0], &in );
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
468 void wxApp::WakeUpIdle()
469 {
470 ::wxBreakDispatch();
471 }
472
473 bool wxInitIdleFds()
474 {
475 if( pipe(idleFds) != 0 )
476 return false;
477 return true;
478 }
479
480 bool wxAddIdleCallback()
481 {
482 if (!wxInitIdleFds())
483 return false;
484
485 // install input handler for wxWakeUpIdle
486 inputId = XtAppAddInput( (XtAppContext) wxTheApp->GetAppContext(),
487 idleFds[0],
488 (XtPointer)XtInputReadMask,
489 wxInputCallback,
490 NULL );
491
492 return true;
493 }
494