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, _T("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 bool wxGUIEventLoop::YieldFor(long eventsToProcess
)
139 if ( !wxThread::IsMain() )
141 // can't call gtk_main_iteration() from other threads like this
144 #endif // wxUSE_THREADS
146 m_isInsideYield
= true;
147 m_eventsToProcessInsideYield
= eventsToProcess
;
150 // disable log flushing from here because a call to wxYield() shouldn't
151 // normally result in message boxes popping up &c
155 // NOTE: gtk_main_iteration() doesn't allow us to filter events, so we
156 // rather use gtk_main_do_event() after filtering the events at
159 GdkDisplay
* disp
= gtk_widget_get_display(wxGetRootWindow());
161 // gdk_display_get_event() will transform X11 events into GDK events
162 // and will queue all of them in the display (private) structure;
163 // finally it will "unqueue" the last one and return it to us
164 GdkEvent
* event
= gdk_display_get_event(disp
);
167 // categorize the GDK event according to wxEventCategory.
168 // See http://library.gnome.org/devel/gdk/unstable/gdk-Events.html#GdkEventType
171 wxEventCategory cat
= wxEVT_CATEGORY_UNKNOWN
;
174 case GDK_SELECTION_REQUEST
:
175 case GDK_SELECTION_NOTIFY
:
176 case GDK_SELECTION_CLEAR
:
177 case GDK_OWNER_CHANGE
:
178 cat
= wxEVT_CATEGORY_CLIPBOARD
;
183 case GDK_KEY_RELEASE
:
184 case GDK_BUTTON_PRESS
:
185 case GDK_2BUTTON_PRESS
:
186 case GDK_3BUTTON_PRESS
:
187 case GDK_BUTTON_RELEASE
:
188 case GDK_SCROLL
: // generated from mouse buttons
189 case GDK_CLIENT_EVENT
:
190 cat
= wxEVT_CATEGORY_USER_INPUT
;
194 case GDK_PROXIMITY_IN
:
195 case GDK_PROXIMITY_OUT
:
197 case GDK_MOTION_NOTIFY
:
198 case GDK_ENTER_NOTIFY
:
199 case GDK_LEAVE_NOTIFY
:
200 case GDK_VISIBILITY_NOTIFY
:
201 case GDK_PROPERTY_NOTIFY
:
203 case GDK_FOCUS_CHANGE
:
205 case GDK_WINDOW_STATE
:
218 case GDK_DRAG_MOTION
:
219 case GDK_DRAG_STATUS
:
221 case GDK_DROP_FINISHED
:
222 case GDK_GRAB_BROKEN
:
223 cat
= wxEVT_CATEGORY_UI
;
227 cat
= wxEVT_CATEGORY_UNKNOWN
;
231 if (eventsToProcess
& cat
)
232 gtk_main_do_event(event
); // process it now
234 m_arrGdkEvents
.Add(event
); // process it later
237 event
= gdk_display_get_event(disp
);
240 if (eventsToProcess
!= wxEVT_CATEGORY_CLIPBOARD
)
242 // It's necessary to call ProcessIdle() to update the frames sizes which
243 // might have been changed (it also will update other things set from
244 // OnUpdateUI() which is a nice (and desired) side effect). But we
245 // call ProcessIdle() only once since this is not meant for longish
246 // background jobs (controlled by wxIdleEvent::RequestMore() and the
247 // return value of Processidle().
248 ProcessIdle(); // ProcessIdle() also calls ProcessPendingEvents()
250 //else: if we are inside ~wxClipboardSync() and we call ProcessIdle() and
251 // the user app contains an UI update handler which calls wxClipboard::IsSupported,
252 // then we fall into a never-ending loop...
254 // put all unprocessed GDK events back in the queue
255 for (size_t i
=0; i
<m_arrGdkEvents
.GetCount(); i
++)
257 GdkEvent
* ev
= (GdkEvent
*)m_arrGdkEvents
[i
];
259 // NOTE: gdk_display_put_event makes a copy of the event passed to it
260 gdk_display_put_event(disp
, ev
);
264 m_arrGdkEvents
.Clear();
267 // let the logs be flashed again
271 m_isInsideYield
= false;