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



commit d0ea4c016a3d22850039342d73a0a139d355048b
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 | 49 +++++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 16 deletions(-)
---
diff --git a/gtk/gtkbuilderparser.c b/gtk/gtkbuilderparser.c
index 5005f787e7..1933f0a524 100644
--- a/gtk/gtkbuilderparser.c
+++ b/gtk/gtkbuilderparser.c
@@ -1051,7 +1051,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))
@@ -1061,22 +1061,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);
@@ -1218,12 +1223,24 @@ expression_info_construct (GtkBuilder      *builder,
       {
         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;
+
+            g_value_init (&value, G_OBJECT_TYPE (o));
+            g_value_set_object (&value, o);
+          }
+        else
+          {
+            if (!gtk_builder_value_from_string_type (builder,
+                                                     info->constant.type,
+                                                     info->constant.text->str,
+                                                     &value,
+                                                     error))
+              return  NULL;
+          }
 
         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]