Constructing Vala Gtk object using builder contents
Asked Answered
T

3

6

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.

Tibetan answered 5/3, 2013 at 20:15 Comment(0)
P
1

Nope, not possible. The C++ binding to Gtk.Builder has this, but unfortunately it relies on C++ templates and hasn't been implemented in any other binding.

Pale answered 5/3, 2013 at 22:3 Comment(1)
Hmm, too bad. Do you think there's anything wrong with the approach of reparenting the content_area?Tibetan
P
3

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/

Percaline answered 16/10, 2013 at 10:13 Comment(0)
S
2

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.

Superimposed answered 8/3, 2013 at 1:15 Comment(1)
Huh, that's funny because I haven't even attempted to use a Box as a top level for years because it never used to work. Thanks a lot that's what I'll be doing from now on, not that re-parenting the content doesn't work, it just seems unnecessary now.Tibetan
P
1

Nope, not possible. The C++ binding to Gtk.Builder has this, but unfortunately it relies on C++ templates and hasn't been implemented in any other binding.

Pale answered 5/3, 2013 at 22:3 Comment(1)
Hmm, too bad. Do you think there's anything wrong with the approach of reparenting the content_area?Tibetan

© 2022 - 2024 — McMap. All rights reserved.