Next: Gtk2::TreeModelFilter | Previous: Gtk2::TreeIter | [Gtk2-Perl - Table of Contents] | [Gtk2-Perl - Index] |
Gtk2::TreeModel
# 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);
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 Gtk2::TreeModel::get_iter
.
These iterators are the primary way of accessing a model and are
similar to the iterators used by Gtk2::TextBuffer. The model interface
defines a set of operations using them for navigating the model.
The iterators are generally used only for a short time, and their behaviour is different to that suggested by the Gtk+ documentation. They are not valid when the model is changed, even though get_flags returns 'iters-persist'. Iterators obtained within a GtkTreeModelForeachFunc are also invalid after the foreach terminates. There may be other such cases. In the foreach case, and perhaps others, a persistent iterator may be obtained by copying it (see Glib::Boxed->copy).
(The preceding description and most of the method descriptions have been adapted directly from the Gtk+ C API reference.)
Glib::Interface +----Gtk2::TreeModel
Returns the type of column $index_ as a package name.
Call $func on each row in $model as
bool = &$func ($model, $path, $iter, $user_data)
If $func returns true, the tree ceases to be walked,
and $treemodel->foreach
returns.
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 get
, so you'll want to use
$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 Gtk2::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.
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.
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.
$path (Gtk2::TreePath) the tree node whose children have been reordered
$iter (Gtk2::TreeIter or undef) the tree node whose children have been reordered
... (list) list of integers mapping the current position of each child to its old position before the re-ordering, i.e. $new_order[$newpos] = $oldpos. There should be as many elements in this list as there are rows in $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.
Since: gtk+ 2.2
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 Gtk2::TreeModel::ref_node
.
Please note that nodes that are deleted are not unreffed.
Alias for get.
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.
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:
The two references, if used, will generally be to data within the model, like a row array, or a node object in a tree or linked list. Keeping the things referred to alive is the model's responsibility. An iter doesn't make them persist, and if the things are destroyed then any iters still containing them will become invalid (and result in memory corruption if used). An iter only has to remain valid until the model contents change, so generally anything internal to the model is fine.
An implementation of
Gtk2::TreeModel
, the class closure, ie. class default signal
handler, for the rows-reordered
signal is called only with an
integer address for the reorder array parameter, not a Perl arrayref
like a handler installed with signal_connect
receives. It works to
signal_chain_from_overridden
with the address, but it's otherwise
fairly useless and will likely change in the future.
Gtk2, Glib::Interface
Copyright (C) 2003-2008 by the gtk2-perl team.
This software is licensed under the LGPL. See Gtk2 for a full notice.