Re: GTkTreeModel

Jacques Garrigue <[email protected]> Thu, 16 Apr 2015 16:11:57 +0900
Newsgroups gmane.comp.lang.ocaml.lib.gtk
Message-ID <[email protected]>
On 2015/04/16 00:28, Ewen Maclean wrote:
> 
> Hello,
> 
> firstly apologies if this question has already been answered, but I
> cannot find the answer. Thanks to an old post on here I have managed
> to populate a GTree.view with cells which contain drop-down combo
> boxes. I cannot work out how though to retrieve the selection made. A
> simple version of the code which exmplifies the problem is below.
> 
> The problem I have is that I use the line:
> 
> let d = model#get ~row:iter ~column:domain_col in
> 
> to retrieve the list when something is selected from the the combo
> cell. The selection (I believe) has iter i at this point.
> 
> I cannot seem to cast this Gobject (d is originally made from
> Gobject.Data.gobject_by_name "GtkTreeModel") to anything useful to
> which I can apply the iter i to retreive the selection.

The answer should be:

	 let m = new GTree.model (GtkTree.TreeModel.cast d) in

However, there is a bug in GtkTree.TreeModel.cast.
The correct definition is:

	let cast w : Gtk.tree_model = Gobject.try_cast w "GtkTreeModel"

The git version is now fixed.
Using the released version, you can just put the above definition in your own program.

Jacques

> =====
> let win = GWindow.window ~show:true () in
> let cols = new GTree.column_list in
> let value_col = cols#add Gobject.Data.string in
> let domain_col = cols#add (Gobject.Data.gobject_by_name "GtkTreeModel") in
> let string_col = (new GTree.column_list)#add Gobject.Data.string in
> let value_renderer =
>  GTree.cell_renderer_combo
>    [ `VISIBLE true;
>      `TEXT_COLUMN string_col;
>      `HAS_ENTRY false;
>      `EDITABLE true; ]
> in
> let view_value_col = GTree.view_column ~renderer:(value_renderer, []) () in
> let _ =
>  view_value_col#add_attribute value_renderer "model" domain_col;
>  view_value_col#add_attribute value_renderer "text" value_col;
> in
> let choices = [("0",["0";"1";"2"]);
>           ("1",["0";"1";"2"]);
>           ("2", ["0";"1";"2"])]
> in
> let list_store = GTree.list_store cols in
> let model = (list_store :> GTree.model) in
> 
> let view = GTree.view ~model ~packing:win#add ~show:true () in
> let _ = view#append_column view_value_col in
> 
> let change_val i p (ls:GTree.list_store) =
>  let iter = model#get_iter p in
>  let d = model#get ~row:iter ~column:domain_col in
>  (*let m = (d :> GTree.model) in*)
>  ()
> in
> ignore(value_renderer#connect#changed ~callback:(fun tp iter ->
> change_val iter tp list_store));
> 
> let append (list_store: GTree.list_store) v d =
>  let d, _ = GTree.store_of_list Gobject.Data.string d in
>  let iter = list_store#append () in
>  list_store#set ~row:iter ~column:value_col v;
>  list_store#set ~row:iter ~column:domain_col d#as_model;
> in
> List.iter (fun (x,y) -> append list_store x y) choices;
> 
> GMain.Main.main ()
> _______________________________________________
> Lablgtk-list mailing list
> [email protected]
> https://lists.ocamlcore.org/cgi-bin/listinfo/lablgtk-list