Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing Vala Gtk object using builder contents

Tags:

gtk

vala

It would be ideal to be able to create a new widget that uses builder to load its contents, eg.

public class MyDialog : Dialog
  {
    public MyDialog
      {
        Gtk.Builder builder = new Gtk.Builder ();
        builder.add_from_file ("dialog.ui");
        this = builder.get_object ("my_dialog") as Gtk.Widget;
      }
    }

Obviously this won't work because this = is an invalid assignment, but I'm wondering if there is a way to set a widget's contents using those that have been loaded from builder.

For the meantime I've replaced the this = ... with

var content = get_content_area ();
var dialog = builder.get_object ("my_dialog") as Gtk.Widget;
var _content = (dialog as Dialog).get_content_area ();
_content.reparent (content);

which does work, but it still would make sense to me to be able to load directly in.

Thanks.

like image 956
Geoff Johnson Avatar asked Mar 05 '13 20:03

Geoff Johnson


2 Answers

In case anyone stumbles on this question in future, Vala 0.22 features composite widget templates, which are a much easier solution to the above problem. Composite templates allow you to define a widget in Glade and use attributes to tell Vala which bits of your class refer to which elements of the widget, and to connect callbacks, without having to use Gtk.Builder manually at all.

Details can be found at http://blogs.gnome.org/tvb/2013/05/29/composite-templates-lands-in-vala/

like image 66
Tristan Brindle Avatar answered Sep 30 '22 10:09

Tristan Brindle


We do this extensively in Geary. The trick I've used most is not to build the containing object (i.e. the Gtk.Dialog) in Glade at all, just its contents. Then you can just code up the dialog/window itself in Vala.

That was kind of pain to do before Glade 3.15 came out since it didn't explicitly support Box, Grid, and other components as toplevels. If you haven't upgraded yet, I recommend it.

like image 34
MrEricSir Avatar answered Sep 30 '22 08:09

MrEricSir