Skip to content

Commit

Permalink
gtkbuilder: check for existing object before extending template
Browse files Browse the repository at this point in the history
If gtk_builder_expose_object() is called twice with the same name, it will
result in a g_critical(). This improves that situation by checking for the
object before exposing additional times.

This turns out to be handy in situations where templates are expanded
multiple times, such as application-side implementations of UI merging.
  • Loading branch information
chergert committed Aug 1, 2022
1 parent 924da0e commit f680b86
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions gtk/gtkbuilder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,7 @@ gtk_builder_extend_with_template (GtkBuilder *builder,
GError **error)
{
GtkBuilderPrivate *priv = gtk_builder_get_instance_private (builder);
const char *name;
GError *tmp_error;
char *filename;

Expand All @@ -1384,8 +1385,15 @@ gtk_builder_extend_with_template (GtkBuilder *builder,
priv->resource_prefix = NULL;
priv->template_type = template_type;

filename = g_strconcat ("<", g_type_name (template_type), " template>", NULL);
gtk_builder_expose_object (builder, g_type_name (template_type), object);
/* We specifically allow this function to be called multiple times with
* the same @template_type as that is used in applications like Builder
* to implement UI merging.
*/
name = g_type_name (template_type);
if (gtk_builder_get_object (builder, name) != object)
gtk_builder_expose_object (builder, name, object);

filename = g_strconcat ("<", name, " template>", NULL);
_gtk_builder_parser_parse_buffer (builder, filename,
buffer, length,
NULL,
Expand Down

0 comments on commit f680b86

Please sign in to comment.