]>
Commit | Line | Data |
---|---|---|
8000ae7f | 1 | /////////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/gtk/evtloop.cpp |
8000ae7f VZ |
3 | // Purpose: implements wxEventLoop for GTK+ |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 10.07.01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
526954c5 | 9 | // Licence: wxWindows licence |
8000ae7f VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
8000ae7f VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
8000ae7f | 27 | #include "wx/evtloop.h" |
5cd99866 | 28 | #include "wx/evtloopsrc.h" |
670f9935 WS |
29 | |
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/app.h" | |
bcf79477 | 32 | #include "wx/log.h" |
670f9935 | 33 | #endif // WX_PRECOMP |
8000ae7f VZ |
34 | |
35 | #include <gtk/gtk.h> | |
6b8ef0b3 | 36 | #include <glib.h> |
8000ae7f | 37 | |
8000ae7f VZ |
38 | // ============================================================================ |
39 | // wxEventLoop implementation | |
40 | // ============================================================================ | |
41 | ||
dde19c21 FM |
42 | extern GtkWidget *wxGetRootWindow(); |
43 | ||
8000ae7f VZ |
44 | // ---------------------------------------------------------------------------- |
45 | // wxEventLoop running and exiting | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
564c7fc4 | 48 | wxGUIEventLoop::wxGUIEventLoop() |
8000ae7f | 49 | { |
564c7fc4 | 50 | m_exitcode = 0; |
8000ae7f VZ |
51 | } |
52 | ||
b46b1d59 | 53 | int wxGUIEventLoop::Run() |
8000ae7f VZ |
54 | { |
55 | // event loops are not recursive, you need to create another loop! | |
564c7fc4 | 56 | wxCHECK_MSG( !IsRunning(), -1, "can't reenter a message loop" ); |
8000ae7f | 57 | |
77fb1a02 | 58 | wxEventLoopActivator activate(this); |
b9f246f7 | 59 | |
8000ae7f VZ |
60 | gtk_main(); |
61 | ||
16d17da6 VZ |
62 | OnExit(); |
63 | ||
564c7fc4 | 64 | return m_exitcode; |
8000ae7f VZ |
65 | } |
66 | ||
b46b1d59 | 67 | void wxGUIEventLoop::Exit(int rc) |
8000ae7f | 68 | { |
564c7fc4 | 69 | wxCHECK_RET( IsRunning(), "can't call Exit() if not running" ); |
8000ae7f | 70 | |
564c7fc4 | 71 | m_exitcode = rc; |
8000ae7f VZ |
72 | |
73 | gtk_main_quit(); | |
74 | } | |
75 | ||
564c7fc4 VZ |
76 | void wxGUIEventLoop::WakeUp() |
77 | { | |
78 | // TODO: idle events handling should really be done by wxEventLoop itself | |
79 | // but for now it's completely in gtk/app.cpp so just call there when | |
80 | // we have wxTheApp and hope that it doesn't matter that we do | |
81 | // nothing when we don't... | |
82 | if ( wxTheApp ) | |
83 | wxTheApp->WakeUpIdle(); | |
84 | } | |
85 | ||
6b8ef0b3 VZ |
86 | // ---------------------------------------------------------------------------- |
87 | // wxEventLoop adding & removing sources | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
5cd99866 VZ |
90 | #if wxUSE_EVENTLOOP_SOURCE |
91 | ||
6b8ef0b3 VZ |
92 | extern "C" |
93 | { | |
357f4c81 | 94 | static gboolean wx_on_channel_event(GIOChannel *channel, |
5cd99866 VZ |
95 | GIOCondition condition, |
96 | gpointer data) | |
6b8ef0b3 | 97 | { |
357f4c81 VZ |
98 | wxUnusedVar(channel); // Unused if !wxUSE_LOG || !wxDEBUG_LEVEL |
99 | ||
5cd99866 VZ |
100 | wxLogTrace(wxTRACE_EVT_SOURCE, |
101 | "wx_on_channel_event, fd=%d, condition=%08x", | |
102 | g_io_channel_unix_get_fd(channel), condition); | |
6b8ef0b3 | 103 | |
5cd99866 VZ |
104 | wxEventLoopSourceHandler * const |
105 | handler = static_cast<wxEventLoopSourceHandler *>(data); | |
6b8ef0b3 VZ |
106 | |
107 | if (condition & G_IO_IN || condition & G_IO_PRI) | |
6b8ef0b3 | 108 | handler->OnReadWaiting(); |
5cd99866 | 109 | if (condition & G_IO_OUT) |
6b8ef0b3 | 110 | handler->OnWriteWaiting(); |
6b8ef0b3 | 111 | else if (condition & G_IO_ERR || condition & G_IO_NVAL) |
6b8ef0b3 | 112 | handler->OnExceptionWaiting(); |
6b8ef0b3 VZ |
113 | |
114 | // we never want to remove source here, so always return true | |
115 | return TRUE; | |
116 | } | |
117 | } | |
118 | ||
5cd99866 VZ |
119 | wxEventLoopSource * |
120 | wxGUIEventLoop::AddSourceForFD(int fd, | |
121 | wxEventLoopSourceHandler *handler, | |
122 | int flags) | |
6b8ef0b3 | 123 | { |
5cd99866 | 124 | wxCHECK_MSG( fd != -1, NULL, "can't monitor invalid fd" ); |
6b8ef0b3 | 125 | |
6b8ef0b3 VZ |
126 | int condition = 0; |
127 | if (flags & wxEVENT_SOURCE_INPUT) | |
128 | condition |= G_IO_IN | G_IO_PRI; | |
129 | if (flags & wxEVENT_SOURCE_OUTPUT) | |
130 | condition |= G_IO_OUT; | |
131 | if (flags & wxEVENT_SOURCE_EXCEPTION) | |
132 | condition |= G_IO_ERR | G_IO_HUP | G_IO_NVAL; | |
133 | ||
5cd99866 VZ |
134 | GIOChannel* channel = g_io_channel_unix_new(fd); |
135 | const unsigned sourceId = g_io_add_watch | |
136 | ( | |
137 | channel, | |
138 | (GIOCondition)condition, | |
139 | &wx_on_channel_event, | |
140 | handler | |
141 | ); | |
142 | // it was ref'd by g_io_add_watch() so we can unref it here | |
6b8ef0b3 VZ |
143 | g_io_channel_unref(channel); |
144 | ||
5cd99866 VZ |
145 | if ( !sourceId ) |
146 | return NULL; | |
147 | ||
148 | wxLogTrace(wxTRACE_EVT_SOURCE, | |
149 | "Adding event loop source for fd=%d with GTK id=%u", | |
150 | fd, sourceId); | |
151 | ||
152 | ||
153 | return new wxGTKEventLoopSource(sourceId, handler, flags); | |
6b8ef0b3 VZ |
154 | } |
155 | ||
5cd99866 | 156 | wxGTKEventLoopSource::~wxGTKEventLoopSource() |
6b8ef0b3 | 157 | { |
6b8ef0b3 | 158 | wxLogTrace(wxTRACE_EVT_SOURCE, |
5cd99866 | 159 | "Removing event loop source with GTK id=%u", m_sourceId); |
6b8ef0b3 | 160 | |
5cd99866 | 161 | g_source_remove(m_sourceId); |
6b8ef0b3 VZ |
162 | } |
163 | ||
5cd99866 VZ |
164 | #endif // wxUSE_EVENTLOOP_SOURCE |
165 | ||
8000ae7f VZ |
166 | // ---------------------------------------------------------------------------- |
167 | // wxEventLoop message processing dispatching | |
168 | // ---------------------------------------------------------------------------- | |
169 | ||
b46b1d59 | 170 | bool wxGUIEventLoop::Pending() const |
8000ae7f | 171 | { |
564c7fc4 VZ |
172 | if ( wxTheApp ) |
173 | { | |
174 | // this avoids false positives from our idle source | |
175 | return wxTheApp->EventsPending(); | |
176 | } | |
177 | ||
178 | return gtk_events_pending() != 0; | |
8000ae7f VZ |
179 | } |
180 | ||
b46b1d59 | 181 | bool wxGUIEventLoop::Dispatch() |
8000ae7f | 182 | { |
9a83f860 | 183 | wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") ); |
8000ae7f | 184 | |
4b8af4ac VZ |
185 | // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called |
186 | return !gtk_main_iteration(); | |
8000ae7f | 187 | } |
564c7fc4 VZ |
188 | |
189 | extern "C" { | |
190 | static gboolean wx_event_loop_timeout(void* data) | |
191 | { | |
192 | bool* expired = static_cast<bool*>(data); | |
193 | *expired = true; | |
194 | ||
195 | // return FALSE to remove this timeout | |
196 | return FALSE; | |
197 | } | |
198 | } | |
199 | ||
200 | int wxGUIEventLoop::DispatchTimeout(unsigned long timeout) | |
201 | { | |
202 | bool expired = false; | |
203 | const unsigned id = g_timeout_add(timeout, wx_event_loop_timeout, &expired); | |
204 | bool quit = gtk_main_iteration() != 0; | |
205 | ||
206 | if ( expired ) | |
207 | return -1; | |
208 | ||
209 | g_source_remove(id); | |
210 | ||
211 | return !quit; | |
212 | } | |
213 | ||
dde19c21 FM |
214 | //----------------------------------------------------------------------------- |
215 | // YieldFor | |
216 | //----------------------------------------------------------------------------- | |
217 | ||
f629f37a PC |
218 | extern "C" { |
219 | static void wxgtk_main_do_event(GdkEvent* event, void* data) | |
387e72ba FM |
220 | { |
221 | // categorize the GDK event according to wxEventCategory. | |
222 | // See http://library.gnome.org/devel/gdk/unstable/gdk-Events.html#GdkEventType | |
223 | // for more info. | |
03647350 | 224 | |
3bb5029d FM |
225 | // NOTE: GDK_* constants which were not present in the GDK2.0 can be tested for |
226 | // only at compile-time; when running the program (compiled with a recent GDK) | |
03647350 VZ |
227 | // on a system with an older GDK lib we can be sure there won't be problems |
228 | // because event->type will never assume those values corresponding to | |
3bb5029d FM |
229 | // new event types (since new event types are always added in GDK with non |
230 | // conflicting values for ABI compatibility). | |
387e72ba | 231 | |
5e4bbd0f VZ |
232 | // Some events (currently only a single one) may be used for more than one |
233 | // category, so we need 2 variables. The second one will remain "unknown" | |
234 | // in most cases. | |
235 | wxEventCategory cat = wxEVT_CATEGORY_UNKNOWN, | |
236 | cat2 = wxEVT_CATEGORY_UNKNOWN; | |
387e72ba FM |
237 | switch (event->type) |
238 | { | |
239 | case GDK_SELECTION_REQUEST: | |
240 | case GDK_SELECTION_NOTIFY: | |
241 | case GDK_SELECTION_CLEAR: | |
3bb5029d | 242 | #if GTK_CHECK_VERSION(2,6,0) |
387e72ba | 243 | case GDK_OWNER_CHANGE: |
3bb5029d | 244 | #endif |
387e72ba FM |
245 | cat = wxEVT_CATEGORY_CLIPBOARD; |
246 | break; | |
247 | ||
387e72ba FM |
248 | case GDK_KEY_PRESS: |
249 | case GDK_KEY_RELEASE: | |
250 | case GDK_BUTTON_PRESS: | |
251 | case GDK_2BUTTON_PRESS: | |
252 | case GDK_3BUTTON_PRESS: | |
253 | case GDK_BUTTON_RELEASE: | |
254 | case GDK_SCROLL: // generated from mouse buttons | |
255 | case GDK_CLIENT_EVENT: | |
256 | cat = wxEVT_CATEGORY_USER_INPUT; | |
257 | break; | |
258 | ||
5e4bbd0f VZ |
259 | case GDK_PROPERTY_NOTIFY: |
260 | // This one is special: it can be used for UI purposes but also for | |
261 | // clipboard operations, so allow it in both cases (we probably could | |
262 | // examine the event itself to distinguish between the two cases but | |
263 | // this would be unnecessarily complicated). | |
264 | cat2 = wxEVT_CATEGORY_CLIPBOARD; | |
265 | // Fall through. | |
266 | ||
387e72ba FM |
267 | case GDK_PROXIMITY_IN: |
268 | case GDK_PROXIMITY_OUT: | |
269 | ||
270 | case GDK_MOTION_NOTIFY: | |
271 | case GDK_ENTER_NOTIFY: | |
272 | case GDK_LEAVE_NOTIFY: | |
273 | case GDK_VISIBILITY_NOTIFY: | |
387e72ba FM |
274 | |
275 | case GDK_FOCUS_CHANGE: | |
276 | case GDK_CONFIGURE: | |
277 | case GDK_WINDOW_STATE: | |
278 | case GDK_SETTING: | |
279 | case GDK_DELETE: | |
280 | case GDK_DESTROY: | |
281 | ||
282 | case GDK_EXPOSE: | |
9dc44eff | 283 | #ifndef __WXGTK3__ |
387e72ba | 284 | case GDK_NO_EXPOSE: |
9dc44eff | 285 | #endif |
387e72ba FM |
286 | case GDK_MAP: |
287 | case GDK_UNMAP: | |
288 | ||
289 | case GDK_DRAG_ENTER: | |
290 | case GDK_DRAG_LEAVE: | |
291 | case GDK_DRAG_MOTION: | |
292 | case GDK_DRAG_STATUS: | |
293 | case GDK_DROP_START: | |
294 | case GDK_DROP_FINISHED: | |
3bb5029d | 295 | #if GTK_CHECK_VERSION(2,8,0) |
387e72ba | 296 | case GDK_GRAB_BROKEN: |
3bb5029d FM |
297 | #endif |
298 | #if GTK_CHECK_VERSION(2,14,0) | |
299 | case GDK_DAMAGE: | |
300 | #endif | |
387e72ba FM |
301 | cat = wxEVT_CATEGORY_UI; |
302 | break; | |
303 | ||
304 | default: | |
305 | cat = wxEVT_CATEGORY_UNKNOWN; | |
306 | break; | |
307 | } | |
308 | ||
f629f37a PC |
309 | wxGUIEventLoop* evtloop = static_cast<wxGUIEventLoop*>(data); |
310 | ||
387e72ba | 311 | // is this event allowed now? |
5e4bbd0f VZ |
312 | if (evtloop->IsEventAllowedInsideYield(cat) || |
313 | (cat2 != wxEVT_CATEGORY_UNKNOWN && | |
314 | evtloop->IsEventAllowedInsideYield(cat2))) | |
315 | { | |
316 | // process it now | |
317 | gtk_main_do_event(event); | |
318 | } | |
387e72ba | 319 | else if (event->type != GDK_NOTHING) |
5e4bbd0f VZ |
320 | { |
321 | // process it later (but make a copy; the caller will free the event | |
322 | // pointer) | |
387e72ba | 323 | evtloop->StoreGdkEventForLaterProcessing(gdk_event_copy(event)); |
5e4bbd0f | 324 | } |
387e72ba | 325 | } |
f629f37a | 326 | } |
387e72ba | 327 | |
dde19c21 FM |
328 | bool wxGUIEventLoop::YieldFor(long eventsToProcess) |
329 | { | |
330 | #if wxUSE_THREADS | |
331 | if ( !wxThread::IsMain() ) | |
332 | { | |
333 | // can't call gtk_main_iteration() from other threads like this | |
334 | return true; | |
335 | } | |
336 | #endif // wxUSE_THREADS | |
337 | ||
338 | m_isInsideYield = true; | |
339 | m_eventsToProcessInsideYield = eventsToProcess; | |
340 | ||
341 | #if wxUSE_LOG | |
342 | // disable log flushing from here because a call to wxYield() shouldn't | |
343 | // normally result in message boxes popping up &c | |
344 | wxLog::Suspend(); | |
345 | #endif | |
346 | ||
387e72ba FM |
347 | // temporarily replace the global GDK event handler with our function, which |
348 | // categorizes the events and using m_eventsToProcessInsideYield decides | |
349 | // if an event should be processed immediately or not | |
350 | // NOTE: this approach is better than using gdk_display_get_event() because | |
351 | // gtk_main_iteration() does more than just calling gdk_display_get_event() | |
352 | // and then call gtk_main_do_event()! | |
353 | // In particular in this way we also process input from sources like | |
354 | // GIOChannels (this is needed for e.g. wxGUIAppTraits::WaitForChild). | |
f629f37a | 355 | gdk_event_handler_set(wxgtk_main_do_event, this, NULL); |
387e72ba FM |
356 | while (Pending()) // avoid false positives from our idle source |
357 | gtk_main_iteration(); | |
358 | gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL); | |
564c7fc4 | 359 | |
dde19c21 FM |
360 | if (eventsToProcess != wxEVT_CATEGORY_CLIPBOARD) |
361 | { | |
362 | // It's necessary to call ProcessIdle() to update the frames sizes which | |
363 | // might have been changed (it also will update other things set from | |
364 | // OnUpdateUI() which is a nice (and desired) side effect). But we | |
365 | // call ProcessIdle() only once since this is not meant for longish | |
366 | // background jobs (controlled by wxIdleEvent::RequestMore() and the | |
367 | // return value of Processidle(). | |
368 | ProcessIdle(); // ProcessIdle() also calls ProcessPendingEvents() | |
369 | } | |
370 | //else: if we are inside ~wxClipboardSync() and we call ProcessIdle() and | |
371 | // the user app contains an UI update handler which calls wxClipboard::IsSupported, | |
372 | // then we fall into a never-ending loop... | |
373 | ||
374 | // put all unprocessed GDK events back in the queue | |
387e72ba | 375 | GdkDisplay* disp = gtk_widget_get_display(wxGetRootWindow()); |
dde19c21 FM |
376 | for (size_t i=0; i<m_arrGdkEvents.GetCount(); i++) |
377 | { | |
378 | GdkEvent* ev = (GdkEvent*)m_arrGdkEvents[i]; | |
379 | ||
380 | // NOTE: gdk_display_put_event makes a copy of the event passed to it | |
381 | gdk_display_put_event(disp, ev); | |
382 | gdk_event_free(ev); | |
383 | } | |
384 | ||
385 | m_arrGdkEvents.Clear(); | |
386 | ||
387 | #if wxUSE_LOG | |
388 | // let the logs be flashed again | |
389 | wxLog::Resume(); | |
390 | #endif | |
391 | ||
392 | m_isInsideYield = false; | |
393 | ||
394 | return true; | |
395 | } |