The problem stems from the fact that the tree view may have a different model. A function TreeView a -> ListStore a
would be partial (not defined for treeviews with different models), and so unsafe to use.
This issue has been raised a number of times, on gtk2hs's trac and Stack Overflow. Proposed solutions are always similar to what you mentioned and want to avoid.
I'm not entirely sure, but I think something along the following lines would implement an unsafe casting:
unsafeCastToListStore :: TreeView a -> ListStore a
unsafeCastToListStore =
unsafeCastGObject . toGObject . treeViewGetModel
You could use the functions for GObject
in the glib library to determine whether the model is indeed a ListStore
and make the casting safe, ie. retuning Maybe (ListStore a)
.
In particular, I'd suggest looking at isA :: GObjectClass o => o -> GType -> Bool
. Sadly, you might have to use use the C function gtk_list_store_get_type
via the FFI if no other function can give you a GType
for a ListStore
.
Alternatively, if you can compile and bind your own fork of gtk2hs, you might be able to just re-export the internal functions/modules that gtk exports but gtk2hs doesn't (if this is for a closed-source project or for internal use), although that will incur in an extra maintenance cost.