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



commit c9b1b85002ded51f2fa4bae62077a55d451b2172
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Thu Jul 7 14:16:37 2011 +0100

    gproperty: Port ClutterSnapConstraint to GProperty

 clutter/clutter-snap-constraint.c |  389 +++++++++++++++++--------------------
 1 files changed, 182 insertions(+), 207 deletions(-)
---
diff --git a/clutter/clutter-snap-constraint.c b/clutter/clutter-snap-constraint.c
index 5077215..6a58031 100644
--- a/clutter/clutter-snap-constraint.c
+++ b/clutter/clutter-snap-constraint.c
@@ -53,10 +53,17 @@
 #define CLUTTER_IS_SNAP_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_SNAP_CONSTRAINT))
 #define CLUTTER_SNAP_CONSTRAINT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_SNAP_CONSTRAINT, ClutterSnapConstraintClass))
 
+typedef struct _ClutterSnapConstraintPrivate    ClutterSnapConstraintPrivate;
+
 struct _ClutterSnapConstraint
 {
   ClutterConstraint parent_instance;
 
+  ClutterSnapConstraintPrivate *priv;
+};
+
+struct _ClutterSnapConstraintPrivate
+{
   ClutterActor *actor;
   ClutterActor *source;
 
@@ -93,15 +100,15 @@ static void
 source_queue_relayout (ClutterActor          *source,
                        ClutterSnapConstraint *constraint)
 {
-  if (constraint->actor != NULL)
-    clutter_actor_queue_relayout (constraint->actor);
+  if (constraint->priv->actor != NULL)
+    clutter_actor_queue_relayout (constraint->priv->actor);
 }
 
 static void
 source_destroyed (ClutterActor          *actor,
                   ClutterSnapConstraint *constraint)
 {
-  constraint->source = NULL;
+  constraint->priv->source = NULL;
 }
 
 static inline void
@@ -138,52 +145,52 @@ clutter_snap_constraint_update_allocation (ClutterConstraint *constraint,
   gfloat source_x, source_y;
   gfloat actor_width, actor_height;
 
-  if (self->source == NULL)
+  if (self->priv->source == NULL)
     return;
 
-  clutter_actor_get_position (self->source, &source_x, &source_y);
-  clutter_actor_get_size (self->source, &source_width, &source_height);
+  clutter_actor_get_position (self->priv->source, &source_x, &source_y);
+  clutter_actor_get_size (self->priv->source, &source_width, &source_height);
 
   clutter_actor_box_get_size (allocation, &actor_width, &actor_height);
 
-  switch (self->to_edge)
+  switch (self->priv->to_edge)
     {
     case CLUTTER_SNAP_EDGE_LEFT:
-      if (self->from_edge == CLUTTER_SNAP_EDGE_LEFT)
-        allocation->x1 = source_x + self->offset;
-      else if (self->from_edge == CLUTTER_SNAP_EDGE_RIGHT)
-        allocation->x2 = source_x + self->offset;
+      if (self->priv->from_edge == CLUTTER_SNAP_EDGE_LEFT)
+        allocation->x1 = source_x + self->priv->offset;
+      else if (self->priv->from_edge == CLUTTER_SNAP_EDGE_RIGHT)
+        allocation->x2 = source_x + self->priv->offset;
       else
-        warn_horizontal_edge ("left", self->actor, self->source);
+        warn_horizontal_edge ("left", self->priv->actor, self->priv->source);
       break;
 
     case CLUTTER_SNAP_EDGE_RIGHT:
-      if (self->from_edge == CLUTTER_SNAP_EDGE_RIGHT)
-        allocation->x2 = source_x + source_height + self->offset;
-      else if (self->from_edge == CLUTTER_SNAP_EDGE_LEFT)
-        allocation->x1 = source_x + source_height + self->offset;
+      if (self->priv->from_edge == CLUTTER_SNAP_EDGE_RIGHT)
+        allocation->x2 = source_x + source_height + self->priv->offset;
+      else if (self->priv->from_edge == CLUTTER_SNAP_EDGE_LEFT)
+        allocation->x1 = source_x + source_height + self->priv->offset;
       else
-        warn_horizontal_edge ("right", self->actor, self->source);
+        warn_horizontal_edge ("right", self->priv->actor, self->priv->source);
       break;
 
       break;
 
     case CLUTTER_SNAP_EDGE_TOP:
-      if (self->from_edge == CLUTTER_SNAP_EDGE_TOP)
-        allocation->y1 = source_y + self->offset;
-      else if (self->from_edge == CLUTTER_SNAP_EDGE_BOTTOM)
-        allocation->y2 = source_y + self->offset;
+      if (self->priv->from_edge == CLUTTER_SNAP_EDGE_TOP)
+        allocation->y1 = source_y + self->priv->offset;
+      else if (self->priv->from_edge == CLUTTER_SNAP_EDGE_BOTTOM)
+        allocation->y2 = source_y + self->priv->offset;
       else
-        warn_vertical_edge ("top", self->actor, self->source);
+        warn_vertical_edge ("top", self->priv->actor, self->priv->source);
       break;
 
     case CLUTTER_SNAP_EDGE_BOTTOM:
-      if (self->from_edge == CLUTTER_SNAP_EDGE_BOTTOM)
-        allocation->y2 = source_y + source_height + self->offset;
-      else if (self->from_edge == CLUTTER_SNAP_EDGE_TOP)
-        allocation->y1 = source_y + source_height + self->offset;
+      if (self->priv->from_edge == CLUTTER_SNAP_EDGE_BOTTOM)
+        allocation->y2 = source_y + source_height + self->priv->offset;
+      else if (self->priv->from_edge == CLUTTER_SNAP_EDGE_TOP)
+        allocation->y1 = source_y + source_height + self->priv->offset;
       else
-        warn_vertical_edge ("bottom", self->actor, self->source);
+        warn_vertical_edge ("bottom", self->priv->actor, self->priv->source);
       break;
 
     default:
@@ -206,7 +213,7 @@ clutter_snap_constraint_set_actor (ClutterActorMeta *meta,
   ClutterActorMetaClass *parent;
 
   /* store the pointer to the actor, for later use */
-  self->actor = new_actor;
+  self->priv->actor = new_actor;
 
   parent = CLUTTER_ACTOR_META_CLASS (clutter_snap_constraint_parent_class);
   parent->set_actor (meta, new_actor);
@@ -217,86 +224,110 @@ clutter_snap_constraint_dispose (GObject *gobject)
 {
   ClutterSnapConstraint *snap = CLUTTER_SNAP_CONSTRAINT (gobject);
 
-  if (snap->source != NULL)
+  if (snap->priv->source != NULL)
     {
-      g_signal_handlers_disconnect_by_func (snap->source,
+      g_signal_handlers_disconnect_by_func (snap->priv->source,
                                             G_CALLBACK (source_destroyed),
                                             snap);
-      g_signal_handlers_disconnect_by_func (snap->source,
+      g_signal_handlers_disconnect_by_func (snap->priv->source,
                                             G_CALLBACK (source_queue_relayout),
                                             snap);
-      snap->source = NULL;
+      snap->priv->source = NULL;
     }
 
   G_OBJECT_CLASS (clutter_snap_constraint_parent_class)->dispose (gobject);
 }
 
-static void
-clutter_snap_constraint_set_property (GObject      *gobject,
-                                      guint         prop_id,
-                                      const GValue *value,
-                                      GParamSpec   *pspec)
+static gboolean
+set_source_internal (gpointer self_,
+                     gpointer value_)
 {
-  ClutterSnapConstraint *self = CLUTTER_SNAP_CONSTRAINT (gobject);
+  ClutterSnapConstraint *constraint = self_;
+  ClutterActor *source = value_;
+  ClutterActor *old_source;
+
+  if (constraint->priv->source == source)
+    return FALSE;
 
-  switch (prop_id)
+  old_source = constraint->priv->source;
+  if (old_source != NULL)
     {
-    case PROP_SOURCE:
-      clutter_snap_constraint_set_source (self, g_value_get_object (value));
-      break;
+      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_FROM_EDGE:
-      clutter_snap_constraint_set_edges (self,
-                                         g_value_get_enum (value),
-                                         self->to_edge);
-      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);
 
-    case PROP_TO_EDGE:
-      clutter_snap_constraint_set_edges (self,
-                                         self->from_edge,
-                                         g_value_get_enum (value));
-      break;
+      if (constraint->priv->actor != NULL)
+        clutter_actor_queue_relayout (constraint->priv->actor);
+    }
 
-    case PROP_OFFSET:
-      clutter_snap_constraint_set_offset (self, g_value_get_float (value));
-      break;
+  return TRUE;
+}
 
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
+static gboolean
+set_from_edge_internal (gpointer self_,
+                        glong    value_)
+{
+  ClutterSnapConstraint *self = self_;
+  ClutterSnapEdge from_edge = value_;
+
+  if (self->priv->from_edge == from_edge)
+    return FALSE;
+
+  self->priv->from_edge = from_edge;
+
+  if (self->priv->actor != NULL)
+    clutter_actor_queue_relayout (self->priv->actor);
+
+  return TRUE;
 }
 
-static void
-clutter_snap_constraint_get_property (GObject    *gobject,
-                                      guint       prop_id,
-                                      GValue     *value,
-                                      GParamSpec *pspec)
+static gboolean
+set_to_edge_internal (gpointer self_,
+                      glong    value_)
 {
-  ClutterSnapConstraint *self = CLUTTER_SNAP_CONSTRAINT (gobject);
+  ClutterSnapConstraint *self = self_;
+  ClutterSnapEdge to_edge = value_;
 
-  switch (prop_id)
-    {
-    case PROP_SOURCE:
-      g_value_set_object (value, self->source);
-      break;
+  if (self->priv->to_edge == to_edge)
+    return FALSE;
 
-    case PROP_FROM_EDGE:
-      g_value_set_enum (value, self->from_edge);
-      break;
+  self->priv->to_edge = to_edge;
 
-    case PROP_TO_EDGE:
-      g_value_set_enum (value, self->to_edge);
-      break;
+  if (self->priv->actor != NULL)
+    clutter_actor_queue_relayout (self->priv->actor);
 
-    case PROP_OFFSET:
-      g_value_set_float (value, self->offset);
-      break;
+  return TRUE;
+}
 
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
-      break;
-    }
+static gboolean
+set_offset_internal (gpointer self_,
+                     gfloat   value)
+{
+  ClutterSnapConstraint *self = self_;
+
+  if (fabs (self->priv->offset - value) < 0.001f)
+    return FALSE;
+
+  self->priv->offset = value;
+
+  if (self->priv->actor != NULL)
+    clutter_actor_queue_relayout (self->priv->actor);
+
+  return TRUE;
 }
 
 static void
@@ -306,9 +337,14 @@ clutter_snap_constraint_class_init (ClutterSnapConstraintClass *klass)
   ClutterConstraintClass *constraint_class = CLUTTER_CONSTRAINT_CLASS (klass);
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
+  g_type_class_add_private (klass, sizeof (ClutterSnapConstraintPrivate));
+
+  gobject_class->dispose = clutter_snap_constraint_dispose;
+
   meta_class->set_actor = clutter_snap_constraint_set_actor;
 
   constraint_class->update_allocation = clutter_snap_constraint_update_allocation;
+
   /**
    * ClutterSnapConstraint:source:
    *
@@ -317,11 +353,15 @@ clutter_snap_constraint_class_init (ClutterSnapConstraintClass *klass)
    * Since: 1.6
    */
   obj_props[PROP_SOURCE] =
-    g_param_spec_object ("source",
-                         P_("Source"),
-                         P_("The source of the constraint"),
-                         CLUTTER_TYPE_ACTOR,
-                         CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+    g_object_property_new ("source", G_PROPERTY_READWRITE,
+                           G_STRUCT_OFFSET (ClutterSnapConstraintPrivate, 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 constraint"));
 
   /**
    * ClutterSnapConstraint:from-edge:
@@ -331,12 +371,17 @@ clutter_snap_constraint_class_init (ClutterSnapConstraintClass *klass)
    * Since: 1.6
    */
   obj_props[PROP_FROM_EDGE] =
-    g_param_spec_enum ("from-edge",
+    g_enum_property_new ("from-edge", G_PROPERTY_READWRITE,
+                         G_STRUCT_OFFSET (ClutterSnapConstraintPrivate, from_edge),
+                         set_from_edge_internal,
+                         NULL);
+  g_property_set_prerequisite (G_PROPERTY (obj_props[PROP_FROM_EDGE]),
+                               CLUTTER_TYPE_SNAP_EDGE);
+  g_property_set_default (G_PROPERTY (obj_props[PROP_FROM_EDGE]),
+                          CLUTTER_SNAP_EDGE_RIGHT);
+  g_property_describe (G_PROPERTY (obj_props[PROP_FROM_EDGE]),
                        P_("From Edge"),
-                       P_("The edge of the actor that should be snapped"),
-                       CLUTTER_TYPE_SNAP_EDGE,
-                       CLUTTER_SNAP_EDGE_RIGHT,
-                       CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+                       P_("The edge of the actor that should be snapped"));
 
   /**
    * ClutterSnapConstraint:to-edge:
@@ -346,12 +391,17 @@ clutter_snap_constraint_class_init (ClutterSnapConstraintClass *klass)
    * Since: 1.6
    */
   obj_props[PROP_TO_EDGE] =
-    g_param_spec_enum ("to-edge",
+    g_enum_property_new ("to-edge", G_PROPERTY_READWRITE,
+                         G_STRUCT_OFFSET (ClutterSnapConstraintPrivate, to_edge),
+                         set_to_edge_internal,
+                         NULL);
+  g_property_set_prerequisite (G_PROPERTY (obj_props[PROP_TO_EDGE]),
+                               CLUTTER_TYPE_SNAP_EDGE);
+  g_property_set_default (G_PROPERTY (obj_props[PROP_TO_EDGE]),
+                          CLUTTER_SNAP_EDGE_RIGHT);
+  g_property_describe (G_PROPERTY (obj_props[PROP_TO_EDGE]),
                        P_("To Edge"),
-                       P_("The edge of the source that should be snapped"),
-                       CLUTTER_TYPE_SNAP_EDGE,
-                       CLUTTER_SNAP_EDGE_RIGHT,
-                       CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+                       P_("The edge of the source that should be snapped"));
 
   /**
    * ClutterSnapConstraint:offset:
@@ -362,29 +412,27 @@ clutter_snap_constraint_class_init (ClutterSnapConstraintClass *klass)
    * Since: 1.6
    */
   obj_props[PROP_OFFSET] =
-    g_param_spec_float ("offset",
-                        P_("Offset"),
-                        P_("The offset in pixels to apply to the constraint"),
-                        -G_MAXFLOAT, G_MAXFLOAT,
-                        0.0f,
-                        CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
+    g_float_property_new ("offset", G_PROPERTY_READWRITE,
+                          G_STRUCT_OFFSET (ClutterSnapConstraintPrivate, 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 constraint"));
 
-  gobject_class->dispose = clutter_snap_constraint_dispose;
-  gobject_class->set_property = clutter_snap_constraint_set_property;
-  gobject_class->get_property = clutter_snap_constraint_get_property;
   g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
 }
 
 static void
 clutter_snap_constraint_init (ClutterSnapConstraint *self)
 {
-  self->actor = NULL;
-  self->source = NULL;
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_SNAP_CONSTRAINT,
+                                            ClutterSnapConstraintPrivate);
 
-  self->from_edge = CLUTTER_SNAP_EDGE_RIGHT;
-  self->to_edge = CLUTTER_SNAP_EDGE_RIGHT;
+  self->priv->from_edge = CLUTTER_SNAP_EDGE_RIGHT;
+  self->priv->to_edge = CLUTTER_SNAP_EDGE_RIGHT;
 
-  self->offset = 0.0f;
+  self->priv->offset = 0.0f;
 }
 
 /**
@@ -427,45 +475,6 @@ clutter_snap_constraint_new (ClutterActor    *source,
  *
  * Since: 1.6
  */
-void
-clutter_snap_constraint_set_source (ClutterSnapConstraint *constraint,
-                                    ClutterActor          *source)
-{
-  ClutterActor *old_source;
-
-  g_return_if_fail (CLUTTER_IS_SNAP_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_snap_constraint_get_source:
@@ -477,13 +486,10 @@ clutter_snap_constraint_set_source (ClutterSnapConstraint *constraint,
  *
  * Since: 1.6
  */
-ClutterActor *
-clutter_snap_constraint_get_source (ClutterSnapConstraint *constraint)
-{
-  g_return_val_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint), NULL);
-
-  return constraint->source;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterSnapConstraint,
+                           clutter_snap_constraint,
+                           ClutterActor *,
+                           source)
 
 /**
  * clutter_snap_constraint_set_edges:
@@ -504,33 +510,16 @@ clutter_snap_constraint_set_edges (ClutterSnapConstraint *constraint,
                                    ClutterSnapEdge        from_edge,
                                    ClutterSnapEdge        to_edge)
 {
-  gboolean from_changed = FALSE, to_changed = FALSE;
-
   g_return_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint));
 
   g_object_freeze_notify (G_OBJECT (constraint));
 
-  if (constraint->from_edge != from_edge)
-    {
-      constraint->from_edge = from_edge;
-      g_object_notify_by_pspec (G_OBJECT (constraint),
-                                obj_props[PROP_FROM_EDGE]);
-      from_changed = TRUE;
-    }
-
-  if (constraint->to_edge != to_edge)
-    {
-      constraint->to_edge = to_edge;
-      g_object_notify_by_pspec (G_OBJECT (constraint),
-                                obj_props[PROP_TO_EDGE]);
-      to_changed = TRUE;
-    }
-
-  if ((from_changed || to_changed) &&
-      constraint->actor != NULL)
-    {
-      clutter_actor_queue_relayout (constraint->actor);
-    }
+  g_property_set (G_PROPERTY (obj_props[PROP_FROM_EDGE]),
+                  constraint,
+                  from_edge);
+  g_property_set (G_PROPERTY (obj_props[PROP_TO_EDGE]),
+                  constraint,
+                  to_edge);
 
   g_object_thaw_notify (G_OBJECT (constraint));
 }
@@ -552,11 +541,15 @@ clutter_snap_constraint_get_edge (ClutterSnapConstraint *constraint,
 {
   g_return_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint));
 
-  if (from_edge)
-    *from_edge = constraint->from_edge;
+  if (from_edge != NULL)
+    g_property_get (G_PROPERTY (obj_props[PROP_FROM_EDGE]),
+                    constraint,
+                    from_edge);
 
-  if (to_edge)
-    *to_edge = constraint->to_edge;
+  if (to_edge != NULL)
+    g_property_get (G_PROPERTY (obj_props[PROP_TO_EDGE]),
+                    constraint,
+                    to_edge);
 }
 
 /**
@@ -568,22 +561,6 @@ clutter_snap_constraint_get_edge (ClutterSnapConstraint *constraint,
  *
  * Since: 1.6
  */
-void
-clutter_snap_constraint_set_offset (ClutterSnapConstraint *constraint,
-                                    gfloat                 offset)
-{
-  g_return_if_fail (CLUTTER_IS_SNAP_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_snap_constraint_get_offset:
@@ -595,10 +572,8 @@ clutter_snap_constraint_set_offset (ClutterSnapConstraint *constraint,
  *
  * Since: 1.6
  */
-gfloat
-clutter_snap_constraint_get_offset (ClutterSnapConstraint *constraint)
-{
-  g_return_val_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint), 0.0);
 
-  return constraint->offset;
-}
+G_DEFINE_PROPERTY_GET_SET (ClutterSnapConstraint,
+                           clutter_snap_constraint,
+                           gfloat,
+                           offset)



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