How to code with Genie and Glade (first part)

this post is a rather dumb translation, for the genie language, of a tutorial written for his cousin language, vala.

the aim of the original tutorial (click here to read it) is to show how to use a glade user interface from a vala program. the ideas are:

  1. install vala and glade;
  2. build a simple ui with glade (i.e. an xml-based description of the interface);
  3. write a vala genie program to use this interface and perform some useful tasks (in this case, write a message to the standard error file descriptor);
  4. build and run

 

Very nice, and simple: let’s try to do the same with genie

the installation: on my OSX box (with port as package manager)

sudo port install vala glade

since vala and genie languages share the same translator to C, valac, no need for further installations.

the ui: let me shamelessly steal it  from here (and save it to a file named sample.ui)

the program: it is the one we have to rewrite in genie from vala. a breeze

[indent=4]

uses
    Gtk

def on_button1_clicked (source : Button) : void
    source.label = "Clicked!"
    stderr.printf ("Clicked! --> ")

def on_window1_destroy (source: Window) : void
    /* When window close signal received */
    Gtk.main_quit()

init

    Gtk.init (ref args)
    var builder = new Builder()

    /* Getting the glade file */
    try
        builder.add_from_file ("sample.ui")
    except ex : GLib.Error
        print "Error: %s", ex.message
        Process.exit(-1)

    builder.connect_signals (null);
    var window = builder.get_object ("window1") as Window
    var entry = builder.get_object ("entry1") as Entry
    var button = builder.get_object ("button1") as Button

    /* another way to do something when signal received */
    /* this will give a warning for deprecated syntax *|
    button.clicked += def(button)
        stderr.printf ("%s\n", entry.get_text())

    window.show_all ()
    Gtk.main ()

For a little improvement, I’ve added a trivial try…except block to control the ui file exists or exits the program.

finally build the program (save it to sample_glade_gtk.gs file)

valac --pkg gtk+-3.0  sample_glade_gtk.gs 

sample_glade_gtk.gs:35.5-38.5: warning: deprecated syntax, use `connect' 
     method instead

Compilation succeeded - 1 warning(s)

The program actually works: the residual problem we have is the warning that advise us we are using a deprecated syntax (anonymous function) for the instruction

    button.clicked += def(button)
        stderr.printf ("%s\n", entry.get_text())

 

Next time, we’ll see that to use the button.clicked.connect(some_callback) as suggested we’ll have to rewrite our sample in a OOP fashion.

 

Thanks for your attention, have a nice day.

Lascia un commento