man Gtk2::TreeModel () - Gtk2::TreeModel

NAME

Gtk2::TreeModel

SYNOPSIS

 # Three ways of getting the iter pointing to the location 3:2:5

 # get the iterator from a string
 $iter = $model->get_iter_from_string ("3:2:5");

 # get the iterator from a path
 $path = Gtk2::TreePath->new_from_string ("3:2:5");
 $iter = $model->get_iter ($path);

 # walk the tree to find the iterator
 $iter = $model->iter_nth_child (undef, 3);
 $iter = $model->iter_nth_child ($iter, 2);
 $iter = $model->iter_nth_child ($iter, 5);

 # getting and setting values

 # assuming a model with these columns
 use constant STRING_COLUMN => 0;
 use constant INT_COLUMN => 1;

 # set values
 $model->set ($iter,
              STRING_COLUMN, $new_string_value,
              INT_COLUMN, $new_int_value);

 # and get values
 ($int, $str) = $model->get ($iter, INT_COLUMN, STRING_COLUMN);

 # if you don't specify a list of column numbers,
 # you get all of them.
 @values = $model->get ($iter);

DESCRIPTION

The Gtk2::TreeModel provides a generic tree interface for use by the Gtk2::TreeView widget. It is an abstract interface, designed to be usable with any appropriate data structure.

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 (i.e. package names like Glib::Int, Gtk2::Button, Glib::Scalar, 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 Gtk2::TreeStore and the Gtk2::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. For information on how to implement your own model in Perl, see CREATING A CUSTOM TREE MODEL.

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: the Gtk2::TreePath and the Gtk2::TreeIter (short for iterator). Most of the interface consists of operations on a Gtk2::TreeIter.

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 Gtk2::TreePath object 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 Gtk2::TreeIter is a reference to a specific node on a specific model. To the user of a model, the iter is merely an opaque object. One can convert a path to an iterator by calling CWGtk2::TreeModel::get_iter. These iterators are the primary way of accessing a model and are similar to the iterators used by Gtk2::TextBuffer. They are generally used only for a short time, and are valid only as long as the model is unchanged. The model interface defines a set of operations using them for navigating the model.

(The preceding description and most of the method descriptions have been adapted directly from the Gtk+ C API reference.)

HIERARCHY

  Glib::Object::_Unregistered::GInterface
  +----Gtk2::TreeModel

METHODS

Returns the type of column $index_ as a package name. Call $func on each row in $model. $func gets the tree path and iter of the current row; if $func returns true, the tree ceases to be walked, and CW$treemodel->foreach returns.

* ... (list) of column indices
Fetch and return the model's values in the row pointed to by $iter. If you specify no column indices, it returns the values for all of the columns, otherwise, returns just those columns' values (in order). This overrides overrides Glib::Object's CWget, so you'll want to use CW$object->get_property to get object properties. Returns undef if $parent has no children, otherwise, returns a new iter to the first child of $parent. $parent is unaltered. If $parent is undef, this is equivalent to CWGtk2::TreeModel::get_iter_first. Return a new iter pointing to the first node in the tree (the one at path 0), or undef if the tree is empty. Returns a new iter pointing to the node described by $path_string, or undef if the path does not exist. Returns a new Gtk2::TreeIter corresponding to $path. Returns true if $iter has child nodes. Returns the number of children $iter has. If $iter is undef (or omitted) then returns the number of toplevel nodes. Return a new iter pointing to node following $iter at the current level, or undef if there is no next node. $iter is unaltered. (Note: this is different from the C version, which modifies the iter.) Returns an iter to the child of $parent at index $n, or undef if there is no such child. $parent is unaltered. Returns a new iter pointing to $child's parent node, or undef if $child doesn't have a parent. $child is unaltered. Return a new Gtk2::TreePath corresponding to $iter.

$tree_model->ref_node ($iter)

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. Emits the row_changed signal on $tree_model.

$tree_model->row_deleted ($path)

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 removed row's old location. It may not be a valid location anymore. Emits the row_has_child_toggled signal on $tree_model. This should be called by models after the child state of a node changes. Emits the row_inserted signal on $tree_model. Emits the rows-reordered signal on $tree_model/ This should be called by models with their rows have been reordered. 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->unref_node ($iter)

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 CWGtk2::TreeModel::ref_node. Please note that nodes that are deleted are not unreffed.

* ... (list) of column indices
Alias for get.

CREATING A CUSTOM TREE MODEL

GTK+ provides two model implementations, Gtk2::TreeStore and Gtk2::ListStore, which should be sufficient in most cases. For some cases, however, it is advantageous to provide a custom tree model implementation. It is possible to create custom tree models in Perl, because we're cool like that.

To do this, you create a Glib::Object derivative which implements the Gtk2::TreeModel interface; this is gtk2-perl-speak for you have to add a special key when you register your object type. For example:

  package MyModel;
  use Gtk2;
  use Glib::Object::Subclass
      Glib::Object::,
      interfaces => [ Gtk2::TreeModel:: ],
      ;

This will cause perl to call several virtual methods with ALL_CAPS_NAMES when Gtk+ attempts to perform certain actions on the model. You simply provide (or override) those methods.

TREE ITERS

Gtk2::TreeIter is normally an opaque object, but on the implementation side of a Gtk2::TreeModel, you have to define what's inside. The virtual methods described below deal with iters as a reference to an array containing four values:

o stamp (integer)
A number unique to this model.
o user_data (integer)
An arbitrary integer value.
o user_data2 (scalar)
An arbitrary scalar. Will not persist. May be undef.
o user_data3 (scalar)
An arbitrary scalar. Will not persist. May be undef.

VIRTUAL METHODS

An implementation of

treemodelflags = GET_FLAGS ($model)
integer = GET_N_COLUMNS ($model)
See above for a description of what goes in the returned array reference.
treepath = GET_PATH ($model, ARRAYREF)
Implements CW$treemodel->get().
ARRAYREF = ITER_NEXT ($model, ARRAYREF)
ARRAYREF = ITER_CHILDREN ($model, ARRAYREF)
boolean = ITER_HAS_CHILD ($model, ARRAYREF)
integer = ITER_N_CHILDREN ($model, ARRAYREF)
ARRAYREF = ITER_PARENT ($model, ARRAYREF)
REF_NODE ($model, ARRAYREF)
Optional.
UNREF_NODE ($model, ARRAYREF)
Optional.

SIGNALS

row-changed (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter)
row-inserted (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter)
row-has-child-toggled (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter)
row-deleted (Gtk2::TreeModel, Gtk2::TreePath)
rows-reordered (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter, gpointer)

ENUMS AND FLAGS

flags Gtk2::TreeModelFlags

* 'iters-persist' / 'GTK_TREE_MODEL_ITERS_PERSIST'
* 'list-only' / 'GTK_TREE_MODEL_LIST_ONLY'

SEE ALSO

Gtk2, Glib::Object::_Unregistered::GInterface

COPYRIGHT

Copyright (C) 2003-2005 by the gtk2-perl team.

This software is licensed under the LGPL. See Gtk2 for a full notice.