[gtk/current-event-apis: 3/7] Add gtk_event_controller_get_current_event[_state]



commit 4e5fc081d0ebb5a65dbf718666acd0d224e76b21
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Apr 11 12:40:21 2020 -0400

    Add gtk_event_controller_get_current_event[_state]
    
    For now these are private, to replace internal uses
    of gtk_get_current_event[_state], but we may want to
    make them public.

 docs/reference/gtk/gtk4-sections.txt |  6 +++
 gtk/gtkeventcontroller.c             | 84 +++++++++++++++++++++++++++++++++++-
 gtk/gtkeventcontroller.h             |  9 ++++
 gtk/gtkeventcontrollerprivate.h      |  3 +-
 4 files changed, 99 insertions(+), 3 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 98d7fed3aa..64c459a197 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -5815,6 +5815,12 @@ gtk_event_controller_set_propagation_limit
 gtk_event_controller_get_widget
 gtk_event_controller_reset
 
+<SUBSECTION>
+gtk_event_controller_get_current_event
+gtk_event_controller_get_current_event_device
+gtk_event_controller_get_current_event_state
+gtk_event_controller_get_current_event_time
+
 <SUBSECTION Standard>
 GTK_TYPE_EVENT_CONTROLLER
 GTK_EVENT_CONTROLLER
diff --git a/gtk/gtkeventcontroller.c b/gtk/gtkeventcontroller.c
index f8a9d701dd..824e75b983 100644
--- a/gtk/gtkeventcontroller.c
+++ b/gtk/gtkeventcontroller.c
@@ -59,6 +59,7 @@ struct _GtkEventControllerPrivate
   GtkPropagationLimit limit;
   char *name;
   GtkWidget *target;
+  GdkEvent *event;
 };
 
 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkEventController, gtk_event_controller, G_TYPE_OBJECT)
@@ -337,13 +338,15 @@ gtk_event_controller_handle_event (GtkEventController *controller,
 
   controller_class = GTK_EVENT_CONTROLLER_GET_CLASS (controller);
 
-  priv->target = target;
+  priv->target = g_object_ref (target);
+  priv->event = gdk_event_ref (event);
 
   g_object_ref (controller);
   retval = controller_class->handle_event (controller, event, x, y);
   g_object_unref (controller);
 
-  priv->target = NULL;
+  g_clear_object (&priv->target);
+  g_clear_pointer (&priv->event, gdk_event_unref);
 
   return retval;
 }
@@ -532,6 +535,83 @@ gtk_event_controller_get_target (GtkEventController *controller)
   return priv->target;
 }
 
+/**
+ * gtk_event_controller_get_current_event:
+ * @controller: a #GtkEventController
+ *
+ * Returns the event that is currently being handled by the
+ * controller, and %NULL at other times.
+ *
+ * Returns: (nullable) the event is current handled by @controller
+ */
+GdkEvent *
+gtk_event_controller_get_current_event (GtkEventController *controller)
+{
+  GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (controller);
+
+  return priv->event;
+}
+
+/**
+ * gtk_event_controller_get_current_event_time:
+ * @controller: a #GtkEventController
+ *
+ * Returns the timestamp of the event that is currently being
+ * handled by the controller, and 0 otherwise.
+ *
+ * Returns: timestamp of the event is current handled by @controller
+ */
+guint32
+gtk_event_controller_get_current_event_time (GtkEventController *controller)
+{
+  GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (controller);
+
+  if (priv->event)
+    return gdk_event_get_time (priv->event);
+
+  return 0;
+}
+
+/**
+ * gtk_event_controller_get_current_event_device:
+ * @controller: a #GtkEventController
+ *
+ * Returns the device of the event that is currently being
+ * handled by the controller, and %NULL otherwise.
+ *
+ * Returns: (nullable): device of the event is current handled by @controller
+ */
+GdkDevice *
+gtk_event_controller_get_current_event_device (GtkEventController *controller)
+{
+  GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (controller);
+
+  if (priv->event)
+    return gdk_event_get_device (priv->event);
+
+  return NULL;
+}
+
+/**
+ * gtk_event_controller_get_current_event_device:
+ * @controller: a #GtkEventController
+ *
+ * Returns the modifier state of the event that is currently being
+ * handled by the controller, and 0 otherwise.
+ *
+ * Returns: (nullable): modifier state of the event is current handled by @controller
+ */
+GdkModifierType
+gtk_event_controller_get_current_event_state (GtkEventController *controller)
+{
+  GtkEventControllerPrivate *priv = gtk_event_controller_get_instance_private (controller);
+
+  if (priv->event)
+    return gdk_event_get_modifier_state (priv->event);
+
+  return 0;
+}
+
 static GtkCrossingData *
 gtk_crossing_data_copy (GtkCrossingData *crossing)
 {
diff --git a/gtk/gtkeventcontroller.h b/gtk/gtkeventcontroller.h
index 6bdcfbb678..4b33432895 100644
--- a/gtk/gtkeventcontroller.h
+++ b/gtk/gtkeventcontroller.h
@@ -72,6 +72,15 @@ GDK_AVAILABLE_IN_ALL
 void                gtk_event_controller_set_name              (GtkEventController *controller,
                                                                 const char         *name);
 
+GDK_AVAILABLE_IN_ALL
+GdkEvent *          gtk_event_controller_get_current_event    (GtkEventController *controller);
+GDK_AVAILABLE_IN_ALL
+guint32             gtk_event_controller_get_current_event_time   (GtkEventController *controller);
+GDK_AVAILABLE_IN_ALL
+GdkDevice *         gtk_event_controller_get_current_event_device (GtkEventController *controller);
+GDK_AVAILABLE_IN_ALL
+GdkModifierType     gtk_event_controller_get_current_event_state (GtkEventController *controller);
+
 G_END_DECLS
 
 #endif /* __GTK_EVENT_CONTROLLER_H__ */
diff --git a/gtk/gtkeventcontrollerprivate.h b/gtk/gtkeventcontrollerprivate.h
index 571d78f960..c997f7951e 100644
--- a/gtk/gtkeventcontrollerprivate.h
+++ b/gtk/gtkeventcontrollerprivate.h
@@ -100,7 +100,8 @@ struct _GtkEventControllerClass
   gpointer padding[10];
 };
 
-GtkWidget *gtk_event_controller_get_target (GtkEventController *controller);
+GtkWidget * gtk_event_controller_get_target (GtkEventController *controller);
+
 
 gboolean   gtk_event_controller_handle_event   (GtkEventController *controller,
                                                 GdkEvent           *event,


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