5.8. How to Pack Icons into the Tree View

So far we have only put text in the tree view. While everything you need to know to display icons (in the form of GdkPixbufs) has been introduced in the previous sections, a short example might help to make things clearer. The following code will pack an icon and some text into the same tree view column:


(* file: icon.ml *)

let cols = new GTree.column_list
let col_icon: GdkPixbuf.pixbuf GTree.column = cols#add Gobject.Data.gobject
let col_text = cols#add Gobject.Data.string

let create_liststore () =
  let store = GTree.list_store cols in

  let icon = GdkPixbuf.from_file "gtk.xpm" in
  let row = store#append () in
  store#set ~row ~column:col_icon icon;
  store#set ~row ~column:col_text "example";
  store

let create_treeview ~packing () =
  let model = create_liststore () in

  let view = GTree.view ~model ~packing () in

  let renderer = (GTree.cell_renderer_pixbuf [], [("pixbuf", col_icon)]) in
  let col = GTree.view_column ~title:"Title" ~renderer () in
  view#append_column col;

  let renderer_text = GTree.cell_renderer_text [] in
  let col = GTree.view_column ~title:"Text"
      ~renderer:(renderer_text, [("text", col_text)]) () in
  view#append_column col;

  view

...

Note that the tree view will not resize icons for you, but displays them in their original size. If you want to display stock icons instead of GdkPixbufs loaded from file, you should have a look at the `STOCK_ID property of GTree.cell_properties_pixbuf (GTree.cell_renderer_pixbuf) (and your model column should be of type string, as all stock IDs are just strings by which to identify the stock icon).