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