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



commit 7fd204899a629acc00fc2f9900decd82235ca8aa
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Thu Jul 7 12:06:05 2011 +0100

    gproperty: Port AlignConstraint to GProperty

 clutter/clutter-align-constraint.c |  298 ++++++++++++++++--------------------
 1 files changed, 130 insertions(+), 168 deletions(-)
---
diff --git a/clutter/clutter-align-constraint.c b/clutter/clutter-align-constraint.c
index 8ec342c..3140425 100644
--- a/clutter/clutter-align-constraint.c
+++ b/clutter/clutter-align-constraint.c
@@ -51,10 +51,17 @@
 #define CLUTTER_IS_ALIGN_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_ALIGN_CONSTRAINT))
 #define CLUTTER_ALIGN_CONSTRAINT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_ALIGN_CONSTRAINT, ClutterAlignConstraintClass))
 
+typedef struct _ClutterAlignConstraintPrivate   ClutterAlignConstraintPrivate;
+
 struct _ClutterAlignConstraint
 {
   ClutterConstraint parent_instance;
 
+  ClutterAlignConstraintPrivate *priv;
+};
+
+struct _ClutterAlignConstraintPrivate
+{
   ClutterActor *actor;
   ClutterActor *source;
   ClutterAlignAxis align_axis;
@@ -89,15 +96,15 @@ source_position_changed (ClutterActor           *actor,
                          ClutterAllocationFlags  flags,
                          ClutterAlignConstraint *align)
 {
-  if (align->actor != NULL)
-    clutter_actor_queue_relayout (align->actor);
+  if (align->priv->actor != NULL)
+    clutter_actor_queue_relayout (align->priv->actor);
 }
 
 static void
 source_destroyed (ClutterActor           *actor,
                   ClutterAlignConstraint *align)
 {
-  align->source = NULL;
+  align->priv->source = NULL;
 }
 
 static void
@@ -108,7 +115,7 @@ clutter_align_constraint_set_actor (ClutterActorMeta *meta,
   ClutterActorMetaClass *parent;
 
   /* store the pointer to the actor, for later use */
-  align->actor = new_actor;
+  align->priv->actor = new_actor;
 
   parent = CLUTTER_ACTOR_META_CLASS (clutter_align_constraint_parent_class);
   parent->set_actor (meta, new_actor);
@@ -124,17 +131,17 @@ clutter_align_constraint_update_allocation (ClutterConstraint *constraint,
   gfloat actor_width, actor_height;
   gfloat source_x, source_y;
 
-  if (align->source == NULL)
+  if (align->priv->source == NULL)
     return;
 
-  clutter_actor_get_position (align->source, &source_x, &source_y);
-  clutter_actor_get_size (align->source, &source_width, &source_height);
+  clutter_actor_get_position (align->priv->source, &source_x, &source_y);
+  clutter_actor_get_size (align->priv->source, &source_width, &source_height);
 
-  switch (align->align_axis)
+  switch (align->priv->align_axis)
     {
     case CLUTTER_ALIGN_X_AXIS:
       actor_width = clutter_actor_box_get_width (allocation);
-      allocation->x1 = ((source_width - actor_width) * align->factor)
+      allocation->x1 = ((source_width - actor_width) * align->priv->factor)
                      + source_x;
       allocation->x1 = floorf (allocation->x1 + 0.5);
       allocation->x2 = allocation->x1 + actor_width;
@@ -142,7 +149,7 @@ clutter_align_constraint_update_allocation (ClutterConstraint *constraint,
 
     case CLUTTER_ALIGN_Y_AXIS:
       actor_height = clutter_actor_box_get_height (allocation);
-      allocation->y1 = ((source_height - actor_height) * align->factor)
+      allocation->y1 = ((source_height - actor_height) * align->priv->factor)
                      + source_y;
       allocation->y1 = floorf (allocation->y1 + 0.5);
       allocation->y2 = allocation->y1 + actor_height;
@@ -159,74 +166,94 @@ clutter_align_constraint_dispose (GObject *gobject)
 {
   ClutterAlignConstraint *align = CLUTTER_ALIGN_CONSTRAINT (gobject);
 
-  if (align->source != NULL)
+  if (align->priv->source != NULL)
     {
-      g_signal_handlers_disconnect_by_func (align->source,
+      g_signal_handlers_disconnect_by_func (align->priv->source,
                                             G_CALLBACK (source_destroyed),
                                             align);
-      g_signal_handlers_disconnect_by_func (align->source,
+      g_signal_handlers_disconnect_by_func (align->priv->source,
                                             G_CALLBACK (source_position_changed),
                                             align);
-      align->source = NULL;
+      align->priv->source = NULL;
     }
 
   G_OBJECT_CLASS (clutter_align_constraint_parent_class)->dispose (gobject);
 }
 
-static void
-clutter_align_constraint_set_property (GObject      *gobject,
-                                       guint         prop_id,
-                                       const GValue *value,
-                                       GParamSpec   *pspec)
+static gboolean
+set_source_internal (gpointer self_,
+                     gpointer value_)
 {
-  ClutterAlignConstraint *align = CLUTTER_ALIGN_CONSTRAINT (gobject);
+  ClutterAlignConstraint *align = self_;
+  ClutterAlignConstraintPrivate *priv = align->priv;
+  ClutterActor *source = value_;
+  ClutterActor *old_source;
 
-  switch (prop_id)
-    {
-    case PROP_SOURCE:
-      clutter_align_constraint_set_source (align, g_value_get_object (value));
-      break;
+  if (priv->source == source)
+    return FALSE;
 
-    case PROP_ALIGN_AXIS:
-      clutter_align_constraint_set_align_axis (align, g_value_get_enum (value));
-      break;
+  old_source = priv->source;
+  if (old_source != NULL)
+    {
+      g_signal_handlers_disconnect_by_func (old_source,
+                                            G_CALLBACK (source_destroyed),
+                                            align);
+      g_signal_handlers_disconnect_by_func (old_source,
+                                            G_CALLBACK (source_position_changed),
+                                            align);
+    }
 
-    case PROP_FACTOR:
-      clutter_align_constraint_set_factor (align, g_value_get_float (value));
-      break;
+  priv->source = source;
+  if (priv->source != NULL)
+    {
+      g_signal_connect (priv->source, "allocation-changed",
+                        G_CALLBACK (source_position_changed),
+                        align);
+      g_signal_connect (priv->source, "destroy",
+                        G_CALLBACK (source_destroyed),
+                        align);
 
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
+      if (priv->actor != NULL)
+        clutter_actor_queue_relayout (priv->actor);
     }
+
+  return TRUE;
 }
 
-static void
-clutter_align_constraint_get_property (GObject    *gobject,
-                                       guint       prop_id,
-                                       GValue     *value,
-                                       GParamSpec *pspec)
+static gboolean
+set_align_axis_internal (gpointer self_,
+                         glong    value_)
 {
-  ClutterAlignConstraint *align = CLUTTER_ALIGN_CONSTRAINT (gobject);
+  ClutterAlignConstraint *align = self_;
+  ClutterAlignConstraintPrivate *priv = align->priv;
+  ClutterAlignAxis axis = value_;
 
-  switch (prop_id)
-    {
-    case PROP_SOURCE:
-      g_value_set_object (value, align->source);
-      break;
+  if (priv->align_axis == axis)
+    return FALSE;
 
-    case PROP_ALIGN_AXIS:
-      g_value_set_enum (value, align->align_axis);
-      break;
+  priv->align_axis = axis;
 
-    case PROP_FACTOR:
-      g_value_set_float (value, align->factor);
-      break;
+  if (priv->actor != NULL)
+    clutter_actor_queue_relayout (priv->actor);
 
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
+  return TRUE;
+}
+
+static gboolean
+set_factor_internal (gpointer self_,
+                     gfloat   value_)
+{
+  ClutterAlignConstraint *align = self_;
+  gfloat old_value = align->priv->factor;
+
+  align->priv->factor = CLAMP (value_, 0.0, 1.0);
+  if (ABS (align->priv->factor - old_value) < 0.001)
+    return FALSE;
+
+  if (align->priv->actor != NULL)
+    clutter_actor_queue_relayout (align->priv->actor);
+
+  return TRUE;
 }
 
 static void
@@ -236,6 +263,10 @@ clutter_align_constraint_class_init (ClutterAlignConstraintClass *klass)
   ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
   ClutterConstraintClass *constraint_class = CLUTTER_CONSTRAINT_CLASS (klass);
 
+  g_type_class_add_private (klass, sizeof (ClutterAlignConstraintPrivate));
+
+  gobject_class->dispose = clutter_align_constraint_dispose;
+
   meta_class->set_actor = clutter_align_constraint_set_actor;
 
   constraint_class->update_allocation = clutter_align_constraint_update_allocation;
@@ -248,11 +279,15 @@ clutter_align_constraint_class_init (ClutterAlignConstraintClass *klass)
    * Since: 1.4
    */
   obj_props[PROP_SOURCE] =
-    g_param_spec_object ("source",
-                           P_("Source"),
-                           P_("The source of the alignment"),
-                           CLUTTER_TYPE_ACTOR,
-                           CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+    g_object_property_new ("source", G_PROPERTY_READWRITE,
+                           G_STRUCT_OFFSET (ClutterAlignConstraintPrivate, 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 alignment"));
 
   /**
    * ClutterAlignConstraint:align-axis:
@@ -262,12 +297,17 @@ clutter_align_constraint_class_init (ClutterAlignConstraintClass *klass)
    * Since: 1.4
    */
   obj_props[PROP_ALIGN_AXIS] =
-    g_param_spec_enum ("align-axis",
+    g_enum_property_new ("align-axis", G_PROPERTY_READWRITE,
+                         G_STRUCT_OFFSET (ClutterAlignConstraintPrivate, align_axis),
+                         set_align_axis_internal,
+                         NULL);
+  g_property_set_prerequisite (G_PROPERTY (obj_props[PROP_ALIGN_AXIS]),
+                               CLUTTER_TYPE_ALIGN_AXIS);
+  g_property_set_default (G_PROPERTY (obj_props[PROP_ALIGN_AXIS]),
+                          CLUTTER_ALIGN_X_AXIS);
+  g_property_describe (G_PROPERTY (obj_props[PROP_ALIGN_AXIS]),
                        P_("Align Axis"),
-                       P_("The axis to align the position to"),
-                       CLUTTER_TYPE_ALIGN_AXIS,
-                       CLUTTER_ALIGN_X_AXIS,
-                       CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+                       P_("The axis to align the position to"));
 
   /**
    * ClutterAlignConstraint:factor:
@@ -282,26 +322,26 @@ clutter_align_constraint_class_init (ClutterAlignConstraintClass *klass)
    * Since: 1.4
    */
   obj_props[PROP_FACTOR] =
-    g_param_spec_float ("factor",
-                        P_("Factor"),
-                        P_("The alignment factor, between 0.0 and 1.0"),
-                        0.0, 1.0,
-                        0.0,
-                        CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+    g_float_property_new ("factor", G_PROPERTY_READWRITE,
+                          G_STRUCT_OFFSET (ClutterAlignConstraintPrivate, factor),
+                          set_factor_internal,
+                          NULL);
+  g_property_set_range (G_PROPERTY (obj_props[PROP_FACTOR]), 0.0, 1.0);
+  g_property_describe (G_PROPERTY (obj_props[PROP_FACTOR]),
+                       P_("Factor"),
+                       P_("The alignment factor, between 0.0 and 1.0"));
 
-  gobject_class->dispose = clutter_align_constraint_dispose;
-  gobject_class->set_property = clutter_align_constraint_set_property;
-  gobject_class->get_property = clutter_align_constraint_get_property;
   g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
 }
 
 static void
 clutter_align_constraint_init (ClutterAlignConstraint *self)
 {
-  self->actor = NULL;
-  self->source = NULL;
-  self->align_axis = CLUTTER_ALIGN_X_AXIS;
-  self->factor = 0.0f;
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_ALIGN_CONSTRAINT,
+                                            ClutterAlignConstraintPrivate);
+
+  self->priv->align_axis = CLUTTER_ALIGN_X_AXIS;
+  self->priv->factor = 0.0f;
 }
 
 /**
@@ -342,45 +382,6 @@ clutter_align_constraint_new (ClutterActor     *source,
  *
  * Since: 1.4
  */
-void
-clutter_align_constraint_set_source (ClutterAlignConstraint *align,
-                                     ClutterActor           *source)
-{
-  ClutterActor *old_source;
-
-  g_return_if_fail (CLUTTER_IS_ALIGN_CONSTRAINT (align));
-  g_return_if_fail (source == NULL || CLUTTER_IS_ACTOR (source));
-
-  if (align->source == source)
-    return;
-
-  old_source = align->source;
-  if (old_source != NULL)
-    {
-      g_signal_handlers_disconnect_by_func (old_source,
-                                            G_CALLBACK (source_destroyed),
-                                            align);
-      g_signal_handlers_disconnect_by_func (old_source,
-                                            G_CALLBACK (source_position_changed),
-                                            align);
-    }
-
-  align->source = source;
-  if (align->source != NULL)
-    {
-      g_signal_connect (align->source, "allocation-changed",
-                        G_CALLBACK (source_position_changed),
-                        align);
-      g_signal_connect (align->source, "destroy",
-                        G_CALLBACK (source_destroyed),
-                        align);
-
-      if (align->actor != NULL)
-        clutter_actor_queue_relayout (align->actor);
-    }
-
-  g_object_notify_by_pspec (G_OBJECT (align), obj_props[PROP_SOURCE]);
-}
 
 /**
  * clutter_align_constraint_get_source:
@@ -393,13 +394,10 @@ clutter_align_constraint_set_source (ClutterAlignConstraint *align,
  *
  * Since: 1.4
  */
-ClutterActor *
-clutter_align_constraint_get_source (ClutterAlignConstraint *align)
-{
-  g_return_val_if_fail (CLUTTER_IS_ALIGN_CONSTRAINT (align), NULL);
-
-  return align->source;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterAlignConstraint,
+                           clutter_align_constraint,
+                           ClutterActor *,
+                           source)
 
 /**
  * clutter_align_constraint_set_align_axis:
@@ -410,22 +408,6 @@ clutter_align_constraint_get_source (ClutterAlignConstraint *align)
  *
  * Since: 1.4
  */
-void
-clutter_align_constraint_set_align_axis (ClutterAlignConstraint *align,
-                                         ClutterAlignAxis        axis)
-{
-  g_return_if_fail (CLUTTER_IS_ALIGN_CONSTRAINT (align));
-
-  if (align->align_axis == axis)
-    return;
-
-  align->align_axis = axis;
-
-  if (align->actor != NULL)
-    clutter_actor_queue_relayout (align->actor);
-
-  g_object_notify_by_pspec (G_OBJECT (align), obj_props[PROP_ALIGN_AXIS]);
-}
 
 /**
  * clutter_align_constraint_get_align_axis:
@@ -437,14 +419,10 @@ clutter_align_constraint_set_align_axis (ClutterAlignConstraint *align,
  *
  * Since: 1.4
  */
-ClutterAlignAxis
-clutter_align_constraint_get_align_axis (ClutterAlignConstraint *align)
-{
-  g_return_val_if_fail (CLUTTER_IS_ALIGN_CONSTRAINT (align),
-                        CLUTTER_ALIGN_X_AXIS);
-
-  return align->align_axis;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterAlignConstraint,
+                           clutter_align_constraint,
+                           ClutterAlignAxis,
+                           align_axis)
 
 /**
  * clutter_align_constraint_set_factor:
@@ -465,19 +443,6 @@ clutter_align_constraint_get_align_axis (ClutterAlignConstraint *align)
  *
  * Since: 1.4
  */
-void
-clutter_align_constraint_set_factor (ClutterAlignConstraint *align,
-                                     gfloat                  factor)
-{
-  g_return_if_fail (CLUTTER_IS_ALIGN_CONSTRAINT (align));
-
-  align->factor = CLAMP (factor, 0.0, 1.0);
-
-  if (align->actor != NULL)
-    clutter_actor_queue_relayout (align->actor);
-
-  g_object_notify_by_pspec (G_OBJECT (align), obj_props[PROP_FACTOR]);
-}
 
 /**
  * clutter_align_constraint_get_factor:
@@ -489,10 +454,7 @@ clutter_align_constraint_set_factor (ClutterAlignConstraint *align,
  *
  * Since: 1.4
  */
-gfloat
-clutter_align_constraint_get_factor (ClutterAlignConstraint *align)
-{
-  g_return_val_if_fail (CLUTTER_IS_ALIGN_CONSTRAINT (align), 0.0);
-
-  return align->factor;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterAlignConstraint,
+                           clutter_align_constraint,
+                           gfloat,
+                           factor)



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