W Wrapl, The Programming Language

Libraries:Gtk:Gtk:TreeModel

Types

T

The T interface defines a generic tree interface for use by the Gtk.Gtk.TreeView.T widget. It is an abstract interface, and is designed to be usable with any appropriate data structure. The programmer just has to implement this interface on their own data type for it to be viewable by a Gtk.Gtk.TreeView.T widget.

The model is represented as a hierarchical tree of strongly-typed, columned data. In other words, the model can be seen as a tree where every node has different values depending on which column is being queried. The type of data found in a column is determined by using the GType system (ie. G_TYPE_INT, GTK_TYPE_BUTTON, G_TYPE_POINTER, etc.). The types are homogeneous per column across all nodes. It is important to note that this interface only provides a way of examining a model and observing changes. The implementation of each individual model decides how and if changes are made.

In order to make life simpler for programmers who do not need to write their own specialized model, two generic models are provided - the Gtk.Gtk.TreeStore.T and the Gtk.Gtk.ListStore.T. To use these, the developer simply pushes data into these models as necessary. These models provide the data structure as well as all appropriate tree interfaces. As a result, implementing drag and drop, sorting, and storing data is trivial. For the vast majority of trees and lists, these two models are sufficient.

Models are accessed on a node/column level of granularity. One can query for the value of a model at a certain node and a certain column on that node. There are two structures used to reference a particular node in a model. They are the Gtk.Gtk.TreePath.T and the Gtk.Gtk.TreeIter.T [4] Most of the interface consists of operations on a Gtk.Gtk.TreeIter.T.

A path is essentially a potential node. It is a location on a model that may or may not actually correspond to a node on a specific model. The Gtk.Gtk.TreePath.T struct can be converted into either an array of unsigned integers or a string. The string form is a list of numbers separated by a colon. Each number refers to the offset at that level. Thus, the path "0" refers to the root node and the path "2:4" refers to the fifth child of the third node.

By contrast, a Gtk.Gtk.TreeIter.T is a reference to a specific node on a specific model. It is a generic struct with an integer and three generic pointers. These are filled in by the model in a model-specific way. One can convert a path to an iterator by calling GetIter. These iterators are the primary way of accessing a model and are similar to the iterators used by Gtk.Gtk.TextBuffer.T. They are generally statically allocated on the stack and only used for a short time. The model interface defines a set of operations using them for navigating the model.

It is expected that models fill in the iterator with private data. For example, the Gtk.Gtk.ListStore.T model, which is internally a simple linked list, stores a list node in one of the pointers. The Gtk.Gtk.TreeModelSort.T stores an array and an offset in two of the pointers. Additionally, there is an integer field. This field is generally filled with a unique stamp per model. This stamp is for catching errors resulting from using invalid iterators with a model.

The lifecycle of an iterator can be a little confusing at first. Iterators are expected to always be valid for as long as the model is unchanged (and doesn't emit a signal). The model is considered to own all outstanding iterators and nothing needs to be done to free them from the user's point of view. Additionally, some models guarantee that an iterator is valid for as long as the node it refers to is valid (most notably the Gtk.Gtk.TreeStore.T and Gtk.Gtk.ListStore.T). Although generally uninteresting, as one always has to allow for the case where iterators do not persist beyond a signal, some very important performance enhancements were made in the sort model. As a result, the Gtk.Gtk.TreeModelFlags.ItersPersist flag was added to indicate this behavior.

To help show some common operation of a model, some examples are provided. The first example shows three ways of getting the iter at the location "3:2:5". While the first method shown is easier, the second is much more common, as you often get paths from callbacks.

Example 21. Acquiring a GtkTreeIter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Three ways of getting the iter pointing to the location
 */
{
  GtkTreePath *path;
  GtkTreeIter iter;
  GtkTreeIter parent_iter;
  /* get the iterator from a string */
  gtk_tree_model_get_iter_from_string (model, &iter, "3:2:5");
  /* get the iterator from a path */
  path = gtk_tree_path_new_from_string ("3:2:5");
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_path_free (path);
  /* walk the tree to find the iterator */
  gtk_tree_model_iter_nth_child (model, &iter, NULL, 3);
  parent_iter = iter;
  gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 2);
  parent_iter = iter;
  gtk_tree_model_iter_nth_child (model, &iter, &parent_iter, 5);
}



This second example shows a quick way of iterating through a list and getting a string and an integer from each row. The populate_model function used below is not shown, as it is specific to the Gtk.Gtk.ListStore.T. For information on how to write such a function, see the Gtk.Gtk.ListStore.T documentation.

Example 22. Reading data from a GtkTreeModel

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
enum
{
  STRING_COLUMN,
  INT_COLUMN,
  N_COLUMNS
};
{
  GtkTreeModel *list_store;
  GtkTreeIter iter;
  gboolean valid;
  gint row_count = 0;
  /* make a new list_store */
  list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
  /* Fill the list store with data */
  populate_model (list_store);
  /* Get the first iter in the list */
  valid = gtk_tree_model_get_iter_first (list_store, &iter);
  while (valid)
    {
      /* Walk through the list, reading each row */
      gchar *str_data;
      gint   int_data;
      /* Make sure you terminate calls to gtk_tree_model_get()
       * with a '-1' value
       */
      gtk_tree_model_get (list_store, &iter,
                          STRING_COLUMN, &str_data,
                          INT_COLUMN, &int_data,
                          -1);
      /* Do something with the data */
      g_print ("Row %d: (%s,%d)\n", row_count, str_data, int_data);
      g_free (str_data);
      row_count ++;
      valid = gtk_tree_model_iter_next (list_store, &iter);
    }
}





ParentT

Constants

InterfaceInfo : Std.Object.T

Nil : T

Functions

GetType() : Gtk.GObject.Type.T



Methods

:Foreach(self @ T, func @ Std.Function.T, user_data) : Std.Object.T

Calls func on each node in model in a depth-first fashion. If func returns TRUE, then the tree ceases to be walked, and Foreach returns.

model A T
func A function to be called on each row. [scope call]
user_data User data to passed to func.


:Get(self @ T, iter @ Gtk.Gtk.TreeIter.T, ... @ Std.Object.T) : Std.Object.T

Gets the value of one or more cells in the row referenced by iter. The variable argument list should contain integer column numbers, each column number followed by a place to store the value being retrieved. The list is terminated by a -1. For example, to get a value from column 0 with type G_TYPE_STRING, you would write: gtk_tree_model_get (model, iter, 0, &place_string_here, -1), where place_string_here is a gchar* to be filled with the string.

Returned values with type G_TYPE_OBJECT have to be unreferenced, values with type G_TYPE_STRING or G_TYPE_BOXED have to be freed. Other values are passed by value.

tree_model a T
iter a row in tree_model


:GetColumnType(self @ T, index_ @ Std.Integer.SmallT) : Gtk.GObject.Type.T

Returns the type of the column.

tree_model A T.
index_ The column index.
Returns The type of the column. [transfer none]


:GetColumnType(_ @ ParentT, _ @ Std.Integer.SmallT)

:GetFlags(self @ T) : Std.Integer.SmallT

Returns a set of flags supported by this interface. The flags are a bitwise combination of Gtk.Gtk.TreeModelFlags.T. The flags supported should not change during the lifecycle of the tree_model.

tree_model A T.
Returns The flags supported by this interface.


:GetFlags(_ @ ParentT)

:GetIter(self @ T, iter @ Gtk.Gtk.TreeIter.T, path @ Gtk.Gtk.TreePath.T) : Std.Symbol.T

Sets iter to a valid iterator pointing to path.

tree_model A T.
iter The uninitialized Gtk.Gtk.TreeIter.T. [out]
path The Gtk.Gtk.TreePath.T.
Returns TRUE, if iter was set.


:GetIter(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T, _ @ Gtk.Gtk.TreePath.T)

:GetIterFirst(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Std.Symbol.T

Initializes iter with the first iterator in the tree (the one at the path "0") and returns TRUE. Returns FALSE if the tree is empty.

tree_model A T.
iter The uninitialized Gtk.Gtk.TreeIter.T. [out]
Returns TRUE, if iter was set.


:GetIterFromString(self @ T, iter @ Gtk.Gtk.TreeIter.T, path_string @ Std.String.T) : Std.Symbol.T

Sets iter to a valid iterator pointing to path_string, if it exists. Otherwise, iter is left invalid and FALSE is returned.

tree_model A T.
iter An uninitialized Gtk.Gtk.TreeIter.T. [out]
path_string A string representation of a Gtk.Gtk.TreePath.T.
Returns TRUE, if iter was set.


:GetNColumns(self @ T) : Std.Integer.SmallT

Returns the number of columns supported by tree_model.

tree_model A T.
Returns The number of columns.


:GetNColumns(_ @ ParentT)

:GetPath(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Gtk.Gtk.TreePath.T

Returns a newly-created Gtk.Gtk.TreePath.T referenced by iter. This path should be freed with Gtk.Gtk.TreePath.Free.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T.
Returns a newly-created Gtk.Gtk.TreePath.T.


:GetPath(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T)

:GetStringFromIter(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Std.String.T

Generates a string representation of the iter. This string is a ':' separated list of numbers. For example, "4:10:0:3" would be an acceptable return value for this string.

tree_model A T.
iter An Gtk.Gtk.TreeIter.T.
Returns A newly-allocated string. Must be freed with g_free().


:GetValist(self @ T, iter @ Gtk.Gtk.TreeIter.T, var_args @ Agg.List.T) : Std.Object.T

See Get, this version takes a va_list for language bindings to use.

tree_model a T
iter a row in tree_model
var_args va_list of column/return location pairs


:GetValue(self @ T, iter @ Gtk.Gtk.TreeIter.T, column @ Std.Integer.SmallT, value @ Gtk.GObject.Value.T) : Std.Object.T

Initializes and sets value to that at column. When done with value, g_value_unset() needs to be called to free any allocated memory.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T.
column The column to lookup the value at.
value An empty GValue to set. [out][transfer none]


:GetValue(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T, _ @ Std.Integer.SmallT, _ @ Gtk.GObject.Value.T)

:IterChildren(self @ T, iter @ Gtk.Gtk.TreeIter.T, parent @ Gtk.Gtk.TreeIter.T) : Std.Symbol.T

Sets iter to point to the first child of parent. If parent has no children, FALSE is returned and iter is set to be invalid. parent will remain a valid node after this function has been called.

If parent is NULL returns the first node, equivalent to gtk_tree_model_get_iter_first (tree_model, iter);

tree_model A T.
iter The new Gtk.Gtk.TreeIter.T to be set to the child. [out]
parent The Gtk.Gtk.TreeIter.T, or NULL. [allow-none]
Returns TRUE, if child has been set to the first child.


:IterChildren(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T, _ @ Gtk.Gtk.TreeIter.T)

:IterHasChild(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Std.Symbol.T

Returns TRUE if iter has children, FALSE otherwise.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T to test for children.
Returns TRUE if iter has children.


:IterHasChild(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T)

:IterNChildren(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Std.Integer.SmallT

Returns the number of children that iter has. As a special case, if iter is NULL, then the number of toplevel nodes is returned.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T, or NULL. [allow-none]
Returns The number of children of iter.


:IterNChildren(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T)

:IterNext(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Std.Symbol.T

Sets iter to point to the node following it at the current level. If there is no next iter, FALSE is returned and iter is set to be invalid.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T. [in]
Returns TRUE if iter has been changed to the next node.


:IterNext(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T)

:IterNthChild(self @ T, iter @ Gtk.Gtk.TreeIter.T, parent @ Gtk.Gtk.TreeIter.T, n @ Std.Integer.SmallT) : Std.Symbol.T

Sets iter to be the child of parent, using the given index. The first index is 0. If n is too big, or parent has no children, iter is set to an invalid iterator and FALSE is returned. parent will remain a valid node after this function has been called. As a special case, if parent is NULL, then the nth root node is set.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T to set to the nth child. [out]
parent The Gtk.Gtk.TreeIter.T to get the child from, or NULL. [allow-none]
n Then index of the desired child.
Returns TRUE, if parent has an nth child.


:IterNthChild(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T, _ @ Gtk.Gtk.TreeIter.T, _ @ Std.Integer.SmallT)

:IterParent(self @ T, iter @ Gtk.Gtk.TreeIter.T, child @ Gtk.Gtk.TreeIter.T) : Std.Symbol.T

Sets iter to be the parent of child. If child is at the toplevel, and doesn't have a parent, then iter is set to an invalid iterator and FALSE is returned. child will remain a valid node after this function has been called.

tree_model A T
iter The new Gtk.Gtk.TreeIter.T to set to the parent. [out]
child The Gtk.Gtk.TreeIter.T.
Returns TRUE, if iter is set to the parent of child.


:IterParent(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T, _ @ Gtk.Gtk.TreeIter.T)

:RefNode(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Std.Object.T

Lets the tree ref the node. This is an optional method for models to implement. To be more specific, models may ignore this call as it exists primarily for performance reasons.

This function is primarily meant as a way for views to let caching model know when nodes are being displayed (and hence, whether or not to cache that node.) For example, a file-system based model would not want to keep the entire file-hierarchy in memory, just the sections that are currently being displayed by every current view.

A model should be expected to be able to get an iter independent of its reffed state.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T.


:RefNode(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T)

:RowChanged(self @ T, path @ Gtk.Gtk.TreePath.T, iter @ Gtk.Gtk.TreeIter.T) : Std.Object.T

Emits the "row-changed" signal on tree_model.

tree_model A T
path A Gtk.Gtk.TreePath.T pointing to the changed row
iter A valid Gtk.Gtk.TreeIter.T pointing to the changed row


:RowChanged(_ @ ParentT, _ @ Gtk.Gtk.TreePath.T, _ @ Gtk.Gtk.TreeIter.T)

:RowDeleted(self @ T, path @ Gtk.Gtk.TreePath.T) : Std.Object.T

Emits the "row-deleted" signal on tree_model. This should be called by models after a row has been removed. The location pointed to by path should be the location that the row previously was at. It may not be a valid location anymore.

tree_model A T
path A Gtk.Gtk.TreePath.T pointing to the previous location of the deleted row.


:RowDeleted(_ @ ParentT, _ @ Gtk.Gtk.TreePath.T)

:RowHasChildToggled(self @ T, path @ Gtk.Gtk.TreePath.T, iter @ Gtk.Gtk.TreeIter.T) : Std.Object.T

Emits the "row-has-child-toggled" signal on tree_model. This should be called by models after the child state of a node changes.

tree_model A T
path A Gtk.Gtk.TreePath.T pointing to the changed row
iter A valid Gtk.Gtk.TreeIter.T pointing to the changed row


:RowHasChildToggled(_ @ ParentT, _ @ Gtk.Gtk.TreePath.T, _ @ Gtk.Gtk.TreeIter.T)

:RowInserted(self @ T, path @ Gtk.Gtk.TreePath.T, iter @ Gtk.Gtk.TreeIter.T) : Std.Object.T

Emits the "row-inserted" signal on tree_model

tree_model A T
path A Gtk.Gtk.TreePath.T pointing to the inserted row
iter A valid Gtk.Gtk.TreeIter.T pointing to the inserted row


:RowInserted(_ @ ParentT, _ @ Gtk.Gtk.TreePath.T, _ @ Gtk.Gtk.TreeIter.T)

:RowsReordered(self @ T, path @ Gtk.Gtk.TreePath.T, iter @ Gtk.Gtk.TreeIter.T, new_order @ Std.Object.T) : Std.Object.T

Emits the "rows-reordered" signal on tree_model. This should be called by models when their rows have been reordered.

tree_model A T
path A Gtk.Gtk.TreePath.T pointing to the tree node whose children have been reordered
iter A valid Gtk.Gtk.TreeIter.T pointing to the node whose children have been reordered, or NULL if the depth of path is 0.
new_order an array of integers mapping the current position of each child to its old position before the re-ordering, i.e. new_order[newpos] = oldpos.


:RowsReordered(_ @ ParentT, _ @ Gtk.Gtk.TreePath.T, _ @ Gtk.Gtk.TreeIter.T, _ @ Std.Object.T)

:UnrefNode(self @ T, iter @ Gtk.Gtk.TreeIter.T) : Std.Object.T

Lets the tree unref the node. This is an optional method for models to implement. To be more specific, models may ignore this call as it exists primarily for performance reasons.

For more information on what this means, see RefNode. Please note that nodes that are deleted are not unreffed.

tree_model A T.
iter The Gtk.Gtk.TreeIter.T.


:UnrefNode(_ @ ParentT, _ @ Gtk.Gtk.TreeIter.T)