[gtk/wip/otte/canvas: 21/36] canvasbox: Get rid of specializing
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/otte/canvas: 21/36] canvasbox: Get rid of specializing
- Date: Tue, 5 Jul 2022 05:41:42 +0000 (UTC)
commit 5ce5ab4bb6cf623a222bac071b9e3683b3a94cbb
Author: Benjamin Otte <otte redhat com>
Date: Wed Jun 29 01:02:46 2022 +0200
canvasbox: Get rid of specializing
A box is a point, a size and an origin. All the specializing shall
happen in size and point.
gtk/gtkcanvasbox.c | 176 +++++++--------------------------------------
gtk/gtkcanvasboxprivate.h | 25 +------
gtk/gtkcanvassize.c | 87 ++++++++++++++++++++++
gtk/gtkcanvassize.h | 3 +
gtk/gtkcanvassizeprivate.h | 13 ++++
5 files changed, 130 insertions(+), 174 deletions(-)
---
diff --git a/gtk/gtkcanvasbox.c b/gtk/gtkcanvasbox.c
index 4260990d78..810ee610f6 100644
--- a/gtk/gtkcanvasbox.c
+++ b/gtk/gtkcanvasbox.c
@@ -36,91 +36,40 @@
/* {{{ Boilerplate */
-struct _GtkCanvasBoxClass
-{
- const char *type_name;
-
- void (* copy) (GtkCanvasBox *self,
- const GtkCanvasBox *source);
- void (* finish) (GtkCanvasBox *self);
- gboolean (* eval) (const GtkCanvasBox *self,
- graphene_rect_t *rect);
-};
-
G_DEFINE_BOXED_TYPE (GtkCanvasBox, gtk_canvas_box,
gtk_canvas_box_copy,
gtk_canvas_box_free)
-static gpointer
-gtk_canvas_box_alloc (const GtkCanvasBoxClass *class)
+void
+gtk_canvas_box_init (GtkCanvasBox *self,
+ const GtkCanvasPoint *point,
+ const GtkCanvasSize *size,
+ float origin_x,
+ float origin_y)
{
- GtkCanvasBox *self = g_slice_new (GtkCanvasBox);
-
- self->class = class;
-
- return self;
+ gtk_canvas_point_init_copy (&self->point, point);
+ gtk_canvas_size_init_copy (&self->size, size);
+ self->origin_x = origin_x;
+ self->origin_y = origin_y;
}
void
gtk_canvas_box_init_copy (GtkCanvasBox *self,
const GtkCanvasBox *source)
{
- self->class = source->class;
- self->class->copy (self, source);
+ gtk_canvas_point_init_copy (&self->point, &source->point);
+ gtk_canvas_size_init_copy (&self->size, &source->size);
+ self->origin_x = source->origin_x;
+ self->origin_y = source->origin_y;
}
void
gtk_canvas_box_finish (GtkCanvasBox *self)
{
- self->class->finish (self);
-}
-
-/* }}} */
-/* {{{ POINTS */
-
-static void
-gtk_canvas_box_points_copy (GtkCanvasBox *box,
- const GtkCanvasBox *source_box)
-{
- GtkCanvasBoxPoints *self = &box->points;
- const GtkCanvasBoxPoints *source = &source_box->points;
-
- gtk_canvas_point_init_copy (&self->point1, &source->point1);
- gtk_canvas_point_init_copy (&self->point2, &source->point2);
-}
-
-static void
-gtk_canvas_box_points_finish (GtkCanvasBox *box)
-{
- GtkCanvasBoxPoints *self = &box->points;
-
- gtk_canvas_point_finish (&self->point1);
- gtk_canvas_point_finish (&self->point2);
-}
-
-static gboolean
-gtk_canvas_box_points_eval (const GtkCanvasBox *box,
- graphene_rect_t *rect)
-{
- const GtkCanvasBoxPoints *self = &box->points;
- float x1, x2, y1, y2;
-
- if (!gtk_canvas_point_eval (&self->point1, &x1, &y1) ||
- !gtk_canvas_point_eval (&self->point2, &x2, &y2))
- return FALSE;
-
- graphene_rect_init (rect, x1, y1, x2 - x1, y2 - y1);
- return TRUE;
+ gtk_canvas_point_finish (&self->point);
+ gtk_canvas_size_finish (&self->size);
}
-static const GtkCanvasBoxClass GTK_CANVAS_BOX_POINTS_CLASS =
-{
- "GtkCanvasBoxPoints",
- gtk_canvas_box_points_copy,
- gtk_canvas_box_points_finish,
- gtk_canvas_box_points_eval,
-};
-
/**
* gtk_canvas_box_new_points:
* @point1: the first point
@@ -135,82 +84,17 @@ GtkCanvasBox *
gtk_canvas_box_new_points (const GtkCanvasPoint *point1,
const GtkCanvasPoint *point2)
{
- GtkCanvasBoxPoints *self;
+ GtkCanvasSize size;
+ GtkCanvasBox *result;
g_return_val_if_fail (point1 != NULL, NULL);
g_return_val_if_fail (point2 != NULL, NULL);
- self = gtk_canvas_box_alloc (>K_CANVAS_BOX_POINTS_CLASS);
-
- gtk_canvas_point_init_copy (&self->point1, point1);
- gtk_canvas_point_init_copy (&self->point2, point2);
+ gtk_canvas_size_init_distance (&size, point1, point2);
+ result = gtk_canvas_box_new (point1, &size, 0, 0);
+ gtk_canvas_size_finish (&size);
- return (GtkCanvasBox *) self;
-}
-
-/* }}} */
-/* {{{ SIZE */
-
-static void
-gtk_canvas_box_size_copy (GtkCanvasBox *box,
- const GtkCanvasBox *source_box)
-{
- const GtkCanvasBoxSize *source = &source_box->size;
-
- gtk_canvas_box_init (box, &source->point, &source->size, source->origin_x, source->origin_y);
-}
-
-static void
-gtk_canvas_box_size_finish (GtkCanvasBox *box)
-{
- GtkCanvasBoxSize *self = &box->size;
-
- gtk_canvas_point_finish (&self->point);
- gtk_canvas_size_finish (&self->size);
-}
-
-static gboolean
-gtk_canvas_box_size_eval (const GtkCanvasBox *box,
- graphene_rect_t *rect)
-{
- const GtkCanvasBoxSize *self = &box->size;
- float x, y, width, height;
-
- if (!gtk_canvas_point_eval (&self->point, &x, &y) ||
- !gtk_canvas_size_eval (&self->size, &width, &height))
- return FALSE;
-
- graphene_rect_init (rect,
- x - width * self->origin_x,
- y - height * self->origin_y,
- width, height);
-
- return TRUE;
-}
-
-static const GtkCanvasBoxClass GTK_CANVAS_BOX_SIZE_CLASS =
-{
- "GtkCanvasBoxSize",
- gtk_canvas_box_size_copy,
- gtk_canvas_box_size_finish,
- gtk_canvas_box_size_eval,
-};
-
-void
-gtk_canvas_box_init (GtkCanvasBox *box,
- const GtkCanvasPoint *point,
- const GtkCanvasSize *size,
- float origin_x,
- float origin_y)
-{
- GtkCanvasBoxSize *self = &box->size;
-
- self->class = >K_CANVAS_BOX_SIZE_CLASS;
-
- gtk_canvas_point_init_copy (&self->point, point);
- gtk_canvas_size_init_copy (&self->size, size);
- self->origin_x = origin_x;
- self->origin_y = origin_y;
+ return result;
}
/**
@@ -238,28 +122,19 @@ gtk_canvas_box_new (const GtkCanvasPoint *point,
g_return_val_if_fail (point != NULL, NULL);
g_return_val_if_fail (size != NULL, NULL);
- self = gtk_canvas_box_alloc (>K_CANVAS_BOX_SIZE_CLASS);
+ self = g_slice_new (GtkCanvasBox);
gtk_canvas_box_init (self, point, size, origin_x, origin_y);
return self;
}
-/* }}} */
-/* {{{ PUBLIC API */
-
GtkCanvasBox *
gtk_canvas_box_copy (const GtkCanvasBox *self)
{
- GtkCanvasBox *copy;
-
g_return_val_if_fail (self != NULL, NULL);
- copy = gtk_canvas_box_alloc (self->class);
-
- gtk_canvas_box_init_copy (copy, self);
-
- return copy;
+ return gtk_canvas_box_new (&self->point, &self->size, self->origin_x, self->origin_y);
}
void
@@ -277,7 +152,8 @@ gtk_canvas_box_eval (const GtkCanvasBox *self,
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (rect != NULL, FALSE);
- if (self->class->eval (self, rect))
+ if (gtk_canvas_point_eval (&self->point, &rect->origin.x, &rect->origin.y) &&
+ gtk_canvas_size_eval (&self->size, &rect->size.width, &rect->size.height))
return TRUE;
*rect = *graphene_rect_zero ();
diff --git a/gtk/gtkcanvasboxprivate.h b/gtk/gtkcanvasboxprivate.h
index f28610c38d..82baa5fb2a 100644
--- a/gtk/gtkcanvasboxprivate.h
+++ b/gtk/gtkcanvasboxprivate.h
@@ -8,37 +8,14 @@
G_BEGIN_DECLS
-typedef struct _GtkCanvasBoxClass GtkCanvasBoxClass;
-typedef struct _GtkCanvasBoxPoints GtkCanvasBoxPoints;
-typedef struct _GtkCanvasBoxSize GtkCanvasBoxSize;
-
-struct _GtkCanvasBoxPoints
-{
- const GtkCanvasBoxClass *class;
-
- GtkCanvasPoint point1;
- GtkCanvasPoint point2;
-};
-
-struct _GtkCanvasBoxSize
+struct _GtkCanvasBox
{
- const GtkCanvasBoxClass *class;
-
GtkCanvasPoint point;
GtkCanvasSize size;
float origin_x;
float origin_y;
};
-struct _GtkCanvasBox
-{
- union {
- const GtkCanvasBoxClass *class;
- GtkCanvasBoxPoints points;
- GtkCanvasBoxSize size;
- };
-};
-
void gtk_canvas_box_init (GtkCanvasBox *self,
const GtkCanvasPoint *point,
diff --git a/gtk/gtkcanvassize.c b/gtk/gtkcanvassize.c
index 73d0534ff0..81fcba2d19 100644
--- a/gtk/gtkcanvassize.c
+++ b/gtk/gtkcanvassize.c
@@ -30,6 +30,7 @@
#include "gtkcanvasbox.h"
#include "gtkcanvasitem.h"
+#include "gtkcanvaspoint.h"
#include "gtkwidget.h"
/* {{{ Boilerplate */
@@ -206,6 +207,92 @@ gtk_canvas_size_new_from_box (const GtkCanvasBox *box)
return (GtkCanvasSize *) self;
}
+/* }}} */
+/* {{{ DISTANCE */
+
+static void
+gtk_canvas_size_distance_copy (GtkCanvasSize *size,
+ const GtkCanvasSize *source_size)
+{
+ const GtkCanvasSizeDistance *source = &source_size->distance;
+
+ gtk_canvas_size_init_distance (size, source->from, source->to);
+}
+
+static void
+gtk_canvas_size_distance_finish (GtkCanvasSize *size)
+{
+ GtkCanvasSizeDistance *self = &size->distance;
+
+ gtk_canvas_point_free (self->from);
+ gtk_canvas_point_free (self->to);
+}
+
+static gboolean
+gtk_canvas_size_distance_eval (const GtkCanvasSize *size,
+ float *width,
+ float *height)
+{
+ const GtkCanvasSizeDistance *self = &size->distance;
+ float x1, y1, x2, y2;
+
+ if (!gtk_canvas_point_eval (self->from, &x1, &y1) ||
+ !gtk_canvas_point_eval (self->from, &x2, &y2))
+ return FALSE;
+
+ *width = x1 - x2;
+ *height = y1 - y2;
+
+ return TRUE;
+}
+
+static const GtkCanvasSizeClass GTK_CANVAS_SIZE_DISTANCE_CLASS =
+{
+ "GtkCanvasSizeDistance",
+ gtk_canvas_size_distance_copy,
+ gtk_canvas_size_distance_finish,
+ gtk_canvas_size_distance_eval,
+};
+
+void
+gtk_canvas_size_init_distance (GtkCanvasSize *size,
+ const GtkCanvasPoint *from,
+ const GtkCanvasPoint *to)
+{
+ GtkCanvasSizeDistance *self = &size->distance;
+
+ self->class = >K_CANVAS_SIZE_DISTANCE_CLASS;
+
+ self->from = gtk_canvas_point_copy (from);
+ self->to = gtk_canvas_point_copy (to);
+}
+
+/**
+ * gtk_canvas_size_new_distance:
+ * @from: point from where to compute the distance
+ * @to: point to where to compute the distance
+ *
+ * Creates a size for the given distance. Note that both width and height
+ * can be negative if @to is smaller than @from in the corresponding dimension.
+ *
+ * Returns: a new size
+ **/
+GtkCanvasSize *
+gtk_canvas_size_new_distance (const GtkCanvasPoint *from,
+ const GtkCanvasPoint *to)
+{
+ GtkCanvasSize *self;
+
+ g_return_val_if_fail (from != NULL, NULL);
+ g_return_val_if_fail (to != NULL, NULL);
+
+ self = gtk_canvas_size_alloc (>K_CANVAS_SIZE_DISTANCE_CLASS);
+
+ gtk_canvas_size_init_distance (self, from, to);
+
+ return self;
+}
+
/* }}} */
/* {{{ MEASURE */
diff --git a/gtk/gtkcanvassize.h b/gtk/gtkcanvassize.h
index 63a9903e6a..9c2558320f 100644
--- a/gtk/gtkcanvassize.h
+++ b/gtk/gtkcanvassize.h
@@ -48,6 +48,9 @@ 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);
+GDK_AVAILABLE_IN_ALL
GtkCanvasSize * gtk_canvas_size_new_from_box (const GtkCanvasBox *box);
typedef enum {
diff --git a/gtk/gtkcanvassizeprivate.h b/gtk/gtkcanvassizeprivate.h
index 2b054fb7b4..fa8dd4cc6b 100644
--- a/gtk/gtkcanvassizeprivate.h
+++ b/gtk/gtkcanvassizeprivate.h
@@ -10,6 +10,7 @@ G_BEGIN_DECLS
typedef struct _GtkCanvasSizeClass GtkCanvasSizeClass;
typedef struct _GtkCanvasSizeAbsolute GtkCanvasSizeAbsolute;
typedef struct _GtkCanvasSizeBox GtkCanvasSizeBox;
+typedef struct _GtkCanvasSizeDistance GtkCanvasSizeDistance;
typedef struct _GtkCanvasSizeMeasure GtkCanvasSizeMeasure;
typedef struct _GtkCanvasSizeReference GtkCanvasSizeReference;
@@ -28,6 +29,14 @@ struct _GtkCanvasSizeBox
GtkCanvasBox *box;
};
+struct _GtkCanvasSizeDistance
+{
+ const GtkCanvasSizeClass *class;
+
+ GtkCanvasPoint *from;
+ GtkCanvasPoint *to;
+};
+
struct _GtkCanvasSizeMeasure
{
const GtkCanvasSizeClass *class;
@@ -49,12 +58,16 @@ struct _GtkCanvasSize
const GtkCanvasSizeClass *class;
GtkCanvasSizeAbsolute absolute;
GtkCanvasSizeBox box;
+ GtkCanvasSizeDistance distance;
GtkCanvasSizeMeasure measure;
GtkCanvasSizeReference reference;
};
};
+void gtk_canvas_size_init_distance (GtkCanvasSize *size,
+ const GtkCanvasPoint *from,
+ const GtkCanvasPoint *to);
void gtk_canvas_size_init_measure_item (GtkCanvasSize *self,
GtkCanvasItem *item,
GtkCanvasItemMeasurement measure);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]