diff options
author | Carlos Garcia Campos <cgarcia@igalia.com> | 2011-02-08 14:49:31 +0100 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2012-03-01 16:25:21 -0500 |
commit | 9f4bfff1b030fffa0d815557ec690d208cb4cd67 (patch) | |
tree | dcab443bc3a3a73a1cc0c57bbdbac870df310ab7 /gtk/gtkmain.c | |
parent | 6c257040a54eb91cc9323c8441b6025667dace69 (diff) | |
download | gtk+-9f4bfff1b030fffa0d815557ec690d208cb4cd67.tar.gz |
gtk: Add a way to do event capture
This patch adds a capture phase to GTK+'s event propagation
model. Events are first propagated from the toplevel (or the
grab widget, if a grab is in place) down to the target widget
and then back up. The second phase is using the existing
::event signal, the new capture phase is using a private
API instead of a public signal for now.
This mechanism can be used in many places where we currently
have to prevent child widgets from getting events by putting
an input-only window over them. It will also be used to implement
kinetic scrolling in subsequent patches.
http://bugzilla.gnome.org/show_bug.cgi?id=641836
We automatically request more motion events in behalf of
the original widget if it listens to motion hints. So
the capturing widget doesn't need to handle such
implementation details.
We are not making event capture part of the public API for 3.4,
which is why there is no ::captured-event signal.
Diffstat (limited to 'gtk/gtkmain.c')
-rw-r--r-- | gtk/gtkmain.c | 224 |
1 files changed, 152 insertions, 72 deletions
diff --git a/gtk/gtkmain.c b/gtk/gtkmain.c index c0cdabd9c8..68ecbfeb65 100644 --- a/gtk/gtkmain.c +++ b/gtk/gtkmain.c @@ -1471,6 +1471,7 @@ gtk_main_do_event (GdkEvent *event) { GtkWidget *event_widget; GtkWidget *grab_widget = NULL; + GtkWidget *topmost_widget = NULL; GtkWindowGroup *window_group; GdkEvent *rewritten_event = NULL; GdkDevice *device; @@ -1530,6 +1531,14 @@ gtk_main_do_event (GdkEvent *event) if (!grab_widget) grab_widget = gtk_window_group_get_current_grab (window_group); + /* Find out the topmost widget where captured event propagation + * should start, which is the widget holding the GTK+ grab + * if any, otherwise it's left NULL and events are emitted + * from the toplevel (or topmost parentless parent). + */ + if (grab_widget) + topmost_widget = grab_widget; + /* If the grab widget is an ancestor of the event widget * then we send the event to the original event widget. * This is the key to implementing modality. @@ -1626,14 +1635,16 @@ gtk_main_do_event (GdkEvent *event) case GDK_WINDOW_STATE: case GDK_GRAB_BROKEN: case GDK_DAMAGE: - gtk_widget_event (event_widget, event); + if (!_gtk_widget_captured_event (event_widget, event)) + gtk_widget_event (event_widget, event); break; case GDK_SCROLL: case GDK_BUTTON_PRESS: case GDK_2BUTTON_PRESS: case GDK_3BUTTON_PRESS: - gtk_propagate_event (grab_widget, event); + if (!_gtk_propagate_captured_event (grab_widget, event, topmost_widget)) + gtk_propagate_event (grab_widget, event); break; case GDK_KEY_PRESS: @@ -1682,7 +1693,8 @@ gtk_main_do_event (GdkEvent *event) case GDK_BUTTON_RELEASE: case GDK_PROXIMITY_IN: case GDK_PROXIMITY_OUT: - gtk_propagate_event (grab_widget, event); + if (!_gtk_propagate_captured_event (grab_widget, event, topmost_widget)) + gtk_propagate_event (grab_widget, event); break; case GDK_ENTER_NOTIFY: @@ -1691,7 +1703,8 @@ gtk_main_do_event (GdkEvent *event) _gtk_widget_set_device_window (event_widget, gdk_event_get_device (event), event->any.window); - if (gtk_widget_is_sensitive (grab_widget)) + if (gtk_widget_is_sensitive (grab_widget) && + !_gtk_propagate_captured_event (grab_widget, event, topmost_widget)) gtk_widget_event (grab_widget, event); break; @@ -1701,7 +1714,8 @@ gtk_main_do_event (GdkEvent *event) _gtk_widget_set_device_window (event_widget, gdk_event_get_device (event), NULL); - if (gtk_widget_is_sensitive (grab_widget)) + if (gtk_widget_is_sensitive (grab_widget) && + !_gtk_propagate_captured_event (grab_widget, event, topmost_widget)) gtk_widget_event (grab_widget, event); break; @@ -2331,45 +2345,96 @@ gtk_get_event_widget (GdkEvent *event) return widget; } -/** - * gtk_propagate_event: - * @widget: a #GtkWidget - * @event: an event - * - * Sends an event to a widget, propagating the event to parent widgets - * if the event remains unhandled. - * - * Events received by GTK+ from GDK normally begin in gtk_main_do_event(). - * Depending on the type of event, existence of modal dialogs, grabs, etc., - * the event may be propagated; if so, this function is used. - * - * gtk_propagate_event() calls gtk_widget_event() on each widget it - * decides to send the event to. So gtk_widget_event() is the lowest-level - * function; it simply emits the #GtkWidget::event and possibly an - * event-specific signal on a widget. gtk_propagate_event() is a bit - * higher-level, and gtk_main_do_event() is the highest level. - * - * All that said, you most likely don't want to use any of these - * functions; synthesizing events is rarely needed. There are almost - * certainly better ways to achieve your goals. For example, use - * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead - * of making up expose events. - */ -void -gtk_propagate_event (GtkWidget *widget, - GdkEvent *event) +static gboolean +propagate_event_up (GtkWidget *widget, + GdkEvent *event, + GtkWidget *topmost) { - gint handled_event; + gboolean handled_event = FALSE; - g_return_if_fail (GTK_IS_WIDGET (widget)); - g_return_if_fail (event != NULL); + /* Propagate event up the widget tree so that + * parents can see the button and motion + * events of the children. + */ + while (TRUE) + { + GtkWidget *tmp; + + g_object_ref (widget); + + /* Scroll events are special cased here because it + * feels wrong when scrolling a GtkViewport, say, + * to have children of the viewport eat the scroll + * event + */ + if (!gtk_widget_is_sensitive (widget)) + handled_event = event->type != GDK_SCROLL; + else + handled_event = gtk_widget_event (widget, event); + + tmp = gtk_widget_get_parent (widget); + g_object_unref (widget); + + if (widget == topmost) + break; + + widget = tmp; + + if (handled_event || !widget) + break; + } + + return handled_event; +} + +static gboolean +propagate_event_down (GtkWidget *widget, + GdkEvent *event, + GtkWidget *topmost) +{ + gint handled_event = FALSE; + GList *widgets = NULL; + GList *l; + + widgets = g_list_prepend (widgets, g_object_ref (widget)); + while (widget && widget != topmost) + { + widget = gtk_widget_get_parent (widget); + if (!widget) + break; + + widgets = g_list_prepend (widgets, g_object_ref (widget)); + + if (widget == topmost) + break; + } + + for (l = widgets; l && !handled_event; l = g_list_next (l)) + { + widget = (GtkWidget *)l->data; - handled_event = FALSE; + if (!gtk_widget_is_sensitive (widget)) + handled_event = TRUE; + else + handled_event = _gtk_widget_captured_event (widget, event); + } + g_list_free_full (widgets, (GDestroyNotify)g_object_unref); - g_object_ref (widget); + return handled_event; +} - if ((event->type == GDK_KEY_PRESS) || - (event->type == GDK_KEY_RELEASE)) +static gboolean +propagate_event (GtkWidget *widget, + GdkEvent *event, + gboolean captured, + GtkWidget *topmost) +{ + gboolean handled_event = FALSE; + gboolean (* propagate_func) (GtkWidget *widget, GdkEvent *event); + + propagate_func = captured ? _gtk_widget_captured_event : gtk_widget_event; + + if (event->type == GDK_KEY_PRESS || event->type == GDK_KEY_RELEASE) { /* Only send key events within Window widgets to the Window * The Window widget will in turn pass the @@ -2381,11 +2446,12 @@ gtk_propagate_event (GtkWidget *widget, window = gtk_widget_get_toplevel (widget); if (GTK_IS_WINDOW (window)) { + g_object_ref (widget); /* If there is a grab within the window, give the grab widget * a first crack at the key event */ if (widget != window && gtk_widget_has_grab (widget)) - handled_event = gtk_widget_event (widget, event); + handled_event = propagate_func (widget, event); if (!handled_event) { @@ -2393,45 +2459,59 @@ gtk_propagate_event (GtkWidget *widget, if (GTK_IS_WINDOW (window)) { if (gtk_widget_is_sensitive (window)) - gtk_widget_event (window, event); + handled_event = propagate_func (window, event); } } - handled_event = TRUE; /* don't send to widget */ + g_object_unref (widget); + return handled_event; } } - /* Other events get propagated up the widget tree - * so that parents can see the button and motion - * events of the children. - */ - if (!handled_event) - { - while (TRUE) - { - GtkWidget *tmp; - - /* Scroll events are special cased here because it - * feels wrong when scrolling a GtkViewport, say, - * to have children of the viewport eat the scroll - * event - */ - if (!gtk_widget_is_sensitive (widget)) - handled_event = event->type != GDK_SCROLL; - else - handled_event = gtk_widget_event (widget, event); + /* Other events get propagated up/down the widget tree */ + return captured ? + propagate_event_down (widget, event, topmost) : + propagate_event_up (widget, event, topmost); +} - tmp = gtk_widget_get_parent (widget); - g_object_unref (widget); +/** + * gtk_propagate_event: + * @widget: a #GtkWidget + * @event: an event + * + * Sends an event to a widget, propagating the event to parent widgets + * if the event remains unhandled. + * + * Events received by GTK+ from GDK normally begin in gtk_main_do_event(). + * Depending on the type of event, existence of modal dialogs, grabs, etc., + * the event may be propagated; if so, this function is used. + * + * gtk_propagate_event() calls gtk_widget_event() on each widget it + * decides to send the event to. So gtk_widget_event() is the lowest-level + * function; it simply emits the #GtkWidget::event and possibly an + * event-specific signal on a widget. gtk_propagate_event() is a bit + * higher-level, and gtk_main_do_event() is the highest level. + * + * All that said, you most likely don't want to use any of these + * functions; synthesizing events is rarely needed. There are almost + * certainly better ways to achieve your goals. For example, use + * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead + * of making up expose events. + */ +void +gtk_propagate_event (GtkWidget *widget, + GdkEvent *event) +{ + g_return_if_fail (GTK_IS_WIDGET (widget)); + g_return_if_fail (event != NULL); - widget = tmp; + propagate_event (widget, event, FALSE, NULL); +} - if (!handled_event && widget) - g_object_ref (widget); - else - break; - } - } - else - g_object_unref (widget); +gboolean +_gtk_propagate_captured_event (GtkWidget *widget, + GdkEvent *event, + GtkWidget *topmost) +{ + return propagate_event (widget, event, TRUE, topmost); } |