Subclassing in Vala/Gtk+

Mikael you forgot to mention one of the other ways to subclass in C (which actually involves no C coding at all), which is Vala, this would be the equivalent of your Ruby sample:


using GLib;
using Gtk;
class OpenFileDialog : FileChooserDialog {
public static string CWD = null;
construct {
set_title ("Open file");
set_action (FileChooserAction.OPEN);
add_button (STOCK_CANCEL, ResponseType.CANCEL);
add_button (STOCK_OPEN, ResponseType.OK);
set_default_response (ResponseType.OK);
response += on_response;
if (CWD != null)
set_current_folder (CWD);
}
[Callback]
private void on_response (Dialog diag, ResponseType resp) {
if (resp == ResponseType.OK)
set_current_folder (CWD);
}
public static void main (string[] args) {
Gtk.init(ref args);
for (int i = 0; i<2; i++) {
var ofd = new OpenFileDialog ();
if (ofd.run() == ResponseType.OK)
stdout.printf("filename = " + ofd.get_filename ());
ofd.destroy();
}
}
}

Download: subclass.vala
Compile, run and ejoy:


$ valac --pkg gtk+-2.0 subclass.vala -o subclass
$ ./subclass 

Update: pinky has figured out a bug in the code which is now fixed in the example and the file above. Thanks pinky! Good catch!

Advertisements

5 thoughts on “Subclassing in Vala/Gtk+

  1. Thanky yoy for the example. I have high hopes on Vala and i’m always happy to see vala examples.
    btw: There is a bug in your code CWD will never be set.
    Here is the right code:
    using GLib;
    using Gtk;
    class OpenFileDialog : FileChooserDialog {
    public static string CWD = null;
    construct {
    set_title (“Open file”);
    set_action (FileChooserAction.OPEN);
    add_button (STOCK_CANCEL, ResponseType.CANCEL);
    add_button (STOCK_OPEN, ResponseType.OK);
    set_default_response (ResponseType.OK);
    response += on_response;
    if (CWD != null)
    set_current_folder(CWD);
    }
    [Callback]
    private void on_response (Dialog diag, ResponseType resp) {
    CWD = get_current_folder();
    }
    public static void main (string[] args) {
    Gtk.init(ref args);
    for (int i = 0; i<2; i++) {
    var ofd = new OpenFileDialog ();
    if (ofd.run() == ResponseType.OK)
    stdout.printf("filename = " + ofd.get_filename() + "\n");
    ofd.destroy();
    }
    }
    }
    (Sorry, couldn't find a way to way to keep the indention in the comment)

    Like

  2. I like Vala, and wanted to do some subclassing with it a while ago. I was told, however, that there was no way to call the constructor of the parent class, and in particular no way to call that constructor with specific parameters.
    Has that been improved meanwhile? I don’t see a call to a parent constructor in your code, is it called automatically? (and, if you needed to send parameters to the parent constructor, how could you do that?)

    Like

  3. @Mikael:
    NP! Glad to spread some word about Vala 😉
    @pinky:
    Thanks for that! Updated!
    @kripken:
    No you cannot call a base class constructor on GObject either.
    And yes, the base constructor is called implicitly.

    Like

  4. This is the first time read about that Vala thig, and (after soome lookup) I can say that Im THRILLED!
    I cannot belive it has not been done dacades ago!
    This is the lang I have been looking for for years because I liked C# but did not liked the perf. in many, many situations, not to mention the problematic mem. management for large objects.
    The only thing I DONT like is the name. It *realy* shoud be something “C” related…
    its better than “Java” though.
    sorry for my english…and that my comment is a bit offtopic.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s