Bug: `Gtk.Builder` need to declare signal callbacks with `G_MODULE_EXPORT` for Win32
Ref: Signal handlers and function pointers.
Example: just the official Gtk.Builder example is enough:
#!/usr/bin/env -S vala --pkg=gtk+-3.0 -X -O2 -X -march=native --cc="ccache cc" -X -pipe
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 ("xml-ui.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;
}
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="window">
<property name="border-width">10</property>
<property name="title" translatable="yes">Sample Application</property>
<property name="window-position">center</property>
<property name="default-width">300</property>
<property name="default-height">70</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child>
<object class="GtkBox" id="hbox1">
<property name="visible">1</property>
<property name="spacing">4</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">Click me!</property>
<property name="visible">1</property>
<property name="can-focus">1</property>
<property name="receives-default">1</property>
<property name="use-action-appearance">0</property>
<signal name="clicked" handler="on_button1_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">Me too!</property>
<property name="visible">1</property>
<property name="can-focus">1</property>
<property name="receives-default">1</property>
<property name="use-action-appearance">0</property>
<signal name="clicked" handler="on_button2_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">1</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
If is not added, the signal handlers will fail and show this:
(xml-ui.exe:16268): Gtk-WARNING **: 10:42:44.021: Could not find signal handler 'on_button2_clicked'. Did you compile with -rdynamic?
(xml-ui.exe:16268): Gtk-WARNING **: 10:42:44.026: Could not find signal handler 'on_button1_clicked'. Did you compile with -rdynamic?
And if compile with -C
and manully add G_MODULE_EXPORT
for on_button1_clicked
and on_button2_clicked
(or replace VALA_EXTERN
with G_MODULE_EXPORT
), it works.
Edited by Zhou Qiankang