[clutter/wip/g-property] gproperty: Port ClutterRectangle to GProperty



commit cf4432ca3692ce27e186ac49f53d4c87f1e75067
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Thu Jul 7 16:43:02 2011 +0100

    gproperty: Port ClutterRectangle to GProperty

 clutter/clutter-rectangle.c |  337 +++++++++++++++----------------------------
 1 files changed, 114 insertions(+), 223 deletions(-)
---
diff --git a/clutter/clutter-rectangle.c b/clutter/clutter-rectangle.c
index 3a96e04..01289e3 100644
--- a/clutter/clutter-rectangle.c
+++ b/clutter/clutter-rectangle.c
@@ -42,20 +42,6 @@
 
 #include "cogl/cogl.h"
 
-G_DEFINE_TYPE (ClutterRectangle, clutter_rectangle, CLUTTER_TYPE_ACTOR);
-
-enum
-{
-  PROP_0,
-
-  PROP_COLOR,
-  PROP_BORDER_COLOR,
-  PROP_BORDER_WIDTH,
-  PROP_HAS_BORDER
-
-  /* FIXME: Add gradient, rounded corner props etc */
-};
-
 #define CLUTTER_RECTANGLE_GET_PRIVATE(obj) \
 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_RECTANGLE, ClutterRectanglePrivate))
 
@@ -66,12 +52,28 @@ struct _ClutterRectanglePrivate
 
   guint border_width;
 
-  guint has_border : 1;
+  gboolean has_border;
+};
+
+enum
+{
+  PROP_0,
+
+  COLOR,
+  BORDER_COLOR,
+  BORDER_WIDTH,
+  HAS_BORDER,
+
+  LAST_PROPERTY
 };
 
+static GParamSpec *obj_props[LAST_PROPERTY] = { NULL, };
+
 static const ClutterColor default_color        = { 255, 255, 255, 255 };
 static const ClutterColor default_border_color = {   0,   0,   0, 255 };
 
+G_DEFINE_TYPE (ClutterRectangle, clutter_rectangle, CLUTTER_TYPE_ACTOR);
+
 static void
 clutter_rectangle_paint (ClutterActor *self)
 {
@@ -170,105 +172,86 @@ clutter_rectangle_has_overlaps (ClutterActor *self)
   return FALSE;
 }
 
-static void
-clutter_rectangle_set_property (GObject      *object,
-				guint         prop_id,
-				const GValue *value,
-				GParamSpec   *pspec)
+static gboolean
+set_color_internal (gpointer self_,
+                    gpointer value_)
 {
-  ClutterRectangle *rectangle = CLUTTER_RECTANGLE(object);
+  ClutterRectangle *self = self_;
+  ClutterColor *color = value_;
 
-  switch (prop_id)
-    {
-    case PROP_COLOR:
-      clutter_rectangle_set_color (rectangle, clutter_value_get_color (value));
-      break;
-    case PROP_BORDER_COLOR:
-      clutter_rectangle_set_border_color (rectangle,
-                                          clutter_value_get_color (value));
-      break;
-    case PROP_BORDER_WIDTH:
-      clutter_rectangle_set_border_width (rectangle,
-                                          g_value_get_uint (value));
-      break;
-    case PROP_HAS_BORDER:
-      rectangle->priv->has_border = g_value_get_boolean (value);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-  }
+  if (clutter_color_equal (&self->priv->color, color))
+    return FALSE;
+
+  self->priv->color = *color;
+
+  clutter_actor_queue_redraw (self_);
+
+  return TRUE;
 }
 
-static void
-clutter_rectangle_get_property (GObject    *object,
-				guint       prop_id,
-				GValue     *value,
-				GParamSpec *pspec)
+static gboolean
+set_border_color_internal (gpointer self_,
+                           gpointer value_)
 {
-  ClutterRectanglePrivate *priv = CLUTTER_RECTANGLE(object)->priv;
+  ClutterRectangle *self = self_;
+  ClutterColor *color = value_;
 
-  switch (prop_id)
-    {
-    case PROP_COLOR:
-      clutter_value_set_color (value, &priv->color);
-      break;
-    case PROP_BORDER_COLOR:
-      clutter_value_set_color (value, &priv->border_color);
-      break;
-    case PROP_BORDER_WIDTH:
-      g_value_set_uint (value, priv->border_width);
-      break;
-    case PROP_HAS_BORDER:
-      g_value_set_boolean (value, priv->has_border);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-    }
-}
+  if (clutter_color_equal (&self->priv->border_color, color))
+    return FALSE;
 
+  self->priv->border_color = *color;
 
-static void
-clutter_rectangle_finalize (GObject *object)
-{
-  G_OBJECT_CLASS (clutter_rectangle_parent_class)->finalize (object);
+  clutter_actor_queue_redraw (self_);
+
+  return TRUE;
 }
 
-static void
-clutter_rectangle_dispose (GObject *object)
+static gboolean
+set_border_width_internal (gpointer self_,
+                           guint    value)
 {
-  G_OBJECT_CLASS (clutter_rectangle_parent_class)->dispose (object);
-}
+  ClutterRectangle *self = self_;
 
+  if (self->priv->border_width == value)
+    return FALSE;
+
+  self->priv->border_width = value;
+  self->priv->has_border = value > 0;
+
+  g_object_notify_by_pspec (self_, obj_props[HAS_BORDER]);
+
+  clutter_actor_queue_redraw (self_);
+
+  return TRUE;
+}
 
 static void
 clutter_rectangle_class_init (ClutterRectangleClass *klass)
 {
-  GObjectClass      *gobject_class = G_OBJECT_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
-  GParamSpec        *pspec;
+
+  g_type_class_add_private (gobject_class, sizeof (ClutterRectanglePrivate));
 
   actor_class->paint            = clutter_rectangle_paint;
   actor_class->get_paint_volume = clutter_rectangle_get_paint_volume;
   actor_class->has_overlaps     = clutter_rectangle_has_overlaps;
 
-  gobject_class->finalize     = clutter_rectangle_finalize;
-  gobject_class->dispose      = clutter_rectangle_dispose;
-  gobject_class->set_property = clutter_rectangle_set_property;
-  gobject_class->get_property = clutter_rectangle_get_property;
-
   /**
    * ClutterRectangle:color:
    *
    * The color of the rectangle.
    */
-  pspec = clutter_param_spec_color ("color",
-                                    P_("Color"),
-                                    P_("The color of the rectangle"),
-                                    &default_color,
-                                    CLUTTER_PARAM_READWRITE);
-  g_object_class_install_property (gobject_class, PROP_COLOR, pspec);
+  obj_props[COLOR] =
+    g_boxed_property_new ("color", G_PROPERTY_READWRITE,
+                          G_STRUCT_OFFSET (ClutterRectanglePrivate, color),
+                          set_color_internal,
+                          NULL);
+  g_property_set_prerequisite (G_PROPERTY (obj_props[COLOR]), CLUTTER_TYPE_COLOR);
+  g_property_set_default (G_PROPERTY (obj_props[COLOR]), &default_color);
+  g_property_describe (G_PROPERTY (obj_props[COLOR]),
+                       P_("Color"),
+                       P_("The color of the rectangle"));
 
   /**
    * ClutterRectangle:border-color:
@@ -277,12 +260,16 @@ clutter_rectangle_class_init (ClutterRectangleClass *klass)
    *
    * Since: 0.2
    */
-  pspec = clutter_param_spec_color ("border-color",
-                                    P_("Border Color"),
-                                    P_("The color of the border of the rectangle"),
-                                    &default_border_color,
-                                    CLUTTER_PARAM_READWRITE);
-  g_object_class_install_property (gobject_class, PROP_BORDER_COLOR, pspec);
+  obj_props[BORDER_COLOR] =
+    g_boxed_property_new ("border-color", G_PROPERTY_READWRITE,
+                          G_STRUCT_OFFSET (ClutterRectanglePrivate, border_color),
+                          set_border_color_internal,
+                          NULL);
+  g_property_set_prerequisite (G_PROPERTY (obj_props[BORDER_COLOR]), CLUTTER_TYPE_COLOR);
+  g_property_set_default (G_PROPERTY (obj_props[BORDER_COLOR]), &default_border_color);
+  g_property_describe (G_PROPERTY (obj_props[BORDER_COLOR]),
+                       P_("Border Color"),
+                       P_("The color of the border of the rectangle"));
 
   /**
    * ClutterRectangle:border-width:
@@ -291,14 +278,15 @@ clutter_rectangle_class_init (ClutterRectangleClass *klass)
    *
    * Since: 0.2
    */
-  g_object_class_install_property (gobject_class,
-                                   PROP_BORDER_WIDTH,
-                                   g_param_spec_uint ("border-width",
-                                                      P_("Border Width"),
-                                                      P_("The width of the border of the rectangle"),
-                                                      0, G_MAXUINT,
-                                                      0,
-                                                      CLUTTER_PARAM_READWRITE));
+  obj_props[BORDER_WIDTH] =
+    g_uint_property_new ("border-width", G_PROPERTY_READWRITE,
+                         G_STRUCT_OFFSET (ClutterRectanglePrivate, border_width),
+                         set_border_width_internal,
+                         NULL);
+  g_property_describe (G_PROPERTY (obj_props[BORDER_WIDTH]),
+                       P_("Border Width"),
+                       P_("The width of the border of the rectangle"));
+
   /**
    * ClutterRectangle:has-border:
    *
@@ -306,15 +294,16 @@ clutter_rectangle_class_init (ClutterRectangleClass *klass)
    *
    * Since: 0.2
    */
-  g_object_class_install_property (gobject_class,
-                                   PROP_HAS_BORDER,
-                                   g_param_spec_boolean ("has-border",
-                                                         P_("Has Border"),
-                                                         P_("Whether the rectangle should have a border"),
-                                                         FALSE,
-                                                         CLUTTER_PARAM_READWRITE));
-
-  g_type_class_add_private (gobject_class, sizeof (ClutterRectanglePrivate));
+  obj_props[HAS_BORDER] =
+    g_boolean_property_new ("has-border", G_PROPERTY_READABLE,
+                            G_STRUCT_OFFSET (ClutterRectanglePrivate, has_border),
+                            NULL,
+                            NULL);
+  g_property_describe (G_PROPERTY (obj_props[HAS_BORDER]),
+                       P_("Has Border"),
+                       P_("Whether the rectangle should have a border"));
+
+  g_object_class_install_properties (gobject_class, LAST_PROPERTY, obj_props);
 }
 
 static void
@@ -373,17 +362,14 @@ void
 clutter_rectangle_get_color (ClutterRectangle *rectangle,
 			     ClutterColor     *color)
 {
-  ClutterRectanglePrivate *priv;
+  ClutterColor *res;
 
   g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
   g_return_if_fail (color != NULL);
 
-  priv = rectangle->priv;
+  g_property_get (G_PROPERTY (obj_props[COLOR]), rectangle, &res);
 
-  color->red = priv->color.red;
-  color->green = priv->color.green;
-  color->blue = priv->color.blue;
-  color->alpha = priv->color.alpha;
+  *color = *res;
 }
 
 /**
@@ -393,38 +379,9 @@ clutter_rectangle_get_color (ClutterRectangle *rectangle,
  *
  * Sets the color of @rectangle.
  */
-void
-clutter_rectangle_set_color (ClutterRectangle   *rectangle,
-			     const ClutterColor *color)
-{
-  ClutterRectanglePrivate *priv;
-
-  g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
-  g_return_if_fail (color != NULL);
-
-  g_object_ref (rectangle);
-
-  priv = rectangle->priv;
-
-  priv->color.red = color->red;
-  priv->color.green = color->green;
-  priv->color.blue = color->blue;
-  priv->color.alpha = color->alpha;
-
-#if 0
-  /* FIXME - appears to be causing border to always get drawn */
-  if (clutter_color_equal (&priv->color, &priv->border_color))
-    priv->has_border = FALSE;
-  else
-    priv->has_border = TRUE;
-#endif
-
-  clutter_actor_queue_redraw (CLUTTER_ACTOR (rectangle));
-
-  g_object_notify (G_OBJECT (rectangle), "color");
-  g_object_notify (G_OBJECT (rectangle), "has-border");
-  g_object_unref (rectangle);
-}
+G_DEFINE_PROPERTY_SET (ClutterRectangle, clutter_rectangle,
+                       const ClutterColor *,
+                       color)
 
 /**
  * clutter_rectangle_get_border_width:
@@ -436,13 +393,6 @@ clutter_rectangle_set_color (ClutterRectangle   *rectangle,
  *
  * Since: 0.2
  */
-guint
-clutter_rectangle_get_border_width (ClutterRectangle *rectangle)
-{
-  g_return_val_if_fail (CLUTTER_IS_RECTANGLE (rectangle), 0);
-
-  return rectangle->priv->border_width;
-}
 
 /**
  * clutter_rectangle_set_border_width:
@@ -454,33 +404,9 @@ clutter_rectangle_get_border_width (ClutterRectangle *rectangle)
  *
  * Since: 0.2
  */
-void
-clutter_rectangle_set_border_width (ClutterRectangle *rectangle,
-                                    guint             width)
-{
-  ClutterRectanglePrivate *priv;
-
-  g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
-  priv = rectangle->priv;
-
-  if (priv->border_width != width)
-    {
-      g_object_ref (rectangle);
-
-      priv->border_width = width;
-
-      if (priv->border_width != 0)
-        priv->has_border = TRUE;
-      else
-        priv->has_border = FALSE;
-
-      clutter_actor_queue_redraw (CLUTTER_ACTOR (rectangle));
-
-      g_object_notify (G_OBJECT (rectangle), "border-width");
-      g_object_notify (G_OBJECT (rectangle), "has-border");
-      g_object_unref (rectangle);
-    }
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterRectangle, clutter_rectangle,
+                           guint,
+                           border_width)
 
 /**
  * clutter_rectangle_get_border_color:
@@ -496,17 +422,14 @@ void
 clutter_rectangle_get_border_color (ClutterRectangle *rectangle,
                                     ClutterColor     *color)
 {
-  ClutterRectanglePrivate *priv;
+  ClutterColor *res;
 
   g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
   g_return_if_fail (color != NULL);
 
-  priv = rectangle->priv;
+  g_property_get (G_PROPERTY (obj_props[BORDER_COLOR]), rectangle, &res);
 
-  color->red = priv->border_color.red;
-  color->green = priv->border_color.green;
-  color->blue = priv->border_color.blue;
-  color->alpha = priv->border_color.alpha;
+  *color = *res;
 }
 
 /**
@@ -516,38 +439,6 @@ clutter_rectangle_get_border_color (ClutterRectangle *rectangle,
  *
  * Sets the color of the border used by @rectangle using @color
  */
-void
-clutter_rectangle_set_border_color (ClutterRectangle   *rectangle,
-                                    const ClutterColor *color)
-{
-  ClutterRectanglePrivate *priv;
-
-  g_return_if_fail (CLUTTER_IS_RECTANGLE (rectangle));
-  g_return_if_fail (color != NULL);
-
-  priv = rectangle->priv;
-
-  if (priv->border_color.red != color->red ||
-      priv->border_color.green != color->green ||
-      priv->border_color.blue != color->blue ||
-      priv->border_color.alpha != color->alpha)
-    {
-      g_object_ref (rectangle);
-
-      priv->border_color.red = color->red;
-      priv->border_color.green = color->green;
-      priv->border_color.blue = color->blue;
-      priv->border_color.alpha = color->alpha;
-
-      if (clutter_color_equal (&priv->color, &priv->border_color))
-        priv->has_border = FALSE;
-      else
-        priv->has_border = TRUE;
-
-      clutter_actor_queue_redraw (CLUTTER_ACTOR (rectangle));
-
-      g_object_notify (G_OBJECT (rectangle), "border-color");
-      g_object_notify (G_OBJECT (rectangle), "has-border");
-      g_object_unref (rectangle);
-    }
-}
+G_DEFINE_PROPERTY_SET (ClutterRectangle, clutter_rectangle,
+                       const ClutterColor *,
+                       border_color)



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