]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: x11/evtloop.cpp | |
3 | // Purpose: implements wxEventLoop for X11 | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01.06.01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2002 Julian Smart | |
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 | #include "wx/window.h" | |
25 | #include "wx/app.h" | |
26 | #include "wx/evtloop.h" | |
27 | #include "wx/tooltip.h" | |
28 | #if wxUSE_THREADS | |
29 | #include "wx/thread.h" | |
30 | #endif | |
31 | #include "wx/timer.h" | |
32 | #include "wx/x11/private.h" | |
33 | #include "X11/Xlib.h" | |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // wxEventLoopImpl | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | class WXDLLEXPORT wxEventLoopImpl | |
40 | { | |
41 | public: | |
42 | // ctor | |
43 | wxEventLoopImpl() { SetExitCode(0); m_keepGoing = FALSE; } | |
44 | ||
45 | // process an XEvent, return TRUE if it was processed | |
46 | bool ProcessEvent(XEvent* event); | |
47 | ||
48 | // generate an idle message, return TRUE if more idle time requested | |
49 | bool SendIdleEvent(); | |
50 | ||
51 | // set/get the exit code | |
52 | void SetExitCode(int exitcode) { m_exitcode = exitcode; } | |
53 | int GetExitCode() const { return m_exitcode; } | |
54 | ||
55 | public: | |
56 | // preprocess an event, return TRUE if processed (i.e. no further | |
57 | // dispatching required) | |
58 | bool PreProcessEvent(XEvent* event); | |
59 | ||
60 | // the exit code of the event loop | |
61 | int m_exitcode; | |
62 | ||
63 | bool m_keepGoing; | |
64 | }; | |
65 | ||
66 | // ============================================================================ | |
67 | // wxEventLoopImpl implementation | |
68 | // ============================================================================ | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // wxEventLoopImpl message processing | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | bool wxEventLoopImpl::ProcessEvent(XEvent *event) | |
75 | { | |
76 | // give us the chance to preprocess the message first | |
77 | if ( PreProcessEvent(event) ) | |
78 | return TRUE; | |
79 | ||
80 | // if it wasn't done, dispatch it to the corresponding window | |
81 | if (wxTheApp) | |
82 | return wxTheApp->ProcessXEvent((WXEvent*) event); | |
83 | ||
84 | return FALSE; | |
85 | } | |
86 | ||
87 | bool wxEventLoopImpl::PreProcessEvent(XEvent *event) | |
88 | { | |
89 | // TODO | |
90 | #if 0 | |
91 | HWND hWnd = msg->hwnd; | |
92 | wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hWnd); | |
93 | ||
94 | ||
95 | // try translations first; find the youngest window with a translation | |
96 | // table. | |
97 | wxWindow *wnd; | |
98 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
99 | { | |
100 | if ( wnd->MSWTranslateMessage((WXMSG *)msg) ) | |
101 | return TRUE; | |
102 | } | |
103 | ||
104 | // Anyone for a non-translation message? Try youngest descendants first. | |
105 | for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) | |
106 | { | |
107 | if ( wnd->MSWProcessMessage((WXMSG *)msg) ) | |
108 | return TRUE; | |
109 | } | |
110 | #endif | |
111 | ||
112 | return FALSE; | |
113 | } | |
114 | ||
115 | // ---------------------------------------------------------------------------- | |
116 | // wxEventLoopImpl idle event processing | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
119 | bool wxEventLoopImpl::SendIdleEvent() | |
120 | { | |
121 | wxIdleEvent event; | |
122 | event.SetEventObject(wxTheApp); | |
123 | ||
124 | return wxTheApp->ProcessEvent(event) && event.MoreRequested(); | |
125 | } | |
126 | ||
127 | // ============================================================================ | |
128 | // wxEventLoop implementation | |
129 | // ============================================================================ | |
130 | ||
131 | wxEventLoop *wxEventLoop::ms_activeLoop = NULL; | |
132 | ||
133 | // ---------------------------------------------------------------------------- | |
134 | // wxEventLoop running and exiting | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | wxEventLoop::~wxEventLoop() | |
138 | { | |
139 | wxASSERT_MSG( !m_impl, _T("should have been deleted in Run()") ); | |
140 | } | |
141 | ||
142 | bool wxEventLoop::IsRunning() const | |
143 | { | |
144 | return m_impl != NULL; | |
145 | } | |
146 | ||
147 | int wxEventLoop::Run() | |
148 | { | |
149 | // event loops are not recursive, you need to create another loop! | |
150 | wxCHECK_MSG( !IsRunning(), -1, _T("can't reenter a message loop") ); | |
151 | ||
152 | m_impl = new wxEventLoopImpl; | |
153 | ||
154 | wxEventLoop *oldLoop = ms_activeLoop; | |
155 | ms_activeLoop = this; | |
156 | ||
157 | m_impl->m_keepGoing = TRUE; | |
158 | while ( m_impl->m_keepGoing ) | |
159 | { | |
160 | #if 0 // wxUSE_THREADS | |
161 | wxMutexGuiLeaveOrEnter(); | |
162 | #endif // wxUSE_THREADS | |
163 | ||
164 | // generate and process idle events for as long as we don't have | |
165 | // anything else to do | |
166 | while ( ! Pending() ) | |
167 | { | |
168 | #if wxUSE_TIMER | |
169 | wxTimer::NotifyTimers(); // TODO: is this the correct place for it? | |
170 | #endif | |
171 | if (!m_impl->SendIdleEvent()) | |
172 | { | |
173 | #if 0 // wxUSE_THREADS | |
174 | // leave the main loop to give other threads a chance to | |
175 | // perform their GUI work | |
176 | wxMutexGuiLeave(); | |
177 | wxUsleep(20); | |
178 | wxMutexGuiEnter(); | |
179 | #endif | |
180 | // Break out of while loop | |
181 | break; | |
182 | } | |
183 | } | |
184 | ||
185 | // a message came or no more idle processing to do, sit in Dispatch() | |
186 | // waiting for the next message | |
187 | if ( !Dispatch() ) | |
188 | { | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | int exitcode = m_impl->GetExitCode(); | |
194 | delete m_impl; | |
195 | m_impl = NULL; | |
196 | ||
197 | ms_activeLoop = oldLoop; | |
198 | ||
199 | return exitcode; | |
200 | } | |
201 | ||
202 | void wxEventLoop::Exit(int rc) | |
203 | { | |
204 | wxCHECK_RET( IsRunning(), _T("can't call Exit() if not running") ); | |
205 | ||
206 | m_impl->SetExitCode(rc); | |
207 | m_impl->m_keepGoing = FALSE; | |
208 | } | |
209 | ||
210 | // ---------------------------------------------------------------------------- | |
211 | // wxEventLoop message processing dispatching | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
214 | bool wxEventLoop::Pending() const | |
215 | { | |
216 | XFlush((Display*) wxGetDisplay()); | |
217 | return (XPending((Display*) wxGetDisplay()) > 0); | |
218 | } | |
219 | ||
220 | bool wxEventLoop::Dispatch() | |
221 | { | |
222 | XEvent event; | |
223 | ||
224 | // TODO allowing for threads, as per e.g. wxMSW | |
225 | ||
226 | XNextEvent((Display*) wxGetDisplay(), & event); | |
227 | (void) m_impl->ProcessEvent(& event); | |
228 | return TRUE; | |
229 | } | |
230 |