[gtk/wip/ebassi/constraint-layout: 39/69] Add a C convenience function for VFL constraints



commit 06c825df9037c506d88e5555c89776520cb3c834
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Sat Jun 29 19:04:29 2019 +0100

    Add a C convenience function for VFL constraints
    
    The dictionary-based function is convenient for language bindings, but C
    developers will feel more at home with a variadic arguments list.

 demos/gtk-demo/constraints3.c | 15 +++----
 gtk/gtkconstraintlayout.c     | 97 ++++++++++++++++++++++++++++++++++++++-----
 gtk/gtkconstraintlayout.h     |  9 ++++
 3 files changed, 101 insertions(+), 20 deletions(-)
---
diff --git a/demos/gtk-demo/constraints3.c b/demos/gtk-demo/constraints3.c
index fee7618f44..648f38125b 100644
--- a/demos/gtk-demo/constraints3.c
+++ b/demos/gtk-demo/constraints3.c
@@ -85,25 +85,20 @@ build_constraints (VflGrid          *self,
     "V:|-[button1]-12-[button3(==button1)]-|",
     "V:|-[button2]-12-[button3(==button2)]-|",
   };
-  GHashTable *views;
   GError *error = NULL;
 
-  views = g_hash_table_new (g_str_hash, g_str_equal);
-  g_hash_table_insert (views, "button1", self->button1);
-  g_hash_table_insert (views, "button2", self->button2);
-  g_hash_table_insert (views, "button3", self->button3);
-
   gtk_constraint_layout_add_constraints_from_description (manager, vfl, G_N_ELEMENTS (vfl),
                                                           8, 8,
-                                                          views,
-                                                          &error);
+                                                          &error,
+                                                          "button1", self->button1,
+                                                          "button2", self->button2,
+                                                          "button3", self->button3,
+                                                          NULL);
   if (error != NULL)
     {
       g_printerr ("VFL parsing error:\n%s", error->message);
       g_error_free (error);
     }
-
-  g_hash_table_unref (views);
 }
 
 static void
diff --git a/gtk/gtkconstraintlayout.c b/gtk/gtkconstraintlayout.c
index 25f5f8fcd8..44554f33f4 100644
--- a/gtk/gtkconstraintlayout.c
+++ b/gtk/gtkconstraintlayout.c
@@ -1612,7 +1612,7 @@ attribute_from_name (const char *name)
 }
 
 /**
- * gtk_constraint_layout_add_constraints_from_description:
+ * gtk_constraint_layout_add_constraints_from_descriptionv: (rename-to 
gtk_constraint_layout_add_constraints_from_description)
  * @layout: a #GtkConstraintLayout
  * @lines: (array length=n_lines): an array of Visual Format Language lines
  *   defining a set of constraints
@@ -1622,14 +1622,15 @@ attribute_from_name (const char *name)
  * @views: (element-type utf8 Gtk.Widget): a dictionary of [ name, widget ]
  *   pairs; the `name` keys map to the view names in the VFL lines, while
  *   the `widget` values map to children of the widget using a #GtkConstraintLayout
+ * @error: return location for a #GError
  *
  * Creates a list of constraints they formal description using a compact
  * description syntax called VFL, or "Visual Format Language".
  *
  * The Visual Format Language is based on Apple's AutoLayout 
[VFL](https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html).
  *
- * The @views dictionary is used to match widgets to the symbolic view name
- * inside the VFL.
+ * The @views dictionary is used to match #GtkConstraintTargets to the symbolic
+ * view name inside the VFL.
  *
  * The VFL grammar is:
  *
@@ -1705,13 +1706,13 @@ attribute_from_name (const char *name)
  * Returns: %TRUE if the constraints were added to the layout
  */
 gboolean
-gtk_constraint_layout_add_constraints_from_description (GtkConstraintLayout *layout,
-                                                        const char * const   lines[],
-                                                        gsize                n_lines,
-                                                        int                  hspacing,
-                                                        int                  vspacing,
-                                                        GHashTable          *views,
-                                                        GError             **error)
+gtk_constraint_layout_add_constraints_from_descriptionv (GtkConstraintLayout *layout,
+                                                         const char * const   lines[],
+                                                         gsize                n_lines,
+                                                         int                  hspacing,
+                                                         int                  vspacing,
+                                                         GHashTable          *views,
+                                                         GError             **error)
 {
   GtkConstraintVflParser *parser;
 
@@ -1802,3 +1803,79 @@ gtk_constraint_layout_add_constraints_from_description (GtkConstraintLayout *lay
 
   return TRUE;
 }
+
+/**
+ * gtk_constraint_layout_add_constraints_from_description:
+ * @layout: a #GtkConstraintLayout
+ * @lines: (array length=n_lines): an array of Visual Format Language lines
+ *   defining a set of constraints
+ * @n_lines: the number of lines
+ * @hspacing: default horizontal spacing value, or -1 for the fallback value
+ * @vspacing: default vertical spacing value, or -1 for the fallback value
+ * @error: return location for a #GError
+ * @first_view: the name of a view in the VFL description, followed by the
+ *   #GtkConstraintTarget to which it maps
+ * @...: a %NULL-terminated list of view names and #GtkConstraintTargets
+ *
+ * Creates a list of constraints they formal description using a compact
+ * description syntax called VFL, or "Visual Format Language".
+ *
+ * This function is a convenience wrapper around
+ * gtk_constraint_layout_add_constraints_from_descriptionv(), using
+ * variadic arguments to populate the view/target map.
+ *
+ * Returns: %TRUE if the constraints were added to the layout
+ */
+gboolean
+gtk_constraint_layout_add_constraints_from_description (GtkConstraintLayout *layout,
+                                                        const char * const   lines[],
+                                                        gsize                n_lines,
+                                                        int                  hspacing,
+                                                        int                  vspacing,
+                                                        GError             **error,
+                                                        const char          *first_view,
+                                                        ...)
+{
+  GtkConstraintVflParser *parser;
+  GHashTable *views;
+  const char *view;
+  gboolean res;
+  va_list args;
+
+  g_return_val_if_fail (GTK_IS_CONSTRAINT_LAYOUT (layout), FALSE);
+  g_return_val_if_fail (lines != NULL, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  g_return_val_if_fail (first_view != NULL, FALSE);
+
+  parser = gtk_constraint_vfl_parser_new ();
+  gtk_constraint_vfl_parser_set_default_spacing (parser, hspacing, vspacing);
+
+  views = g_hash_table_new (g_str_hash, g_str_equal);
+
+  va_start (args, first_view);
+
+  view = first_view;
+  while (view != NULL)
+    {
+      GtkConstraintTarget *target = va_arg (args, GtkConstraintTarget *);
+
+      if (target == NULL)
+        break;
+
+      g_hash_table_insert (views, (gpointer) view, target);
+
+      view = va_arg (args, const char *);
+    }
+
+  va_end (args);
+
+  res =
+    gtk_constraint_layout_add_constraints_from_descriptionv (layout, lines, n_lines,
+                                                             hspacing, vspacing,
+                                                             views,
+                                                             error);
+
+  g_hash_table_unref (views);
+
+  return res;
+}
diff --git a/gtk/gtkconstraintlayout.h b/gtk/gtkconstraintlayout.h
index 39d3aecaf0..227a8bfbc7 100644
--- a/gtk/gtkconstraintlayout.h
+++ b/gtk/gtkconstraintlayout.h
@@ -78,6 +78,15 @@ void                    gtk_constraint_layout_remove_guide      (GtkConstraintLa
 
 GDK_AVAILABLE_IN_ALL
 gboolean                gtk_constraint_layout_add_constraints_from_description  (GtkConstraintLayout 
*manager,
+                                                                                 const char * const   
lines[],
+                                                                                 gsize                
n_lines,
+                                                                                 int                  
hspacing,
+                                                                                 int                  
vspacing,
+                                                                                 GError             **error,
+                                                                                 const char          
*first_view,
+                                                                                 ...) G_GNUC_NULL_TERMINATED;
+GDK_AVAILABLE_IN_ALL
+gboolean                gtk_constraint_layout_add_constraints_from_descriptionv (GtkConstraintLayout 
*manager,
                                                                                  const char * const   
lines[],
                                                                                  gsize                
n_lines,
                                                                                  int                  
hspacing,


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