GTree is a module that displays single- or multi-columned lists and trees. It replaces the old Gtk+-1.2 GList and GTree modules. Even though GTree is slightly harder to master than its predecessors, it is so much more powerful and flexible that most application developers will not want to miss it once they have come to know it.
The purpose of this chapter is not to provide an exhaustive documentation of GtkTreeView - that is what the API documentation is for, which should be read alongside with this tutorial. The goal is rather to present an introduction to the most commonly-used aspects of GTree (GtkTreeView), and to demonstrate how the various GTree (GtkTreeView) components and concepts work together. Furthermore, an attempt has been made to shed some light on custom tree models and custom cell renderers, which seem to be often-mentioned, but rarely explained.
Developers looking for a quick and dirty introduction that teaches them everything they need to know in less than five paragraphs will not find it here. In the author's experience, developers who do not understand how the tree view and the models work together will run into problems once they try to modify the given examples, whereas developers who have worked with other toolkits that employ the Model/View/Controller-design will find that the API reference provides all the information they need to know in more condensed form anyway. Those who disagree may jump straight to the working example code of course.
Please note that the code examples in the following sections do not necessarily demonstrate how GTree is used best in a particular situation. There are different ways to achieve the same result, and the examples merely show those different ways, so that developers are able to decide which one is most suitable for the task at hand.
For the impatient, here is a small treeview 'Hello World' program.
(* file: helloworld.ml *) (* Compile with: ocamlc -I +lablgtk2 -o helloworld lablgtk.cma gtkInit.cmo helloworld.ml *) open Gobject.Data let cols = new GTree.column_list let col_name = cols#add string (* string column *) let col_age = cols#add int (* int column *) let create_model () = let data = [("Heinz El-Mann", 51); ("Jane Doe", 23); ("Joe Bungop", 91)] in let store = GTree.list_store cols in let fill (name, age) = let iter = store#append () in store#set ~row:iter ~column:col_name name; store#set ~row:iter ~column:col_age age in List.iter fill data; store let create_view ~model ~packing () = let view = GTree.view ~model ~packing () in (* Column #1: col_name is string column *) let col = GTree.view_column ~title:"Name" ~renderer:(GTree.cell_renderer_text [], ["text", col_name]) () in ignore (view#append_column col); (* Column #2: col_age is int column *) let col = GTree.view_column ~title:"Age" ~renderer:(GTree.cell_renderer_text [], ["text", col_age]) () in ignore (view#append_column col); view let main () = let window = GWindow.window ~title:"GTree Demo" () in window#connect#destroy ~callback:GMain.Main.quit; let model = create_model () in create_view ~model ~packing:window#add (); window#show (); GMain.Main.main () let _ = Printexc.print main ()