GTK+ 2.0 Tree View Tutorial using Ocaml

Tim-Philipp Muller

Ocaml adaptation: SooHyoung Oh


This is a tutorial on how to use GTK+ 2.0 GtkTreeView widget in Ocaml language. Ocaml has a very nice Gtk+ interface called LablGtk in which GtkTreeView widget is implemented as GTree module.

The LablGtk version used here is LablGTK 2.4.0.

This tutorial is adapted from the original C language version written by Tim-Philipp Muller.

Please mail all comments and suggestions about Ocaml adaptation to

There is also a version in PDF format (for easier printing) and the raw docbook XML source document.

Some examples can be found in treeview_tutorial_examples.tar.gz tarball.

This tutorial is work-in-progress. The latest version can be found at

"Gtk+ 2.0 Tutorial using Ocaml" can be found at

Last updated: 2004-09-18

Table of Contents

1. Lists and Trees: the GTree Module
Hello World
2. Components: Model, Renderer, Column, View
3. GTree.models for Data Storage: GTree.list_store and GTree.tree_store
How Data is Organised in a Store
Refering to Rows: Gtk.tree_iter, Gtk.tree_path, Gtk.row_reference, GTree.row_reference
Gtk.row_reference, GTree.row_reference
Adding Rows to a Store
Adding Rows to a List Store
Adding Rows to a Tree Store
Speed Issues when Adding a Lot of Rows
Manipulating Row Data
Retrieving Row Data
Removing Rows
Removing Multiple Rows
4. Creating a Tree View
Connecting Tree View and Model
Tree View Look and Feel
5. Mapping Data to the Screen: GTree.view_column and GTree.cell_renderer_*
Cell Renderers
Cell Data Functions
GTree.cell_renderer_text and Integer, Boolean and Float Types
GTree.cell_renderer_text, UTF8, and pango markup
A Working Example
How to Make a Whole Row Bold or Coloured
How to Pack Icons into the Tree View
6. Selections, Double-Clicks and Context Menus
Handling Selections
Selection Modes
Getting the Currently Selected Rows
Using Selection Functions
Checking Whether a Row is Selected
Selecting and Unselecting Rows
Double-Clicks on a Row
Context Menus on Right Click
7. Sorting
Sorting and Tree View Column Headers
8. Editable Cells
Editable Text Cells
Setting the cursor to a specific cell
Editable Toggle and Radio Button Cells
9. Other Resources
10. Copyright, License, Credits, and Revision History
Copyright and License
Ocaml Adaptation
Original Document
Ocaml Adaptation
Original Document
Revision History
Ocaml Adaptation
Original Document

List of Figures

3.1. Tree Paths
5.1. Cell Renderer Properties
5.2. GtkTreeViewColumns and GtkCellRenderers
5.3. Persistent Cell Renderer Properties