On 9/6/18 8:29 PM, Russel Winder wrote:

Say I have:

class PresentationTreeView: TreeView { … }

and am using Glade which declares a TreeView instance. Clearly one can:

TreeView tv = cast(TreeView)builder.getObject("presentationList");

but I need tv to be a PresentationTreeView. If I just cast I end up with a run time error 139. So the question is what is the correct way to downcast in this situation?

gtkmm uses the get_widget_derived approach. Is something similar not possible in D?

(Turns out I can use the same approach in D as is needed in Python if not.)

The problem is that GtkBuilder doesn't instantiate a new
PresentationTreeView but only a TreeView.

One way around this is to make the PresentationTreeView a proper Gtk
class by using gtkd.Implement.ImplementClass, and then use
PresentationTreeView in the builder file.

The type name would be the fully qualified name in pascal case, and you
would need to call PresentationTreeView.getType() atleast once before
using GtkBuilder.

As for workarounds you could add a constructor to PresentationTreeView
that passes the gtk handle from the treeview to super.

Or add this to PresentationTreeView:

//Should be the first field in the class.
protected GtkTreeView* handle;

public static GType getType()
{
	return TreeView.getType();
}

public this (GtkTreeView* gtkTreeView, bool ownedRef = false)
{
	this.gtkTreeView = gtkTreeView;
	super(cast(GtkContainer*)gtkTreeView, ownedRef);
}

With that in place the opCast defined for ObjectG should recognize you
class as one that is part of GtkD/Gtk and allow you to cast the TreeView
to it.