Re: Pixbuf column for a tree custom model

Dmitry Bely <[email protected]> Tue, 6 Jul 2010 15:04:14 +0400
Newsgroups gmane.comp.lang.ocaml.lib.gtk
Message-ID <[email protected]>
On Mon, Jul 5, 2010 at 10:48 PM, Maxence Guesdon
<[email protected]> wrote:
> Le Mon, 5 Jul 2010 14:56:43 +0400,
> Dmitry Bely <[email protected]> a écrit :
>
>> Hi Maxence,
>>
>> Thanks a lot but that's not what I need. You describe how to create a
>> Gtk list store that holds pixbuf data; I know that. But my question
>> was about the custom model
>
> Oups, sorry, I read too fast. I made an attempt to modify the example
> custom_tree_generic.ml. Here is a modified version where I managed to
> display a pixbuf in an additional column, but I get warning about
> incompatible types:
>  (ocaml:3769): GLib-GObject-WARNING **: unable to set property
>  `pixbuf' of type `GdkPixbuf' from value of type `Caml'

OK, thanks. To get rid of this you should use

  let _ = GdkPixbuf.create ~width:1 ~height:1 ();;
    (* at least one GdkPixbuf should exist or
(Gobject.Data.gobject_by_name "GdkPixbuf") will fail *)
  let col_pb = (column_list#add (Gobject.Data.gobject_by_name "GdkPixbuf"));;

instead of

  let col_pb = (column_list#add Gobject.Data.caml: GdkPixbuf.pixbuf
GTree.column);;

because pixbuf is Gtk object, not Caml one! That's what I use for
conventional list models. But then you should return the proper
variant from custom_value method, otherwise you will have

Exception:
Failure
 "custom_value returned a value of incompatible type for column 4 of
type GdkPixbuf".

The question how to return GdkPixbuf is still open...

> I may have missed something.
>
> The custom_tree_generic.ml example works fine on my system (but I had
> to make clean lablgtk to prevent some problem with
> ml_gtk_tree_....._visible_range).

Yes, it works. I was just not patient enough: it needs more than 20
seconds to initialize in my system (Windows).

>> (http://www.tupelo-schneck.org/robert/custom-model), now integrated
>> into Lablgtk. There are some examples in Lablgtk distribution
>> (examples/custom_*.ml) with a custom_value method like
>>
>>   method custom_value (t:Gobject.g_type) (row:custom_tree) ~column =
>>     if column = 0 then `CAML (Obj.repr row)
>>     else if column = 1 then
>>       `BOOL (match row with File {finfo={fchecked=b}} -> b
>>              | _ -> false )
>>     else if column = 2 then
>>       `INT (5+(get_nb row))
>>     else assert false
>>
>> It illustrates that returning basic Ocaml types is trivial. But how to
>> return pixbuf? It's not clear to me.
>>
>> - Dmitry Bely
>>
>> BTW, custom_tree_generic.ml example does not work in my system. What
>> about you?
>>
>> On Mon, Jul 5, 2010 at 2:13 PM, Maxence Guesdon
>> <[email protected]> wrote:
>> > Le Mon, 5 Jul 2010 11:02:17 +0400,
>> > Dmitry Bely <[email protected]> a écrit :
>> >
>> >> Hello,
>> >
>> > Hello,
>> >
>> >>
>> >> How to return GdkPixbuf.pixbuf from a user-defined method
>> >> custom_get_value (custom_tree_model_type)? Probably it should be
>> >> `POINTER but I fail to see how to construct it from a pixbuf. Any
>> >> hints are greatly appreciated.
>> >
>> > You may hav a look at the Gmytree module included in cameleon2:
>> >  http://svn.gna.org/viewcvs/cameleon/trunk/src/utils/gmytree.ml?rev=749&view=markup
>> >
>> > It gives a way to define a tree with a description of the columns.
>> > For a use case, you may look at
>> >  http://svn.gna.org/viewcvs/cameleon/trunk/src/editor/ed_odoc.ml?rev=749&view=markup
>> >  (in class view)
>> >
>> > The important part of Gmytree is;
>> >
>> >  ...
>> >  let tcols = new GTree.column_list in
>> >  let disp_cols = List.map
>> >      (function
>> >          `String _ ->
>> >            `String (tcols#add Gobject.Data.string)
>> >        | `Pixmap _ ->
>> >            `Pixbuf (tcols#add (Gobject.Data.gobject :
>> > GdkPixbuf.pixbuf Gobject.data_conv)) ) cols
>> >  in
>> >  let (datacol : 'a GTree.column) =  tcols#add Gobject.Data.caml in
>> >  let store = GTree.tree_store tcols in
>> >  let view = GTree.view
>> >      ~headers_visible: false
>> >      ~model: store ~packing: wscroll#add_with_viewport () in
>> >  let renderer = GTree.cell_renderer_text [] in
>> >  let pix_renderer = GTree.cell_renderer_pixbuf [] in
>> >  let _ =
>> >    List.iter
>> >      (fun c ->
>> >        let col =
>> >          match c with
>> >            `String c -> GTree.view_column () ~renderer: (renderer,
>> >  ["text", c]) | `Pixbuf c -> GTree.view_column () ~renderer:
>> >  (pix_renderer, ["pixbuf",c]) in
>> >        ignore (view#append_column col)
>> >      )
>> >      disp_cols
>> >  in
>> >  ...
>> >
>> > Hope this helps,
>> >
>> > Maxence
>> >
>> >
>> > _______________________________________________
>> > Lablgtk mailing list
>> > [email protected]
>> > http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk
>> >
>>
>> _______________________________________________
>> Lablgtk mailing list
>> [email protected]
>> http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk
>
> _______________________________________________
> Lablgtk mailing list
> [email protected]
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk
>

- Dmitry Bely