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 // License: 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"
36 // ============================================================================
37 // wxEventLoop implementation
38 // ============================================================================
40 extern GtkWidget
*wxGetRootWindow();
42 // ----------------------------------------------------------------------------
43 // wxEventLoop running and exiting
44 // ----------------------------------------------------------------------------
46 wxGUIEventLoop::wxGUIEventLoop()
51 int wxGUIEventLoop::Run()
53 // event loops are not recursive, you need to create another loop!
54 wxCHECK_MSG( !IsRunning(), -1, "can't reenter a message loop" );
56 wxEventLoopActivator
activate(this);
65 void wxGUIEventLoop::Exit(int rc
)
67 wxCHECK_RET( IsRunning(), "can't call Exit() if not running" );
74 void wxGUIEventLoop::WakeUp()
76 // TODO: idle events handling should really be done by wxEventLoop itself
77 // but for now it's completely in gtk/app.cpp so just call there when
78 // we have wxTheApp and hope that it doesn't matter that we do
79 // nothing when we don't...
81 wxTheApp
->WakeUpIdle();
84 // ----------------------------------------------------------------------------
85 // wxEventLoop message processing dispatching
86 // ----------------------------------------------------------------------------
88 bool wxGUIEventLoop::Pending() const
92 // this avoids false positives from our idle source
93 return wxTheApp
->EventsPending();
96 return gtk_events_pending() != 0;
99 bool wxGUIEventLoop::Dispatch()
101 wxCHECK_MSG( IsRunning(), false, wxT("can't call Dispatch() if not running") );
103 // gtk_main_iteration() returns TRUE only if gtk_main_quit() was called
104 return !gtk_main_iteration();
108 static gboolean
wx_event_loop_timeout(void* data
)
110 bool* expired
= static_cast<bool*>(data
);
113 // return FALSE to remove this timeout
118 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout
)
120 bool expired
= false;
121 const unsigned id
= g_timeout_add(timeout
, wx_event_loop_timeout
, &expired
);
122 bool quit
= gtk_main_iteration() != 0;
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
136 static void wxgtk_main_do_event(GdkEvent
*event
, wxGUIEventLoop
* evtloop
)
138 // categorize the GDK event according to wxEventCategory.
139 // See http://library.gnome.org/devel/gdk/unstable/gdk-Events.html#GdkEventType
142 // NOTE: GDK_* constants which were not present in the GDK2.0 can be tested for
143 // only at compile-time; when running the program (compiled with a recent GDK)
144 // on a system with an older GDK lib we can be sure there won't be problems
145 // because event->type will never assume those values corresponding to
146 // new event types (since new event types are always added in GDK with non
147 // conflicting values for ABI compatibility).
149 wxEventCategory cat
= wxEVT_CATEGORY_UNKNOWN
;
152 case GDK_SELECTION_REQUEST
:
153 case GDK_SELECTION_NOTIFY
:
154 case GDK_SELECTION_CLEAR
:
155 #if GTK_CHECK_VERSION(2,6,0)
156 case GDK_OWNER_CHANGE
:
158 cat
= wxEVT_CATEGORY_CLIPBOARD
;
162 case GDK_KEY_RELEASE
:
163 case GDK_BUTTON_PRESS
:
164 case GDK_2BUTTON_PRESS
:
165 case GDK_3BUTTON_PRESS
:
166 case GDK_BUTTON_RELEASE
:
167 case GDK_SCROLL
: // generated from mouse buttons
168 case GDK_CLIENT_EVENT
:
169 cat
= wxEVT_CATEGORY_USER_INPUT
;
172 case GDK_PROXIMITY_IN
:
173 case GDK_PROXIMITY_OUT
:
175 case GDK_MOTION_NOTIFY
:
176 case GDK_ENTER_NOTIFY
:
177 case GDK_LEAVE_NOTIFY
:
178 case GDK_VISIBILITY_NOTIFY
:
179 case GDK_PROPERTY_NOTIFY
:
181 case GDK_FOCUS_CHANGE
:
183 case GDK_WINDOW_STATE
:
195 case GDK_DRAG_MOTION
:
196 case GDK_DRAG_STATUS
:
198 case GDK_DROP_FINISHED
:
199 #if GTK_CHECK_VERSION(2,8,0)
200 case GDK_GRAB_BROKEN
:
202 #if GTK_CHECK_VERSION(2,14,0)
205 cat
= wxEVT_CATEGORY_UI
;
209 cat
= wxEVT_CATEGORY_UNKNOWN
;
213 // is this event allowed now?
214 if (evtloop
->IsEventAllowedInsideYield(cat
))
215 gtk_main_do_event(event
); // process it now
216 else if (event
->type
!= GDK_NOTHING
)
217 evtloop
->StoreGdkEventForLaterProcessing(gdk_event_copy(event
));
218 // process it later (but make a copy; the caller will free the event pointer)
221 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
224 if ( !wxThread::IsMain() )
226 // can't call gtk_main_iteration() from other threads like this
229 #endif // wxUSE_THREADS
231 m_isInsideYield
= true;
232 m_eventsToProcessInsideYield
= eventsToProcess
;
235 // disable log flushing from here because a call to wxYield() shouldn't
236 // normally result in message boxes popping up &c
240 // temporarily replace the global GDK event handler with our function, which
241 // categorizes the events and using m_eventsToProcessInsideYield decides
242 // if an event should be processed immediately or not
243 // NOTE: this approach is better than using gdk_display_get_event() because
244 // gtk_main_iteration() does more than just calling gdk_display_get_event()
245 // and then call gtk_main_do_event()!
246 // In particular in this way we also process input from sources like
247 // GIOChannels (this is needed for e.g. wxGUIAppTraits::WaitForChild).
248 gdk_event_handler_set ((GdkEventFunc
)wxgtk_main_do_event
, this, NULL
);
249 while (Pending()) // avoid false positives from our idle source
250 gtk_main_iteration();
251 gdk_event_handler_set ((GdkEventFunc
)gtk_main_do_event
, NULL
, NULL
);
253 if (eventsToProcess
!= wxEVT_CATEGORY_CLIPBOARD
)
255 // It's necessary to call ProcessIdle() to update the frames sizes which
256 // might have been changed (it also will update other things set from
257 // OnUpdateUI() which is a nice (and desired) side effect). But we
258 // call ProcessIdle() only once since this is not meant for longish
259 // background jobs (controlled by wxIdleEvent::RequestMore() and the
260 // return value of Processidle().
261 ProcessIdle(); // ProcessIdle() also calls ProcessPendingEvents()
263 //else: if we are inside ~wxClipboardSync() and we call ProcessIdle() and
264 // the user app contains an UI update handler which calls wxClipboard::IsSupported,
265 // then we fall into a never-ending loop...
267 // put all unprocessed GDK events back in the queue
268 GdkDisplay
* disp
= gtk_widget_get_display(wxGetRootWindow());
269 for (size_t i
=0; i
<m_arrGdkEvents
.GetCount(); i
++)
271 GdkEvent
* ev
= (GdkEvent
*)m_arrGdkEvents
[i
];
273 // NOTE: gdk_display_put_event makes a copy of the event passed to it
274 gdk_display_put_event(disp
, ev
);
278 m_arrGdkEvents
.Clear();
281 // let the logs be flashed again
285 m_isInsideYield
= false;