]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/evtloop.cpp
9d43d56d9e8030c56b96b568f607ec3bd1e9c3f4
[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 // License: 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
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #endif // WX_PRECOMP
33
34 #include <gtk/gtk.h>
35 #include <glib.h>
36
37 // ============================================================================
38 // wxEventLoop implementation
39 // ============================================================================
40
41 extern GtkWidget *wxGetRootWindow();
42
43 // ----------------------------------------------------------------------------
44 // wxEventLoop running and exiting
45 // ----------------------------------------------------------------------------
46
47 wxGUIEventLoop::wxGUIEventLoop()
48 {
49 m_exitcode = 0;
50 }
51
52 int wxGUIEventLoop::Run()
53 {
54 // event loops are not recursive, you need to create another loop!
55 wxCHECK_MSG( !IsRunning(), -1, "can't reenter a message loop" );
56
57 wxEventLoopActivator activate(this);
58
59 gtk_main();
60
61 OnExit();
62
63 return m_exitcode;
64 }
65
66 void wxGUIEventLoop::Exit(int rc)
67 {
68 wxCHECK_RET( IsRunning(), "can't call Exit() if not running" );
69
70 m_exitcode = rc;
71
72 gtk_main_quit();
73 }
74
75 void wxGUIEventLoop::WakeUp()
76 {
77 // TODO: idle events handling should really be done by wxEventLoop itself
78 // but for now it's completely in gtk/app.cpp so just call there when
79 // we have wxTheApp and hope that it doesn't matter that we do
80 // nothing when we don't...
81 if ( wxTheApp )
82 wxTheApp->WakeUpIdle();
83 }
84
85 // ----------------------------------------------------------------------------
86 // wxEventLoop adding & removing sources
87 // ----------------------------------------------------------------------------
88
89 extern "C"
90 {
91 static gboolean wx_on_channel_event(GIOChannel *channel,
92 GIOCondition condition, gpointer data)
93 {
94 wxLogTrace(wxTRACE_EVT_SOURCE, "wx_on_channel_event, gtk_source_id=%d",
95 g_io_channel_unix_get_fd(channel));
96
97 wxEventLoopSourceHandler* handler =
98 static_cast<wxEventLoopSourceHandler*>(data);
99
100 if (condition & G_IO_IN || condition & G_IO_PRI)
101 {
102 handler->OnReadWaiting();
103 }
104 else if (condition & G_IO_OUT)
105 {
106 handler->OnWriteWaiting();
107 }
108 else if (condition & G_IO_ERR || condition & G_IO_NVAL)
109 {
110 handler->OnExceptionWaiting();
111 }
112 else
113 {
114 wxFAIL_MSG(wxString::Format("Inavlid condition=%d", condition));
115 }
116
117 // we never want to remove source here, so always return true
118 return TRUE;
119 }
120 }
121
122 bool wxGUIEventLoop::DoAddSource(wxAbstractEventLoopSource* src)
123 {
124 Source* source = dynamic_cast<Source*>(src);
125 wxCHECK_MSG( source, false, "Invalid source type" );
126
127 wxLogTrace(wxTRACE_EVT_SOURCE,
128 "wxGUIEventLoop::DoAddSource() source=%d",
129 source->GetResource());
130
131 int flags = source->GetFlags();
132 int condition = 0;
133 if (flags & wxEVENT_SOURCE_INPUT)
134 condition |= G_IO_IN | G_IO_PRI;
135 if (flags & wxEVENT_SOURCE_OUTPUT)
136 condition |= G_IO_OUT;
137 if (flags & wxEVENT_SOURCE_EXCEPTION)
138 condition |= G_IO_ERR | G_IO_HUP | G_IO_NVAL;
139
140 GIOChannel* channel = g_io_channel_unix_new(source->GetResource());
141 int gtk_id = g_io_add_watch(channel, (GIOCondition)condition,
142 &wx_on_channel_event, source->GetHandler());
143 g_io_channel_unref(channel);
144
145 wxEventLoopSourceIdMap::value_type val(source, gtk_id);
146 return m_sourceIdMap.insert(val).second;
147 }
148
149 bool wxGUIEventLoop::DoRemoveSource(wxAbstractEventLoopSource* src)
150 {
151 Source* source = dynamic_cast<Source*>(src);
152 wxCHECK_MSG( source, false, "Invalid source type" );
153
154 wxLogTrace(wxTRACE_EVT_SOURCE,
155 "wxGUIEventLoop::DoRemoveSource() source=%d",
156 source->GetResource());
157
158 wxEventLoopSourceIdMap::iterator it = m_sourceIdMap.find(source);
159 wxCHECK_MSG( it != m_sourceIdMap.end(), false, "Source not on the list" );
160
161 int gtk_id = it->second;
162 m_sourceIdMap.erase(it);
163 return g_source_remove(gtk_id);
164 }
165
166 // ----------------------------------------------------------------------------
167 // wxEventLoop message processing dispatching
168 // ----------------------------------------------------------------------------
169
170 bool wxGUIEventLoop::Pending() const
171 {
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;
179 }
180
181 bool wxGUIEventLoop::Dispatch()
182 {
183 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
184
185 // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called
186 return !gtk_main_iteration();
187 }
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
214 //-----------------------------------------------------------------------------
215 // YieldFor
216 //-----------------------------------------------------------------------------
217
218 extern "C" {
219 static void wxgtk_main_do_event(GdkEvent* event, void* data)
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.
224
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).
231
232 wxEventCategory cat = wxEVT_CATEGORY_UNKNOWN;
233 switch (event->type)
234 {
235 case GDK_SELECTION_REQUEST:
236 case GDK_SELECTION_NOTIFY:
237 case GDK_SELECTION_CLEAR:
238 #if GTK_CHECK_VERSION(2,6,0)
239 case GDK_OWNER_CHANGE:
240 #endif
241 cat = wxEVT_CATEGORY_CLIPBOARD;
242 break;
243
244 case GDK_KEY_PRESS:
245 case GDK_KEY_RELEASE:
246 case GDK_BUTTON_PRESS:
247 case GDK_2BUTTON_PRESS:
248 case GDK_3BUTTON_PRESS:
249 case GDK_BUTTON_RELEASE:
250 case GDK_SCROLL: // generated from mouse buttons
251 case GDK_CLIENT_EVENT:
252 cat = wxEVT_CATEGORY_USER_INPUT;
253 break;
254
255 case GDK_PROXIMITY_IN:
256 case GDK_PROXIMITY_OUT:
257
258 case GDK_MOTION_NOTIFY:
259 case GDK_ENTER_NOTIFY:
260 case GDK_LEAVE_NOTIFY:
261 case GDK_VISIBILITY_NOTIFY:
262 case GDK_PROPERTY_NOTIFY:
263
264 case GDK_FOCUS_CHANGE:
265 case GDK_CONFIGURE:
266 case GDK_WINDOW_STATE:
267 case GDK_SETTING:
268 case GDK_DELETE:
269 case GDK_DESTROY:
270
271 case GDK_EXPOSE:
272 case GDK_NO_EXPOSE:
273 case GDK_MAP:
274 case GDK_UNMAP:
275
276 case GDK_DRAG_ENTER:
277 case GDK_DRAG_LEAVE:
278 case GDK_DRAG_MOTION:
279 case GDK_DRAG_STATUS:
280 case GDK_DROP_START:
281 case GDK_DROP_FINISHED:
282 #if GTK_CHECK_VERSION(2,8,0)
283 case GDK_GRAB_BROKEN:
284 #endif
285 #if GTK_CHECK_VERSION(2,14,0)
286 case GDK_DAMAGE:
287 #endif
288 cat = wxEVT_CATEGORY_UI;
289 break;
290
291 default:
292 cat = wxEVT_CATEGORY_UNKNOWN;
293 break;
294 }
295
296 wxGUIEventLoop* evtloop = static_cast<wxGUIEventLoop*>(data);
297
298 // is this event allowed now?
299 if (evtloop->IsEventAllowedInsideYield(cat))
300 gtk_main_do_event(event); // process it now
301 else if (event->type != GDK_NOTHING)
302 evtloop->StoreGdkEventForLaterProcessing(gdk_event_copy(event));
303 // process it later (but make a copy; the caller will free the event pointer)
304 }
305 }
306
307 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
308 {
309 #if wxUSE_THREADS
310 if ( !wxThread::IsMain() )
311 {
312 // can't call gtk_main_iteration() from other threads like this
313 return true;
314 }
315 #endif // wxUSE_THREADS
316
317 m_isInsideYield = true;
318 m_eventsToProcessInsideYield = eventsToProcess;
319
320 #if wxUSE_LOG
321 // disable log flushing from here because a call to wxYield() shouldn't
322 // normally result in message boxes popping up &c
323 wxLog::Suspend();
324 #endif
325
326 // temporarily replace the global GDK event handler with our function, which
327 // categorizes the events and using m_eventsToProcessInsideYield decides
328 // if an event should be processed immediately or not
329 // NOTE: this approach is better than using gdk_display_get_event() because
330 // gtk_main_iteration() does more than just calling gdk_display_get_event()
331 // and then call gtk_main_do_event()!
332 // In particular in this way we also process input from sources like
333 // GIOChannels (this is needed for e.g. wxGUIAppTraits::WaitForChild).
334 gdk_event_handler_set(wxgtk_main_do_event, this, NULL);
335 while (Pending()) // avoid false positives from our idle source
336 gtk_main_iteration();
337 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
338
339 if (eventsToProcess != wxEVT_CATEGORY_CLIPBOARD)
340 {
341 // It's necessary to call ProcessIdle() to update the frames sizes which
342 // might have been changed (it also will update other things set from
343 // OnUpdateUI() which is a nice (and desired) side effect). But we
344 // call ProcessIdle() only once since this is not meant for longish
345 // background jobs (controlled by wxIdleEvent::RequestMore() and the
346 // return value of Processidle().
347 ProcessIdle(); // ProcessIdle() also calls ProcessPendingEvents()
348 }
349 //else: if we are inside ~wxClipboardSync() and we call ProcessIdle() and
350 // the user app contains an UI update handler which calls wxClipboard::IsSupported,
351 // then we fall into a never-ending loop...
352
353 // put all unprocessed GDK events back in the queue
354 GdkDisplay* disp = gtk_widget_get_display(wxGetRootWindow());
355 for (size_t i=0; i<m_arrGdkEvents.GetCount(); i++)
356 {
357 GdkEvent* ev = (GdkEvent*)m_arrGdkEvents[i];
358
359 // NOTE: gdk_display_put_event makes a copy of the event passed to it
360 gdk_display_put_event(disp, ev);
361 gdk_event_free(ev);
362 }
363
364 m_arrGdkEvents.Clear();
365
366 #if wxUSE_LOG
367 // let the logs be flashed again
368 wxLog::Resume();
369 #endif
370
371 m_isInsideYield = false;
372
373 return true;
374 }