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"
31 #include "wx/x11/private.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 class WXDLLEXPORT wxEventLoopImpl
42 wxEventLoopImpl() { SetExitCode(0); m_keepGoing
= FALSE
; }
45 void ProcessEvent(XEvent
* event
);
47 // generate an idle message, return TRUE if more idle time requested
50 // set/get the exit code
51 void SetExitCode(int exitcode
) { m_exitcode
= exitcode
; }
52 int GetExitCode() const { return m_exitcode
; }
55 // preprocess an event, return TRUE if processed (i.e. no further
56 // dispatching required)
57 bool PreProcessEvent(XEvent
* event
);
59 // the exit code of the event loop
65 // ============================================================================
66 // wxEventLoopImpl implementation
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // wxEventLoopImpl message processing
71 // ----------------------------------------------------------------------------
73 void wxEventLoopImpl::ProcessEvent(XEvent
*event
)
75 // give us the chance to preprocess the message first
76 if ( !PreProcessEvent(event
) )
78 // if it wasn't done, dispatch it to the corresponding window
80 wxTheApp
->ProcessXEvent((WXEvent
*) event
);
84 bool wxEventLoopImpl::PreProcessEvent(XEvent
*event
)
88 HWND hWnd
= msg
->hwnd
;
89 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
92 // try translations first; find the youngest window with a translation
95 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
97 if ( wnd
->MSWTranslateMessage((WXMSG
*)msg
) )
101 // Anyone for a non-translation message? Try youngest descendants first.
102 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
104 if ( wnd
->MSWProcessMessage((WXMSG
*)msg
) )
112 // ----------------------------------------------------------------------------
113 // wxEventLoopImpl idle event processing
114 // ----------------------------------------------------------------------------
116 bool wxEventLoopImpl::SendIdleEvent()
119 event
.SetEventObject(wxTheApp
);
121 return wxTheApp
->ProcessEvent(event
) && event
.MoreRequested();
124 // ============================================================================
125 // wxEventLoop implementation
126 // ============================================================================
128 wxEventLoop
*wxEventLoop::ms_activeLoop
= NULL
;
130 // ----------------------------------------------------------------------------
131 // wxEventLoop running and exiting
132 // ----------------------------------------------------------------------------
134 wxEventLoop::~wxEventLoop()
136 wxASSERT_MSG( !m_impl
, _T("should have been deleted in Run()") );
139 bool wxEventLoop::IsRunning() const
141 return m_impl
!= NULL
;
144 int wxEventLoop::Run()
146 // event loops are not recursive, you need to create another loop!
147 wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") );
149 m_impl
= new wxEventLoopImpl
;
151 wxEventLoop
*oldLoop
= ms_activeLoop
;
152 ms_activeLoop
= this;
154 m_impl
->m_keepGoing
= TRUE
;
155 while ( m_impl
->m_keepGoing
)
157 #if 0 // wxUSE_THREADS
158 wxMutexGuiLeaveOrEnter();
159 #endif // wxUSE_THREADS
161 // generate and process idle events for as long as we don't have
162 // anything else to do
163 while ( ! Pending() )
165 if (!m_impl
->SendIdleEvent())
167 #if 0 // wxUSE_THREADS
168 // leave the main loop to give other threads a chance to
169 // perform their GUI work
174 // Break out of while loop
179 // a message came or no more idle processing to do, sit in Dispatch()
180 // waiting for the next message
187 int exitcode
= m_impl
->GetExitCode();
191 ms_activeLoop
= oldLoop
;
196 void wxEventLoop::Exit(int rc
)
198 wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") );
200 m_impl
->SetExitCode(rc
);
201 m_impl
->m_keepGoing
= FALSE
;
204 // ----------------------------------------------------------------------------
205 // wxEventLoop message processing dispatching
206 // ----------------------------------------------------------------------------
208 bool wxEventLoop::Pending() const
210 XFlush((Display
*) wxGetDisplay());
211 return (XPending((Display
*) wxGetDisplay()) > 0);
214 bool wxEventLoop::Dispatch()
218 // TODO allowing for threads, as per e.g. wxMSW
220 XNextEvent((Display
*) wxGetDisplay(), & event
);
221 m_impl
->ProcessEvent(& event
);