[gtk/wip/otte/canvas: 29/36] canvas: CanvasPoint => CanvasVector




commit 068d73f3aee960d1d3c6f74b89ec757891d94932
Author: Benjamin Otte <otte redhat com>
Date:   Mon Jul 4 08:04:23 2022 +0200

    canvas: CanvasPoint => CanvasVector
    
    Rename GtkCanvasPoint to GtkCanvasVector.

 demos/gtk-demo/canvas_intro.c               |  10 +-
 demos/gtk-demo/canvas_puzzle.c              |  10 +-
 gtk/gtk.h                                   |   2 +-
 gtk/gtkcanvasbox.c                          |  16 +--
 gtk/gtkcanvasbox.h                          |  24 ++---
 gtk/gtkcanvaspoint.c                        | 150 ----------------------------
 gtk/gtkcanvassize.c                         |  16 +--
 gtk/gtkcanvassize.h                         |   4 +-
 gtk/gtkcanvasvector.c                       | 126 +++++++++++++++++++++++
 gtk/{gtkcanvaspoint.h => gtkcanvasvector.h} |  30 +++---
 gtk/gtkcanvasvectorimpl.c                   |  15 +++
 gtk/gtkcanvasvectorprivate.h                |   6 +-
 gtk/gtktypes.h                              |   2 +-
 gtk/meson.build                             |   4 +-
 14 files changed, 199 insertions(+), 216 deletions(-)
---
diff --git a/demos/gtk-demo/canvas_intro.c b/demos/gtk-demo/canvas_intro.c
index 715bb2529f..2ab4974076 100644
--- a/demos/gtk-demo/canvas_intro.c
+++ b/demos/gtk-demo/canvas_intro.c
@@ -14,24 +14,24 @@ static void
 bind_item (GtkListItemFactory *factory,
            GtkCanvasItem      *ci)
 {
-  GtkCanvasPoint *point;
+  GtkCanvasVector *point;
   GtkCanvasSize *size;
   GtkCanvasBox *box;
 
   gtk_canvas_item_set_widget (ci, gtk_canvas_item_get_item (ci));
 
   /* Also center the item, so we do something interesting */
-  point = gtk_canvas_point_new (0, 0);
+  point = gtk_canvas_vector_new (0, 0);
   box = gtk_canvas_box_new (point,
                             gtk_canvas_get_viewport_size (gtk_canvas_item_get_canvas (ci)),
                             0.0, 0.0);
-  gtk_canvas_point_free (point);
+  gtk_canvas_vector_free (point);
 
-  point = gtk_canvas_point_new_from_box (box, 0.5, 0.5);
+  point = gtk_canvas_vector_new_from_box (box, 0.5, 0.5);
   gtk_canvas_box_free (box);
   size = gtk_canvas_size_new_measure_item (ci, GTK_CANVAS_ITEM_MEASURE_MIN_FOR_MIN);
   box = gtk_canvas_box_new (point, size, 0.5, 0.5);
-  gtk_canvas_point_free (point);
+  gtk_canvas_vector_free (point);
   gtk_canvas_size_free (size);
 
   gtk_canvas_item_set_bounds (ci, box);
diff --git a/demos/gtk-demo/canvas_puzzle.c b/demos/gtk-demo/canvas_puzzle.c
index c30d24a346..09cb2a6eff 100644
--- a/demos/gtk-demo/canvas_puzzle.c
+++ b/demos/gtk-demo/canvas_puzzle.c
@@ -14,24 +14,24 @@ set_item_position (GtkCanvasItem *ci,
                    float          x,
                    float          y)
 {
-  GtkCanvasPoint *point;
+  GtkCanvasVector *point;
   GtkCanvasSize *size;
   GtkCanvasBox *box, *viewport;
 
   x = CLAMP (x, 0, 1);
   y = CLAMP (y, 0, 1);
 
-  point = gtk_canvas_point_new (0, 0);
+  point = gtk_canvas_vector_new (0, 0);
   viewport = gtk_canvas_box_new (point,
                                  gtk_canvas_get_viewport_size (gtk_canvas_item_get_canvas (ci)),
                                  0.0, 0.0);
-  gtk_canvas_point_free (point);
+  gtk_canvas_vector_free (point);
 
-  point = gtk_canvas_point_new_from_box (viewport, x, y);
+  point = gtk_canvas_vector_new_from_box (viewport, x, y);
   gtk_canvas_box_free (viewport);
   size = gtk_canvas_size_new (0, 0);
   box = gtk_canvas_box_new (point, size, x, y);
-  gtk_canvas_point_free (point);
+  gtk_canvas_vector_free (point);
   gtk_canvas_size_free (size);
 
   gtk_canvas_item_set_bounds (ci, box);
diff --git a/gtk/gtk.h b/gtk/gtk.h
index 9ed46e9f96..d534c33517 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -62,7 +62,7 @@
 #include <gtk/gtkcanvas.h>
 #include <gtk/gtkcanvasbox.h>
 #include <gtk/gtkcanvasitem.h>
-#include <gtk/gtkcanvaspoint.h>
+#include <gtk/gtkcanvasvector.h>
 #include <gtk/gtkcanvassize.h>
 #include <gtk/gtkcellarea.h>
 #include <gtk/gtkcellareabox.h>
diff --git a/gtk/gtkcanvasbox.c b/gtk/gtkcanvasbox.c
index ac76fce336..7851d7b0fe 100644
--- a/gtk/gtkcanvasbox.c
+++ b/gtk/gtkcanvasbox.c
@@ -90,8 +90,8 @@ gtk_canvas_box_update_variable (GtkCanvasBox       *self,
  * Returns: a new box
  **/
 GtkCanvasBox *
-gtk_canvas_box_new_points (const GtkCanvasPoint *point1,
-                           const GtkCanvasPoint *point2)
+gtk_canvas_box_new_points (const GtkCanvasVector *point1,
+                           const GtkCanvasVector *point2)
 {
   GtkCanvasVector size;
   GtkCanvasBox *result;
@@ -129,10 +129,10 @@ gtk_canvas_box_new_points (const GtkCanvasPoint *point1,
  * Returns: a new box
  **/
 GtkCanvasBox *
-gtk_canvas_box_new (const GtkCanvasPoint *point,
-                    const GtkCanvasSize  *size,
-                    float                 origin_x,
-                    float                 origin_y)
+gtk_canvas_box_new (const GtkCanvasVector *point,
+                    const GtkCanvasSize   *size,
+                    float                  origin_x,
+                    float                  origin_y)
 {
   GtkCanvasBox *self;
   graphene_vec2_t origin;
@@ -199,12 +199,12 @@ gtk_canvas_box_eval (const GtkCanvasBox *self,
   return TRUE;
 }
 
-const GtkCanvasPoint *
+const GtkCanvasVector *
 gtk_canvas_box_get_point (const GtkCanvasBox *self)
 {
   g_return_val_if_fail (self != NULL, NULL);
 
-  return (GtkCanvasPoint *) &self->point;
+  return &self->point;
 }
 
 const GtkCanvasSize *
diff --git a/gtk/gtkcanvasbox.h b/gtk/gtkcanvasbox.h
index 4bbfde583a..7101be12d5 100644
--- a/gtk/gtkcanvasbox.h
+++ b/gtk/gtkcanvasbox.h
@@ -36,31 +36,31 @@ GDK_AVAILABLE_IN_ALL
 GType                   gtk_canvas_box_get_type               (void) G_GNUC_CONST;
 
 GDK_AVAILABLE_IN_ALL
-GtkCanvasBox *          gtk_canvas_box_copy                   (const GtkCanvasBox   *self);
+GtkCanvasBox *          gtk_canvas_box_copy                   (const GtkCanvasBox       *self);
 GDK_AVAILABLE_IN_ALL
-void                    gtk_canvas_box_free                   (GtkCanvasBox         *self);
+void                    gtk_canvas_box_free                   (GtkCanvasBox             *self);
 
 GDK_AVAILABLE_IN_ALL
-const GtkCanvasPoint *  gtk_canvas_box_get_point              (const GtkCanvasBox   *self) G_GNUC_PURE;
+const GtkCanvasVector * gtk_canvas_box_get_point              (const GtkCanvasBox       *self) G_GNUC_PURE;
 GDK_AVAILABLE_IN_ALL
-const GtkCanvasSize *   gtk_canvas_box_get_size               (const GtkCanvasBox   *self) G_GNUC_PURE;
+const GtkCanvasSize *   gtk_canvas_box_get_size               (const GtkCanvasBox       *self) G_GNUC_PURE;
 GDK_AVAILABLE_IN_ALL
-void                    gtk_canvas_box_get_origin             (const GtkCanvasBox   *self,
-                                                               float                *x,
-                                                               float                *y);
+void                    gtk_canvas_box_get_origin             (const GtkCanvasBox       *self,
+                                                               float                    *x,
+                                                               float                    *y);
 
 GDK_AVAILABLE_IN_ALL
-gboolean                gtk_canvas_box_eval                   (const GtkCanvasBox   *self,
-                                                               graphene_rect_t      *rect) 
G_GNUC_WARN_UNUSED_RESULT;
+gboolean                gtk_canvas_box_eval                   (const GtkCanvasBox       *self,
+                                                               graphene_rect_t          *rect) 
G_GNUC_WARN_UNUSED_RESULT;
 
 GDK_AVAILABLE_IN_ALL
-GtkCanvasBox *          gtk_canvas_box_new                    (const GtkCanvasPoint     *point,
+GtkCanvasBox *          gtk_canvas_box_new                    (const GtkCanvasVector    *point,
                                                                const GtkCanvasSize      *size,
                                                                float                     origin_x,
                                                                float                     origin_y);
 GDK_AVAILABLE_IN_ALL
-GtkCanvasBox *          gtk_canvas_box_new_points             (const GtkCanvasPoint     *point1,
-                                                               const GtkCanvasPoint     *point2);
+GtkCanvasBox *          gtk_canvas_box_new_points             (const GtkCanvasVector    *point1,
+                                                               const GtkCanvasVector    *point2);
 
 GDK_AVAILABLE_IN_ALL
 const GtkCanvasBox *    gtk_canvas_box_get_item_bounds        (GtkCanvasItem            *item);
diff --git a/gtk/gtkcanvassize.c b/gtk/gtkcanvassize.c
index 72406e574d..3da516faf7 100644
--- a/gtk/gtkcanvassize.c
+++ b/gtk/gtkcanvassize.c
@@ -30,7 +30,7 @@
 
 #include "gtkcanvasboxprivate.h"
 #include "gtkcanvasitemprivate.h"
-#include "gtkcanvaspoint.h"
+#include "gtkcanvasvector.h"
 #include "gtkcanvasvectorprivate.h"
 
 G_DEFINE_BOXED_TYPE (GtkCanvasSize, gtk_canvas_size,
@@ -100,8 +100,8 @@ gtk_canvas_size_new_from_box (const GtkCanvasBox *box)
  * Returns: a new size
  **/
 GtkCanvasSize *
-gtk_canvas_size_new_distance (const GtkCanvasPoint *from,
-                              const GtkCanvasPoint *to)
+gtk_canvas_size_new_distance (const GtkCanvasVector *from,
+                              const GtkCanvasVector *to)
 {
   GtkCanvasSize *self;
   graphene_vec2_t minus_one;
@@ -113,11 +113,11 @@ gtk_canvas_size_new_distance (const GtkCanvasPoint *from,
 
   self = gtk_canvas_size_alloc ();
   gtk_canvas_vector_init_sum (&self->vec2,
-                            graphene_vec2_one (),
-                            from,
-                            &minus_one,
-                            to,
-                            NULL);
+                              graphene_vec2_one (),
+                              from,
+                              &minus_one,
+                              to,
+                              NULL);
 
   return self;
 }
diff --git a/gtk/gtkcanvassize.h b/gtk/gtkcanvassize.h
index 9c2558320f..26e5ef1733 100644
--- a/gtk/gtkcanvassize.h
+++ b/gtk/gtkcanvassize.h
@@ -48,8 +48,8 @@ GDK_AVAILABLE_IN_ALL
 GtkCanvasSize *         gtk_canvas_size_new                     (float                   width,
                                                                  float                   height);
 GDK_AVAILABLE_IN_ALL
-GtkCanvasSize *         gtk_canvas_size_new_distance            (const GtkCanvasPoint   *from,
-                                                                 const GtkCanvasPoint   *to);
+GtkCanvasSize *         gtk_canvas_size_new_distance            (const GtkCanvasVector  *from,
+                                                                 const GtkCanvasVector  *to);
 GDK_AVAILABLE_IN_ALL
 GtkCanvasSize *         gtk_canvas_size_new_from_box            (const GtkCanvasBox     *box);
 
diff --git a/gtk/gtkcanvasvector.c b/gtk/gtkcanvasvector.c
new file mode 100644
index 0000000000..81b87c81fd
--- /dev/null
+++ b/gtk/gtkcanvasvector.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2022 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>
+ */
+
+
+/**
+ * GtkCanvasVector:
+ *
+ * `GtkCanvasVector` describes a point, size or scale in the coordinate
+ * system of a `GtkCanvas`.
+ *
+ * Vectors are automatically-updating expressions that can track other vectors
+ * in the canvas, and constructing the vectors to place `GtkCanvasItem`s on the
+ * canvas is the main thing about `GtkCanvas`.
+ */
+
+#include "config.h"
+
+#include "gtkcanvasvectorprivate.h"
+
+#include "gtkcanvasboxprivate.h"
+
+G_DEFINE_BOXED_TYPE (GtkCanvasVector, gtk_canvas_vector,
+                     gtk_canvas_vector_copy,
+                     gtk_canvas_vector_free)
+
+static GtkCanvasVector *
+gtk_canvas_vector_alloc (void)
+{
+  return g_slice_new (GtkCanvasVector);
+}
+
+/**
+ * gtk_canvas_vector_new:
+ * @x: x coordinate
+ * @y: y coordinate
+ *
+ * Creates a new vector at the given coordinate.
+ *
+ * Returns: a new vector
+ **/
+GtkCanvasVector *
+gtk_canvas_vector_new (float x,
+                       float y)
+{
+  GtkCanvasVector *self;
+
+  self = gtk_canvas_vector_alloc ();
+  gtk_canvas_vector_init_constant (self, x, y);
+
+  return self;
+}
+
+/**
+ * gtk_canvas_vector_new_from_box:
+ * @box: a box
+ * @origin_x: x coordinate of box origin
+ * @origin_y: y coordinate of box origin
+ *
+ * Creates a vector relative to the given box.
+ *
+ * The origin describes where in the box the vector is, with
+ * (0, 0) being the top left and (1, 1) being the bottom right
+ * corner of the box.
+ *
+ * Returns: a new vector
+ **/
+GtkCanvasVector *
+gtk_canvas_vector_new_from_box (const GtkCanvasBox *box,
+                                float               origin_x,
+                                float               origin_y)
+{
+  GtkCanvasVector *self;
+  graphene_vec2_t origin;
+ 
+  g_return_val_if_fail (box != NULL, NULL);
+ 
+  graphene_vec2_init (&origin, origin_x, origin_y);
+  graphene_vec2_subtract (&origin, &box->origin, &origin);
+ 
+  self = gtk_canvas_vector_alloc ();
+  gtk_canvas_vector_init_sum (self,
+                              graphene_vec2_one (),
+                              &box->point,
+                              &origin,
+                              &box->size,
+                              NULL);
+  return self;
+}
+
+GtkCanvasVector *
+gtk_canvas_vector_copy (const GtkCanvasVector *self)
+{
+  GtkCanvasVector *copy;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  copy = gtk_canvas_vector_alloc ();
+  gtk_canvas_vector_init_copy (copy, self);
+
+  return copy;
+}
+
+void
+gtk_canvas_vector_free (GtkCanvasVector *self)
+{
+  gtk_canvas_vector_finish (self);
+
+  g_slice_free (GtkCanvasVector, self);
+}
+
diff --git a/gtk/gtkcanvaspoint.h b/gtk/gtkcanvasvector.h
similarity index 53%
rename from gtk/gtkcanvaspoint.h
rename to gtk/gtkcanvasvector.h
index f59db58e1a..3222dae8c8 100644
--- a/gtk/gtkcanvaspoint.h
+++ b/gtk/gtkcanvasvector.h
@@ -18,46 +18,40 @@
  */
 
 
-#ifndef __GTK_CANVAS_POINT_H__
-#define __GTK_CANVAS_POINT_H__
+#ifndef __GTK_CANVAS_VECTOR_H__
+#define __GTK_CANVAS_VECTOR_H__
 
 #if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
 #error "Only <gtk/gtk.h> can be included directly."
 #endif
 
 #include <gtk/gtktypes.h>
+#include <graphene.h>
 
 G_BEGIN_DECLS
 
-#define GTK_TYPE_CANVAS_POINT (gtk_canvas_point_get_type ())
+#define GTK_TYPE_CANVAS_VECTOR (gtk_canvas_vector_get_type ())
 
 GDK_AVAILABLE_IN_ALL
-GType                   gtk_canvas_point_get_type               (void) G_GNUC_CONST;
+GType                   gtk_canvas_vector_get_type              (void) G_GNUC_CONST;
 
 GDK_AVAILABLE_IN_ALL
-GtkCanvasPoint *        gtk_canvas_point_copy                   (const GtkCanvasPoint   *self);
+GtkCanvasVector *       gtk_canvas_vector_copy                  (const GtkCanvasVector  *self);
 GDK_AVAILABLE_IN_ALL
-void                    gtk_canvas_point_free                   (GtkCanvasPoint         *self);
+void                    gtk_canvas_vector_free                  (GtkCanvasVector        *self);
 
 GDK_AVAILABLE_IN_ALL
-gboolean                gtk_canvas_point_eval                   (const GtkCanvasPoint   *self,
-                                                                 float                  *x,
-                                                                 float                  *y) 
G_GNUC_WARN_UNUSED_RESULT;
+gboolean                gtk_canvas_vector_eval                  (const GtkCanvasVector  *self,
+                                                                 graphene_vec2_t        *result) 
G_GNUC_WARN_UNUSED_RESULT;
 
 GDK_AVAILABLE_IN_ALL
-GtkCanvasPoint *        gtk_canvas_point_new                    (float                   x,
+GtkCanvasVector *       gtk_canvas_vector_new                   (float                   x,
                                                                  float                   y);
 GDK_AVAILABLE_IN_ALL
-GtkCanvasPoint *        gtk_canvas_point_new_from_box           (const GtkCanvasBox     *box,
+GtkCanvasVector *       gtk_canvas_vector_new_from_box          (const GtkCanvasBox     *box,
                                                                  float                   origin_x,
                                                                  float                   origin_y);
-GDK_AVAILABLE_IN_ALL
-GtkCanvasPoint *        gtk_canvas_point_new_from_item          (GtkCanvasItem          *item,
-                                                                 float                   origin_x,
-                                                                 float                   origin_y,
-                                                                 float                   offset_x,
-                                                                 float                   offset_y);
 
 G_END_DECLS
 
-#endif /* __GTK_POINT_H__ */
+#endif /* __GTK_VECTOR_H__ */
diff --git a/gtk/gtkcanvasvectorimpl.c b/gtk/gtkcanvasvectorimpl.c
index 154e314d8f..3f75156315 100644
--- a/gtk/gtkcanvasvectorimpl.c
+++ b/gtk/gtkcanvasvectorimpl.c
@@ -332,6 +332,21 @@ gtk_canvas_vector_finish (GtkCanvasVector *self)
   self->class->finish (self);
 }
 
+/**
+ * gtk_canvas_vector_eval:
+ * @self: a `GtkCanvasVector`
+ * @result: (out) (caller-allocates): The current value
+ *   of the vector
+ *
+ * Evaluates the given vector and returns its x and y value.
+ *
+ * If the vector currently has no value - because it
+ * references and object that has been deleted or because
+ * the value is in the process of being updated - %FALSE
+ * is returned. Think of this as an exception being raised.
+ *
+ * Returns: %FALSE if the vector currently has no value.
+ **/
 gboolean
 gtk_canvas_vector_eval (const GtkCanvasVector *self,
                         graphene_vec2_t       *result)
diff --git a/gtk/gtkcanvasvectorprivate.h b/gtk/gtkcanvasvectorprivate.h
index 1a60406ab0..f4f63d6880 100644
--- a/gtk/gtkcanvasvectorprivate.h
+++ b/gtk/gtkcanvasvectorprivate.h
@@ -20,7 +20,8 @@
 #ifndef __GTK_CANVAS_VECTOR_PRIVATE_H__
 #define __GTK_CANVAS_VECTOR_PRIVATE_H__
 
-#include <glib.h>
+#include "gtkcanvasvector.h"
+
 #include <graphene.h>
 
 G_BEGIN_DECLS
@@ -78,9 +79,6 @@ void                    gtk_canvas_vector_init_copy             (GtkCanvasVector
                                                                  const GtkCanvasVector  *source);
 void                    gtk_canvas_vector_finish                (GtkCanvasVector        *self);
 
-gboolean                gtk_canvas_vector_eval                  (const GtkCanvasVector  *self,
-                                                                 graphene_vec2_t        *result);
-
 void                    gtk_canvas_vector_init_invalid          (GtkCanvasVector        *vector);
 void                    gtk_canvas_vector_init_constant         (GtkCanvasVector        *vector,
                                                                  float                   x,
diff --git a/gtk/gtktypes.h b/gtk/gtktypes.h
index 0bfe629a89..72f36347a4 100644
--- a/gtk/gtktypes.h
+++ b/gtk/gtktypes.h
@@ -40,7 +40,7 @@ typedef struct _GtkBuilderScope        GtkBuilderScope;
 typedef struct _GtkCanvas              GtkCanvas;
 typedef struct _GtkCanvasBox           GtkCanvasBox;
 typedef struct _GtkCanvasItem          GtkCanvasItem;
-typedef struct _GtkCanvasPoint         GtkCanvasPoint;
+typedef struct _GtkCanvasVector        GtkCanvasVector;
 typedef struct _GtkCanvasSize          GtkCanvasSize;
 typedef struct _GtkCssStyleChange      GtkCssStyleChange;
 typedef struct _GtkEventController     GtkEventController;
diff --git a/gtk/meson.build b/gtk/meson.build
index d534f0f2d5..f447ff26ed 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -186,7 +186,7 @@ gtk_public_sources = files([
   'gtkcanvas.c',
   'gtkcanvasbox.c',
   'gtkcanvasitem.c',
-  'gtkcanvaspoint.c',
+  'gtkcanvasvector.c',
   'gtkcanvassize.c',
   'gtkcellarea.c',
   'gtkcellareabox.c',
@@ -482,7 +482,7 @@ gtk_public_headers = files([
   'gtkcanvas.h',
   'gtkcanvasbox.h',
   'gtkcanvasitem.h',
-  'gtkcanvaspoint.h',
+  'gtkcanvasvector.h',
   'gtkcanvassize.h',
   'gtkcenterbox.h',
   'gtkcenterlayout.h',


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