1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/evtloop.cpp
3 // Purpose: implements wxEventLoop for GTK+
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/evtloop.h"
28 #include "wx/evtloopsrc.h"
38 // ============================================================================
39 // wxEventLoop implementation
40 // ============================================================================
42 extern GtkWidget
*wxGetRootWindow();
44 // ----------------------------------------------------------------------------
45 // wxEventLoop running and exiting
46 // ----------------------------------------------------------------------------
48 wxGUIEventLoop::wxGUIEventLoop()
53 int wxGUIEventLoop::Run()
55 // event loops are not recursive, you need to create another loop!
56 wxCHECK_MSG( !IsRunning(), -1, "can't reenter a message loop" );
58 wxEventLoopActivator
activate(this);
67 void wxGUIEventLoop::Exit(int rc
)
69 wxCHECK_RET( IsRunning(), "can't call Exit() if not running" );
76 void wxGUIEventLoop::WakeUp()
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...
83 wxTheApp
->WakeUpIdle();
86 // ----------------------------------------------------------------------------
87 // wxEventLoop adding & removing sources
88 // ----------------------------------------------------------------------------
90 #if wxUSE_EVENTLOOP_SOURCE
94 static gboolean
wx_on_channel_event(GIOChannel
*channel
,
95 GIOCondition condition
,
98 wxUnusedVar(channel
); // Unused if !wxUSE_LOG || !wxDEBUG_LEVEL
100 wxLogTrace(wxTRACE_EVT_SOURCE
,
101 "wx_on_channel_event, fd=%d, condition=%08x",
102 g_io_channel_unix_get_fd(channel
), condition
);
104 wxEventLoopSourceHandler
* const
105 handler
= static_cast<wxEventLoopSourceHandler
*>(data
);
107 if (condition
& G_IO_IN
|| condition
& G_IO_PRI
)
108 handler
->OnReadWaiting();
109 if (condition
& G_IO_OUT
)
110 handler
->OnWriteWaiting();
111 else if (condition
& G_IO_ERR
|| condition
& G_IO_NVAL
)
112 handler
->OnExceptionWaiting();
114 // we never want to remove source here, so always return true
120 wxGUIEventLoop::AddSourceForFD(int fd
,
121 wxEventLoopSourceHandler
*handler
,
124 wxCHECK_MSG( fd
!= -1, NULL
, "can't monitor invalid fd" );
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
;
134 GIOChannel
* channel
= g_io_channel_unix_new(fd
);
135 const unsigned sourceId
= g_io_add_watch
138 (GIOCondition
)condition
,
139 &wx_on_channel_event
,
142 // it was ref'd by g_io_add_watch() so we can unref it here
143 g_io_channel_unref(channel
);
148 wxLogTrace(wxTRACE_EVT_SOURCE
,
149 "Adding event loop source for fd=%d with GTK id=%u",
153 return new wxGTKEventLoopSource(sourceId
, handler
, flags
);
156 wxGTKEventLoopSource::~wxGTKEventLoopSource()
158 wxLogTrace(wxTRACE_EVT_SOURCE
,
159 "Removing event loop source with GTK id=%u", m_sourceId
);
161 g_source_remove(m_sourceId
);
164 #endif // wxUSE_EVENTLOOP_SOURCE
166 // ----------------------------------------------------------------------------
167 // wxEventLoop message processing dispatching
168 // ----------------------------------------------------------------------------
170 bool wxGUIEventLoop::Pending() const
174 // this avoids false positives from our idle source
175 return wxTheApp
->EventsPending();
178 return gtk_events_pending() != 0;
181 bool wxGUIEventLoop::Dispatch()
183 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
185 // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called
186 return !gtk_main_iteration();
190 static gboolean
wx_event_loop_timeout(void* data
)
192 bool* expired
= static_cast<bool*>(data
);
195 // return FALSE to remove this timeout
200 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout
)
202 bool expired
= false;
203 const unsigned id
= g_timeout_add(timeout
, wx_event_loop_timeout
, &expired
);
204 bool quit
= gtk_main_iteration() != 0;
214 //-----------------------------------------------------------------------------
216 //-----------------------------------------------------------------------------
219 static void wxgtk_main_do_event(GdkEvent
* event
, void* data
)
221 // categorize the GDK event according to wxEventCategory.
222 // See http://library.gnome.org/devel/gdk/unstable/gdk-Events.html#GdkEventType
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)
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
229 // new event types (since new event types are always added in GDK with non
230 // conflicting values for ABI compatibility).
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"
235 wxEventCategory cat
= wxEVT_CATEGORY_UNKNOWN
,
236 cat2
= wxEVT_CATEGORY_UNKNOWN
;
239 case GDK_SELECTION_REQUEST
:
240 case GDK_SELECTION_NOTIFY
:
241 case GDK_SELECTION_CLEAR
:
242 case GDK_OWNER_CHANGE
:
243 cat
= wxEVT_CATEGORY_CLIPBOARD
;
247 case GDK_KEY_RELEASE
:
248 case GDK_BUTTON_PRESS
:
249 case GDK_2BUTTON_PRESS
:
250 case GDK_3BUTTON_PRESS
:
251 case GDK_BUTTON_RELEASE
:
252 case GDK_SCROLL
: // generated from mouse buttons
253 case GDK_CLIENT_EVENT
:
254 cat
= wxEVT_CATEGORY_USER_INPUT
;
257 case GDK_PROPERTY_NOTIFY
:
258 // This one is special: it can be used for UI purposes but also for
259 // clipboard operations, so allow it in both cases (we probably could
260 // examine the event itself to distinguish between the two cases but
261 // this would be unnecessarily complicated).
262 cat2
= wxEVT_CATEGORY_CLIPBOARD
;
265 case GDK_PROXIMITY_IN
:
266 case GDK_PROXIMITY_OUT
:
268 case GDK_MOTION_NOTIFY
:
269 case GDK_ENTER_NOTIFY
:
270 case GDK_LEAVE_NOTIFY
:
271 case GDK_VISIBILITY_NOTIFY
:
273 case GDK_FOCUS_CHANGE
:
275 case GDK_WINDOW_STATE
:
289 case GDK_DRAG_MOTION
:
290 case GDK_DRAG_STATUS
:
292 case GDK_DROP_FINISHED
:
293 #if GTK_CHECK_VERSION(2,8,0)
294 case GDK_GRAB_BROKEN
:
296 #if GTK_CHECK_VERSION(2,14,0)
299 cat
= wxEVT_CATEGORY_UI
;
303 cat
= wxEVT_CATEGORY_UNKNOWN
;
307 wxGUIEventLoop
* evtloop
= static_cast<wxGUIEventLoop
*>(data
);
309 // is this event allowed now?
310 if (evtloop
->IsEventAllowedInsideYield(cat
) ||
311 (cat2
!= wxEVT_CATEGORY_UNKNOWN
&&
312 evtloop
->IsEventAllowedInsideYield(cat2
)))
315 gtk_main_do_event(event
);
317 else if (event
->type
!= GDK_NOTHING
)
319 // process it later (but make a copy; the caller will free the event
321 evtloop
->StoreGdkEventForLaterProcessing(gdk_event_copy(event
));
326 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
329 if ( !wxThread::IsMain() )
331 // can't call gtk_main_iteration() from other threads like this
334 #endif // wxUSE_THREADS
336 m_isInsideYield
= true;
337 m_eventsToProcessInsideYield
= eventsToProcess
;
340 // disable log flushing from here because a call to wxYield() shouldn't
341 // normally result in message boxes popping up &c
345 // temporarily replace the global GDK event handler with our function, which
346 // categorizes the events and using m_eventsToProcessInsideYield decides
347 // if an event should be processed immediately or not
348 // NOTE: this approach is better than using gdk_display_get_event() because
349 // gtk_main_iteration() does more than just calling gdk_display_get_event()
350 // and then call gtk_main_do_event()!
351 // In particular in this way we also process input from sources like
352 // GIOChannels (this is needed for e.g. wxGUIAppTraits::WaitForChild).
353 gdk_event_handler_set(wxgtk_main_do_event
, this, NULL
);
354 while (Pending()) // avoid false positives from our idle source
355 gtk_main_iteration();
356 gdk_event_handler_set ((GdkEventFunc
)gtk_main_do_event
, NULL
, NULL
);
358 // Process all pending events too, this is consistent with wxMSW behaviour
359 // and the behaviour of wxGTK itself in the previous versions.
361 wxTheApp
->ProcessPendingEvents();
363 if (eventsToProcess
!= wxEVT_CATEGORY_CLIPBOARD
)
365 // It's necessary to call ProcessIdle() to update the frames sizes which
366 // might have been changed (it also will update other things set from
367 // OnUpdateUI() which is a nice (and desired) side effect). But we
368 // call ProcessIdle() only once since this is not meant for longish
369 // background jobs (controlled by wxIdleEvent::RequestMore() and the
370 // return value of Processidle().
373 //else: if we are inside ~wxClipboardSync() and we call ProcessIdle() and
374 // the user app contains an UI update handler which calls wxClipboard::IsSupported,
375 // then we fall into a never-ending loop...
377 // put all unprocessed GDK events back in the queue
378 GdkDisplay
* disp
= gtk_widget_get_display(wxGetRootWindow());
379 for (size_t i
=0; i
<m_arrGdkEvents
.GetCount(); i
++)
381 GdkEvent
* ev
= (GdkEvent
*)m_arrGdkEvents
[i
];
383 // NOTE: gdk_display_put_event makes a copy of the event passed to it
384 gdk_display_put_event(disp
, ev
);
388 m_arrGdkEvents
.Clear();
391 // let the logs be flashed again
395 m_isInsideYield
= false;