builder-tool: Try harder to find template parents
authorMatthias Clasen <mclasen@redhat.com>
Sun, 19 Feb 2023 18:07:16 +0000 (13:07 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 19 Feb 2023 23:17:34 +0000 (18:17 -0500)
This will make it work (in some cases) to add extra
libraries via LD_PRELOAD.

Based on work by Julian Sparber,
https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/5084

Fixes: #5232
docs/reference/gtk/gtk4-builder-tool.rst
tools/gtk-builder-tool-validate.c

index a7e38e73d48977495c387a5faee671c2cb69f8c3..ad38532949c47ce4e9aac67636eacdb352f91209 100644 (file)
@@ -33,6 +33,11 @@ Validation
 The ``validate`` command validates the given UI definition file and reports
 errors to ``stderr``.
 
+Note that there are limitations to the validation that can be done for templates,
+since they are closely tied to the class_init function they are used in.
+If your UI file uses types from third-party libraries, it may help to add those
+libraries to the `LD_PRELOAD` environment variable.
+
 ``--deprecations``
 
   Warn about uses of deprecated types in the UI definition file.
index 244771f875c3f5b43d6ca80dc53cbd585218c443..db94242dffacaa9ebaf2a8c94c8ad6091bdba0d6 100644 (file)
@@ -149,10 +149,15 @@ validate_template (const char *filename,
   GError *error = NULL;
   gboolean ret;
 
+  builder = gtk_builder_new ();
+  scope = fake_scope_new ();
+  gtk_builder_set_scope (builder, GTK_BUILDER_SCOPE (scope));
+  g_object_unref (scope);
+
   /* Only make a fake type if it doesn't exist yet.
    * This lets us e.g. validate the GtkFileChooserWidget template.
    */
-  template_type = g_type_from_name (type_name);
+  template_type = gtk_builder_get_type_from_name (builder, type_name);
   if (template_type == G_TYPE_INVALID)
     template_type = make_fake_type (type_name, parent_name);
 
@@ -163,15 +168,11 @@ validate_template (const char *filename,
       return FALSE;
     }
 
-  builder = gtk_builder_new ();
-  scope = fake_scope_new ();
-  gtk_builder_set_scope (builder, GTK_BUILDER_SCOPE (scope));
   ret = gtk_builder_extend_with_template (builder, object, template_type, " ", 1, &error);
   if (ret)
     ret = gtk_builder_add_from_file (builder, filename, &error);
   if (ret && deprecations)
     ret = fake_scope_check_deprecations (scope, &error);
-  g_object_unref (scope);
   g_object_unref (builder);
 
   if (!ret)