[gtk/constraint-flexible-space: 3/5] Redefine constraints with GtkConstraintTarget



commit 322526488660eb0af17e9a18db33dd242146a8d7
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jun 25 15:16:02 2019 -0400

    Redefine constraints with GtkConstraintTarget
    
    This is in preparation for allowing non-widgets
    to act as constraint targets.

 gtk/gtkconstraint.c        | 114 ++++++++++++++++++++++++++-------------------
 gtk/gtkconstraint.h        |  10 ++--
 gtk/gtkconstraintlayout.c  |   2 +-
 gtk/gtkconstraintprivate.h |   4 +-
 4 files changed, 77 insertions(+), 53 deletions(-)
---
diff --git a/gtk/gtkconstraint.c b/gtk/gtkconstraint.c
index 249e2a762a..d030e04ef8 100644
--- a/gtk/gtkconstraint.c
+++ b/gtk/gtkconstraint.c
@@ -46,10 +46,10 @@
 #include "gtkwidget.h"
 
 enum {
-  PROP_TARGET_WIDGET = 1,
+  PROP_TARGET = 1,
   PROP_TARGET_ATTRIBUTE,
   PROP_RELATION,
-  PROP_SOURCE_WIDGET,
+  PROP_SOURCE,
   PROP_SOURCE_ATTRIBUTE,
   PROP_MULTIPLIER,
   PROP_CONSTANT,
@@ -72,8 +72,8 @@ gtk_constraint_set_property (GObject      *gobject,
 
   switch (prop_id)
     {
-    case PROP_TARGET_WIDGET:
-      self->target_widget = g_value_get_object (value);
+    case PROP_TARGET:
+      self->target = g_value_get_object (value);
       break;
 
     case PROP_TARGET_ATTRIBUTE:
@@ -84,8 +84,8 @@ gtk_constraint_set_property (GObject      *gobject,
       self->relation = g_value_get_enum (value);
       break;
 
-    case PROP_SOURCE_WIDGET:
-      self->source_widget = g_value_get_object (value);
+    case PROP_SOURCE:
+      self->source = g_value_get_object (value);
       break;
 
     case PROP_SOURCE_ATTRIBUTE:
@@ -120,8 +120,8 @@ gtk_constraint_get_property (GObject    *gobject,
 
   switch (prop_id)
     {
-    case PROP_TARGET_WIDGET:
-      g_value_set_object (value, self->target_widget);
+    case PROP_TARGET:
+      g_value_set_object (value, self->target);
       break;
 
     case PROP_TARGET_ATTRIBUTE:
@@ -132,8 +132,8 @@ gtk_constraint_get_property (GObject    *gobject,
       g_value_set_enum (value, self->relation);
       break;
 
-    case PROP_SOURCE_WIDGET:
-      g_value_set_object (value, self->source_widget);
+    case PROP_SOURCE:
+      g_value_set_object (value, self->source);
       break;
 
     case PROP_SOURCE_ATTRIBUTE:
@@ -178,31 +178,31 @@ gtk_constraint_class_init (GtkConstraintClass *klass)
   gobject_class->finalize = gtk_constraint_finalize;
 
   /**
-   * GtkConstraint:target-widget:
+   * GtkConstraint:target:
    *
-   * The target widget of the constraint.
+   * The target of the constraint.
    *
    * The constraint will set the #GtkConstraint:target-attribute of the
-   * target widget using the #GtkConstraint:source-attribute of the source
+   * target using the #GtkConstraint:source-attribute of the source
    * widget.
    */
-  obj_props[PROP_TARGET_WIDGET] =
-    g_param_spec_object ("target-widget",
-                         P_("Target Widget"),
-                         P_("The target widget of the constraint"),
-                         GTK_TYPE_WIDGET,
+  obj_props[PROP_TARGET] =
+    g_param_spec_object ("target",
+                         P_("Target"),
+                         P_("The target of the constraint"),
+                         GTK_TYPE_CONSTRAINT_TARGET,
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS |
                          G_PARAM_CONSTRUCT_ONLY);
   /**
    * GtkConstraint:target-attribute:
    *
-   * The attribute of the #GtkConstraint:target-widget set by the constraint.
+   * The attribute of the #GtkConstraint:target set by the constraint.
    */
   obj_props[PROP_TARGET_ATTRIBUTE] =
     g_param_spec_enum ("target-attribute",
                        P_("Target Attribute"),
-                       P_("The attribute of the target widget set by the constraint"),
+                       P_("The attribute of the target set by the constraint"),
                        GTK_TYPE_CONSTRAINT_ATTRIBUTE,
                        GTK_CONSTRAINT_ATTRIBUTE_NONE,
                        G_PARAM_READWRITE |
@@ -223,26 +223,25 @@ gtk_constraint_class_init (GtkConstraintClass *klass)
                        G_PARAM_STATIC_STRINGS |
                        G_PARAM_CONSTRUCT_ONLY);
   /**
-   * GtkConstraint:source-widget:
+   * GtkConstraint:source:
    *
-   * The source widget of the constraint.
+   * The source of the constraint.
    *
    * The constraint will set the #GtkConstraint:target-attribute of the
-   * target widget using the #GtkConstraint:source-attribute of the source
-   * widget.
+   * target using the #GtkConstraint:source-attribute of the source.
    */
-  obj_props[PROP_SOURCE_WIDGET] =
-    g_param_spec_object ("source-widget",
-                         P_("Source Widget"),
-                         P_("The source widget of the constraint"),
-                         GTK_TYPE_WIDGET,
+  obj_props[PROP_SOURCE] =
+    g_param_spec_object ("source",
+                         P_("Source"),
+                         P_("The source of the constraint"),
+                         GTK_TYPE_CONSTRAINT_TARGET,
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS |
                          G_PARAM_CONSTRUCT_ONLY);
   /**
    * GtkConstraint:source-attribute:
    *
-   * The attribute of the #GtkConstraint:source-widget read by the constraint.
+   * The attribute of the #GtkConstraint:source read by the constraint.
    */
   obj_props[PROP_SOURCE_ATTRIBUTE] =
     g_param_spec_enum ("source-attribute",
@@ -316,39 +315,38 @@ gtk_constraint_init (GtkConstraint *self)
 
 /**
  * gtk_constraint_new:
- * @target_widget: (nullable): a #GtkWidget
+ * @target: (nullable): a #GtkConstraintTarget
  * @target_attribute: the attribute of @target_widget to be set
  * @relation: the relation equivalence between @target_attribute and @source_attribute
- * @source_widget: (nullable): a #GtkWidget
+ * @source: (nullable): a #GtkConstraintTarget
  * @source_attribute: the attribute of @source_widget to be read
  * @multiplier: a multiplication factor to be applied to @source_attribute
  * @constant: a constant factor to be added to @source_attribute
  * @strength: the strength of the constraint
  *
  * Creates a new #GtkConstraint representing a relation between a layout
- * attribute on a source #GtkWidget and a layout attribute on a target
- * #GtkWidget.
+ * attribute on a source and a layout attribute on a target.
  *
  * Returns: the newly created #GtkConstraint
  */
 GtkConstraint *
-gtk_constraint_new (GtkWidget              *target_widget,
+gtk_constraint_new (GtkConstraintTarget    *target,
                     GtkConstraintAttribute  target_attribute,
                     GtkConstraintRelation   relation,
-                    GtkWidget              *source_widget,
+                    GtkConstraintTarget    *source,
                     GtkConstraintAttribute  source_attribute,
                     double                  multiplier,
                     double                  constant,
                     int                     strength)
 {
-  g_return_val_if_fail (target_widget == NULL || GTK_IS_WIDGET (target_widget), NULL);
-  g_return_val_if_fail (source_widget == NULL || GTK_IS_WIDGET (source_widget), NULL);
+  g_return_val_if_fail (target == NULL || GTK_IS_CONSTRAINT_TARGET (target), NULL);
+  g_return_val_if_fail (source == NULL || GTK_IS_CONSTRAINT_TARGET (source), NULL);
 
   return g_object_new (GTK_TYPE_CONSTRAINT,
-                       "target-widget", target_widget,
+                       "target", target,
                        "target-attribute", target_attribute,
                        "relation", relation,
-                       "source-widget", source_widget,
+                       "source", source,
                        "source-attribute", source_attribute,
                        "multiplier", multiplier,
                        "constant", constant,
@@ -358,28 +356,28 @@ gtk_constraint_new (GtkWidget              *target_widget,
 
 /**
  * gtk_constraint_new_constant:
- * @target_widget: (nullable): a #GtkWidget
+ * @target: (nullable): a #GtkConstraintTarget
  * @target_attribute: the attribute of @target_widget to be set
  * @relation: the relation equivalence between @target_attribute and @constant
  * @constant: a constant factor to be set on @target_attribute
  * @strength: the strength of the constraint
  *
  * Creates a new #GtkConstraint representing a relation between a layout
- * attribute on a target #GtkWidget and a constant value.
+ * attribute on a target and a constant value.
  *
  * Returns: the newly created #GtkConstraint
  */
 GtkConstraint *
-gtk_constraint_new_constant (GtkWidget              *target_widget,
+gtk_constraint_new_constant (GtkConstraintTarget    *target,
                              GtkConstraintAttribute  target_attribute,
                              GtkConstraintRelation   relation,
                              double                  constant,
                              int                     strength)
 {
-  g_return_val_if_fail (target_widget == NULL || GTK_IS_WIDGET (target_widget), NULL);
+  g_return_val_if_fail (target == NULL || GTK_IS_CONSTRAINT_TARGET (target), NULL);
 
   return g_object_new (GTK_TYPE_CONSTRAINT,
-                       "target-widget", target_widget,
+                       "target", target,
                        "target-attribute", target_attribute,
                        "relation", relation,
                        "source-attribute", GTK_CONSTRAINT_ATTRIBUTE_NONE,
@@ -401,7 +399,18 @@ gtk_constraint_get_target_widget (GtkConstraint *constraint)
 {
   g_return_val_if_fail (GTK_IS_CONSTRAINT (constraint), NULL);
 
-  return constraint->target_widget;
+  if (GTK_IS_WIDGET (constraint->target))
+    return GTK_WIDGET (constraint->target);
+
+  return NULL;
+}
+
+GtkConstraintTarget *
+gtk_constraint_get_target (GtkConstraint *constraint)
+{
+  g_return_val_if_fail (GTK_IS_CONSTRAINT (constraint), NULL);
+
+  return constraint->target;
 }
 
 GtkConstraintAttribute
@@ -425,7 +434,18 @@ gtk_constraint_get_source_widget (GtkConstraint *constraint)
 {
   g_return_val_if_fail (GTK_IS_CONSTRAINT (constraint), NULL);
 
-  return constraint->source_widget;
+  if (GTK_IS_WIDGET (constraint->source))
+    return GTK_WIDGET (constraint->source);
+
+  return NULL;
+}
+
+GtkConstraintTarget *
+gtk_constraint_get_source (GtkConstraint *constraint)
+{
+  g_return_val_if_fail (GTK_IS_CONSTRAINT (constraint), NULL);
+
+  return constraint->source;
 }
 
 GtkConstraintAttribute
diff --git a/gtk/gtkconstraint.h b/gtk/gtkconstraint.h
index 57a42716d5..f129d5a67c 100644
--- a/gtk/gtkconstraint.h
+++ b/gtk/gtkconstraint.h
@@ -50,16 +50,16 @@ GDK_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (GtkConstraint, gtk_constraint, GTK, CONSTRAINT, GObject)
 
 GDK_AVAILABLE_IN_ALL
-GtkConstraint *         gtk_constraint_new                      (GtkWidget              *target_widget,
+GtkConstraint *         gtk_constraint_new                      (GtkConstraintTarget    *target_widget,
                                                                  GtkConstraintAttribute  target_attribute,
                                                                  GtkConstraintRelation   relation,
-                                                                 GtkWidget              *source_widget,
+                                                                 GtkConstraintTarget    *source_widget,
                                                                  GtkConstraintAttribute  source_attribute,
                                                                  double                  multiplier,
                                                                  double                  constant,
                                                                  int                     strength);
 GDK_AVAILABLE_IN_ALL
-GtkConstraint *         gtk_constraint_new_constant             (GtkWidget              *target_widget,
+GtkConstraint *         gtk_constraint_new_constant             (GtkConstraintTarget    *target_widget,
                                                                  GtkConstraintAttribute  target_attribute,
                                                                  GtkConstraintRelation   relation,
                                                                  double                  constant,
@@ -68,10 +68,14 @@ GtkConstraint *         gtk_constraint_new_constant             (GtkWidget
 GDK_AVAILABLE_IN_ALL
 GtkWidget *             gtk_constraint_get_target_widget        (GtkConstraint          *constraint);
 GDK_AVAILABLE_IN_ALL
+GtkConstraintTarget *   gtk_constraint_get_target               (GtkConstraint          *constraint);
+GDK_AVAILABLE_IN_ALL
 GtkConstraintAttribute  gtk_constraint_get_target_attribute     (GtkConstraint          *constraint);
 GDK_AVAILABLE_IN_ALL
 GtkWidget *             gtk_constraint_get_source_widget        (GtkConstraint          *constraint);
 GDK_AVAILABLE_IN_ALL
+GtkConstraintTarget *   gtk_constraint_get_source               (GtkConstraint          *constraint);
+GDK_AVAILABLE_IN_ALL
 GtkConstraintAttribute  gtk_constraint_get_source_attribute     (GtkConstraint          *constraint);
 GDK_AVAILABLE_IN_ALL
 GtkConstraintRelation   gtk_constraint_get_relation             (GtkConstraint          *constraint);
diff --git a/gtk/gtkconstraintlayout.c b/gtk/gtkconstraintlayout.c
index 6cd6a7c70e..0cd0da8282 100644
--- a/gtk/gtkconstraintlayout.c
+++ b/gtk/gtkconstraintlayout.c
@@ -1083,7 +1083,7 @@ gtk_constraint_layout_new (void)
  *
  * Adds a #GtkConstraint to the layout manager.
  *
- * The #GtkConstraint:source-widget and #GtkConstraint:target-widget
+ * The #GtkConstraint:source and #GtkConstraint:target
  * properties of @constraint can be:
  *
  *  - set to %NULL to indicate that the constraint refers to the
diff --git a/gtk/gtkconstraintprivate.h b/gtk/gtkconstraintprivate.h
index 90f70234a2..8a7fa147f1 100644
--- a/gtk/gtkconstraintprivate.h
+++ b/gtk/gtkconstraintprivate.h
@@ -31,8 +31,8 @@ struct _GtkConstraint
   GtkConstraintAttribute target_attribute;
   GtkConstraintAttribute source_attribute;
 
-  GtkWidget *target_widget;
-  GtkWidget *source_widget;
+  GtkConstraintTarget *target;
+  GtkConstraintTarget *source;
 
   GtkConstraintRelation relation;
 


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