I have a handle to a TreeView that happens to have a ListStore as its model. What I want to do is get back a handle to that ListStore from my handle to the TreeView.
As pointed out here, the treeViewGetModel getter returns a generic TreeModel rather than an instance of TreeModelClass such as, in particular here, a ListStore. I am not aware of any cast function from TreeModel to ListStore either...
I also do not want to do this (that is, just keep a handle to the model when I define it and pass it through to where I want to use it).
Is anybody aware of a good solution to that problem ?
The problem stems from the fact that the tree view may have a different model. A function
TreeView a -> ListStore awould 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:
You could use the functions for
GObjectin the glib library to determine whether the model is indeed aListStoreand make the casting safe, ie. retuningMaybe (ListStore a).In particular, I'd suggest looking at
isA :: GObjectClass o => o -> GType -> Bool. Sadly, you might have to use use the C functiongtk_list_store_get_typevia the FFI if no other function can give you aGTypefor aListStore.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.