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