gtk2hs - Getting back a ListStore from a TreeView

125 Views Asked by At

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 ?

1

There are 1 best solutions below

0
On

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.