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