Re: GTkTreeModel

Ewen Maclean <[email protected]> Thu, 16 Apr 2015 09:58:24 +0100
Newsgroups gmane.comp.lang.ocaml.lib.gtk
Message-ID <CAEC1HmknYpZDctZE76fr8yBpgYXJEaK0+Ndrm4HMyNfQ6DRaBA@mail.gmail.com>
Thanks a lot for this help. I am struggling a bit though to make it
work - I get the error:

Failure("Gobject.is_a : GtkTreeModel is not an object type)

when I try to change the value in the combo cell. Also using  the get
method I am not sure how to specify a column - i.e.:

let el = m#get ~row:i ~column:?

clearly it is set at this line (in the second argument)

 let d, _ = GTree.store_of_list Gobject.Data.string d in

but I can't quite work out how to retireve it.

Thanks again for the reply and any help with this.

Ewen


On 16 April 2015 at 08:11, Jacques Garrigue
<[email protected]> wrote:
> 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
>
>