2008-07-10 03:27:56 +00:00
|
|
|
<?xml version="1.0"?>
|
|
|
|
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
|
|
|
|
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
|
|
|
|
]>
|
2011-01-15 00:35:34 +00:00
|
|
|
<refentry id="TreeWidget">
|
2000-10-30 23:21:38 +00:00
|
|
|
<refmeta>
|
|
|
|
<refentrytitle>Tree and List Widget Overview</refentrytitle>
|
|
|
|
<manvolnum>3</manvolnum>
|
|
|
|
<refmiscinfo>GTK Library</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>Tree and List Widget Overview</refname>
|
2007-06-11 03:41:27 +00:00
|
|
|
<refpurpose>Overview of GtkTreeModel, GtkTreeView, and friends</refpurpose>
|
2000-10-30 23:21:38 +00:00
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Overview</title>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
To create a tree or list in GTK+, use the #GtkTreeModel interface in
|
|
|
|
conjunction with the #GtkTreeView widget. This widget is
|
2004-01-22 06:48:20 +00:00
|
|
|
designed around a <firstterm>Model/View/Controller</firstterm>
|
|
|
|
design and consists of four major parts:
|
2001-09-08 18:23:47 +00:00
|
|
|
<simplelist>
|
2004-01-22 06:48:20 +00:00
|
|
|
<member>The tree view widget (<structname>GtkTreeView</structname>)</member>
|
|
|
|
<member>The view column (<structname>GtkTreeViewColumn</structname>)</member>
|
|
|
|
<member>The cell renderers (<structname>GtkCellRenderer</structname> etc.)</member>
|
|
|
|
<member>The model interface (<structname>GtkTreeModel</structname>)</member>
|
2001-09-08 18:23:47 +00:00
|
|
|
</simplelist>
|
2004-01-22 06:48:20 +00:00
|
|
|
The <emphasis>View</emphasis> is composed of the first three
|
|
|
|
objects, while the last is the <emphasis>Model</emphasis>. One
|
|
|
|
of the prime benefits of the MVC design is that multiple views
|
|
|
|
can be created of a single model. For example, a model mapping
|
|
|
|
the file system could be created for a file manager. Many views
|
|
|
|
could be created to display various parts of the file system,
|
|
|
|
but only one copy need be kept in memory.
|
2000-10-30 23:21:38 +00:00
|
|
|
</para>
|
2002-03-21 04:48:31 +00:00
|
|
|
<para>
|
|
|
|
The purpose of the cell renderers is to provide extensibility to the
|
|
|
|
widget and to allow multiple ways of rendering the same type of data.
|
2004-01-22 06:48:20 +00:00
|
|
|
For example, consider how to render a boolean variable. Should it
|
2002-03-21 04:48:31 +00:00
|
|
|
render it as a string of "True" or "False", "On" or "Off", or should
|
2004-01-22 06:48:20 +00:00
|
|
|
it be rendered as a checkbox?
|
2002-03-21 04:48:31 +00:00
|
|
|
</para>
|
2000-10-30 23:21:38 +00:00
|
|
|
</refsect1>
|
2002-03-21 04:48:31 +00:00
|
|
|
<refsect1>
|
|
|
|
<title>Creating a model</title>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
GTK+ provides two simple models that can be used: the #GtkListStore
|
|
|
|
and the #GtkTreeStore. GtkListStore is used to model list widgets,
|
|
|
|
while the GtkTreeStore models trees. It is possible to develop a new
|
|
|
|
type of model, but the existing models should be satisfactory for all
|
|
|
|
but the most specialized of situations. Creating the model is quite
|
|
|
|
simple:
|
2002-03-21 04:48:31 +00:00
|
|
|
</para>
|
|
|
|
<informalexample><programlisting><![CDATA[
|
|
|
|
GtkListStore *store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
|
|
|
|
]]></programlisting></informalexample>
|
|
|
|
<para>
|
|
|
|
This creates a list store with two columns: a string column and a boolean
|
|
|
|
column. Typically the 2 is never passed directly like that; usually an
|
|
|
|
enum is created wherein the different columns are enumerated, followed by
|
|
|
|
a token that represents the total number of columns. The next example will
|
|
|
|
illustrate this, only using a tree store instead of a list store. Creating
|
|
|
|
a tree store operates almost exactly the same.
|
|
|
|
</para>
|
|
|
|
<informalexample><programlisting><![CDATA[
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TITLE_COLUMN,
|
|
|
|
AUTHOR_COLUMN,
|
|
|
|
CHECKED_COLUMN,
|
|
|
|
N_COLUMNS
|
|
|
|
};
|
|
|
|
|
|
|
|
GtkTreeStore *store = gtk_tree_store_new (N_COLUMNS, /* Total number of columns */
|
|
|
|
G_TYPE_STRING, /* Book title */
|
|
|
|
G_TYPE_STRING, /* Author */
|
|
|
|
G_TYPE_BOOLEAN); /* Is checked out? */
|
2002-05-28 22:23:55 +00:00
|
|
|
]]></programlisting></informalexample>
|
2002-03-21 04:48:31 +00:00
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
Adding data to the model is done using gtk_tree_store_set() or
|
|
|
|
gtk_list_store_set(), depending upon which sort of model was
|
|
|
|
created. To do this, a #GtkTreeIter must be acquired. The iterator
|
|
|
|
points to the location where data will be added.
|
2002-03-21 04:48:31 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
Once an iterator has been acquired, gtk_tree_store_set() is used to
|
|
|
|
apply data to the part of the model that the iterator points to.
|
|
|
|
Consider the following example:
|
2002-03-21 04:48:31 +00:00
|
|
|
</para>
|
|
|
|
<informalexample><programlisting><![CDATA[
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
gtk_tree_store_append (store, &iter, NULL); /* Acquire an iterator */
|
|
|
|
|
|
|
|
gtk_tree_store_set (store, &iter,
|
|
|
|
TITLE_COLUMN, "The Principle of Reason",
|
|
|
|
AUTHOR_COLUMN, "Martin Heidegger",
|
2003-11-23 20:00:25 +00:00
|
|
|
CHECKED_COLUMN, FALSE,
|
2002-03-25 16:33:21 +00:00
|
|
|
-1);
|
2002-03-21 04:48:31 +00:00
|
|
|
]]></programlisting></informalexample>
|
|
|
|
|
|
|
|
<para>
|
2002-03-25 16:33:21 +00:00
|
|
|
Notice that the last argument is -1. This is always done because
|
2002-03-21 04:48:31 +00:00
|
|
|
this is a variable-argument function and it needs to know when to stop
|
|
|
|
processing arguments. It can be used to set the data in any or all
|
|
|
|
columns in a given row.
|
|
|
|
</para>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
The third argument to gtk_tree_store_append() is the parent iterator. It
|
2002-03-21 04:48:31 +00:00
|
|
|
is used to add a row to a GtkTreeStore as a child of an existing row. This
|
|
|
|
means that the new row will only be visible when its parent is visible and
|
|
|
|
in its expanded state. Consider the following example:
|
|
|
|
</para>
|
|
|
|
<informalexample><programlisting><![CDATA[
|
|
|
|
GtkTreeIter iter1; /* Parent iter */
|
|
|
|
GtkTreeIter iter2; /* Child iter */
|
|
|
|
|
|
|
|
gtk_tree_store_append (store, &iter1, NULL); /* Acquire a top-level iterator */
|
|
|
|
gtk_tree_store_set (store, &iter1,
|
|
|
|
TITLE_COLUMN, "The Art of Computer Programming",
|
|
|
|
AUTHOR_COLUMN, "Donald E. Knuth",
|
|
|
|
CHECKED_COLUMN, FALSE,
|
2002-03-25 16:33:21 +00:00
|
|
|
-1);
|
2002-03-21 04:48:31 +00:00
|
|
|
|
|
|
|
gtk_tree_store_append (store, &iter2, &iter1); /* Acquire a child iterator */
|
|
|
|
gtk_tree_store_set (store, &iter2,
|
|
|
|
TITLE_COLUMN, "Volume 1: Fundamental Algorithms",
|
2002-03-25 16:33:21 +00:00
|
|
|
-1);
|
2002-03-21 04:48:31 +00:00
|
|
|
|
|
|
|
gtk_tree_store_append (store, &iter2, &iter1);
|
|
|
|
gtk_tree_store_set (store, &iter2,
|
|
|
|
TITLE_COLUMN, "Volume 2: Seminumerical Algorithms",
|
2002-03-25 16:33:21 +00:00
|
|
|
-1);
|
2002-03-21 04:48:31 +00:00
|
|
|
|
|
|
|
gtk_tree_store_append (store, &iter2, &iter1);
|
|
|
|
gtk_tree_store_set (store, &iter2,
|
|
|
|
TITLE_COLUMN, "Volume 3: Sorting and Searching",
|
2002-03-25 16:33:21 +00:00
|
|
|
-1);
|
2002-03-21 04:48:31 +00:00
|
|
|
]]></programlisting></informalexample>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Creating the view component</title>
|
|
|
|
<para>
|
2002-03-25 16:33:21 +00:00
|
|
|
While there are several different models to choose from, there is
|
|
|
|
only one view widget to deal with. It works with either the list
|
2005-05-16 18:16:42 +00:00
|
|
|
or the tree store. Setting up a #GtkTreeView is not a difficult
|
|
|
|
matter. It needs a #GtkTreeModel to know where to retrieve its data
|
|
|
|
from.
|
2002-03-21 04:48:31 +00:00
|
|
|
</para>
|
|
|
|
<informalexample><programlisting><![CDATA[
|
|
|
|
GtkWidget *tree;
|
|
|
|
|
|
|
|
tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
|
|
|
|
]]></programlisting></informalexample>
|
|
|
|
|
|
|
|
<refsect2>
|
|
|
|
<title>Columns and cell renderers</title>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
Once the #GtkTreeView widget has a model, it will need to know how
|
|
|
|
to display the model. It does this with columns and cell renderers.
|
2002-03-25 16:33:21 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Cell renderers are used to draw the data in the tree model in a
|
2014-09-18 20:39:07 +00:00
|
|
|
way. There are a number of cell renderers that come with GTK+,
|
2005-05-16 18:16:42 +00:00
|
|
|
including the #GtkCellRendererText, #GtkCellRendererPixbuf and
|
|
|
|
the #GtkCellRendererToggle.
|
2002-03-25 16:33:21 +00:00
|
|
|
It is relatively easy to write a custom renderer.
|
|
|
|
</para>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
A #GtkTreeViewColumn is the object that GtkTreeView uses to organize
|
|
|
|
the vertical columns in the tree view. It needs to know the name of
|
|
|
|
the column to label for the user, what type of cell renderer to use,
|
|
|
|
and which piece of data to retrieve from the model for a given row.
|
2002-03-21 04:48:31 +00:00
|
|
|
</para>
|
2006-05-15 14:34:19 +00:00
|
|
|
<informalexample><programlisting>
|
2002-03-21 04:48:31 +00:00
|
|
|
GtkCellRenderer *renderer;
|
|
|
|
GtkTreeViewColumn *column;
|
|
|
|
|
2005-05-16 18:16:42 +00:00
|
|
|
renderer = gtk_cell_renderer_text_new (<!-- -->);
|
2002-03-21 04:48:31 +00:00
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Author",
|
|
|
|
renderer,
|
|
|
|
"text", AUTHOR_COLUMN,
|
|
|
|
NULL);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
|
2006-05-15 14:34:19 +00:00
|
|
|
</programlisting></informalexample>
|
2002-03-21 04:48:31 +00:00
|
|
|
<para>
|
|
|
|
At this point, all the steps in creating a displayable tree have been
|
|
|
|
covered. The model is created, data is stored in it, a tree view is
|
|
|
|
created and columns are added to it.
|
|
|
|
</para>
|
|
|
|
</refsect2>
|
|
|
|
|
|
|
|
<refsect2>
|
|
|
|
<title>Selection handling</title>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
Most applications will need to not only deal with displaying data, but
|
|
|
|
also receiving input events from users. To do this, simply get a
|
2007-06-10 06:52:51 +00:00
|
|
|
reference to a selection object and connect to the
|
|
|
|
#GtkTreeSelection::changed signal.
|
2002-03-21 04:48:31 +00:00
|
|
|
</para>
|
|
|
|
<informalexample><programlisting><![CDATA[
|
|
|
|
/* Prototype for selection handler callback */
|
|
|
|
static void tree_selection_changed_cb (GtkTreeSelection *selection, gpointer data);
|
|
|
|
|
|
|
|
/* Setup the selection handler */
|
|
|
|
GtkTreeSelection *select;
|
|
|
|
|
|
|
|
select = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
|
Minor documentation fixes
2002-08-10 Soren Sandmann <sandmann@daimi.au.dk>
* docs/reference/gtk/tree_widget.sgml,
docs/reference/gtk/tmpl/gtkdialog.sgml,
docs/reference/gtk/tmpl/gtkentry.sgml,
docs/reference/gtk/tmpl/gtkfilesel.sgml,
docs/reference/gtk/tmpl/gtkfontsel.sgml,
docs/reference/gtk/tmpl/gtkfontseldlg.sgml,
docs/reference/gtk/tmpl/gtktreemodel.sgml,
docs/reference/gtk/tmpl/gtkwidget.sgml, gdk/x11/gdkdisplay-x11.c,
gtk/gtkbbox.c, gtk/gtkbox.c, gtk/gtkbutton.c, gtk/gtkcellrenderer.c,
gtk/gtkcellrendererpixbuf.c, gtk/gtkcellrenderertext.c,
gtk/gtkcheckmenuitem.c, gtk/gtkcontainer.c, gtk/gtkcurve.c,
gtk/gtkdialog.h, gtk/gtkentry.c, gtk/gtkfilesel.c, gtk/gtkfontsel.c,
gtk/gtkframe.c, gtk/gtkhandlebox.c, gtk/gtkiconfactory.c,
gtk/gtkimage.c, gtk/gtkinvisible.c, gtk/gtkitemfactory.c,
gtk/gtklabel.c, gtk/gtklayout.c, gtk/gtkmenu.c, gtk/gtkprogress.c,
gtk/gtkprogressbar.c, gtk/gtkscrolledwindow.c, gtk/gtksizegroup.c,
gtk/gtktable.c, gtk/gtktextiter.c, gtk/gtktexttag.c,
gtk/gtktexttag.h, gtk/gtktextview.c, gtk/gtktogglebutton.c,
gtk/gtktoolbar.c, gtk/gtktreemodel.c, gtk/gtktreeselection.c,
gtk/gtktreestore.c, gtk/gtktreeview.c, gtk/gtktreeviewcolumn.c,
gtk/gtkviewport.c, gtk/gtkwidget.c, gtk/gtkwidget.h,
gtk/gtkwindow.c:
Minor documentation fixes
(#89254, patch from Brett Nash;
#85809, patch from daten@dnetc.org;
#76391, patch from Ross Burton;
#74559, Manuel Clos;
#73569, #72005, Alexey A. Malyshev;
#70061, patch from Dennis Bj"orklund;
#64566, #63388, #58328, #57499, #81007, #77349, Vitaly Tishkov;
#78932, Vitaly Tishkov, patch from Ross Burton;
#73306)
2002-08-10 09:55:41 +00:00
|
|
|
gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
|
2002-03-21 04:48:31 +00:00
|
|
|
g_signal_connect (G_OBJECT (select), "changed",
|
|
|
|
G_CALLBACK (tree_selection_changed_cb),
|
|
|
|
NULL);
|
|
|
|
]]></programlisting></informalexample>
|
|
|
|
<para>
|
|
|
|
Then to retrieve data for the row selected:
|
|
|
|
</para>
|
|
|
|
<informalexample><programlisting><![CDATA[
|
|
|
|
static void
|
|
|
|
tree_selection_changed_cb (GtkTreeSelection *selection, gpointer data)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model;
|
|
|
|
gchar *author;
|
|
|
|
|
|
|
|
if (gtk_tree_selection_get_selected (selection, &model, &iter))
|
|
|
|
{
|
|
|
|
gtk_tree_model_get (model, &iter, AUTHOR_COLUMN, &author, -1);
|
|
|
|
|
|
|
|
g_print ("You selected a book by %s\n", author);
|
|
|
|
|
|
|
|
g_free (author);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]]></programlisting></informalexample>
|
|
|
|
</refsect2>
|
|
|
|
</refsect1>
|
|
|
|
|
2000-10-30 23:21:38 +00:00
|
|
|
<refsect1>
|
|
|
|
<title>Simple Example</title>
|
|
|
|
<para>
|
2005-05-16 18:16:42 +00:00
|
|
|
Here is a simple example of using a #GtkTreeView widget in context
|
|
|
|
of the other widgets. It simply creates a simple model and view,
|
|
|
|
and puts them together. Note that the model is never populated
|
|
|
|
with data — that is left as an exercise for the reader.
|
|
|
|
More information can be found on this in the #GtkTreeModel section.
|
2006-05-15 14:37:32 +00:00
|
|
|
<informalexample><programlisting>
|
2002-03-06 19:22:36 +00:00
|
|
|
enum
|
|
|
|
{
|
2002-03-21 04:48:31 +00:00
|
|
|
TITLE_COLUMN,
|
|
|
|
AUTHOR_COLUMN,
|
|
|
|
CHECKED_COLUMN,
|
|
|
|
N_COLUMNS
|
2002-03-06 19:22:36 +00:00
|
|
|
};
|
|
|
|
|
2002-03-21 04:48:31 +00:00
|
|
|
void
|
|
|
|
setup_tree (void)
|
2000-10-30 23:21:38 +00:00
|
|
|
{
|
2002-03-21 04:48:31 +00:00
|
|
|
GtkTreeStore *store;
|
|
|
|
GtkWidget *tree;
|
|
|
|
GtkTreeViewColumn *column;
|
|
|
|
GtkCellRenderer *renderer;
|
|
|
|
|
|
|
|
/* Create a model. We are using the store model for now, though we
|
|
|
|
* could use any other GtkTreeModel */
|
|
|
|
store = gtk_tree_store_new (N_COLUMNS,
|
|
|
|
G_TYPE_STRING,
|
|
|
|
G_TYPE_STRING,
|
|
|
|
G_TYPE_BOOLEAN);
|
|
|
|
|
|
|
|
/* custom function to fill the model with data */
|
|
|
|
populate_tree_model (store);
|
|
|
|
|
|
|
|
/* Create a view */
|
2002-04-21 23:07:49 +00:00
|
|
|
tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
|
2002-03-21 04:48:31 +00:00
|
|
|
|
|
|
|
/* The view now holds a reference. We can get rid of our own
|
|
|
|
* reference */
|
|
|
|
g_object_unref (G_OBJECT (store));
|
|
|
|
|
|
|
|
/* Create a cell render and arbitrarily make it red for demonstration
|
|
|
|
* purposes */
|
2005-05-16 18:16:42 +00:00
|
|
|
renderer = gtk_cell_renderer_text_new (<!-- -->);
|
2002-03-25 16:33:21 +00:00
|
|
|
g_object_set (G_OBJECT (renderer),
|
|
|
|
"foreground", "red",
|
|
|
|
NULL);
|
2002-03-21 04:48:31 +00:00
|
|
|
|
|
|
|
/* Create a column, associating the "text" attribute of the
|
|
|
|
* cell_renderer to the first column of the model */
|
2002-03-25 16:33:21 +00:00
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Author", renderer,
|
2002-03-21 04:48:31 +00:00
|
|
|
"text", AUTHOR_COLUMN,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Add the column to the view. */
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
|
|
|
|
|
|
|
|
/* Second column.. title of the book. */
|
2005-05-16 18:16:42 +00:00
|
|
|
renderer = gtk_cell_renderer_text_new (<!-- -->);
|
2002-03-21 04:48:31 +00:00
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Title",
|
|
|
|
renderer,
|
|
|
|
"text", TITLE_COLUMN,
|
|
|
|
NULL);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
|
|
|
|
|
|
|
|
/* Last column.. whether a book is checked out. */
|
2005-05-16 18:16:42 +00:00
|
|
|
renderer = gtk_cell_renderer_toggle_new (<!-- -->);
|
2002-03-21 04:48:31 +00:00
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Checked out",
|
|
|
|
renderer,
|
|
|
|
"active", CHECKED_COLUMN,
|
|
|
|
NULL);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
|
|
|
|
|
|
|
|
/* Now we can manipulate the view just like any other GTK widget */
|
|
|
|
...
|
2000-10-30 23:21:38 +00:00
|
|
|
}
|
Small additions.
* gtk/tmpl/gtktextview.sgml: Small additions.
* gtk/tmpl/gtksignal.sgml: Explain what to use instead.
* gtk/question_index.sgml, gtk/text_widget.sgml, gtk/tree_widget.sgml,
gtk/changes-1.2.sgml, gtk/changes-2.0.sgml,
gtk/framebuffer.sgml: SGML fixes and additions.
* gtk/tmpl/gtksignal.sgml, gtk/tmpl/gtkdialog.sgml,
gtk/tmpl/gtkeditable.sgml, gtk/tmpl/gtkfilesel.sgml,
gtk/tmpl/gtkmain.sgml, gtk/tmpl/gtkmenu.sgml,
gtk/tmpl/gtkmessagedialog.sgml, gtk/tmpl/gtkrc.sgml,
gtk/tmpl/gtktogglebutton.sgml, gtk/tmpl/gtkspinbutton.sgml,
gtk/tmpl/gtkpaned.sgml, gtk/tmpl/gtkwindow.sgml:
Replace references to deprecated functions.
2002-01-03 23:04:44 +00:00
|
|
|
</programlisting></informalexample>
|
2000-10-30 23:21:38 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|