[gimp] app: implement the "points" property using GimpArray



commit ba6aa8258ed4f1858612aa4f32ac17df6be5800c
Author: Michael Natterer <mitch gimp org>
Date:   Sun Oct 3 01:38:59 2010 +0200

    app: implement the "points" property using GimpArray
    
    instead of poking into private from new(). This way the item is
    properly subclassable.

 app/display/gimpcanvaspolygon.c |   81 +++++++++++++++++++++++++++------------
 app/display/gimpcanvaspolygon.h |    4 +-
 2 files changed, 58 insertions(+), 27 deletions(-)
---
diff --git a/app/display/gimpcanvaspolygon.c b/app/display/gimpcanvaspolygon.c
index 90d5c80..282d51b 100644
--- a/app/display/gimpcanvaspolygon.c
+++ b/app/display/gimpcanvaspolygon.c
@@ -138,6 +138,19 @@ gimp_canvas_polygon_set_property (GObject      *object,
   switch (property_id)
     {
     case PROP_POINTS:
+      {
+        GimpArray *array = g_value_get_boxed (value);
+
+        g_free (private->points);
+        private->points = NULL;
+        private->n_points = 0;
+
+        if (array)
+          {
+            private->points = g_memdup (array->data, array->length);
+            private->n_points = array->length / sizeof (GimpVector2);
+          }
+      }
       break;
     case PROP_FILLED:
       private->filled = g_value_get_boolean (value);
@@ -151,15 +164,28 @@ gimp_canvas_polygon_set_property (GObject      *object,
 
 static void
 gimp_canvas_polygon_get_property (GObject    *object,
-                                    guint       property_id,
-                                    GValue     *value,
-                                    GParamSpec *pspec)
+                                  guint       property_id,
+                                  GValue     *value,
+                                  GParamSpec *pspec)
 {
   GimpCanvasPolygonPrivate *private = GET_PRIVATE (object);
 
   switch (property_id)
     {
     case PROP_POINTS:
+      if (private->points)
+        {
+          GimpArray *array;
+
+          array = gimp_array_new ((const guint8 *) private->points,
+                                  private->n_points * sizeof (GimpVector2),
+                                  FALSE);
+          g_value_take_boxed (value, array);
+        }
+      else
+        {
+          g_value_set_boxed (value, NULL);
+        }
       break;
     case PROP_FILLED:
       g_value_set_boolean (value, private->filled);
@@ -268,53 +294,58 @@ gimp_canvas_polygon_new (GimpDisplayShell  *shell,
                          gint               n_points,
                          gboolean           filled)
 {
-  GimpCanvasItem           *item;
-  GimpCanvasPolygonPrivate *private;
+  GimpCanvasItem *item;
+  GimpArray      *array;
 
   g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);
   g_return_val_if_fail (points != NULL && n_points > 1, NULL);
 
+  array = gimp_array_new ((const guint8 *) points,
+                          n_points * sizeof (GimpVector2), TRUE);
+
   item = g_object_new (GIMP_TYPE_CANVAS_POLYGON,
                        "shell",  shell,
                        "filled", filled,
+                       "points", array,
                        NULL);
-  private = GET_PRIVATE (item);
 
-  /* puke */
-  private->points   = g_memdup (points, n_points * sizeof (GimpVector2));
-  private->n_points = n_points;
+  gimp_array_free (array);
 
   return item;
 }
 
 GimpCanvasItem *
 gimp_canvas_polygon_new_from_coords (GimpDisplayShell *shell,
-                                     const GimpCoords *points,
-                                     gint              n_points,
+                                     const GimpCoords *coords,
+                                     gint              n_coords,
                                      gboolean          filled)
 {
-  GimpCanvasItem           *item;
-  GimpCanvasPolygonPrivate *private;
-  gint                      i;
+  GimpCanvasItem *item;
+  GimpVector2    *points;
+  GimpArray      *array;
+  gint            i;
 
   g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);
-  g_return_val_if_fail (points != NULL && n_points > 1, NULL);
+  g_return_val_if_fail (coords != NULL && n_coords > 1, NULL);
+
+  points = g_new (GimpVector2, n_coords);
+
+  for (i = 0; i < n_coords; i++)
+    {
+      points[i].x = coords[i].x;
+      points[i].y = coords[i].y;
+    }
+
+  array = gimp_array_new ((const guint8 *) points,
+                          n_coords * sizeof (GimpVector2), FALSE);
 
   item = g_object_new (GIMP_TYPE_CANVAS_POLYGON,
                        "shell",  shell,
                        "filled", filled,
+                       "points", array,
                        NULL);
-  private = GET_PRIVATE (item);
-
-  /* puke */
-  private->points   = g_new (GimpVector2, n_points);
-  private->n_points = n_points;
 
-  for (i = 0; i < n_points; i++)
-    {
-      private->points[i].x = points[i].x;
-      private->points[i].y = points[i].y;
-    }
+  gimp_array_free (array);
 
   return item;
 }
diff --git a/app/display/gimpcanvaspolygon.h b/app/display/gimpcanvaspolygon.h
index 0fb02fb..0812129 100644
--- a/app/display/gimpcanvaspolygon.h
+++ b/app/display/gimpcanvaspolygon.h
@@ -54,8 +54,8 @@ GimpCanvasItem * gimp_canvas_polygon_new             (GimpDisplayShell  *shell,
                                                       gint               n_points,
                                                       gboolean           filled);
 GimpCanvasItem * gimp_canvas_polygon_new_from_coords (GimpDisplayShell  *shell,
-                                                      const GimpCoords  *points,
-                                                      gint               n_points,
+                                                      const GimpCoords  *coords,
+                                                      gint               n_coords,
                                                       gboolean           filled);
 
 



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