[gtk/wip/otte/canvas: 138/138] canvas: Add gtk_canvas_get_viewport_size()




commit e64c59b8a74ac5098fadf5e5032a63f1ee437af5
Author: Benjamin Otte <otte redhat com>
Date:   Tue Jun 28 20:53:35 2022 +0200

    canvas: Add gtk_canvas_get_viewport_size()
    
    A meek attempt at exporting numbers into the sizing magic.
    
    Update the demo to really center the label now

 demos/gtk-demo/canvas_intro.c | 14 +++++++++---
 gtk/gtkcanvas.c               | 25 +++++++++++++++++++++
 gtk/gtkcanvas.h               |  2 ++
 gtk/gtkcanvassize.c           | 52 +++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkcanvassizeprivate.h    | 15 +++++++++++++
 5 files changed, 105 insertions(+), 3 deletions(-)
---
diff --git a/demos/gtk-demo/canvas_intro.c b/demos/gtk-demo/canvas_intro.c
index 3d3de59760..7216b7a7e7 100644
--- a/demos/gtk-demo/canvas_intro.c
+++ b/demos/gtk-demo/canvas_intro.c
@@ -21,13 +21,21 @@ bind_item (GtkListItemFactory *factory,
   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 (WIDTH / 2.0, HEIGHT / 2.0);
+  point = gtk_canvas_point_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);
+
+  point = gtk_canvas_point_new_from_box (box, 0.5, 0.5, 0, 0);
+  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_size_free (size);
+
   gtk_canvas_item_set_bounds (ci, box);
   gtk_canvas_box_free (box);
-  gtk_canvas_size_free (size);
-  gtk_canvas_point_free (point);
 }
 
 GtkWidget *
diff --git a/gtk/gtkcanvas.c b/gtk/gtkcanvas.c
index b69ca50429..37843b367e 100644
--- a/gtk/gtkcanvas.c
+++ b/gtk/gtkcanvas.c
@@ -23,6 +23,7 @@
 
 #include "gtkcanvasbox.h"
 #include "gtkcanvasitemprivate.h"
+#include "gtkcanvassizeprivate.h"
 #include "gtkintl.h"
 #include "gtklistitemfactory.h"
 #include "gtkwidgetprivate.h"
@@ -50,6 +51,8 @@ struct _GtkCanvas
   GtkListItemFactory *factory;
 
   GtkCanvasItems items;
+
+  GtkCanvasSize viewport_size;
 };
 
 enum
@@ -149,6 +152,17 @@ gtk_canvas_dispose (GObject *object)
   G_OBJECT_CLASS (gtk_canvas_parent_class)->dispose (object);
 }
 
+static void
+gtk_canvas_finalize (GObject *object)
+{
+  GtkCanvas *self = GTK_CANVAS (object);
+
+  gtk_canvas_clear_model (self);
+  gtk_canvas_size_finish (&self->viewport_size);
+
+  G_OBJECT_CLASS (gtk_canvas_parent_class)->finalize (object);
+}
+
 static void
 gtk_canvas_get_property (GObject    *object,
                          guint       property_id,
@@ -206,6 +220,9 @@ gtk_canvas_allocate (GtkWidget *widget,
   GtkCanvas *self = GTK_CANVAS (widget);
   gsize i;
 
+  self->viewport_size.reference.reference->width = width;
+  self->viewport_size.reference.reference->height = height;
+
   for (i = 0; i < gtk_canvas_items_get_size (&self->items); i++)
     {
       GtkCanvasItem *ci = gtk_canvas_items_get (&self->items, i);
@@ -250,6 +267,7 @@ gtk_canvas_class_init (GtkCanvasClass *klass)
   widget_class->size_allocate = gtk_canvas_allocate;
 
   gobject_class->dispose = gtk_canvas_dispose;
+  gobject_class->finalize = gtk_canvas_finalize;
   gobject_class->get_property = gtk_canvas_get_property;
   gobject_class->set_property = gtk_canvas_set_property;
 
@@ -281,6 +299,8 @@ gtk_canvas_class_init (GtkCanvasClass *klass)
 static void
 gtk_canvas_init (GtkCanvas *self)
 {
+  gtk_canvas_size_init_reference (&self->viewport_size,
+                                  g_rc_box_new0 (graphene_size_t));
 }
 
 /**
@@ -424,3 +444,8 @@ gtk_canvas_get_model (GtkCanvas *self)
   return self->model;
 }
 
+const GtkCanvasSize *
+gtk_canvas_get_viewport_size (GtkCanvas *self)
+{
+  return &self->viewport_size;
+}
diff --git a/gtk/gtkcanvas.h b/gtk/gtkcanvas.h
index d8425997a3..e1ef9f4b20 100644
--- a/gtk/gtkcanvas.h
+++ b/gtk/gtkcanvas.h
@@ -49,6 +49,8 @@ void                    gtk_canvas_set_factory                  (GtkCanvas
 GDK_AVAILABLE_IN_ALL
 GtkListItemFactory*     gtk_canvas_get_factory                  (GtkCanvas              *self);
 
+GDK_AVAILABLE_IN_ALL
+const GtkCanvasSize *   gtk_canvas_get_viewport_size            (GtkCanvas              *self);
 
 G_END_DECLS
 
diff --git a/gtk/gtkcanvassize.c b/gtk/gtkcanvassize.c
index b67b870eec..73d0534ff0 100644
--- a/gtk/gtkcanvassize.c
+++ b/gtk/gtkcanvassize.c
@@ -348,6 +348,58 @@ gtk_canvas_size_new_measure_item (GtkCanvasItem            *item,
   return self;
 }
 
+/* }}} */
+/* {{{ REFERENCE */
+
+static void
+gtk_canvas_size_reference_copy (GtkCanvasSize       *size,
+                                const GtkCanvasSize *source_size)
+{
+  const GtkCanvasSizeReference *source = &source_size->reference;
+
+  gtk_canvas_size_init_reference (size, g_rc_box_acquire (source->reference));
+}
+
+static void
+gtk_canvas_size_reference_finish (GtkCanvasSize *size)
+{
+  const GtkCanvasSizeReference *self = &size->reference;
+
+  g_rc_box_release (self->reference);
+}
+
+static gboolean
+gtk_canvas_size_reference_eval (const GtkCanvasSize *size,
+                                float               *width,
+                                float               *height)
+{
+  const GtkCanvasSizeReference *self = &size->reference;
+
+  *width = self->reference->width;
+  *height = self->reference->height;
+
+  return TRUE;
+}
+
+static const GtkCanvasSizeClass GTK_CANVAS_SIZE_REFERENCE_CLASS =
+{
+  "GtkCanvasSizeReference",
+  gtk_canvas_size_reference_copy,
+  gtk_canvas_size_reference_finish,
+  gtk_canvas_size_reference_eval,
+};
+
+void
+gtk_canvas_size_init_reference (GtkCanvasSize   *size,
+                                graphene_size_t *reference)
+{
+  GtkCanvasSizeReference *self = &size->reference;
+
+  self->class = &GTK_CANVAS_SIZE_REFERENCE_CLASS;
+  
+  self->reference = reference;
+}
+
 /* }}} */
 /* {{{ PUBLIC API */
 
diff --git a/gtk/gtkcanvassizeprivate.h b/gtk/gtkcanvassizeprivate.h
index 83e14800fa..2b054fb7b4 100644
--- a/gtk/gtkcanvassizeprivate.h
+++ b/gtk/gtkcanvassizeprivate.h
@@ -3,12 +3,15 @@
 
 #include "gtkcanvassize.h"
 
+#include <graphene.h>
+
 G_BEGIN_DECLS
 
 typedef struct _GtkCanvasSizeClass GtkCanvasSizeClass;
 typedef struct _GtkCanvasSizeAbsolute GtkCanvasSizeAbsolute;
 typedef struct _GtkCanvasSizeBox GtkCanvasSizeBox;
 typedef struct _GtkCanvasSizeMeasure GtkCanvasSizeMeasure;
+typedef struct _GtkCanvasSizeReference GtkCanvasSizeReference;
 
 struct _GtkCanvasSizeAbsolute
 {
@@ -33,6 +36,13 @@ struct _GtkCanvasSizeMeasure
   GtkCanvasItemMeasurement measure;
 };
 
+struct _GtkCanvasSizeReference
+{
+  const GtkCanvasSizeClass *class;
+
+  graphene_size_t *reference;
+};
+
 struct _GtkCanvasSize
 {
   union {
@@ -40,6 +50,7 @@ struct _GtkCanvasSize
     GtkCanvasSizeAbsolute absolute;
     GtkCanvasSizeBox box;
     GtkCanvasSizeMeasure measure;
+    GtkCanvasSizeReference reference;
   };
 };
 
@@ -51,6 +62,10 @@ void                    gtk_canvas_size_init_copy               (GtkCanvasSize
                                                                  const GtkCanvasSize   *source);
 void                    gtk_canvas_size_finish                  (GtkCanvasSize         *self);
 
+/* NB: Takes ownership of reference */
+void                    gtk_canvas_size_init_reference          (GtkCanvasSize         *size,
+                                                                 graphene_size_t       *reference);
+
 G_END_DECLS
 
 #endif /* __GTK_CANVAS_SIZE_PRIVATE_H__ */


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