1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: x11/evtloop.cpp
3 // Purpose: implements wxEventLoop for X11
4 // Author: Julian Smart
8 // Copyright: (c) 2002 Julian Smart
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "evtloop.h"
24 #include "wx/window.h"
26 #include "wx/evtloop.h"
27 #include "wx/tooltip.h"
29 #include "wx/thread.h"
32 #include "wx/x11/private.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxEventLoopImpl
43 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= FALSE
; }
45 // process an XEvent, return TRUE if it was processed
46 bool ProcessEvent(XEvent
* event
);
48 // generate an idle message, return TRUE if more idle time requested
51 // set/get the exit code
52 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
53 int GetExitCode() const { return m_exitcode
; }
56 // preprocess an event, return TRUE if processed (i.e. no further
57 // dispatching required)
58 bool PreProcessEvent(XEvent
* event
);
60 // the exit code of the event loop
66 // ============================================================================
67 // wxEventLoopImpl implementation
68 // ============================================================================
70 // ----------------------------------------------------------------------------
71 // wxEventLoopImpl message processing
72 // ----------------------------------------------------------------------------
74 bool wxEventLoopImpl::ProcessEvent(XEvent
*event
)
76 // give us the chance to preprocess the message first
77 if ( PreProcessEvent(event
) )
80 // if it wasn't done, dispatch it to the corresponding window
82 return wxTheApp
->ProcessXEvent((WXEvent
*) event
);
87 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
91 HWND hWnd
= msg
->hwnd
;
92 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
95 // try translations first; find the youngest window with a translation
98 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
100 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
104 // Anyone for a non-translation message? Try youngest descendants first.
105 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
107 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
115 // ----------------------------------------------------------------------------
116 // wxEventLoopImpl idle event processing
117 // ----------------------------------------------------------------------------
119 bool wxEventLoopImpl::SendIdleEvent()
122 event
.SetEventObject(wxTheApp
);
124 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
127 // ============================================================================
128 // wxEventLoop implementation
129 // ============================================================================
131 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
133 // ----------------------------------------------------------------------------
134 // wxEventLoop running and exiting
135 // ----------------------------------------------------------------------------
137 wxEventLoop::~wxEventLoop()
139 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
142 bool wxEventLoop::IsRunning() const
144 return m_impl
!= NULL
;
147 int wxEventLoop::Run()
149 // event loops are not recursive, you need to create another loop!
150 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
152 m_impl
= new wxEventLoopImpl
;
154 wxEventLoop
*oldLoop
= ms_activeLoop
;
155 ms_activeLoop
= this;
157 m_impl
->m_keepGoing
= TRUE
;
158 while ( m_impl
->m_keepGoing
)
160 #if 0 // wxUSE_THREADS
161 wxMutexGuiLeaveOrEnter();
162 #endif // wxUSE_THREADS
164 // generate and process idle events for as long as we don't have
165 // anything else to do
166 while ( ! Pending() )
169 wxTimer::NotifyTimers(); // TODO: is this the correct place for it?
171 if (!m_impl
->SendIdleEvent())
173 #if 0 // wxUSE_THREADS
174 // leave the main loop to give other threads a chance to
175 // perform their GUI work
180 // Break out of while loop
185 // a message came or no more idle processing to do, sit in Dispatch()
186 // waiting for the next message
193 int exitcode
= m_impl
->GetExitCode();
197 ms_activeLoop
= oldLoop
;
202 void wxEventLoop::Exit(int rc
)
204 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
206 m_impl
->SetExitCode(rc
);
207 m_impl
->m_keepGoing
= FALSE
;
210 // ----------------------------------------------------------------------------
211 // wxEventLoop message processing dispatching
212 // ----------------------------------------------------------------------------
214 bool wxEventLoop::Pending() const
216 XFlush((Display
*) wxGetDisplay());
217 return (XPending((Display
*) wxGetDisplay()) > 0);
220 bool wxEventLoop::Dispatch()
224 // TODO allowing for threads, as per e.g. wxMSW
226 XNextEvent((Display
*) wxGetDisplay(), & event
);
227 (void) m_impl
->ProcessEvent(& event
);