From 8f5515d498e564af65ee9f21528c6bd66146ac59 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 25 Jun 2019 15:16:02 -0400 Subject: [PATCH] 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 d7bf3e0a08..98a6afab2b 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 1467c86d9c..320795b0e7 100644 --- a/gtk/gtkconstraint.h +++ b/gtk/gtkconstraint.h @@ -58,16 +58,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, @@ -76,10 +76,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 bbdd27746c..4fbf5fb188 100644 --- a/gtk/gtkconstraintlayout.c +++ b/gtk/gtkconstraintlayout.c @@ -1074,7 +1074,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;