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



commit 5fa8c99f9f648356d033d55a21ead688aa7e647c
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Thu Jul 7 12:55:03 2011 +0100

    gproperty: Port ClutterBindConstraint to GProperty

 clutter/clutter-bind-constraint.c |  314 ++++++++++++++++---------------------
 1 files changed, 134 insertions(+), 180 deletions(-)
---
diff --git a/clutter/clutter-bind-constraint.c b/clutter/clutter-bind-constraint.c
index 472562e..53df192 100644
--- a/clutter/clutter-bind-constraint.c
+++ b/clutter/clutter-bind-constraint.c
@@ -97,10 +97,17 @@
 #define CLUTTER_IS_BIND_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_BIND_CONSTRAINT))
 #define CLUTTER_BIND_CONSTRAINT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_BIND_CONSTRAINT, ClutterBindConstraintClass))
 
+typedef struct _ClutterBindConstraintPrivate    ClutterBindConstraintPrivate;
+
 struct _ClutterBindConstraint
 {
   ClutterConstraint parent_instance;
 
+  ClutterBindConstraintPrivate *priv;
+};
+
+struct _ClutterBindConstraintPrivate
+{
   ClutterActor *actor;
   ClutterActor *source;
   ClutterBindCoordinate coordinate;
@@ -133,15 +140,15 @@ static void
 source_queue_relayout (ClutterActor          *source,
                        ClutterBindConstraint *bind)
 {
-  if (bind->actor != NULL)
-    clutter_actor_queue_relayout (bind->actor);
+  if (bind->priv->actor != NULL)
+    clutter_actor_queue_relayout (bind->priv->actor);
 }
 
 static void
 source_destroyed (ClutterActor          *actor,
                   ClutterBindConstraint *bind)
 {
-  bind->source = NULL;
+  bind->priv->source = NULL;
 }
 
 static void
@@ -154,45 +161,45 @@ clutter_bind_constraint_update_allocation (ClutterConstraint *constraint,
   gfloat actor_width, actor_height;
   ClutterVertex source_position = { 0., };
 
-  if (bind->source == NULL)
+  if (bind->priv->source == NULL)
     return;
 
-  source_position.x = clutter_actor_get_x (bind->source);
-  source_position.y = clutter_actor_get_y (bind->source);
-  clutter_actor_get_size (bind->source, &source_width, &source_height);
+  source_position.x = clutter_actor_get_x (bind->priv->source);
+  source_position.y = clutter_actor_get_y (bind->priv->source);
+  clutter_actor_get_size (bind->priv->source, &source_width, &source_height);
 
   clutter_actor_box_get_size (allocation, &actor_width, &actor_height);
 
-  switch (bind->coordinate)
+  switch (bind->priv->coordinate)
     {
     case CLUTTER_BIND_X:
-      allocation->x1 = source_position.x + bind->offset;
+      allocation->x1 = source_position.x + bind->priv->offset;
       allocation->x2 = allocation->x1 + actor_width;
       break;
 
     case CLUTTER_BIND_Y:
-      allocation->y1 = source_position.y + bind->offset;
+      allocation->y1 = source_position.y + bind->priv->offset;
       allocation->y2 = allocation->y1 + actor_height;
       break;
 
     case CLUTTER_BIND_POSITION:
-      allocation->x1 = source_position.x + bind->offset;
-      allocation->y1 = source_position.y + bind->offset;
+      allocation->x1 = source_position.x + bind->priv->offset;
+      allocation->y1 = source_position.y + bind->priv->offset;
       allocation->x2 = allocation->x1 + actor_width;
       allocation->y2 = allocation->y1 + actor_height;
       break;
 
     case CLUTTER_BIND_WIDTH:
-      allocation->x2 = allocation->x1 + source_width + bind->offset;
+      allocation->x2 = allocation->x1 + source_width + bind->priv->offset;
       break;
 
     case CLUTTER_BIND_HEIGHT:
-      allocation->y2 = allocation->y1 + source_height + bind->offset;
+      allocation->y2 = allocation->y1 + source_height + bind->priv->offset;
       break;
 
     case CLUTTER_BIND_SIZE:
-      allocation->x2 = allocation->x1 + source_width + bind->offset;
-      allocation->y2 = allocation->y1 + source_height + bind->offset;
+      allocation->x2 = allocation->x1 + source_width + bind->priv->offset;
+      allocation->y2 = allocation->y1 + source_height + bind->priv->offset;
       break;
 
     default:
@@ -209,7 +216,7 @@ clutter_bind_constraint_set_actor (ClutterActorMeta *meta,
   ClutterActorMetaClass *parent;
 
   /* store the pointer to the actor, for later use */
-  bind->actor = new_actor;
+  bind->priv->actor = new_actor;
 
   parent = CLUTTER_ACTOR_META_CLASS (clutter_bind_constraint_parent_class);
   parent->set_actor (meta, new_actor);
@@ -220,74 +227,92 @@ clutter_bind_constraint_dispose (GObject *gobject)
 {
   ClutterBindConstraint *bind = CLUTTER_BIND_CONSTRAINT (gobject);
 
-  if (bind->source != NULL)
+  if (bind->priv->source != NULL)
     {
-      g_signal_handlers_disconnect_by_func (bind->source,
+      g_signal_handlers_disconnect_by_func (bind->priv->source,
                                             G_CALLBACK (source_destroyed),
                                             bind);
-      g_signal_handlers_disconnect_by_func (bind->source,
+      g_signal_handlers_disconnect_by_func (bind->priv->source,
                                             G_CALLBACK (source_queue_relayout),
                                             bind);
-      bind->source = NULL;
+      bind->priv->source = NULL;
     }
 
   G_OBJECT_CLASS (clutter_bind_constraint_parent_class)->dispose (gobject);
 }
 
-static void
-clutter_bind_constraint_set_property (GObject      *gobject,
-                                      guint         prop_id,
-                                      const GValue *value,
-                                      GParamSpec   *pspec)
+static gboolean
+set_source_internal (gpointer self_,
+                     gpointer value_)
 {
-  ClutterBindConstraint *bind = CLUTTER_BIND_CONSTRAINT (gobject);
+  ClutterBindConstraint *constraint = self_;
+  ClutterActor *source = value_;
+  ClutterActor *old_source;
 
-  switch (prop_id)
-    {
-    case PROP_SOURCE:
-      clutter_bind_constraint_set_source (bind, g_value_get_object (value));
-      break;
+  if (constraint->priv->source == source)
+    return FALSE;
 
-    case PROP_COORDINATE:
-      clutter_bind_constraint_set_coordinate (bind, g_value_get_enum (value));
-      break;
+  old_source = constraint->priv->source;
+  if (old_source != NULL)
+    {
+      g_signal_handlers_disconnect_by_func (old_source,
+                                            G_CALLBACK (source_destroyed),
+                                            constraint);
+      g_signal_handlers_disconnect_by_func (old_source,
+                                            G_CALLBACK (source_queue_relayout),
+                                            constraint);
+    }
 
-    case PROP_OFFSET:
-      clutter_bind_constraint_set_offset (bind, g_value_get_float (value));
-      break;
+  constraint->priv->source = source;
+  if (constraint->priv->source != NULL)
+    {
+      g_signal_connect (constraint->priv->source, "queue-relayout",
+                        G_CALLBACK (source_queue_relayout),
+                        constraint);
+      g_signal_connect (constraint->priv->source, "destroy",
+                        G_CALLBACK (source_destroyed),
+                        constraint);
 
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
+      if (constraint->priv->actor != NULL)
+        clutter_actor_queue_relayout (constraint->priv->actor);
     }
+
+  return TRUE;
 }
 
-static void
-clutter_bind_constraint_get_property (GObject    *gobject,
-                                      guint       prop_id,
-                                      GValue     *value,
-                                      GParamSpec *pspec)
+static gboolean
+set_offset_internal (gpointer self_,
+                     gfloat   value)
 {
-  ClutterBindConstraint *bind = CLUTTER_BIND_CONSTRAINT (gobject);
+  ClutterBindConstraint *constraint = self_;
 
-  switch (prop_id)
-    {
-    case PROP_SOURCE:
-      g_value_set_object (value, bind->source);
-      break;
+  if (fabs (constraint->priv->offset - value) < 0.00001f)
+    return FALSE;
 
-    case PROP_COORDINATE:
-      g_value_set_enum (value, bind->coordinate);
-      break;
+  constraint->priv->offset = value;
 
-    case PROP_OFFSET:
-      g_value_set_float (value, bind->offset);
-      break;
+  if (constraint->priv->actor != NULL)
+    clutter_actor_queue_relayout (constraint->priv->actor);
 
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
+  return TRUE;
+}
+
+static gboolean
+set_coordinate_internal (gpointer self_,
+                         glong    value_)
+{
+  ClutterBindConstraint *constraint = self_;
+  ClutterBindCoordinate coordinate = value_;
+
+  if (constraint->priv->coordinate == coordinate)
+    return FALSE;
+
+  constraint->priv->coordinate = coordinate;
+
+  if (constraint->priv->actor != NULL)
+    clutter_actor_queue_relayout (constraint->priv->actor);
+
+  return TRUE;
 }
 
 static void
@@ -297,13 +322,14 @@ clutter_bind_constraint_class_init (ClutterBindConstraintClass *klass)
   ClutterConstraintClass *constraint_class = CLUTTER_CONSTRAINT_CLASS (klass);
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
-  gobject_class->set_property = clutter_bind_constraint_set_property;
-  gobject_class->get_property = clutter_bind_constraint_get_property;
+  g_type_class_add_private (klass, sizeof (ClutterBindConstraintPrivate));
+
   gobject_class->dispose = clutter_bind_constraint_dispose;
 
   meta_class->set_actor = clutter_bind_constraint_set_actor;
 
   constraint_class->update_allocation = clutter_bind_constraint_update_allocation;
+
   /**
    * ClutterBindConstraint:source:
    *
@@ -312,11 +338,15 @@ clutter_bind_constraint_class_init (ClutterBindConstraintClass *klass)
    * Since: 1.4
    */
   obj_props[PROP_SOURCE] =
-    g_param_spec_object ("source",
-                         P_("Source"),
-                         P_("The source of the binding"),
-                         CLUTTER_TYPE_ACTOR,
-                         CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+    g_object_property_new ("source", G_PROPERTY_READWRITE,
+                           G_STRUCT_OFFSET (ClutterBindConstraintPrivate, source),
+                           set_source_internal,
+                           NULL);
+  g_property_set_prerequisite (G_PROPERTY (obj_props[PROP_SOURCE]),
+                               CLUTTER_TYPE_ACTOR);
+  g_property_describe (G_PROPERTY (obj_props[PROP_SOURCE]),
+                       P_("Source"),
+                       P_("The source of the binding"));
 
   /**
    * ClutterBindConstraint:coordinate:
@@ -326,12 +356,17 @@ clutter_bind_constraint_class_init (ClutterBindConstraintClass *klass)
    * Since: 1.4
    */
   obj_props[PROP_COORDINATE] =
-    g_param_spec_enum ("coordinate",
+    g_enum_property_new ("coordinate", G_PROPERTY_READWRITE,
+                         G_STRUCT_OFFSET (ClutterBindConstraintPrivate, coordinate),
+                         set_coordinate_internal,
+                         NULL);
+  g_property_set_prerequisite (G_PROPERTY (obj_props[PROP_COORDINATE]),
+                               CLUTTER_TYPE_BIND_COORDINATE);
+  g_property_set_default (G_PROPERTY (obj_props[PROP_COORDINATE]),
+                          CLUTTER_BIND_X);
+  g_property_describe (G_PROPERTY (obj_props[PROP_COORDINATE]),
                        P_("Coordinate"),
-                       P_("The coordinate to bind"),
-                       CLUTTER_TYPE_BIND_COORDINATE,
-                       CLUTTER_BIND_X,
-                       CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+                       P_("The coordinate to bind"));
 
   /**
    * ClutterBindConstraint:offset:
@@ -341,25 +376,25 @@ clutter_bind_constraint_class_init (ClutterBindConstraintClass *klass)
    * Since: 1.4
    */
   obj_props[PROP_OFFSET] =
-    g_param_spec_float ("offset",
-                        P_("Offset"),
-                        P_("The offset in pixels to apply to the binding"),
-                        -G_MAXFLOAT, G_MAXFLOAT,
-                        0.0f,
-                        CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
-
-  g_object_class_install_properties (gobject_class,
-                                     PROP_LAST,
-                                     obj_props);
+    g_float_property_new ("offset", G_PROPERTY_READWRITE,
+                          G_STRUCT_OFFSET (ClutterBindConstraintPrivate, offset),
+                          set_offset_internal,
+                          NULL);
+  g_property_describe (G_PROPERTY (obj_props[PROP_OFFSET]),
+                       P_("Offset"),
+                       P_("The offset in pixels to apply to the binding"));
+
+  g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
 }
 
 static void
 clutter_bind_constraint_init (ClutterBindConstraint *self)
 {
-  self->actor = NULL;
-  self->source = NULL;
-  self->coordinate = CLUTTER_BIND_X;
-  self->offset = 0.0f;
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_BIND_CONSTRAINT,
+                                            ClutterBindConstraintPrivate);
+
+  self->priv->coordinate = CLUTTER_BIND_X;
+  self->priv->offset = 0.0f;
 }
 
 /**
@@ -399,45 +434,6 @@ clutter_bind_constraint_new (ClutterActor          *source,
  *
  * Since: 1.4
  */
-void
-clutter_bind_constraint_set_source (ClutterBindConstraint *constraint,
-                                    ClutterActor          *source)
-{
-  ClutterActor *old_source;
-
-  g_return_if_fail (CLUTTER_IS_BIND_CONSTRAINT (constraint));
-  g_return_if_fail (source == NULL || CLUTTER_IS_ACTOR (source));
-
-  if (constraint->source == source)
-    return;
-
-  old_source = constraint->source;
-  if (old_source != NULL)
-    {
-      g_signal_handlers_disconnect_by_func (old_source,
-                                            G_CALLBACK (source_destroyed),
-                                            constraint);
-      g_signal_handlers_disconnect_by_func (old_source,
-                                            G_CALLBACK (source_queue_relayout),
-                                            constraint);
-    }
-
-  constraint->source = source;
-  if (constraint->source != NULL)
-    {
-      g_signal_connect (constraint->source, "queue-relayout",
-                        G_CALLBACK (source_queue_relayout),
-                        constraint);
-      g_signal_connect (constraint->source, "destroy",
-                        G_CALLBACK (source_destroyed),
-                        constraint);
-
-      if (constraint->actor != NULL)
-        clutter_actor_queue_relayout (constraint->actor);
-    }
-
-  g_object_notify_by_pspec (G_OBJECT (constraint), obj_props[PROP_SOURCE]);
-}
 
 /**
  * clutter_bind_constraint_get_source:
@@ -449,13 +445,10 @@ clutter_bind_constraint_set_source (ClutterBindConstraint *constraint,
  *
  * Since: 1.4
  */
-ClutterActor *
-clutter_bind_constraint_get_source (ClutterBindConstraint *constraint)
-{
-  g_return_val_if_fail (CLUTTER_IS_BIND_CONSTRAINT (constraint), NULL);
-
-  return constraint->source;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterBindConstraint,
+                           clutter_bind_constraint,
+                           ClutterActor *,
+                           source)
 
 /**
  * clutter_bind_constraint_set_coordinate:
@@ -466,22 +459,6 @@ clutter_bind_constraint_get_source (ClutterBindConstraint *constraint)
  *
  * Since: 1.4
  */
-void
-clutter_bind_constraint_set_coordinate (ClutterBindConstraint *constraint,
-                                        ClutterBindCoordinate  coordinate)
-{
-  g_return_if_fail (CLUTTER_IS_BIND_CONSTRAINT (constraint));
-
-  if (constraint->coordinate == coordinate)
-    return;
-
-  constraint->coordinate = coordinate;
-
-  if (constraint->actor != NULL)
-    clutter_actor_queue_relayout (constraint->actor);
-
-  g_object_notify_by_pspec (G_OBJECT (constraint), obj_props[PROP_COORDINATE]);
-}
 
 /**
  * clutter_bind_constraint_get_coordinate:
@@ -493,14 +470,11 @@ clutter_bind_constraint_set_coordinate (ClutterBindConstraint *constraint,
  *
  * Since: 1.4
  */
-ClutterBindCoordinate
-clutter_bind_constraint_get_coordinate (ClutterBindConstraint *constraint)
-{
-  g_return_val_if_fail (CLUTTER_IS_BIND_CONSTRAINT (constraint),
-                        CLUTTER_BIND_X);
 
-  return constraint->coordinate;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterBindConstraint,
+                           clutter_bind_constraint,
+                           ClutterBindCoordinate,
+                           coordinate)
 
 /**
  * clutter_bind_constraint_set_offset:
@@ -511,22 +485,6 @@ clutter_bind_constraint_get_coordinate (ClutterBindConstraint *constraint)
  *
  * Since: 1.4
  */
-void
-clutter_bind_constraint_set_offset (ClutterBindConstraint *constraint,
-                                    gfloat                 offset)
-{
-  g_return_if_fail (CLUTTER_IS_BIND_CONSTRAINT (constraint));
-
-  if (fabs (constraint->offset - offset) < 0.00001f)
-    return;
-
-  constraint->offset = offset;
-
-  if (constraint->actor != NULL)
-    clutter_actor_queue_relayout (constraint->actor);
-
-  g_object_notify_by_pspec (G_OBJECT (constraint), obj_props[PROP_OFFSET]);
-}
 
 /**
  * clutter_bind_constraint_get_offset:
@@ -538,10 +496,6 @@ clutter_bind_constraint_set_offset (ClutterBindConstraint *constraint,
  *
  * Since: 1.4
  */
-gfloat
-clutter_bind_constraint_get_offset (ClutterBindConstraint *bind)
-{
-  g_return_val_if_fail (CLUTTER_IS_BIND_CONSTRAINT (bind), 0.0);
-
-  return bind->offset;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterBindConstraint,
+                           clutter_bind_constraint,
+                           gfloat, offset)



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