Xfce Foundation Classes
Main Page  | IndexNamespace List  |  Alphabetical List  |  Class List  |  File List


Xfc::Gtk::TreeModel Class Reference

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

#include <xfc/gtk/treemodel.hh>

Inheritance diagram for Xfc::Gtk::TreeModel:

Xfc::G::TypeInterface Xfc::G::TypeInstance Xfc::Trackable Xfc::Gtk::ListStore Xfc::Gtk::TreeModelFilter Xfc::Gtk::TreeModelSort Xfc::Gtk::TreeStore List of all members.

Signal Prototpyes

Public Types

Public Member Functions

Accessors
Templates
Methods
Signal Proxies

Protected Member Functions

Constructors

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);
    }

See also: the TreeView Widget HOWTO and example.


Member Typedef Documentation

typedef sigc::slot<bool, const TreePath&, const TreeIter&> Xfc::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 Xfc::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 Xfc::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 Xfc::Gtk::TreeModel::get_enum const TreeIter iter,
int  column,
DataType &  data
const
 

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 gfc-demo program <demos/gfc-demo/liststore.cc>.

TreeModelFlagsField Xfc::Gtk::TreeModel::get_flags  )  const
 

Returns a set of flags supported by this interface.

Returns:
The Gtk::TreeModelFlags 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 Xfc::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 Xfc::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 Xfc::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 Xfc::Gtk::TreeModel::get_object const TreeIter iter,
int  column,
DataType &  data
const
 

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. The object is has its reference count incremented so you will have to call unref(). There is a good example of getting values in the gfc-demo program <demos/gfc-demo/liststore.cc>.

Pointer<TreePath> Xfc::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 Xfc::Gtk::TreeModel::get_pointer const TreeIter iter,
int  column,
DataType &  data
const
 

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 gfc-demo program <demos/gfc-demo/liststore.cc>.

String Xfc::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 Xfc::Gtk::TreeModel::get_value const TreeIter iter,
int  column,
DataType &  data
const
 

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 gfc-demo program <demos/gfc-demo/liststore.cc>.

bool Xfc::Gtk::TreeModel::get_value const TreeIter iter,
int  column,
std::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 std::string value is retrieved.

bool Xfc::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 Xfc::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 Xfc::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 Xfc::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 Xfc::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 Xfc::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 Xfc::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 Xfc::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.

void Xfc::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 Xfc::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 Xfc::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 Xfc::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 Xfc::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 Xfc::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 Xfc::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.


Member Data Documentation

const RowChangedSignalType Xfc::Gtk::TreeModel::row_changed_signal [static, protected]
 

Row changed signal (see signal_row_changed()).

Calls a slot with the signature:

             void function(const TreePath& path, const TreeIter& iter);
             // path: A TreePath pointing to the changed row.
             // iter: A valid TreeIter pointing to the changed row.

const RowDeletedSignalType Xfc::Gtk::TreeModel::row_deleted_signal [static, protected]
 

Row deleted signal (see signal_row_deleted()).

Calls a slot with the signature:

             void function(const TreePath& path);
             // path: A TreePath pointing to the previous location of the deleted row.

const RowHasChildToggledSignalType Xfc::Gtk::TreeModel::row_has_child_toggled_signal [static, protected]
 

Row has child toggled signal (see signal_row_has_child_toggled()).

Calls a slot with the signature:

             void function(const TreePath& path, const TreeIter& iter);
             // path: A TreePath pointing to the changed row.
             // iter: A valid TreeIter pointing to the changed row.

const RowInsertedSignalType Xfc::Gtk::TreeModel::row_inserted_signal [static, protected]
 

Row inserted signal (see signal_row_inserted()).

Calls a slot with the signature:

             void function(const TreePath& path, const TreeIter& iter);
             // path: A TreePath pointing to the inserted row.
             // iter: A valid TreeIter pointing to the inserted row.

const RowsReorderedSignalType Xfc::Gtk::TreeModel::rows_reordered_signal [static, protected]
 

Rows reordered signal (see signal_rows_reordered()).

Calls a slot with the signature:

             void function(const TreePath& path, const TreeIter& iter, int *new_order);
             // 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.


The documentation for this class was generated from the following file: Xfce Foundation Classes
Copyright © 2004-2005 The XFC Development Team XFC 4.3