Search code examples
gtkvala

is possible to change the orientation of a box in gtk?


I did a class whoss parent class is gtk.box in vala. linking to box constructor is not supported, so, how can I set the Orientation of the box in the constructor??


Solution

  • While calling this.set_orientation (Gtk.Orientation.VERTICAL) might work, the more correct way to do this would be to set the orientation property at construct time, just like the Gtk.Box default constructor does. In Vala, you would do something like this:

    public class MyBox : Gtk.Box {
      public MyBox () {
        GLib.Object (orientation: Gtk.Orientation.VERTICAL);
      }
    }
    

    At the C level, this is a bit different than just calling set_orientation... it will generate something a bit like this (simplified for clarity):

    MyBox* my_box_new () {
      return g_object_new (GTK_TYPE_BOX, "orientation", GTK_ORIENTATION_VERTICAL, NULL);
    }
    

    Calling set_orientation (or setting the orientation property), on the other hand, would generate something like this:

    MyBox* my_box_new () {
      MyBox* self = g_object_new (GTK_TYPE_BOX, NULL);
      gtk_box_set_orientation (GTK_BOX(self), GTK_ORIENTATION_VERTICAL);
      return self;
    }
    

    The difference is that for the first version the orientation will be set correctly during instantiation (in other words, during the construct block of each of the ancestor classes), whereas for the second version the object will first be created with the wrong orientation, then the orientation will be changed. I'm not sure whether or not this difference is significant for Gtk.Box, but in some cases it is very important so it's probably a good idea to get in the habit of doing it the right way.

    If you're not sure what I mean by "construct block", take a look a the section in the Vala Tutorial on GObject-Style Construction.