On Vala and Composite Templates

If you want to use the GTK+ composite templates in Vala, please remember : you have to use a GResource. The syntax is not exactly hard, here’s an example:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gtk/Example">
<file compressed="true">window.ui</file>
</gresource>
</gresources>

Which is pretty simple. Let’s say you called that file project.gresource.xml, so to include it, you do (at compile time)

valac --target-glib=2.38 --gresources project.gresource.xml --pkg your_package1 --pkg you_package2 ...

Of course, it can’t be that simple, so first you have to _compile_ that xml file (crazy, isn’t it?), doing

glib-compile-resources project.gresource.xml

and then include the generated C file. As this won’t scale, and you probably aren’t compiling that way, the documentation suggest to include a make rule for this. So, our make rule should look like this:

project-gresource.c: project.gresource.xml $(shell $(GLIB_COMPILE_RESOURCES) --generate-dependencies project.gresource.xml)

and we can include the generated C file in our valac call:

valac --target-glib=2.38 --gresources project.gresource.xml --pkg your_package1 --pkg you_package2 file1.vala file2.vala project-gresource.c ...

Please don’t forget to read the announcement here for more info.

Advertisements