[gtk] widget: Add gtk_widget_compute_point()



commit e223f16aa912487751d1c098d8da780fa9c09dfa
Author: Benjamin Otte <otte redhat com>
Date:   Tue Feb 19 19:15:16 2019 +0100

    widget: Add gtk_widget_compute_point()
    
    It's the replacement for gtk_widget_translate_coordinates()

 docs/reference/gtk/gtk4-sections.txt |  1 +
 gtk/gtkgesturestylus.c               | 14 +++---
 gtk/gtkprivate.h                     |  7 ---
 gtk/gtkwidget.c                      | 84 ++++++++++++++++++------------------
 gtk/gtkwidget.h                      | 19 +++++---
 5 files changed, 62 insertions(+), 63 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index b6785ed0c5..6c084c4063 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -4542,6 +4542,7 @@ gtk_widget_get_width
 gtk_widget_get_height
 gtk_widget_compute_bounds
 gtk_widget_compute_transform
+gtk_widget_compute_point
 gtk_widget_contains
 gtk_widget_pick
 gtk_widget_get_can_default
diff --git a/gtk/gtkgesturestylus.c b/gtk/gtkgesturestylus.c
index 8084689bfb..4c9d704f6d 100644
--- a/gtk/gtkgesturestylus.c
+++ b/gtk/gtkgesturestylus.c
@@ -280,15 +280,17 @@ gtk_gesture_stylus_get_backlog (GtkGestureStylus  *gesture,
   for (l = history; l; l = l->next)
     {
       GdkTimeCoord *time_coord = l->data;
+      graphene_point_t p;
 
       g_array_append_val (backlog_array, *time_coord);
       time_coord = &g_array_index (backlog_array, GdkTimeCoord, backlog_array->len - 1);
-      gtk_widget_translate_coordinatesf (gtk_get_event_widget (event),
-                                         gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)),
-                                         time_coord->axes[GDK_AXIS_X],
-                                         time_coord->axes[GDK_AXIS_Y],
-                                         &time_coord->axes[GDK_AXIS_X],
-                                         &time_coord->axes[GDK_AXIS_Y]);
+      gtk_widget_compute_point (gtk_get_event_widget (event),
+                                gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)),
+                                &GRAPHENE_POINT_INIT (time_coord->axes[GDK_AXIS_X],
+                                                      time_coord->axes[GDK_AXIS_Y]),
+                                &p);
+      time_coord->axes[GDK_AXIS_X] = p.x;
+      time_coord->axes[GDK_AXIS_Y] = p.y;
     }
 
   *n_elems = backlog_array->len;
diff --git a/gtk/gtkprivate.h b/gtk/gtkprivate.h
index 4f35ebec66..81f9068693 100644
--- a/gtk/gtkprivate.h
+++ b/gtk/gtkprivate.h
@@ -94,13 +94,6 @@ void             gtk_propagate_event_internal  (GtkWidget       *widget,
                                                 GdkEvent        *event,
                                                 GtkWidget       *topmost);
 
-gboolean         gtk_widget_translate_coordinatesf (GtkWidget  *src_widget,
-                                                    GtkWidget  *dest_widget,
-                                                    double      src_x,
-                                                    double      src_y,
-                                                    double     *dest_x,
-                                                    double     *dest_y);
-
 gdouble _gtk_get_slowdown (void);
 void    _gtk_set_slowdown (gdouble slowdown_factor);
 
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 6a62f5b3e4..31ade6ded0 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -2536,6 +2536,7 @@ _gtk_widget_emulate_press (GtkWidget      *widget,
   GtkWidget *event_widget, *next_child, *parent;
   GdkEvent *press;
   gdouble x, y;
+  graphene_point_t p;
 
   event_widget = gtk_get_event_target ((GdkEvent *) event);
 
@@ -2543,10 +2544,10 @@ _gtk_widget_emulate_press (GtkWidget      *widget,
     return;
 
   gdk_event_get_coords (event, &x, &y);
-  gtk_widget_translate_coordinatesf (event_widget,
-                                     gtk_widget_get_toplevel (event_widget),
-                                     x, y,
-                                     &x, &y);
+  gtk_widget_compute_point (event_widget,
+                            gtk_widget_get_toplevel (event_widget),
+                            &GRAPHENE_POINT_INIT (x, y),
+                            &p);
 
   if (event->any.type == GDK_TOUCH_BEGIN ||
       event->any.type == GDK_TOUCH_UPDATE ||
@@ -2592,7 +2593,7 @@ _gtk_widget_emulate_press (GtkWidget      *widget,
   else
     return;
 
-  gdk_event_set_coords (press, x, y);
+  gdk_event_set_coords (press, p.x, p.y);
 
   press->any.send_event = TRUE;
   next_child = event_widget;
@@ -4475,58 +4476,55 @@ gtk_widget_translate_coordinates (GtkWidget  *src_widget,
                                   gint       *dest_x,
                                   gint       *dest_y)
 {
-  double x, y;
+  graphene_point_t p;
 
-  if (!gtk_widget_translate_coordinatesf (src_widget, dest_widget,
-                                          src_x, src_y, &x, &y))
+  if (!gtk_widget_compute_point (src_widget, dest_widget,
+                                 &GRAPHENE_POINT_INIT (src_x, src_y),
+                                 &p))
     return FALSE;
 
   if (dest_x)
-    *dest_x =x;
+    *dest_x = p.x;
 
   if (dest_y)
-    *dest_y = y;
+    *dest_y = p.y;
 
   return TRUE;
 }
 
-/* This is the same as translate_coordinates, but it works on doubles.
- * We use this for event coordinates.
+/**
+ * gtk_widget_compute_point:
+ * @widget: the #GtkWidget to query
+ * @target: the #GtkWidget to transform into
+ * @point: a point in @widget's coordinate system
+ * @out_point: (out caller-allocates): Set to the corresponding coordinates in
+ *     @target's coordinate system
+ *
+ * Translates the given @point in @widget's coordinates to coordinates
+ * relative to @target’s coodinate system. In order to perform this
+ * operation, both widgets must share a common ancestor.
  *
- * We should probably decide for only one of the 2 versions at some point */
+ * Returns: %TRUE if the point could be determined, %FALSE on failure.
+ *   In this case, 0 is stored in @out_point.
+ **/
 gboolean
-gtk_widget_translate_coordinatesf (GtkWidget  *src_widget,
-                                   GtkWidget  *dest_widget,
-                                   double      src_x,
-                                   double      src_y,
-                                   double     *dest_x,
-                                   double     *dest_y)
+gtk_widget_compute_point (GtkWidget              *widget,
+                          GtkWidget              *target,
+                          const graphene_point_t *point,
+                          graphene_point_t       *out_point)
 {
   graphene_matrix_t transform;
-  graphene_point_t p;
 
-  g_return_val_if_fail (GTK_IS_WIDGET (src_widget), FALSE);
-  g_return_val_if_fail (GTK_IS_WIDGET (dest_widget), FALSE);
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
+  g_return_val_if_fail (GTK_IS_WIDGET (target), FALSE);
 
-  if (!gtk_widget_compute_transform (src_widget, dest_widget, &transform))
+  if (!gtk_widget_compute_transform (widget, target, &transform))
     {
-      if (dest_x)
-        *dest_x = 0;
-
-      if (dest_y)
-        *dest_y = 0;
-
+      graphene_point_init (out_point, 0, 0);
       return FALSE;
     }
 
-  graphene_point_init (&p, src_x, src_y);
-  graphene_matrix_transform_point (&transform, &p, &p);
-
-  if (dest_x)
-    *dest_x = p.x;
-
-  if (dest_y)
-    *dest_y = p.y;
+  graphene_matrix_transform_point (&transform, point, out_point);
 
   return TRUE;
 }
@@ -5204,19 +5202,19 @@ translate_event_coordinates (GdkEvent  *event,
 {
   GtkWidget *event_widget;
   double x, y;
-  double dx = 0.0, dy = 0.0;
+  graphene_point_t p;
 
   if (!gdk_event_get_coords (event, &x, &y))
     return;
 
   event_widget = gtk_get_event_widget (event);
 
-  gtk_widget_translate_coordinatesf (event_widget,
-                                     widget,
-                                     x, y,
-                                     &dx, &dy);
+  gtk_widget_compute_point (event_widget,
+                            widget,
+                            &GRAPHENE_POINT_INIT (x, y),
+                            &p);
 
-  gdk_event_set_coords (event, dx, dy);
+  gdk_event_set_coords (event, p.x, p.y);
 }
 
 static gboolean
diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h
index f62049a894..1d06747d57 100644
--- a/gtk/gtkwidget.h
+++ b/gtk/gtkwidget.h
@@ -614,13 +614,18 @@ GDK_AVAILABLE_IN_ALL
 void                  gtk_widget_get_allocation         (GtkWidget     *widget,
                                                          GtkAllocation *allocation);
 GDK_AVAILABLE_IN_ALL
-gboolean              gtk_widget_compute_transform      (GtkWidget         *widget,
-                                                         GtkWidget         *target,
-                                                         graphene_matrix_t *out_transform);
-GDK_AVAILABLE_IN_ALL
-gboolean              gtk_widget_compute_bounds         (GtkWidget     *widget,
-                                                         GtkWidget     *target,
-                                                         graphene_rect_t *out_bounds);
+gboolean                gtk_widget_compute_transform            (GtkWidget              *widget,
+                                                                 GtkWidget              *target,
+                                                                 graphene_matrix_t      *out_transform);
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_widget_compute_bounds               (GtkWidget              *widget,
+                                                                 GtkWidget              *target,
+                                                                 graphene_rect_t        *out_bounds);
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_widget_compute_point                (GtkWidget              *widget,
+                                                                 GtkWidget              *target,
+                                                                 const graphene_point_t *point,
+                                                                 graphene_point_t       *out_point);
 
 GDK_AVAILABLE_IN_ALL
 int                   gtk_widget_get_width              (GtkWidget     *widget);


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