|
|
Example
gtkaml example
What is gtkaml
Gtkaml is an XML syntax + a preprocessor that transforms an UI description into Vala code.
Example of gtkaml syntax
Which would produce this:
MyVBox.gtkaml
Here is what you write: (note: this is a gtkaml 0.2 example. To compile this in 0.1, replace hyphens with underscores)
<VBox xmlns="Gtk" xmlns:glib="GLib" xmlns:class="http://gtkaml.org/0.2"
class:name="MyVBox">
<Label use-markup="true" label="<b>Dialog Box Title Here</b>" expand="false" fill="false" padding="0" />
<Notebook can-focus="true" tab-vborder="1">
<Label>
<tab-label>
<Label label="Page 1"/>
</tab-label>
<label>label
multilne</label>
</Label>
<HBox homogeneous="false" spacing="0">
<tab-label>
<CheckButton label="check button" active="true" />
</tab-label>
<Label label="Nothing to see here, please move along" />
</HBox>
</Notebook>
<HButtonBox>
<Button with-mnemonic="true" label="_abort" clicked="on_click()"/>
<Button label="gtk-redo" use-stock="true"/>
<Button label="fail"/>
</HButtonBox>
<![CDATA[
private void on_click() { message("you clicked me!"); }
static int main (string[] args)
{
Gtk.init (ref args);
Window w = new Gtk.Window (WindowType.TOPLEVEL);
w.delete_event += (x,y) => { Gtk.main_quit(); };
MyVBox v = new MyVBox ();
w.add (v);
w.show_all ();
Gtk.main ();
return 0;
}
]]>
</VBox>Um.. what was that again? Here's what happens (same source, annotated):
<!-- MyVBox class extends VBox and imports the Gtk and GLib namespaces -->
<VBox class:name="MyVBox" xmlns="Gtk" xmlns:glib="GLib" xmlns:class="http://gtkaml.org/0.2">
<!-- a label is added to the VBox, you can see the box_pack_start () parameters -->
<Label use-markup="true" label="<b>Dialog Box Title Here</b>" expand="false" fill="false" padding="0" />
<!-- then a notebook (tab navigator) is added, with box_pack_start_defaults () -->
<Notebook can-focus="true" tab-vborder="1">
<!-- the first page in the notebook is only a label -->
<Label>
<!-- this is the widget used for notebook tab label -->
<tab-label>
<!-- this label's text is set using the attribute label="" -->
<Label label="Page 1"/>
</tab-label>
<!-- the parent's text label is set using this sub-tag -->
<label>label
multilne</label>
</Label>
<!-- the second page is an HBox -->
<HBox homogeneous="false" spacing="0">
<!-- actually this sub-tag is a parameter to gtk_notebook_append_page (notebook, child, tab_label) -->
<tab-label>
<!-- this uses a check button as its notebook tab -->
<CheckButton label="check button" active="true" />
</tab-label>
<!-- this is what the HBox contains -->
<Label label="Nothing to see here, please move along" />
</HBox>
</Notebook>
<!-- next we add an HButtonBox to our VBox -->
<HButtonBox>
<!-- this button is created with gtk_button_new_with_mnemonic ()
'clicked' contains the body of the event handler lambda function -->
<Button with-mnemonic="true" label="_abort" clicked="on_click()"/>
<Button label="gtk-redo" use-stock="true"/>
<Button label="fail"/>
</HButtonBox>
<!-- cdata sections are used for Vala code -->
<![CDATA[
private void on_click() { stdout.printf("you clicked me!"); }
static int main (string[] args)
{
Gtk.init (ref args);
Window w = new Gtk.Window (WindowType.TOPLEVEL);
w.delete_event += (x,y) => { Gtk.main_quit(); };
MyVBox v = new MyVBox ();
w.add (v);
w.show_all ();
Gtk.main ();
return 0;
}
]]>
</VBox>and the output of
$ gtkamlc --pkg gtk+2.0 MyVBox.gtkaml
is the following MyVBox.vala file (along with MyVBox.c and MyVBox.h).
(to compile the executable you need to run
$ gcc `pkg-config --cflags gtk+-2.0` MyVBox.c -o myvbox `pkg-config --libs gtk+-2.0`
then ./myvbox will execute it)
MyVBox.vala
using Gtk;
using GLib;
public class MyVBox : Gtk.VBox
{
private void on_click() { stdout.printf("you clicked me!"); }
static int main (string[] args)
{
Gtk.init (ref args);
Window w = new Gtk.Window (WindowType.TOPLEVEL);
MyVBox v = new MyVBox ();
w.add (v);
w.show_all ();
Gtk.main ();
return 0;
}
construct {
Gtk.Label _label0;
Gtk.Notebook _notebook0;
Gtk.Label _label1;
Gtk.Label _label2;
Gtk.HBox _hbox0;
Gtk.CheckButton _checkbutton0;
Gtk.Label _label3;
Gtk.HButtonBox _hbuttonbox0;
Gtk.Button _button0;
Gtk.Button _button1;
Gtk.Button _button2;
_label0 = new Gtk.Label ("<b>Dialog Box Title Here</b>");
_notebook0 = new Gtk.Notebook ();
_label2 = new Gtk.Label ("label for a tab");
_label1 = new Gtk.Label ("label\nmultilne");
_checkbutton0 = new Gtk.CheckButton.with_label ("check button");
_hbox0 = new Gtk.HBox (false, 0);
_label3 = new Gtk.Label ("Nothing to see here, please move along");
_hbuttonbox0 = new Gtk.HButtonBox ();
_button0 = new Gtk.Button.with_mnemonic ("_abort");
_button1 = new Gtk.Button.with_label ("gtk-redo");
_button2 = new Gtk.Button.with_label ("fail");
this.name = "MyVBox";
_label0.use_markup = true;
this.pack_start (_label0, false, false, 0);
_notebook0.can_focus = true;
_notebook0.tab_vborder = 1;
this.pack_start_defaults (_notebook0);
_notebook0.append_page (_label1, _label2);
_checkbutton0.active = true;
_notebook0.append_page (_hbox0, _checkbutton0);
_hbox0.pack_start_defaults (_label3);
this.pack_start_defaults (_hbuttonbox0);
_button0.has_default = true;
_button0.clicked += target => { on_click(); };
_hbuttonbox0.pack_start_defaults (_button0);
_button1.use_stock = true;
_hbuttonbox0.pack_start_defaults (_button1);
_hbuttonbox0.pack_start_defaults (_button2);
}
}Why?
Beacause vala further transforms the source into .c and .h and gcc compiles them (of course) to native code;)
Sign in to add a comment
