[gtk/wip/otte/dnd: 137/144] Add GtkDropControllerMotion



commit 9646a1f0afcb271e7a839234f6fb82fddab3e11b
Author: Benjamin Otte <otte redhat com>
Date:   Sat Feb 22 00:58:57 2020 +0100

    Add GtkDropControllerMotion

 docs/reference/gtk/gtk4-sections.txt |  21 ++
 gtk/gtk.h                            |   1 +
 gtk/gtkdropcontrollermotion.c        | 394 +++++++++++++++++++++++++++++++++++
 gtk/gtkdropcontrollermotion.h        |  57 +++++
 gtk/meson.build                      |   2 +
 5 files changed, 475 insertions(+)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 94a9419415..654044f16a 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -6909,6 +6909,27 @@ GTK_DROP_TARGET_GET_CLASS
 gtk_drop_target_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtkdropcontrollermotion</FILE>
+<TITLE>GtkDropControllerMotion</TITLE>
+GtkDropControllerMotion
+gtk_drop_controller_motion_new
+gtk_drop_controller_motion_contains_pointer
+gtk_drop_controller_motion_is_pointer
+gtk_drop_controller_motion_get_drop
+
+<SUBSECTION Standard>
+GTK_TYPE_DROP_CONTROLLER_MOTION
+GTK_DROP_CONTROLLER_MOTION
+GTK_DROP_CONTROLLER_MOTION_CLASS
+GTK_IS_DROP_CONTROLLER_MOTION
+GTK_IS_DROP_CONTROLLER_MOTION_CLASS
+GTK_DROP_CONTROLLER_MOTION_GET_CLASS
+
+<SUBSECTION Private>
+gtk_drop_controller_motion_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtkdragicon</FILE>
 GtkDragIcon
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 46c7aabacc..5322b95984 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -94,6 +94,7 @@
 #include <gtk/gtkdragicon.h>
 #include <gtk/gtkdragsource.h>
 #include <gtk/gtkdrawingarea.h>
+#include <gtk/gtkdropcontrollermotion.h>
 #include <gtk/gtkeditable.h>
 #include <gtk/gtkemojichooser.h>
 #include <gtk/gtkentry.h>
diff --git a/gtk/gtkdropcontrollermotion.c b/gtk/gtkdropcontrollermotion.c
new file mode 100644
index 0000000000..8c89411c99
--- /dev/null
+++ b/gtk/gtkdropcontrollermotion.c
@@ -0,0 +1,394 @@
+/*
+ * Copyright © 2020 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+/**
+ * SECTION:gtkdropcontrollermotion
+ * @Short_description: Event controller for motion events during a drop
+ * @Title: GtkDropControllerMotion
+ * @See_also: #GtkDropControllerMotion, #GdkDrop, #GtkDropTarget
+ *
+ * #GtkDropControllerMotion is an event controller meant for tracking
+ * the pointer hovering over a widget during a drag and drop operation.
+ *
+ * It is modeled after #GtkEventControllerMotion so if you have used
+ * that, this should feel really familiar.
+ *
+ * The drop controller is not able to accept drops, use #GtkDropTarget
+ * for that purpose.
+ **/
+
+#include "config.h"
+
+#include "gtkdropcontrollermotion.h"
+
+#include "gtkintl.h"
+#include "gtkprivate.h"
+#include "gtkwidgetprivate.h"
+#include "gtkmarshalers.h"
+#include "gtkeventcontrollerprivate.h"
+#include "gtktypebuiltins.h"
+#include "gtkmarshalers.h"
+
+struct _GtkDropControllerMotion
+{
+  GtkEventController parent_instance;
+
+  GdkDrop *drop;
+  guint is_pointer             : 1;
+  guint contains_pointer       : 1;
+};
+
+struct _GtkDropControllerMotionClass
+{
+  GtkEventControllerClass parent_class;
+};
+
+enum {
+  ENTER,
+  LEAVE,
+  MOTION,
+  N_SIGNALS
+};
+
+enum {
+  PROP_0,
+  PROP_CONTAINS_POINTER,
+  PROP_DROP,
+  PROP_IS_POINTER,
+  NUM_PROPERTIES
+};
+
+static GParamSpec *props[NUM_PROPERTIES] = { NULL, };
+
+static guint signals[N_SIGNALS] = { 0 };
+
+G_DEFINE_TYPE (GtkDropControllerMotion, gtk_drop_controller_motion, GTK_TYPE_EVENT_CONTROLLER)
+
+static gboolean
+gtk_drop_controller_motion_handle_event (GtkEventController *controller,
+                                         GdkEvent           *event,
+                                         double              x,
+                                         double              y)
+{
+  GtkEventControllerClass *parent_class;
+  GdkEventType type;
+
+  type = gdk_event_get_event_type (event);
+  if (type == GDK_DRAG_MOTION)
+    g_signal_emit (controller, signals[MOTION], 0, x, y);
+
+  parent_class = GTK_EVENT_CONTROLLER_CLASS (gtk_drop_controller_motion_parent_class);
+
+  return parent_class->handle_event (controller, event, x, y);
+}
+
+static void
+update_pointer_focus (GtkEventController    *controller,
+                      const GtkCrossingData *crossing,
+                      double                 x,
+                      double                 y)
+{
+  GtkDropControllerMotion *self = GTK_DROP_CONTROLLER_MOTION (controller);
+  GtkWidget *widget = gtk_event_controller_get_widget (controller);
+  gboolean is_pointer = FALSE;
+  gboolean contains_pointer = FALSE;
+  gboolean enter = FALSE;
+  gboolean leave = FALSE;
+
+  if (crossing->direction == GTK_CROSSING_IN)
+    {
+     if (crossing->new_descendent != NULL)
+        {
+          contains_pointer = TRUE;
+        }
+      if (crossing->new_target == widget)
+        {
+          contains_pointer = TRUE;
+          is_pointer = TRUE;
+        }
+    }
+  else
+    {
+      if (crossing->new_descendent != NULL ||
+          crossing->new_target == widget)
+        contains_pointer = TRUE;
+      is_pointer = FALSE;
+    }
+
+  if (self->contains_pointer != contains_pointer)
+    {
+      enter = contains_pointer;
+      leave = !contains_pointer;
+    }
+
+  if (leave)
+    g_signal_emit (controller, signals[LEAVE], 0);
+
+  g_object_freeze_notify (G_OBJECT (self));
+  if (self->is_pointer != is_pointer)
+    {
+      self->is_pointer = is_pointer;
+      g_object_notify (G_OBJECT (self), "is-pointer");
+    }
+  if (self->contains_pointer != contains_pointer)
+    {
+      self->contains_pointer = contains_pointer;
+      if (contains_pointer)
+        self->drop = g_object_ref (crossing->drop);
+      else
+        g_clear_object (&self->drop);
+      g_object_notify (G_OBJECT (self), "contains-pointer");
+      g_object_notify (G_OBJECT (self), "drop");
+    }
+  g_object_thaw_notify (G_OBJECT (self));
+
+  if (enter)
+    g_signal_emit (controller, signals[ENTER], 0, x, y);
+}
+
+static void
+gtk_drop_controller_motion_handle_crossing (GtkEventController    *controller,
+                                            const GtkCrossingData *crossing,
+                                            double                 x,
+                                            double                 y)
+{
+  if (crossing->type == GTK_CROSSING_DROP)
+    update_pointer_focus (controller, crossing, x, y);
+}
+
+static void
+gtk_drop_controller_motion_get_property (GObject    *object,
+                                         guint       prop_id,
+                                         GValue     *value,
+                                         GParamSpec *pspec)
+{
+  GtkDropControllerMotion *self = GTK_DROP_CONTROLLER_MOTION (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONTAINS_POINTER:
+      g_value_set_boolean (value, self->contains_pointer);
+      break;
+
+    case PROP_DROP:
+      g_value_set_object (value, self->drop);
+      break;
+
+    case PROP_IS_POINTER:
+      g_value_set_boolean (value, self->is_pointer);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gtk_drop_controller_motion_class_init (GtkDropControllerMotionClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
+
+  object_class->get_property = gtk_drop_controller_motion_get_property;
+
+  controller_class->handle_event = gtk_drop_controller_motion_handle_event;
+  controller_class->handle_crossing = gtk_drop_controller_motion_handle_crossing;
+
+  /**
+   * GtkDropControllerMotion:contains-pointer:
+   *
+   * Whether the pointer of a drag and drop operation is in the controller's
+   * widget or a descendant.
+   * See also #GtkDropControllerMotion:is-pointer.
+   *
+   * When handling crossing events, this property is updated
+   * before #GtkDropControllerMotion::enter but after
+   * #GtkDropControllerMotion::leave is emitted.
+   */
+  props[PROP_CONTAINS_POINTER] =
+      g_param_spec_boolean ("contains-pointer",
+                            P_("Contains Pointer"),
+                            P_("Whether the pointer is inthe controllers widget or a descendant"),
+                            FALSE,
+                            G_PARAM_READABLE);
+
+  /**
+   * GtkDropControllerMotion:drop:
+   *
+   * The ongoing drop operation over the controller's widget or its descendant.  
+   * If no drop operation is going on, this property returns %NULL.
+   *
+   * The event controller should not modify the @drop, but it might want to query
+   * its properties.
+   *
+   * When handling crossing events, this property is updated
+   * before #GtkDropControllerMotion::enter but after
+   * #GtkDropControllerMotion::leave is emitted.
+   */
+  props[PROP_DROP] =
+      g_param_spec_object ("drop",
+                           P_("Drop"),
+                           P_("The ongoing drop operation"),
+                           GDK_TYPE_DROP,
+                           G_PARAM_READABLE);
+
+  /**
+   * GtkDropControllerMotion:is-pointer:
+   *
+   * Whether the pointer is in the controllers widget itself,
+   * as opposed to in a descendent widget. See also
+   * #GtkDropControllerMotion:contains-pointer.
+   *
+   * When handling crossing events, this property is updated
+   * before #GtkDropControllerMotion::enter but after
+   * #GtkDropControllerMotion::leave is emitted.
+   */
+  props[PROP_IS_POINTER] =
+      g_param_spec_boolean ("is-pointer",
+                            P_("Is Pointer"),
+                            P_("Whether the pointer is in the controllers widget"),
+                            FALSE,
+                            G_PARAM_READABLE);
+
+  g_object_class_install_properties (object_class, NUM_PROPERTIES, props);
+
+  /**
+   * GtkDropControllerMotion::enter:
+   * @self: the object which received the signal
+   * @x: coordinates of pointer location
+   * @y: coordinates of pointer location
+   *
+   * Signals that the pointer has entered the widget.
+   */
+  signals[ENTER] =
+    g_signal_new (I_("enter"),
+                  GTK_TYPE_DROP_CONTROLLER_MOTION,
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 2,
+                  G_TYPE_DOUBLE,
+                  G_TYPE_DOUBLE);
+  g_signal_set_va_marshaller (signals[ENTER],
+                              G_TYPE_FROM_CLASS (klass),
+                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);
+
+  /**
+   * GtkDropControllerMotion::leave:
+   * @self: the object which received the signal
+   *
+   * Signals that the pointer has left the widget.
+   */
+  signals[LEAVE] =
+    g_signal_new (I_("leave"),
+                  GTK_TYPE_DROP_CONTROLLER_MOTION,
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 0);
+
+  /**
+   * GtkDropControllerMotion::self:
+   * @self: The object that received the signal
+   * @x: the x coordinate
+   * @y: the y coordinate
+   *
+   * Emitted when the pointer moves inside the widget.
+   */
+  signals[MOTION] =
+    g_signal_new (I_("self"),
+                  GTK_TYPE_DROP_CONTROLLER_MOTION,
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  _gtk_marshal_VOID__DOUBLE_DOUBLE,
+                  G_TYPE_NONE, 2,
+                  G_TYPE_DOUBLE,
+                  G_TYPE_DOUBLE);
+  g_signal_set_va_marshaller (signals[MOTION],
+                              G_TYPE_FROM_CLASS (klass),
+                              _gtk_marshal_VOID__DOUBLE_DOUBLEv);
+}
+
+static void
+gtk_drop_controller_motion_init (GtkDropControllerMotion *self)
+{
+}
+
+/**
+ * gtk_drop_controller_motion_new:
+ *
+ * Creates a new event controller that will handle pointer motion
+ * events during drag and drop.
+ *
+ * Returns: a new #GtkDropControllerMotion
+ **/
+GtkEventController *
+gtk_drop_controller_motion_new (void)
+{
+  return g_object_new (GTK_TYPE_DROP_CONTROLLER_MOTION,
+                       NULL);
+}
+
+/**
+ * gtk_drop_controller_motion_contains_pointer:
+ * @self: a #GtkDropControllerMotion
+ *
+ * Returns the value of the GtkDropControllerMotion:contains-pointer property.
+ *
+ * Returns: %TRUE if a dragging pointer is within @self or one of its children.
+ */
+gboolean
+gtk_drop_controller_motion_contains_pointer (GtkDropControllerMotion *self)
+{
+  g_return_val_if_fail (GTK_IS_DROP_CONTROLLER_MOTION (self), FALSE);
+
+  return self->contains_pointer;
+}
+
+/**
+ * gtk_drop_controller_motion_get_drop:
+ * @self: a #GtkDropControllerMotion
+ *
+ * Returns the value of the GtkDropControllerMotion:drop property.
+ *
+ * Returns: The #GdkDrop currently happening within @self or %NULL if none
+ */
+GdkDrop *
+gtk_drop_controller_motion_get_drop (GtkDropControllerMotion *self)
+{
+  g_return_val_if_fail (GTK_IS_DROP_CONTROLLER_MOTION (self), FALSE);
+
+  return self->drop;
+}
+
+/**
+ * gtk_drop_controller_motion_is_pointer:
+ * @self: a #GtkEventControllerKey
+ *
+ * Returns the value of the GtkDropControllerMotion:is-pointer property.
+ *
+ * Returns: %TRUE if a dragging pointer is within @self but not one of its children
+ */
+gboolean
+gtk_drop_controller_motion_is_pointer (GtkDropControllerMotion *self)
+{
+  g_return_val_if_fail (GTK_IS_DROP_CONTROLLER_MOTION (self), FALSE);
+
+  return self->is_pointer;
+}
diff --git a/gtk/gtkdropcontrollermotion.h b/gtk/gtkdropcontrollermotion.h
new file mode 100644
index 0000000000..5eee5c6478
--- /dev/null
+++ b/gtk/gtkdropcontrollermotion.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright © 2020 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_DROP_CONTROLLER_MOTION_H__
+#define __GTK_DROP_CONTROLLER_MOTION_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gdk/gdk.h>
+#include <gtk/gtkeventcontroller.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_DROP_CONTROLLER_MOTION         (gtk_drop_controller_motion_get_type ())
+#define GTK_DROP_CONTROLLER_MOTION(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), 
GTK_TYPE_DROP_CONTROLLER_MOTION, GtkDropControllerMotion))
+#define GTK_DROP_CONTROLLER_MOTION_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), 
GTK_TYPE_DROP_CONTROLLER_MOTION, GtkDropControllerMotionClass))
+#define GTK_IS_DROP_CONTROLLER_MOTION(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), 
GTK_TYPE_DROP_CONTROLLER_MOTION))
+#define GTK_IS_DROP_CONTROLLER_MOTION_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), 
GTK_TYPE_DROP_CONTROLLER_MOTION))
+#define GTK_DROP_CONTROLLER_MOTION_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), 
GTK_TYPE_DROP_CONTROLLER_MOTION, GtkDropControllerMotionClass))
+
+typedef struct _GtkDropControllerMotion GtkDropControllerMotion;
+typedef struct _GtkDropControllerMotionClass GtkDropControllerMotionClass;
+
+GDK_AVAILABLE_IN_ALL
+GType                   gtk_drop_controller_motion_get_type             (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_ALL
+GtkEventController *    gtk_drop_controller_motion_new                  (void);
+
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_drop_controller_motion_contains_pointer     (GtkDropControllerMotion        
*self);
+GDK_AVAILABLE_IN_ALL
+GdkDrop *               gtk_drop_controller_motion_get_drop             (GtkDropControllerMotion        
*self);
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_drop_controller_motion_is_pointer           (GtkDropControllerMotion        
*self);
+
+G_END_DECLS
+
+#endif /* __GTK_DROP_CONTROLLER_MOTION_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index a09b672130..6851816f2c 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -211,6 +211,7 @@ gtk_public_sources = files([
   'gtkdragicon.c',
   'gtkdragsource.c',
   'gtkdrawingarea.c',
+  'gtkdropcontrollermotion.c',
   'gtkeditable.c',
   'gtkemojichooser.c',
   'gtkemojicompletion.c',
@@ -461,6 +462,7 @@ gtk_public_headers = files([
   'gtkdragicon.h',
   'gtkdragsource.h',
   'gtkdrawingarea.h',
+  'gtkdropcontrollermotion.h',
   'gtkeditable.h',
   'gtkemojichooser.h',
   'gtkentry.h',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]