Reference Manual

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# Inti::Gtk::TreeModel Class Reference

A GtkTreeModel C++ wrapper class. More...

`#include <inti/gtk/treemodel.h>`

Inheritance diagram for Inti::Gtk::TreeModel:

List of all members.

## Public Types

- typedef Slot2< bool, const
  TreePath &, const TreeIter & > ForeachSlot
    Signature of the callback slot to be called on each node in the model in a depth-first fashion.

## Public Member Functions

Accessors

- GtkTreeModel * gtk_tree_model () const
    Get a pointer to the GtkTreeModel structure.
- GtkTreeModelIface * gtk_tree_model_iface () const
    Get a pointer to the GtkTreeModelIface structure.
- operator GtkTreeModel * () const
    Conversion operator; safely converts a TreeModel to a GtkTreeModel pointer.
- TreeModelFlagsField get_flags () const
    Returns a set of flags supported by this interface.
- int get_n_columns () const
    Returns the number of columns supported by the tree model.
- GType get_column_type (int index) const
    Returns the type of the column at index.
- bool get_iter (TreeIter &iter, const TreePath &path) const
    Sets iter to a valid iterator pointing to path.
- bool get_iter (TreeIter &iter, const String &path) const
    Sets iter to a valid iterator pointing to path, if it exists, otherwise, iter is left invalid and false is returned.
- String get_string_from_iter (const TreeIter &iter) const
    Generates a string representation of the iter.
- bool get_iter_first (TreeIter &iter) const
    Initializes iter with the first iterator in the tree (the one at the path "0").
- Pointer< TreePath > get_path (const TreeIter &iter) const
    Returns a newly-created TreePath referenced by iter.
- void get_value (const TreeIter &iter, int column, G::Value &value) const
    Initializes and sets value to that at column.
- String get_value (const TreeIter &iter, int column) const
    Retrieves the string value at column.
- bool get_value (const TreeIter &iter, int column, String &str) const
    Convenience method to make retrieving the string value at column easier.
- bool iter_has_child (const TreeIter &iter) const
    Returns true if iter has children, false otherwise.

Templates

- template<typename DataType> void get_value (const TreeIter &iter, int column, DataType &data) const
    Gets the data set in the cell specified by iter and column.
- template<typename DataType> void get_enum (const TreeIter &iter, int column, DataType &data) const
    Gets the enum value set in the cell specified by iter and column.
- template<typename DataType> void get_object (const TreeIter &iter, int column, DataType &data) const
    Gets the object pointer set in the cell specified by iter and column.
- template<typename DataType> void get_pointer (const TreeIter &iter, int column, DataType &data) const
    Gets the pointer set in the cell specified by iter and column.

Methods

- bool iter_next (TreeIter &iter)
    Sets iter to point to the node following it at the current level.
- bool iter_children (TreeIter &iter, const TreeIter *parent=0)
    Sets iter to point to the first child of parent.
- int iter_n_children (const TreeIter *iter)
    Returns the number of children that iter has.
- bool iter_nth_child (TreeIter &iter, const TreeIter *parent, int n)
    Sets iter to be the child of parent, using the given index.
- bool iter_parent (TreeIter &iter, const TreeIter &child)
    Sets iter to be the parent of child.
- void ref_node (TreeIter *iter)
    Lets the tree ref the node.
- void unref_node (TreeIter *iter)
    Lets the tree unref the node.
- void foreach (const ForeachSlot *each)
    Calls each on each node in model in a depth-first fashion.
- void row_changed (const TreePath &path, const TreeIter &iter)
    Emits the "row_changed" signal on the tree model.
- void row_inserted (const TreePath &path, const TreeIter &iter)
    Emits the "row_inserted" signal on the tree model.
- void row_has_child_toggled (const TreePath &path, const TreeIter &iter)
    Emits the "row_has_child_toggled" signal on the tree model.
- void row_deleted (const TreePath &path)
    Emits the "row_deleted" signal on the tree model.
- void rows_reordered (const TreePath &path, const TreeIter &iter, int *new_order)
    Emits the "rows_reordered" signal on tree_model.

Signal Proxies

- const RowChangedSignalProxy sig_row_changed ()
    Connect to the row_changed_signal; emitted when a row in the model is changed.
- const RowInsertedSignalProxy sig_row_inserted ()
    Connect to the row_inserted_signal; emitted when a row is inserted into the model.
- const RowHasChildToggledSignalProxy sig_row_has_child_toggled ()
    Connect to the row_has_child_toggled_signal; emitted after the child state of a node changes.
- const RowDeletedSignalProxy sig_row_deleted ()
    Connect to the row_deleted_signal; emitted when a row is removed from the model.
- const RowsReorderedSignalProxy sig_rows_reordered ()
    Connect to the rows_reordered_signal; emitted when the model's rows have been reordered.

## Protected Member Functions

Constructors

- virtual ~TreeModel ()=0
    Destructor.

Signal Handlers

- virtual void on_row_changed (const TreePath &path, const TreeIter &iter)
    Called when a row in the model is changed.
- virtual void on_row_inserted (const TreePath &path, const TreeIter &iter)
    Called when a row is inserted into the model.
- virtual void on_row_has_child_toggled (const TreePath &path, const TreeIter &iter)
    Called after the child state of a node changes.
- virtual void on_row_deleted (const TreePath &path)
    Called when a row is removed from the model.
- virtual void on_rows_reordered (const TreePath &path, const TreeIter &iter, int *new_order)
    Called when the model's rows have been reordered.

## Detailed Description

A GtkTreeModel C++ wrapper class.

The TreeModel interface defines a generic tree interface for use by the TreeView widget. It is an abstract interface, and is designed to be usable with any derived class. The programmer just has to multiplely inherit this interface on their own class for it to be viewable by a TreeView 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 TreeStore and the ListStore. 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 TreePath and the TreeIter. Most of the interface consists of operations on a TreeIter. A tree 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 TreePath object can be converted into either an vector of 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 TreeIter 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 get_iter(). These iterators are the primary way of accessing a model and are similar to the iterators used by TextBuffer. They are generally statically allocated on the heap 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 ListStore model, which is internally a simple linked list, stores a list node in one of the pointers. The TreeModelSort 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 TreeStore and ListStore). 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 TREE_MODEL_ITERS_PERSIST flag was added to indicate this behavior.

To help show some common operations of a model, two 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 1: Three ways of getting the TreeIter pointing to a location

        Gtk::TreeIter iter;

        // Get the iterator from a string
        model->get_iter(iter, "3:2:5");

        // Get the iterator from a path
        Pointer<Gtk::TreePath> path = new Gtk::TreePath("3:2:5");
        model->get_iter(iter, path);

        // Walk the tree to find the iterator
        model->iter_nth_child(iter, 0, 3);
        Gtk::TreeIter parent_iter = iter;
        model->iter_nth_child(iter, parent_iter, 2);
        parent_iter = iter;
        model->iter_nth_child(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. See the ListStore documentation for an example on constructing a ListStore model.

Example 2: Reading data from a TreeModel

        Gtk::TreeIter iter;

        // Get the first iter in the list
        bool valid = model->get_iter_first(iter);
        int row_count = 0;

        while (valid)
        {
            String str_data;
            model->get_value(iter, STRING_COLUMN, str_data);
            int int_data;
            model->get_value(iter, INT_COLUMN, int_data);

            // Do something with the data
            g_print("Row %d: (%s,%d)\n", row_count, str_data.c_str(), int_data);

            row_count++;
            valid = model->iter_next(iter);
        }

## Member Typedef Documentation

typedef Slot2<bool, const TreePath&, const TreeIter&> Inti::Gtk::TreeModel::ForeachSlot

Signature of the callback slot to be called on each node in the model in a depth-first fashion.

Example: Method signature for ForeachSlot.

                 bool method(const TreePath& path, const TreeIter& iter);

                 // path: A TreePath pointing to a row.
                 // iter: A TreeIter pointing to a row.
                 // return: true to stop walking the tree.

If the slot returns true, then the tree ceases to be walked, and foreach() returns.

## Member Function Documentation

void Inti::Gtk::TreeModel::foreach | ( | const ForeachSlot * | each | ) |

Calls each on each node in model in a depth-first fashion.

Parameters:

  each | The callback slot to be called on each row.

If each returns true, then the tree ceases to be walked, and foreach() returns.

GType Inti::Gtk::TreeModel::get_column_type | ( | int | index | ) | const

Returns the type of the column at index.

Parameters:

  index | The column index.

Returns:
  The type of the column.

template<typename DataType>
void Inti::Gtk::TreeModel::get_enum | ( | const TreeIter & | iter,
|  | int | column,
|  | DataType & | data
| ) | const`[inline]`

Gets the enum value set in the cell specified by iter and column.

Parameters:

  iter | A valid TreeIter for the row.
  column | The column to lookup the data at.
  data | The return location for the enum value of type DataType.

This method is used to get enumeration values. It is a separate method because G::Value handles an enumeration internally as an int. The DataType can be any enumeration. There is a good example of setting values in the inti-demo program <demos/inti-demo/liststore.cc>.

TreeModelFlagsField Inti::Gtk::TreeModel::get_flags | ( |  | ) | const

Returns a set of flags supported by this interface.

Returns:
  The flags supported by this interface.

The flags are a bitwise combination of TreeModelFlags. The flags supported should not change during the lifecycle of the tree_model.

bool Inti::Gtk::TreeModel::get_iter | ( | TreeIter & | iter,
|  | const String & | path
| ) | const

Sets iter to a valid iterator pointing to path, if it exists, otherwise, iter is left invalid and false is returned.

Parameters:

  iter | A TreeIter.
  path | A string representation of a TreePath.

Returns:
  true if iter was set.

bool Inti::Gtk::TreeModel::get_iter | ( | TreeIter & | iter,
|  | const TreePath & | path
| ) | const

Sets iter to a valid iterator pointing to path.

Parameters:

  iter | The TreeIter.
  path | The TreePath.

Returns:
  true if iter was set.

bool Inti::Gtk::TreeModel::get_iter_first | ( | TreeIter & | iter | ) | const

Initializes iter with the first iterator in the tree (the one at the path "0").

Parameters:

  iter | The TreeIter.

Returns:
  true if iter was set; false if the tree is empty.

template<typename DataType>
void Inti::Gtk::TreeModel::get_object | ( | const TreeIter & | iter,
|  | int | column,
|  | DataType & | data
| ) | const`[inline]`

Gets the object pointer set in the cell specified by iter and column.

Parameters:

  iter | A valid TreeIter for the row.
  column | The column to lookup the data at.
  data | The return location for the object pointer of type DataType.

The data argument is a pointer to an object derived from G::Object, passed by reference. There is a good example of setting values in the inti-demo program <demos/inti-demo/liststore.cc>.

Pointer<TreePath> Inti::Gtk::TreeModel::get_path | ( | const TreeIter & | iter | ) | const

Returns a newly-created TreePath referenced by iter.

Parameters:

  iter | The TreeIter.

Returns:
  A smart pointer to a newly-created TreePath.

template<typename DataType>
void Inti::Gtk::TreeModel::get_pointer | ( | const TreeIter & | iter,
|  | int | column,
|  | DataType & | data
| ) | const`[inline]`

Gets the pointer set in the cell specified by iter and column.

Parameters:

  iter | A valid TreeIter for the row.
  column | The column to lookup the data at.
  data | The return location for the pointer of type DataType.

The data argument can be a pointer to any object. The pointer is managed internally as a generic (void*) pointer. Unlike get_object() which passes the G::Object pointer internally as a GObject pointer, get_pointer() passes the pointer as is, without interpretation. There is a good example of setting values in the inti-demo program <demos/inti-demo/liststore.cc>.

String Inti::Gtk::TreeModel::get_string_from_iter | ( | const TreeIter & | iter | ) | const

Generates a string representation of the iter.

Parameters:

  iter | A TreeIter.

Returns:
  A String.

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

template<typename DataType>
void Inti::Gtk::TreeModel::get_value | ( | const TreeIter & | iter,
|  | int | column,
|  | DataType & | data
| ) | const`[inline]`

Gets the data set in the cell specified by iter and column.

Parameters:

  iter | A valid TreeIter for the row.
  column | The column to lookup the data at.
  data | The return location for the data of type DataType.

This method is used to get values corresponding to the standard data types used by G::Value, such as bool, int, double, String and unsigned int. There is a good example of setting values in the inti-demo program <demos/inti-demo/liststore.cc>.

bool Inti::Gtk::TreeModel::get_value | ( | const TreeIter & | iter,
|  | int | column,
|  | String & | str
| ) | const

Convenience method to make retrieving the string value at column easier.

Parameters:

  iter | The TreeIter.
  column | The column to lookup the value at.
  str | An empty String to set.

Returns:
  true if a String value is retrieved.

String Inti::Gtk::TreeModel::get_value | ( | const TreeIter & | iter,
|  | int | column
| ) | const

Retrieves the string value at column.

Parameters:

  iter | The TreeIter.
  column | The column to lookup the value at.

Returns:
  The String value at coulmn, or a null String if no value is set.

void Inti::Gtk::TreeModel::get_value | ( | const TreeIter & | iter,
|  | int | column,
|  | G::Value & | value
| ) | const

Initializes and sets value to that at column.

Parameters:

  iter | The TreeIter.
  column | The column to lookup the value at.
  value | An empty G::Value to set.

bool Inti::Gtk::TreeModel::iter_children | ( | TreeIter & | iter,
|  | const TreeIter * | parent = 0
| ) |

Sets iter to point to the first child of parent.

Parameters:

  iter | The TreeIter to be set to the child.
  parent | The parent TreeIter, or null.

Returns:
  true if child has been set to the first child.

If parent has no children, false is returned and iter is set to be invalid. parent will remain a valid node after this method has been called. If parent is null this method returns the first node, equivalent to get_iter_first(iter);

int Inti::Gtk::TreeModel::iter_n_children | ( | const TreeIter * | iter | ) |

Returns the number of children that iter has.

Parameters:

  iter | The TreeIter, or null.

Returns:
  The number of children of iter.

As a special case, if iter is null, then the number of toplevel nodes is returned.

bool Inti::Gtk::TreeModel::iter_next | ( | TreeIter & | iter | ) |

Sets iter to point to the node following it at the current level.

Parameters:

  iter | The TreeIter.

Returns:
  true if iter has been changed to the next node.

If there is no next iter, false is returned and iter is set to be invalid.

bool Inti::Gtk::TreeModel::iter_nth_child | ( | TreeIter & | iter,
|  | const TreeIter * | parent,
|  | int | n
| ) |

Sets iter to be the child of parent, using the given index.

Parameters:

  iter | The TreeIter to set to the nth child.
  parent | The TreeIter to get the child from, or null.
  n | The n index of the desired child.

Returns:
  true if parent has an nth child.

The first index is 0. If index 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 method has been called. As a special case, if parent is null, then the nth root node is set.

bool Inti::Gtk::TreeModel::iter_parent | ( | TreeIter & | iter,
|  | const TreeIter & | child
| ) |

Sets iter to be the parent of child.

Parameters:

  iter | The new TreeIter to set to the parent.
  child | The TreeIter.

Returns:
  true if iter is set to 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 method has been called.

virtual void Inti::Gtk::TreeModel::on_row_changed | ( | const TreePath & | path,
|  | const TreeIter & | iter
| ) | `[protected, virtual]`

Called when a row in the model is changed.

Parameters:

  path | A TreePath pointing to the changed row.
  iter | A valid TreeIter pointing to the changed row.

virtual void Inti::Gtk::TreeModel::on_row_deleted | ( | const TreePath & | path | ) | `[protected, virtual]`

Called when a row is removed from the model.

Parameters:

  path | A TreePath pointing to the previous location of the deleted row.

virtual void Inti::Gtk::TreeModel::on_row_has_child_toggled | ( | const TreePath & | path,
|  | const TreeIter & | iter
| ) | `[protected, virtual]`

Called after the child state of a node changes.

Parameters:

  path | A TreePath pointing to the changed row.
  iter | A valid TreeIter pointing to the changed row.

virtual void Inti::Gtk::TreeModel::on_row_inserted | ( | const TreePath & | path,
|  | const TreeIter & | iter
| ) | `[protected, virtual]`

Called when a row is inserted into the model.

Parameters:

  path | A TreePath pointing to the inserted row.
  iter | A valid TreeIter pointing to the inserted row.

virtual void Inti::Gtk::TreeModel::on_rows_reordered | ( | const TreePath & | path,
|  | const TreeIter & | iter,
|  | int * | new_order
| ) | `[protected, virtual]`

Called when the model's rows have been reordered.

Parameters:

  path | A TreePath pointing to the tree node whose children have been reordered.
  iter | A valid TreeIter pointing to the node whose children have been reordered.
  new_order | An array of integers containing the new indices of the children, i.e. the former child n is now at position new_order[n].

void Inti::Gtk::TreeModel::ref_node | ( | TreeIter * | iter | ) |

Lets the tree ref the node.

Parameters:

  iter | The TreeIter.

This is an optional method for models to implement. Models may ignore this call as it exists primarily for performance reasons. This method is primarily meant as a way for views to let a 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 it's reffed state.

void Inti::Gtk::TreeModel::row_changed | ( | const TreePath & | path,
|  | const TreeIter & | iter
| ) |

Emits the "row_changed" signal on the tree model.

Parameters:

  path | A TreePath pointing to the changed row.
  iter | A valid TreeIter pointing to the changed row.

void Inti::Gtk::TreeModel::row_deleted | ( | const TreePath & | path | ) |

Emits the "row_deleted" signal on the tree model.

Parameters:

  path | A TreePath pointing to the previous location of the deleted row.

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.

void Inti::Gtk::TreeModel::row_has_child_toggled | ( | const TreePath & | path,
|  | const TreeIter & | iter
| ) |

Emits the "row_has_child_toggled" signal on the tree model.

Parameters:

  path | A TreePath pointing to the changed row.
  iter | A valid TreeIter pointing to the changed row.

This should be called by models after the child state of a node changes.

void Inti::Gtk::TreeModel::row_inserted | ( | const TreePath & | path,
|  | const TreeIter & | iter
| ) |

Emits the "row_inserted" signal on the tree model.

Parameters:

  path | A TreePath pointing to the inserted row.
  iter | A valid TreeIter pointing to the inserted row.

void Inti::Gtk::TreeModel::rows_reordered | ( | const TreePath & | path,
|  | const TreeIter & | iter,
|  | int * | new_order
| ) |

Emits the "rows_reordered" signal on tree_model.

Parameters:

  path | A TreePath pointing to the tree node whose children have been reordered.
  iter | A valid TreeIter pointing to the node whose children have been reordered.
  new_order | An array of integers containing the new indices of the children, that is, the former child n is now at position new_order[n].

This should be called by models when their rows have been reordered.

void Inti::Gtk::TreeModel::unref_node | ( | TreeIter * | iter | ) |

Lets the tree unref the node.

Parameters:

  iter | The TreeIter.

This is an optional method for models to implement. Models may ignore this call as it exists primarily for performance reasons. For more information on what this means, see ref_node(). Please note that nodes that are deleted are not unreffed.

The documentation for this class was generated from the following file:

- treemodel.h

Main Page - Footer