]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/evtloop.cpp
Don't exclude a bunch of wxDir methods when wxUSE_LONGLONG==0.
[wxWidgets.git] / src / gtk / evtloop.cpp
CommitLineData
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
42extern GtkWidget *wxGetRootWindow();
43
8000ae7f
VZ
44// ----------------------------------------------------------------------------
45// wxEventLoop running and exiting
46// ----------------------------------------------------------------------------
47
564c7fc4 48wxGUIEventLoop::wxGUIEventLoop()
8000ae7f 49{
564c7fc4 50 m_exitcode = 0;
8000ae7f
VZ
51}
52
b46b1d59 53int 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 67void 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
76void 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
92extern "C"
93{
f675b4f5 94static gboolean wx_on_channel_event(GIOChannel * WXUNUSED_UNLESS_DEBUG(channel),
5cd99866
VZ
95 GIOCondition condition,
96 gpointer data)
6b8ef0b3 97{
5cd99866
VZ
98 wxLogTrace(wxTRACE_EVT_SOURCE,
99 "wx_on_channel_event, fd=%d, condition=%08x",
100 g_io_channel_unix_get_fd(channel), condition);
6b8ef0b3 101
5cd99866
VZ
102 wxEventLoopSourceHandler * const
103 handler = static_cast<wxEventLoopSourceHandler *>(data);
6b8ef0b3
VZ
104
105 if (condition & G_IO_IN || condition & G_IO_PRI)
6b8ef0b3 106 handler->OnReadWaiting();
5cd99866 107 if (condition & G_IO_OUT)
6b8ef0b3 108 handler->OnWriteWaiting();
6b8ef0b3 109 else if (condition & G_IO_ERR || condition & G_IO_NVAL)
6b8ef0b3 110 handler->OnExceptionWaiting();
6b8ef0b3
VZ
111
112 // we never want to remove source here, so always return true
113 return TRUE;
114}
115}
116
5cd99866
VZ
117wxEventLoopSource *
118wxGUIEventLoop::AddSourceForFD(int fd,
119 wxEventLoopSourceHandler *handler,
120 int flags)
6b8ef0b3 121{
5cd99866 122 wxCHECK_MSG( fd != -1, NULL, "can't monitor invalid fd" );
6b8ef0b3 123
6b8ef0b3
VZ
124 int condition = 0;
125 if (flags & wxEVENT_SOURCE_INPUT)
126 condition |= G_IO_IN | G_IO_PRI;
127 if (flags & wxEVENT_SOURCE_OUTPUT)
128 condition |= G_IO_OUT;
129 if (flags & wxEVENT_SOURCE_EXCEPTION)
130 condition |= G_IO_ERR | G_IO_HUP | G_IO_NVAL;
131
5cd99866
VZ
132 GIOChannel* channel = g_io_channel_unix_new(fd);
133 const unsigned sourceId = g_io_add_watch
134 (
135 channel,
136 (GIOCondition)condition,
137 &wx_on_channel_event,
138 handler
139 );
140 // it was ref'd by g_io_add_watch() so we can unref it here
6b8ef0b3
VZ
141 g_io_channel_unref(channel);
142
5cd99866
VZ
143 if ( !sourceId )
144 return NULL;
145
146 wxLogTrace(wxTRACE_EVT_SOURCE,
147 "Adding event loop source for fd=%d with GTK id=%u",
148 fd, sourceId);
149
150
151 return new wxGTKEventLoopSource(sourceId, handler, flags);
6b8ef0b3
VZ
152}
153
5cd99866 154wxGTKEventLoopSource::~wxGTKEventLoopSource()
6b8ef0b3 155{
6b8ef0b3 156 wxLogTrace(wxTRACE_EVT_SOURCE,
5cd99866 157 "Removing event loop source with GTK id=%u", m_sourceId);
6b8ef0b3 158
5cd99866 159 g_source_remove(m_sourceId);
6b8ef0b3
VZ
160}
161
5cd99866
VZ
162#endif // wxUSE_EVENTLOOP_SOURCE
163
8000ae7f
VZ
164// ----------------------------------------------------------------------------
165// wxEventLoop message processing dispatching
166// ----------------------------------------------------------------------------
167
b46b1d59 168bool wxGUIEventLoop::Pending() const
8000ae7f 169{
564c7fc4
VZ
170 if ( wxTheApp )
171 {
172 // this avoids false positives from our idle source
173 return wxTheApp->EventsPending();
174 }
175
176 return gtk_events_pending() != 0;
8000ae7f
VZ
177}
178
b46b1d59 179bool wxGUIEventLoop::Dispatch()
8000ae7f 180{
9a83f860 181 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
8000ae7f 182
4b8af4ac
VZ
183 // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called
184 return !gtk_main_iteration();
8000ae7f 185}
564c7fc4
VZ
186
187extern "C" {
188static gboolean wx_event_loop_timeout(void* data)
189{
190 bool* expired = static_cast<bool*>(data);
191 *expired = true;
192
193 // return FALSE to remove this timeout
194 return FALSE;
195}
196}
197
198int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
199{
200 bool expired = false;
201 const unsigned id = g_timeout_add(timeout, wx_event_loop_timeout, &expired);
202 bool quit = gtk_main_iteration() != 0;
203
204 if ( expired )
205 return -1;
206
207 g_source_remove(id);
208
209 return !quit;
210}
211
dde19c21
FM
212//-----------------------------------------------------------------------------
213// YieldFor
214//-----------------------------------------------------------------------------
215
f629f37a
PC
216extern "C" {
217static void wxgtk_main_do_event(GdkEvent* event, void* data)
387e72ba
FM
218{
219 // categorize the GDK event according to wxEventCategory.
220 // See http://library.gnome.org/devel/gdk/unstable/gdk-Events.html#GdkEventType
221 // for more info.
03647350 222
3bb5029d
FM
223 // NOTE: GDK_* constants which were not present in the GDK2.0 can be tested for
224 // only at compile-time; when running the program (compiled with a recent GDK)
03647350
VZ
225 // on a system with an older GDK lib we can be sure there won't be problems
226 // because event->type will never assume those values corresponding to
3bb5029d
FM
227 // new event types (since new event types are always added in GDK with non
228 // conflicting values for ABI compatibility).
387e72ba
FM
229
230 wxEventCategory cat = wxEVT_CATEGORY_UNKNOWN;
231 switch (event->type)
232 {
233 case GDK_SELECTION_REQUEST:
234 case GDK_SELECTION_NOTIFY:
235 case GDK_SELECTION_CLEAR:
3bb5029d 236#if GTK_CHECK_VERSION(2,6,0)
387e72ba 237 case GDK_OWNER_CHANGE:
3bb5029d 238#endif
387e72ba
FM
239 cat = wxEVT_CATEGORY_CLIPBOARD;
240 break;
241
387e72ba
FM
242 case GDK_KEY_PRESS:
243 case GDK_KEY_RELEASE:
244 case GDK_BUTTON_PRESS:
245 case GDK_2BUTTON_PRESS:
246 case GDK_3BUTTON_PRESS:
247 case GDK_BUTTON_RELEASE:
248 case GDK_SCROLL: // generated from mouse buttons
249 case GDK_CLIENT_EVENT:
250 cat = wxEVT_CATEGORY_USER_INPUT;
251 break;
252
253 case GDK_PROXIMITY_IN:
254 case GDK_PROXIMITY_OUT:
255
256 case GDK_MOTION_NOTIFY:
257 case GDK_ENTER_NOTIFY:
258 case GDK_LEAVE_NOTIFY:
259 case GDK_VISIBILITY_NOTIFY:
260 case GDK_PROPERTY_NOTIFY:
261
262 case GDK_FOCUS_CHANGE:
263 case GDK_CONFIGURE:
264 case GDK_WINDOW_STATE:
265 case GDK_SETTING:
266 case GDK_DELETE:
267 case GDK_DESTROY:
268
269 case GDK_EXPOSE:
270 case GDK_NO_EXPOSE:
271 case GDK_MAP:
272 case GDK_UNMAP:
273
274 case GDK_DRAG_ENTER:
275 case GDK_DRAG_LEAVE:
276 case GDK_DRAG_MOTION:
277 case GDK_DRAG_STATUS:
278 case GDK_DROP_START:
279 case GDK_DROP_FINISHED:
3bb5029d 280#if GTK_CHECK_VERSION(2,8,0)
387e72ba 281 case GDK_GRAB_BROKEN:
3bb5029d
FM
282#endif
283#if GTK_CHECK_VERSION(2,14,0)
284 case GDK_DAMAGE:
285#endif
387e72ba
FM
286 cat = wxEVT_CATEGORY_UI;
287 break;
288
289 default:
290 cat = wxEVT_CATEGORY_UNKNOWN;
291 break;
292 }
293
f629f37a
PC
294 wxGUIEventLoop* evtloop = static_cast<wxGUIEventLoop*>(data);
295
387e72ba
FM
296 // is this event allowed now?
297 if (evtloop->IsEventAllowedInsideYield(cat))
298 gtk_main_do_event(event); // process it now
299 else if (event->type != GDK_NOTHING)
300 evtloop->StoreGdkEventForLaterProcessing(gdk_event_copy(event));
301 // process it later (but make a copy; the caller will free the event pointer)
302}
f629f37a 303}
387e72ba 304
dde19c21
FM
305bool wxGUIEventLoop::YieldFor(long eventsToProcess)
306{
307#if wxUSE_THREADS
308 if ( !wxThread::IsMain() )
309 {
310 // can't call gtk_main_iteration() from other threads like this
311 return true;
312 }
313#endif // wxUSE_THREADS
314
315 m_isInsideYield = true;
316 m_eventsToProcessInsideYield = eventsToProcess;
317
318#if wxUSE_LOG
319 // disable log flushing from here because a call to wxYield() shouldn't
320 // normally result in message boxes popping up &c
321 wxLog::Suspend();
322#endif
323
387e72ba
FM
324 // temporarily replace the global GDK event handler with our function, which
325 // categorizes the events and using m_eventsToProcessInsideYield decides
326 // if an event should be processed immediately or not
327 // NOTE: this approach is better than using gdk_display_get_event() because
328 // gtk_main_iteration() does more than just calling gdk_display_get_event()
329 // and then call gtk_main_do_event()!
330 // In particular in this way we also process input from sources like
331 // GIOChannels (this is needed for e.g. wxGUIAppTraits::WaitForChild).
f629f37a 332 gdk_event_handler_set(wxgtk_main_do_event, this, NULL);
387e72ba
FM
333 while (Pending()) // avoid false positives from our idle source
334 gtk_main_iteration();
335 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
564c7fc4 336
dde19c21
FM
337 if (eventsToProcess != wxEVT_CATEGORY_CLIPBOARD)
338 {
339 // It's necessary to call ProcessIdle() to update the frames sizes which
340 // might have been changed (it also will update other things set from
341 // OnUpdateUI() which is a nice (and desired) side effect). But we
342 // call ProcessIdle() only once since this is not meant for longish
343 // background jobs (controlled by wxIdleEvent::RequestMore() and the
344 // return value of Processidle().
345 ProcessIdle(); // ProcessIdle() also calls ProcessPendingEvents()
346 }
347 //else: if we are inside ~wxClipboardSync() and we call ProcessIdle() and
348 // the user app contains an UI update handler which calls wxClipboard::IsSupported,
349 // then we fall into a never-ending loop...
350
351 // put all unprocessed GDK events back in the queue
387e72ba 352 GdkDisplay* disp = gtk_widget_get_display(wxGetRootWindow());
dde19c21
FM
353 for (size_t i=0; i<m_arrGdkEvents.GetCount(); i++)
354 {
355 GdkEvent* ev = (GdkEvent*)m_arrGdkEvents[i];
356
357 // NOTE: gdk_display_put_event makes a copy of the event passed to it
358 gdk_display_put_event(disp, ev);
359 gdk_event_free(ev);
360 }
361
362 m_arrGdkEvents.Clear();
363
364#if wxUSE_LOG
365 // let the logs be flashed again
366 wxLog::Resume();
367#endif
368
369 m_isInsideYield = false;
370
371 return true;
372}