In GTK 3, there was gtk_signal_connect() to connect the signals of an XML file loader by gtk_builder_* functions automatically to same-named functions. This worked also in Python in OOP. Now I want to try to achieve the same in node-js. I am developing an own small project by using node-gtk (not: GJS).
I think that node-gtk is missing a class resembling gtk_buildercscope, something named like Gtk.BilderJSScope. However, I wanted to understand how this works in GTKMM before I do anything in node-gtk, but I don’t get it. All examples which use a builder file connect all signals one by one, not by using a single function which connects all at once. Can someone please show me a MWE in either C++ or Python (because these are two languages I roughly understand) where GTK 4 is used with a builder UI file and uses a single function to connect signals?
You only have to worry about this if you were previously using gtk_builder_connect_signals(). If you are using templates, then gtk_widget_init_template() will call gtk_builder_set_current_object() for you, so templates work like before.
PyGObject defines it’s BuilderScope here, as far as I know Gtkmm doesn’t have one (how would that even work?)
Ok, these are two different things to consider: Either Using gtk_builder_set_current_object or templates. As far as I understood, as long as I do not have to pass user data, templates is the way to go.
So, let us start with templates: Can someone please provide me a minimum working example using templates, preferably in Python, because it is OOP just as JavaScript is?
Sorry, but since I am quite new to Phyton, I have a problem in understanding the class decorator. Does this mean that the class Foo is derived from Gtk.Template? I try to translate this code into JavaScript. In node-gtk, I started out with this:
class Foo extends Gtk.Box
What do I do with the constructor then? I think there is something called implicitly in Python, and I guess it is this parameter string=xml which should give me a clue, but I cannot work it out. Somehow, a superclass might invoke gtk_widget_init_template (whatever its Python equivalent is called).
This is my entire code:
#!/usr/bin/env node
const gi = require('node-gtk')
const GLib = gi.require('GLib', '2.0')
const Gtk = gi.require('Gtk', '4.0')
class Foo extends Gtk.Box {
GTypeName = "example1"
__gtype__ = "example1" // I think that this is correct
#btn_hello = null;
constructor() {
let xml = `<interface>
<template class="example1" parent="GtkBox">
<child>
<object class="GtkButton" id="hello_button">
<property name="label">Hello World</property>
<signal name="clicked" handler="hello_button_clicked" swapped="no" />
</object>
</child>
</template>
</interface>
`
super()
this.setTemplate(xml) // does not work
//this.initTemplate()
}
btn_hello_clicked() {
console.log("btn clicked")
}
}
var myapp = null;
function onActivate() {
myapp = new Foo()
}
const loop = GLib.MainLoop.new(null, false)
const app = new Gtk.Application("rexkogitans.mwe-node-gtk", 0)
app.on('activate', onActivate)
const status = app.run([])
gi.startLoop()
loop.run()
However, this does not load the XML. I don’t understand how the XML comes into play now.
It is this “providing sugar” I want to understand what happens under the hood, because I fear that this is not implemented in node-gtk. If so, I want to try to improve node-gtk, but first I need to be sure what do exactly instead of tinkering around. @zbrown says, it is hiding gtk_widget_class_set_template and gtk_widget_init_template. Can someome give me exact details (a code snippet) of what is hidden?