Example: Builder

<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.8 -->
<template class="BarWindow" parent="GtkWindow">
<signal name="destroy" handler="gtk_main_quit"/>
<child>
<object class="GtkGrid">
<property name="visible">true</property>
<property name="margin">12</property>
<property name="row_spacing">6</property>
<child>
<object class="GtkLabel">
<property name="visible">true</property>
<property name="expand">true</property>
<property name="label" translatable="yes">My first label</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry">
<property name="visible">true</property>
<property name="can_focus">true</property>
<signal name="changed" handler="entry_changed" object="BarWindow" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">true</property>
<property name="expand">true</property>
<property name="label" translatable="yes">My second label</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">button</property>
<property name="visible">true</property>
<property name="can_focus">true</property>
<property name="receives_default">true</property>
<signal name="clicked" handler="button_clicked" object="BarWindow" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">3</property>
</packing>
</child>
</object>
</child>
</template>
</interface>

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

[GtkTemplate (ui = "/foo/bar/Gtk.Builder.ui")]
public class BarWindow : Gtk.Window {
public string text {
get { return entry.text; }
set { entry.text = value; }
}

[GtkChild]
private Gtk.Entry entry;

[GtkCallback]
private void button_clicked (Gtk.Button button) {
message ("Button clicked, entry with text: %s", entry.text);
}

[GtkCallback]
private void entry_changed (Gtk.Editable editable) {
message ("Entry changed, text: %s\n", entry.text);

notify_property ("text");
}
}

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

var app = new BarWindow ();
app.show_all ();
Gtk.main ();
}



2022 vala-language.org