[gtk/wip/otte/listview: 13/138] builder: Allow <constant> without a type



commit 42f3e2bae572496eab123e98bd69c6a735267f07
Author: Benjamin Otte <otte redhat com>
Date:   Mon Nov 25 08:04:24 2019 +0100

    builder: Allow <constant> without a type
    
    A constant without a type is assumed to be an object. This is the most
    common case and allows
      <constant>foo</constant>
    without requiring updates to the type whenever the foo object changes.

 gtk/gtkbuilderparser.c | 60 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 39 insertions(+), 21 deletions(-)
---
diff --git a/gtk/gtkbuilderparser.c b/gtk/gtkbuilderparser.c
index 149dfd6a70..fe1788f94f 100644
--- a/gtk/gtkbuilderparser.c
+++ b/gtk/gtkbuilderparser.c
@@ -1036,7 +1036,7 @@ parse_constant_expression (ParserData   *data,
                            GError      **error)
 {
   ExpressionInfo *info;
-  const char *type_name;
+  const char *type_name = NULL;
   GType type;
 
   if (!check_expression_parent (data))
@@ -1046,22 +1046,27 @@ parse_constant_expression (ParserData   *data,
     }
 
   if (!g_markup_collect_attributes (element_name, names, values, error,
-                                    G_MARKUP_COLLECT_STRING, "type", &type_name,
+                                    G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "type", &type_name,
                                     G_MARKUP_COLLECT_INVALID))
     {
       _gtk_builder_prefix_error (data->builder, &data->ctx, error);
       return;
     }
 
-  type = gtk_builder_get_type_from_name (data->builder, type_name);
-  if (type == G_TYPE_INVALID)
+  if (type_name == NULL)
+    type = G_TYPE_INVALID;
+  else
     {
-      g_set_error (error,
-                   GTK_BUILDER_ERROR,
-                   GTK_BUILDER_ERROR_INVALID_VALUE,
-                   "Invalid type '%s'", type_name);
-      _gtk_builder_prefix_error (data->builder, &data->ctx, error);
-      return;
+      type = gtk_builder_get_type_from_name (data->builder, type_name);
+      if (type == G_TYPE_INVALID)
+        {
+          g_set_error (error,
+                       GTK_BUILDER_ERROR,
+                       GTK_BUILDER_ERROR_INVALID_VALUE,
+                       "Invalid type '%s'", type_name);
+          _gtk_builder_prefix_error (data->builder, &data->ctx, error);
+          return;
+        }
     }
 
   info = g_slice_new0 (ExpressionInfo);
@@ -1202,20 +1207,33 @@ expression_info_construct (GtkBuilder      *builder,
     case EXPRESSION_CONSTANT:
       {
         GtkExpression *expr;
-        GValue value = G_VALUE_INIT;
 
-        if (!gtk_builder_value_from_string_type (builder,
-                                                 info->constant.type,
-                                                 info->constant.text->str,
-                                                 &value,
-                                                 error))
-          return  NULL;
+        if (info->constant.type == G_TYPE_INVALID)
+          {
+            GObject *o = gtk_builder_lookup_object (builder, info->constant.text->str, 0, 0, error);
+            if (o == NULL)
+              return NULL;
 
-        if (G_VALUE_HOLDS_OBJECT (&value))
-          expr = gtk_object_expression_new (g_value_get_object (&value));
+            expr = gtk_object_expression_new (o);
+          }
         else
-          expr = gtk_constant_expression_new_for_value (&value);
-        g_value_unset (&value);
+          {
+            GValue value = G_VALUE_INIT;
+
+            if (!gtk_builder_value_from_string_type (builder,
+                                                     info->constant.type,
+                                                     info->constant.text->str,
+                                                     &value,
+                                                     error))
+              return  NULL;
+
+            if (G_VALUE_HOLDS_OBJECT (&value))
+              expr = gtk_object_expression_new (g_value_get_object (&value));
+            else
+              expr = gtk_constant_expression_new_for_value (&value);
+
+            g_value_unset (&value);
+          }
 
         g_string_free (info->constant.text, TRUE);
         info->expression_type = EXPRESSION_EXPRESSION;


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