how gtkaml works
If you are writing something based on GTK+ / GObject then you should be interested by this:
- Vala is a very productivity-boosting language based on GObject OOP-ness, which translates to C and therefore can export GObject classes and interfaces from shared libraries.
- gtkaml is a thin SAX Parser on top of Vala that allows you to write XML user interface descriptions
This is how a minimal Vala class described with gtkaml looks like:
<VBox class:name="MyVBox" xmlns="Gtk" xmlns:class="http://gtkaml.org/0.1"> <Label label="Hi_story" expand="false" fill="false" padding="0" /> </VBox>
This code creates a class named MyVBox that extends VBox and contains a Label. The label has the value “History”, it does not expand or fill its box and is not padded.Easy, no?
Which leads us to the first subject:
Container Add Methods
gtkaml uses a file named implicits.ini to determine which containers have which add methods. This is for “implicit” decisions it makes based on your UI layout.
For the example above, it simply finds this:
[Gtk.Container] adds=add_with_properties [Gtk.Box] adds = pack_start;pack_end;pack_start_defaults;pack_end_defaults
This simply lists all the functions that one can use to add widgets to a Box and to a Container. Since VBox is a Box and Box is a Container, gtkaml finds these two entries ‘significant’ and merges them in this order (most specialized first):
Then it loops over these methods – in this order – finds the first matching one and ‘picks’ the parameters from the child widget tag: that is, it removes “expand”, “fill” and “padding” from the Label and makes a note to use their value when adding the label in the VBox.
What if I want to use another add method?
Well, in that case simply specify the method’s name as an attribute of the child tag, with the value “true”:
<Label pack_end="true" label="Hi_story" expand="false" fill="false" padding="0" />
In this same example we notice that the label contains an underscore: this is intended to show up as a “mnemonic” (an keyboard shortcut) or simply as “History”.
Looking again at the GtkLabel documentation we notice that the only ways to get a mnemonic is to use either gtk_label_set_text_with_mnemonic () or gtk_label_new_with_mnemonic (). But since gtkaml can only help you with public fields, properties and signals, and not with function calls, how does one specify a mnemonic?
The answer is that you can select a “creation method”, and gtk_label_new_with_mnemonic () is not an ordinary function but a creation method. That is, a method that does not require the object as the first parameter but returns a new object.
So simply specify the creation method name as an attribute with a value of “true”: with_mnemonic=”true”.
Wait, but how about the creation method’s parameters? with_mnemonic has one parameter named str..
Well, usually Gtk widgets’ creation methods have their parameters named after the field/property they are initializing. gtkaml recognizes them from the specified attributes.
In this case, though, str is not a field or parameter of GtkLabel. But we know that it actually refers to the label property.
So what gtkaml does is to specify, again, in implicits.ini, the attributes that will go as parameters to the creation method:
[Gtk.Label] new = label new.with_mnemonic = label
So the code becomes:
<Label pack_end="true" with_mnemonic="true" label="Hi_story" expand="false" fill="false" padding="0" />
That’s it! – more on what gtkaml 0.2 will do for you in a future article.
p.s. as I am writing this, gtkaml was accepted on freshmeat.net! yay!