1Gtk2::TreeModel(3) User Contributed Perl Documentation Gtk2::TreeModel(3)
2
3
4
6 Gtk2::TreeModel
7
9 # Three ways of getting the iter pointing to the location 3:2:5
10
11 # get the iterator from a string
12 $iter = $model->get_iter_from_string ("3:2:5");
13
14 # get the iterator from a path
15 $path = Gtk2::TreePath->new_from_string ("3:2:5");
16 $iter = $model->get_iter ($path);
17
18 # walk the tree to find the iterator
19 $iter = $model->iter_nth_child (undef, 3);
20 $iter = $model->iter_nth_child ($iter, 2);
21 $iter = $model->iter_nth_child ($iter, 5);
22
23 # getting and setting values
24
25 # assuming a model with these columns
26 use constant STRING_COLUMN => 0;
27 use constant INT_COLUMN => 1;
28
29 # set values
30 $model->set ($iter,
31 STRING_COLUMN, $new_string_value,
32 INT_COLUMN, $new_int_value);
33
34 # and get values
35 ($int, $str) = $model->get ($iter, INT_COLUMN, STRING_COLUMN);
36
37 # if you don't specify a list of column numbers,
38 # you get all of them.
39 @values = $model->get ($iter);
40
42 The Gtk2::TreeModel provides a generic tree interface for use by the
43 Gtk2::TreeView widget. It is an abstract interface, designed to be
44 usable with any appropriate data structure.
45
46 The model is represented as a hierarchical tree of strongly-typed,
47 columned data. In other words, the model can be seen as a tree where
48 every node has different values depending on which column is being
49 queried. The type of data found in a column is determined by using the
50 GType system (i.e. package names like Glib::Int, Gtk2::Button,
51 Glib::Scalar, etc). The types are homogeneous per column across all
52 nodes. It is important to note that this interface only provides a way
53 of examining a model and observing changes. The implementation of each
54 individual model decides how and if changes are made.
55
56 In order to make life simpler for programmers who do not need to write
57 their own specialized model, two generic models are provided - the
58 Gtk2::TreeStore and the Gtk2::ListStore. To use these, the developer
59 simply pushes data into these models as necessary. These models pro‐
60 vide the data structure as well as all appropriate tree interfaces. As
61 a result, implementing drag and drop, sorting, and storing data is
62 trivial. For the vast majority of trees and lists, these two models
63 are sufficient. For information on how to implement your own model in
64 Perl, see "CREATING A CUSTOM TREE MODEL".
65
66 Models are accessed on a node/column level of granularity. One can
67 query for the value of a model at a certain node and a certain column
68 on that node. There are two structures used to reference a particular
69 node in a model: the Gtk2::TreePath and the Gtk2::TreeIter (short for
70 "iterator"). Most of the interface consists of operations on a
71 Gtk2::TreeIter.
72
73 A path is essentially a potential node. It is a location on a model
74 that may or may not actually correspond to a node on a specific model.
75 The Gtk2::TreePath object can be converted into either an array of
76 unsigned integers or a string. The string form is a list of numbers
77 separated by a colon. Each number refers to the offset at that level.
78 Thus, the path '0' refers to the root node and the path '2:4' refers to
79 the fifth child of the third node.
80
81 By contrast, a Gtk2::TreeIter is a reference to a specific node on a
82 specific model. To the user of a model, the iter is merely an opaque
83 object. One can convert a path to an iterator by calling
84 "Gtk2::TreeModel::get_iter". These iterators are the primary way of
85 accessing a model and are similar to the iterators used by
86 Gtk2::TextBuffer. They are generally used only for a short time, and
87 are valid only as long as the model is unchanged. The model interface
88 defines a set of operations using them for navigating the model.
89
90 (The preceding description and most of the method descriptions have
91 been adapted directly from the Gtk+ C API reference.)
92
94 Glib::Object::_Unregistered::GInterface
95 +----Gtk2::TreeModel
96
98 string = $tree_model->get_column_type ($index_)
99
100 * $index_ (integer)
101
102 Returns the type of column $index_ as a package name.
103
104 treemodelflags = $tree_model->get_flags
105
106 $model->foreach ($func, $user_data=undef)
107
108 * $func (subroutine)
109 * $user_data (scalar)
110
111 Call $func on each row in $model. $func gets the tree path and
112 iter of the current row; if $func returns true, the tree ceases to
113 be walked, and "$treemodel->foreach" returns.
114
115 list = $tree_model->get ($iter, ...)
116
117 * $iter (Gtk2::TreeIter)
118 * ... (list) of column indices
119
120 Fetch and return the model's values in the row pointed to by $iter.
121 If you specify no column indices, it returns the values for all of
122 the columns, otherwise, returns just those columns' values (in
123 order).
124
125 This overrides overrides Glib::Object's "get", so you'll want to
126 use "$object->get_property" to get object properties.
127
128 treeiter = $tree_model->iter_children ($parent)
129
130 * $parent (Gtk2::TreeIter or undef)
131
132 Returns undef if $parent has no children, otherwise, returns a new
133 iter to the first child of $parent. $parent is unaltered. If
134 $parent is undef, this is equivalent to
135 "Gtk2::TreeModel::get_iter_first".
136
137 treeiter = $tree_model->get_iter_first
138
139 Return a new iter pointing to the first node in the tree (the one
140 at path "0"), or undef if the tree is empty.
141
142 treeiter = $tree_model->get_iter_from_string ($path_string)
143
144 * $path_string (string)
145
146 Returns a new iter pointing to the node described by $path_string,
147 or undef if the path does not exist.
148
149 treeiter = $tree_model->get_iter ($path)
150
151 * $path (Gtk2::TreePath)
152
153 Returns a new Gtk2::TreeIter corresponding to $path.
154
155 boolean = $tree_model->iter_has_child ($iter)
156
157 * $iter (Gtk2::TreeIter)
158
159 Returns true if $iter has child nodes.
160
161 integer = $tree_model->iter_n_children ($iter=undef)
162
163 * $iter (Gtk2::TreeIter or undef)
164
165 Returns the number of children $iter has. If $iter is undef (or
166 omitted) then returns the number of toplevel nodes.
167
168 treeiter = $tree_model->iter_next ($iter)
169
170 * $iter (Gtk2::TreeIter)
171
172 Return a new iter pointing to node following $iter at the current
173 level, or undef if there is no next node. $iter is unaltered.
174 (Note: this is different from the C version, which modifies the
175 iter.)
176
177 treeiter = $tree_model->iter_nth_child ($parent, $n)
178
179 * $parent (Gtk2::TreeIter or undef)
180 * $n (integer)
181
182 Returns an iter to the child of $parent at index $n, or undef if
183 there is no such child. $parent is unaltered.
184
185 treeiter = $tree_model->iter_parent ($child)
186
187 * $child (Gtk2::TreeIter)
188
189 Returns a new iter pointing to $child's parent node, or undef if
190 $child doesn't have a parent. $child is unaltered.
191
192 integer = $tree_model->get_n_columns
193
194 treepath = $tree_model->get_path ($iter)
195
196 * $iter (Gtk2::TreeIter)
197
198 Return a new Gtk2::TreePath corresponding to $iter.
199
200 $tree_model->ref_node ($iter)
201
202 * $iter (Gtk2::TreeIter)
203
204 Lets the tree ref the node. This is an optional method for models
205 to implement. To be more specific, models may ignore this call as
206 it exists primarily for performance reasons.
207
208 This function is primarily meant as a way for views to let caching
209 model know when nodes are being displayed (and hence, whether or
210 not to cache that node.) For example, a file-system based model
211 would not want to keep the entire file-hierarchy in memory, just
212 the sections that are currently being displayed by every current
213 view.
214
215 A model should be expected to be able to get an iter independent of
216 its reffed state.
217
218 $tree_model->row_changed ($path, $iter)
219
220 * $path (Gtk2::TreePath)
221 * $iter (Gtk2::TreeIter)
222
223 Emits the "row_changed" signal on $tree_model.
224
225 $tree_model->row_deleted ($path)
226
227 * $path (Gtk2::TreePath)
228
229 Emits the "row_deleted" signal on $tree_model. This should be
230 called by models after a row has been removed. The location
231 pointed to by $path should be the removed row's old location. It
232 may not be a valid location anymore.
233
234 $tree_model->row_has_child_toggled ($path, $iter)
235
236 * $path (Gtk2::TreePath)
237 * $iter (Gtk2::TreeIter)
238
239 Emits the "row_has_child_toggled" signal on $tree_model. This
240 should be called by models after the child state of a node changes.
241
242 $tree_model->row_inserted ($path, $iter)
243
244 * $path (Gtk2::TreePath)
245 * $iter (Gtk2::TreeIter)
246
247 Emits the "row_inserted" signal on $tree_model.
248
249 $tree_model->rows_reordered ($path, $iter, ...)
250
251 * $path (Gtk2::TreePath) the tree node whose children have been
252 reordered
253 * $iter (Gtk2::TreeIter or undef) the tree node whose children have
254 been reordered
255 * ... (list) array of integers mapping the current position of each
256 child to its old position before the re-ordering, i.e.
257 $new_order[$newpos] = $oldpos. There should be as many elements in
258 this list as there are rows in $tree_model.
259
260 Emits the "rows-reordered" signal on $tree_model/ This should be
261 called by models with their rows have been reordered.
262
263 string = $tree_model->get_string_from_iter ($iter)
264
265 * $iter (Gtk2::TreeIter)
266
267 Generates a string representation of the iter. This string is a
268 ':' separated list of numbers. For example, "4:10:0:3" would be an
269 acceptable return value for this string.
270
271 $tree_model->unref_node ($iter)
272
273 * $iter (Gtk2::TreeIter)
274
275 Lets the tree unref the node. This is an optional method for models
276 to implement. To be more specific, models may ignore this call as
277 it exists primarily for performance reasons.
278
279 For more information on what this means, see
280 "Gtk2::TreeModel::ref_node". Please note that nodes that are
281 deleted are not unreffed.
282
283 list = $tree_model->get_value ($iter, ...)
284
285 * $iter (Gtk2::TreeIter)
286 * ... (list) of column indices
287
288 Alias for get.
289
291 GTK+ provides two model implementations, Gtk2::TreeStore and
292 Gtk2::ListStore, which should be sufficient in most cases. For some
293 cases, however, it is advantageous to provide a custom tree model
294 implementation. It is possible to create custom tree models in Perl,
295 because we're cool like that.
296
297 To do this, you create a Glib::Object derivative which implements the
298 Gtk2::TreeModel interface; this is gtk2-perl-speak for "you have to add
299 a special key when you register your object type." For example:
300
301 package MyModel;
302 use Gtk2;
303 use Glib::Object::Subclass
304 Glib::Object::,
305 interfaces => [ Gtk2::TreeModel:: ],
306 ;
307
308 This will cause perl to call several virtual methods with
309 ALL_CAPS_NAMES when Gtk+ attempts to perform certain actions on the
310 model. You simply provide (or override) those methods.
311
312 TREE ITERS
313
314 Gtk2::TreeIter is normally an opaque object, but on the implementation
315 side of a Gtk2::TreeModel, you have to define what's inside. The vir‐
316 tual methods described below deal with iters as a reference to an array
317 containing four values:
318
319 o stamp (integer)
320 A number unique to this model.
321
322 o user_data (integer)
323 An arbitrary integer value.
324
325 o user_data2 (scalar)
326 An arbitrary scalar. Will not persist. May be undef.
327
328 o user_data3 (scalar)
329 An arbitrary scalar. Will not persist. May be undef.
330
331 VIRTUAL METHODS
332
333 An implementation of
334
335 treemodelflags = GET_FLAGS ($model)
336 integer = GET_N_COLUMNS ($model)
337 string = GET_COLUMN_TYPE ($model, $index)
338 ARRAYREF = GET_ITER ($model, $path)
339 See above for a description of what goes in the returned array ref‐
340 erence.
341
342 treepath = GET_PATH ($model, ARRAYREF)
343 scalar = GET_VALUE ($model, ARRAYREF, $column)
344 Implements $treemodel->get().
345
346 ARRAYREF = ITER_NEXT ($model, ARRAYREF)
347 ARRAYREF = ITER_CHILDREN ($model, ARRAYREF)
348 boolean = ITER_HAS_CHILD ($model, ARRAYREF)
349 integer = ITER_N_CHILDREN ($model, ARRAYREF)
350 ARRAYREF = ITER_NTH_CHILD ($model, ARRAYREF, $n)
351 ARRAYREF = ITER_PARENT ($model, ARRAYREF)
352 REF_NODE ($model, ARRAYREF)
353 Optional.
354
355 UNREF_NODE ($model, ARRAYREF)
356 Optional.
357
359 row-changed (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter)
360 row-inserted (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter)
361 row-has-child-toggled (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter)
362 row-deleted (Gtk2::TreeModel, Gtk2::TreePath)
363 rows-reordered (Gtk2::TreeModel, Gtk2::TreePath, Gtk2::TreeIter,
364 gpointer)
365
367 flags Gtk2::TreeModelFlags
368
369 * 'iters-persist' / 'GTK_TREE_MODEL_ITERS_PERSIST'
370 * 'list-only' / 'GTK_TREE_MODEL_LIST_ONLY'
371
373 Gtk2, Glib::Object::_Unregistered::GInterface
374
376 Copyright (C) 2003-2007 by the gtk2-perl team.
377
378 This software is licensed under the LGPL. See Gtk2 for a full notice.
379
380
381
382perl v5.8.8 2007-03-18 Gtk2::TreeModel(3)