Compare commits

...

1 Commits

Author SHA1 Message Date
Matthias Clasen
542d766469 builder: Accept standard C type names
We are much more likely to use standard C types
in our code nowadays, and it is a bit of a
dissonance to have to use the g-names when
referring to the same types in ui files.

This adds code to our type name resolver
to map the standard C numeric type names
to their glib equivalents.

Tests included.
2023-01-25 23:49:22 -05:00
2 changed files with 59 additions and 0 deletions

View File

@@ -71,17 +71,49 @@
G_DEFINE_INTERFACE (GtkBuilderScope, gtk_builder_scope, G_TYPE_OBJECT)
static const char *
find_fallback_name (const char *type_name)
{
static struct {
const char *name;
const char *gname;
} std_names[] = {
{ "int", "gint" },
{ "unsigned int", "guint" },
{ "long", "glong" },
{ "int64_t", "gint64" },
{ "uint64_t", "guint64" },
{ "float", "gfloat" },
{ "double", "gdouble" },
{ "char*", "gchararray" },
{ "void*", "gpointer" },
};
for (unsigned int i = 0; i < G_N_ELEMENTS (std_names); i++)
{
if (strcmp (type_name, std_names[i].name) == 0)
return std_names[i].gname;
}
return NULL;
}
static GType
gtk_builder_scope_default_get_type_from_name (GtkBuilderScope *self,
GtkBuilder *builder,
const char *type_name)
{
GType type;
const char *fallback_name;
type = g_type_from_name (type_name);
if (type != G_TYPE_INVALID)
return type;
fallback_name = find_fallback_name (type_name);
if (fallback_name)
return g_type_from_name (fallback_name);
gtk_test_register_all_types ();
return g_type_from_name (type_name);
}
@@ -265,11 +297,16 @@ gtk_builder_cscope_get_type_from_name (GtkBuilderScope *scope,
{
GtkBuilderCScope *self = GTK_BUILDER_CSCOPE (scope);
GType type;
const char *fallback_name;
type = g_type_from_name (type_name);
if (type != G_TYPE_INVALID)
return type;
fallback_name = find_fallback_name (type_name);
if (fallback_name)
return g_type_from_name (fallback_name);
type = gtk_builder_cscope_resolve_type_lazily (self, type_name);
if (type != G_TYPE_INVALID)
return type;

View File

@@ -2689,6 +2689,27 @@ test_expressions (void)
}
}
/* Check that standard type names work */
static void
test_typenames (void)
{
GtkBuilder *builder;
builder = gtk_builder_new ();
g_assert_true (gtk_builder_get_type_from_name (builder, "int") == G_TYPE_INT);
g_assert_true (gtk_builder_get_type_from_name (builder, "unsigned int") == G_TYPE_UINT);
g_assert_true (gtk_builder_get_type_from_name (builder, "long") == G_TYPE_LONG);
g_assert_true (gtk_builder_get_type_from_name (builder, "int64_t") == G_TYPE_INT64);
g_assert_true (gtk_builder_get_type_from_name (builder, "uint64_t") == G_TYPE_UINT64);
g_assert_true (gtk_builder_get_type_from_name (builder, "float") == G_TYPE_FLOAT);
g_assert_true (gtk_builder_get_type_from_name (builder, "double") == G_TYPE_DOUBLE);
g_assert_true (gtk_builder_get_type_from_name (builder, "char*") == G_TYPE_STRING);
g_assert_true (gtk_builder_get_type_from_name (builder, "void*") == G_TYPE_POINTER);
g_object_unref (builder);
}
int
main (int argc, char **argv)
{
@@ -2736,6 +2757,7 @@ main (int argc, char **argv)
g_test_add_func ("/Builder/Shortcuts", test_shortcuts);
g_test_add_func ("/Builder/Transforms", test_transforms);
g_test_add_func ("/Builder/Expressions", test_expressions);
g_test_add_func ("/Builder/typenames", test_typenames);
return g_test_run();
}