Tree and List Widget Overview: CTK+ 3 Reference Manual

## Tree and List Widget Overview Tree and List Widget Overview — Overview of CtkTreeModel, CtkTreeView, and friends |

## Overview

To create a tree or list in CTK+, use the CtkTreeModel interface in conjunction with the CtkTreeView widget. This widget is designed around a Model/View/Controller design and consists of four major parts:

The tree view widget (CtkTreeView)
The view column (CtkTreeViewColumn)
The cell renderers (CtkCellRenderer etc.)
The model interface (CtkTreeModel)

The View is composed of the first three objects, while the last is the Model. 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.

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. For example, consider how to render a boolean variable. Should it render it as a string of "True" or "False", "On" or "Off", or should it be rendered as a checkbox?

## Creating a model

CTK+ provides two simple models that can be used: the CtkListStore and the CtkTreeStore. CtkListStore is used to model list widgets, while the CtkTreeStore 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:

    1
    CtkListStore *store = ctk_list_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);

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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    enum
    {
       TITLE_COLUMN,
       AUTHOR_COLUMN,
       CHECKED_COLUMN,
       N_COLUMNS
    };

    CtkTreeStore *store = ctk_tree_store_new (N_COLUMNS,       /* Total number of columns */
                                              G_TYPE_STRING,   /* Book title              */
                                              G_TYPE_STRING,   /* Author                  */
                                              G_TYPE_BOOLEAN); /* Is checked out?         */

Adding data to the model is done using `ctk_tree_store_set()` or `ctk_list_store_set()`, depending upon which sort of model was created. To do this, a CtkTreeIter must be acquired. The iterator points to the location where data will be added.

Once an iterator has been acquired, `ctk_tree_store_set()` is used to apply data to the part of the model that the iterator points to. Consider the following example:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    CtkTreeIter   iter;

    ctk_tree_store_append (store, &iter, NULL);  /* Acquire an iterator */

    ctk_tree_store_set (store, &iter,
                        TITLE_COLUMN, "The Principle of Reason",
                        AUTHOR_COLUMN, "Martin Heidegger",
                        CHECKED_COLUMN, FALSE,
                        -1);

Notice that the last argument is -1. This is always done because 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.

The third argument to `ctk_tree_store_append()` is the parent iterator. It is used to add a row to a CtkTreeStore 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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    CtkTreeIter iter1;  /* Parent iter */
    CtkTreeIter iter2;  /* Child iter  */

    ctk_tree_store_append (store, &iter1, NULL);  /* Acquire a top-level iterator */
    ctk_tree_store_set (store, &iter1,
                        TITLE_COLUMN, "The Art of Computer Programming",
                        AUTHOR_COLUMN, "Donald E. Knuth",
                        CHECKED_COLUMN, FALSE,
                        -1);

    ctk_tree_store_append (store, &iter2, &iter1);  /* Acquire a child iterator */
    ctk_tree_store_set (store, &iter2,
                        TITLE_COLUMN, "Volume 1: Fundamental Algorithms",
                        -1);

    ctk_tree_store_append (store, &iter2, &iter1);
    ctk_tree_store_set (store, &iter2,
                        TITLE_COLUMN, "Volume 2: Seminumerical Algorithms",
                        -1);

    ctk_tree_store_append (store, &iter2, &iter1);
    ctk_tree_store_set (store, &iter2,
                        TITLE_COLUMN, "Volume 3: Sorting and Searching",
                        -1);

## Creating the view component

While there are several different models to choose from, there is only one view widget to deal with. It works with either the list or the tree store. Setting up a CtkTreeView is not a difficult matter. It needs a CtkTreeModel to know where to retrieve its data from.

    1
    2
    3
    CtkWidget *tree;

    tree = ctk_tree_view_new_with_model (CTK_TREE_MODEL (store));

### Columns and cell renderers

Once the CtkTreeView widget has a model, it will need to know how to display the model. It does this with columns and cell renderers.

Cell renderers are used to draw the data in the tree model in a way. There are a number of cell renderers that come with CTK+, including the CtkCellRendererText, CtkCellRendererPixbuf and the CtkCellRendererToggle. It is relatively easy to write a custom renderer.

A CtkTreeViewColumn is the object that CtkTreeView 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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    CtkCellRenderer *renderer;
    CtkTreeViewColumn *column;

    renderer = ctk_cell_renderer_text_new ();
    column = ctk_tree_view_column_new_with_attributes ("Author",
                                                       renderer,
                                                       "text", AUTHOR_COLUMN,
                                                       NULL);
    ctk_tree_view_append_column (CTK_TREE_VIEW (tree), column);

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.

### Selection handling

Most applications will need to not only deal with displaying data, but also receiving input events from users. To do this, simply get a reference to a selection object and connect to the “changed” signal.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /* Prototype for selection handler callback */
    static void tree_selection_changed_cb (CtkTreeSelection *selection, gpointer data);

    /* Setup the selection handler */
    CtkTreeSelection *select;

    select = ctk_tree_view_get_selection (CTK_TREE_VIEW (tree));
    ctk_tree_selection_set_mode (select, CTK_SELECTION_SINGLE);
    g_signal_connect (G_OBJECT (select), "changed",
                      G_CALLBACK (tree_selection_changed_cb),
                      NULL);

Then to retrieve data for the row selected:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    static void
    tree_selection_changed_cb (CtkTreeSelection *selection, gpointer data)
    {
            CtkTreeIter iter;
            CtkTreeModel *model;
            gchar *author;

            if (ctk_tree_selection_get_selected (selection, &model, &iter))
            {
                    ctk_tree_model_get (model, &iter, AUTHOR_COLUMN, &author, -1);

                    g_print ("You selected a book by %s\n", author);

                    g_free (author);
            }
    }

## Simple Example

Here is a simple example of using a CtkTreeView 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 CtkTreeModel section.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    enum
    {
       TITLE_COLUMN,
       AUTHOR_COLUMN,
       CHECKED_COLUMN,
       N_COLUMNS
    };

    void
    setup_tree (void)
    {
       CtkTreeStore *store;
       CtkWidget *tree;
       CtkTreeViewColumn *column;
       CtkCellRenderer *renderer;

       /* Create a model.  We are using the store model for now, though we
        * could use any other CtkTreeModel */
       store = ctk_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 */
       tree = ctk_tree_view_new_with_model (CTK_TREE_MODEL (store));

       /* 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 */
       renderer = ctk_cell_renderer_text_new ();
       g_object_set (G_OBJECT (renderer),
                     "foreground", "red",
                     NULL);

       /* Create a column, associating the "text" attribute of the
        * cell_renderer to the first column of the model */
       column = ctk_tree_view_column_new_with_attributes ("Author", renderer,
                                                          "text", AUTHOR_COLUMN,
                                                          NULL);

       /* Add the column to the view. */
       ctk_tree_view_append_column (CTK_TREE_VIEW (tree), column);

       /* Second column.. title of the book. */
       renderer = ctk_cell_renderer_text_new ();
       column = ctk_tree_view_column_new_with_attributes ("Title",
                                                          renderer,
                                                          "text", TITLE_COLUMN,
                                                          NULL);
       ctk_tree_view_append_column (CTK_TREE_VIEW (tree), column);

       /* Last column.. whether a book is checked out. */
       renderer = ctk_cell_renderer_toggle_new ();
       column = ctk_tree_view_column_new_with_attributes ("Checked out",
                                                          renderer,
                                                          "active", CHECKED_COLUMN,
                                                          NULL);
       ctk_tree_view_append_column (CTK_TREE_VIEW (tree), column);

       /* Now we can manipulate the view just like any other CTK widget */
       ...
    }