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.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With