3

First of all, your official tutorial on getting started with Vala GUI development is excellent. I am wanting to move away from hand-coding my window layouts and I want to use Glade XML files. I have the following code which works:

using Gtk;

public void on_button1_clicked (Button source) {
    source.label = "Thank you!";
}

public void on_button2_clicked (Button source) {
    source.label = "Thanks!";
}

int main (string[] args) {
    Gtk.init (ref args);

    try {
        // If the UI contains custom widgets, their types must've been instantiated once
        // Type type = typeof(Foo.BarEntry);
        // assert(type != 0);
        var builder = new Builder ();
        builder.add_from_file ("sample.ui");
        builder.connect_signals (null);
        var window = builder.get_object ("window") as Window;
        window.show_all ();
        Gtk.main ();
    } catch (Error e) {
        stderr.printf ("Could not load UI: %s\n", e.message);
        return 1;
    }

    return 0;
}

However, it only works because I have hard-coded the location of the glade XML file (sample.ui). If I move the .ui file then the app breaks. I have done some research and it looks like I need to use a GResource to specify the XML file and then load it.

Would it be possible to create a short tutorial on how to properly compile and access assets when building a Vala GUI app please?

2
  • A lot of the answer depends on your build system. I don't know of a tutorial, but there is an example project at github.com/flplv/vala-cmake-example which includes a simple GUI loaded through GResource. If you're using autotools, gitg (git.gnome.org/browse/gitg) is a good example.
    – nemequ
    Commented Apr 19, 2017 at 0:54
  • I don't really have a build system as such at the moment (just getting started). I've just been running valac --pkg gtk+-3.0 hello.vala from the Terminal to compile my hello.vala file to a binary. The leap from this level of compiling to these build systems is too complicated... Commented Apr 19, 2017 at 10:14

1 Answer 1

3

This is one of those things where you probably want to use a build system to handle everything for you.

First you need an XML file to describe your resources. Something like

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/com/foo/bar">
    <file compressed="true" preprocess="xml-stripblanks">sample.ui</file>
  </gresource>
</gresources>

Then you use the glib-compile-resources tool to compile the XML description and the resources into a C file with something like

glib-compile-resources --generate-source --target sample.c sample.xml

Then pass the generated C to valac along with the XML description

valac ... --gresources=sample.xml sample.c yourfile.vala

At that point you can either have Gtk.Builder load from a resource

var builder = new Gtk.Builder.from_resource("/com/foo/bar/sample.ui");

Or, if your UI file is a template, you can just do something like

[GtkTemplate (ui = "/com/foo/bar/sample.ui")]
public class MainWindow : Gtk.Window {
  ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.