]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/tmpl/gtktreemodel.sgml
Replace homegrown "nbsp", "hash" and "percent" entities by standard ISO
[~andy/gtk] / docs / reference / gtk / tmpl / gtktreemodel.sgml
1 <!-- ##### SECTION Title ##### -->
2 GtkTreeModel
3
4 <!-- ##### SECTION Short_Description ##### -->
5 The tree interface used by #GtkTreeView
6
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
9 The #GtkTreeModel interface defines a generic tree interface for use by
10 the #GtkTreeView widget.  It is an abstract interface, and is designed
11 to be usable with any appropriate data structure.  The programmer just
12 has to implement this interface on their own data type for it to be
13 viewable by a #GtkTreeView widget.
14 </para>
15
16 <para>
17 The model is represented as a hierarchical tree of strongly-typed,
18 columned data.  In other words, the model can be seen as a tree where
19 every node has different values depending on which column is being
20 queried.  The type of data found in a column is determined by using the
21 GType system (ie. #G_TYPE_INT, #GTK_TYPE_BUTTON, #G_TYPE_POINTER, etc.).
22 The types are homogeneous per column across all nodes.  It is important
23 to note that this interface only provides a way of examining a model and
24 observing changes.  The implementation of each individual model decides
25 how and if changes are made.
26 </para>
27
28 <para>
29 In order to make life simpler for programmers who do not need to write
30 their own specialized model, two generic models are provided &mdash; the
31 #GtkTreeStore and the #GtkListStore.  To use these, the developer simply
32 pushes data into these models as necessary.  These models provide the
33 data structure as well as all appropriate tree interfaces.  As a result,
34 implementing drag and drop, sorting, and storing data is trivial.  For
35 the vast majority of trees and lists, these two models are sufficient.
36 </para>
37
38 <para>
39 Models are accessed on a node/column level of granularity.  One can
40 query for the value of a model at a certain node and a certain column
41 on that node.  There are two structures used to reference a particular
42 node in a model.  They are the #GtkTreePath and the #GtkTreeIter
43 <footnote>
44 <para>
45 Here, <abbrev>iter</abbrev> is short for <quote>iterator</quote>
46 </para>
47 </footnote>
48 Most of the interface consists of operations on a #GtkTreeIter.
49 </para>
50
51 <para>
52 A path is essentially a potential node.  It is a location on a model
53 that may or may not actually correspond to a node on a specific model.
54 The #GtkTreePath struct can be converted into either an array of
55 unsigned integers or a string.  The string form is a list of numbers
56 separated by a colon.  Each number refers to the offset at that level.
57 Thus, the path <quote>0</quote> refers to the root node and the path
58 <quote>2:4</quote> refers to the fifth child of the third node.
59 </para>
60
61 <para>
62 By contrast, a #GtkTreeIter is a reference to a specific node on a
63 specific model.  It is a generic struct with an integer and three
64 generic pointers.  These are filled in by the model in a model-specific
65 way.  One can convert a path to an iterator by calling
66 gtk_tree_model_get_iter().  These iterators are the primary way of
67 accessing a model and are similar to the iterators used by
68 #GtkTextBuffer.  They are generally statically allocated on the heap and
69 only used for a short time.  The model interface defines a set of
70 operations using them for navigating the model.
71 </para>
72
73 <para>
74 It is expected that models fill in the iterator with private data.  For
75 example, the #GtkListStore model, which is internally a simple linked
76 list, stores a list node in one of the pointers.  The #GtkTreeModelSort
77 stores an array and an offset in two of the pointers.  Additionally,
78 there is an integer field.  This field is generally filled with a unique
79 stamp per model.  This stamp is for catching errors resulting from using
80 invalid iterators with a model.
81 </para>
82
83 <para>
84 The lifecycle of an iterator can be a little confusing at first.
85 Iterators are expected to always be valid for as long as the model is
86 unchanged (and doesn't emit a signal).  The model is considered to own
87 all outstanding iterators and nothing needs to be done to free them from
88 the user's point of view.  Additionally, some models guarantee that an
89 iterator is valid for as long as the node it refers to is valid (most
90 notably the #GtkTreeStore and #GtkListStore).  Although generally
91 uninteresting, as one always has to allow for the case where iterators
92 do not persist beyond a signal, some very important performance
93 enhancements were made in the sort model.  As a result, the
94 #GTK_TREE_MODEL_ITERS_PERSIST flag was added to indicate this behavior.
95 </para>
96
97 <para>
98 To help show some common operation of a model, some examples are
99 provided.  The first example shows three ways of getting the iter at the
100 location <quote>3:2:5</quote>.  While the first method shown is easier,
101 the second is much more common, as you often get paths from callbacks.
102 </para>
103 <para>
104 <example>
105 <title>Acquiring a <structname>GtkTreeIter</structname></title>
106 <programlisting>
107 /* Three ways of getting the iter pointing to the location
108  */
109 {
110   GtkTreePath *path;
111   GtkTreeIter iter;
112   GtkTreeIter parent_iter;
113
114   /* get the iterator from a string */
115   gtk_tree_model_get_iter_from_string (model, &amp;iter, "3:2:5");
116
117   /* get the iterator from a path */
118   path = gtk_tree_path_new_from_string ("3:2:5");
119   gtk_tree_model_get_iter (model, &amp;iter, path);
120   gtk_tree_path_free (path);
121
122
123   /* walk the tree to find the iterator */
124   gtk_tree_model_get_nth_child (model, &amp;iter, NULL, 3);
125   parent_iter = iter;
126   gtk_tree_model_get_nth_child (model, &amp;iter, &amp;parent_iter, 2);
127   parent_iter = iter;
128   gtk_tree_model_get_nth_child (model, &amp;iter, NULL, 5);
129 }
130 </programlisting>
131 </example>
132 </para>
133
134 <para>
135 This second example shows a quick way of iterating through a list and
136 getting a string and an integer from each row.  The
137 <function>populate_model</function> function used below is not shown, as
138 it is specific to the #GtkListStore.  For information on how to write
139 such a function, see the #GtkListStore documentation.
140 <example>
141 <title>Reading data from a <structname>GtkTreeModel</structname></title>
142 <programlisting>
143 enum
144 {
145   STRING_COLUMN,
146   INT_COLUMN,
147   N_COLUMNS
148 };
149
150 {
151   GtkTreeModel *list_store;
152   GtkTreeIter iter;
153   gboolean valid;
154   gint row_count = 0;
155
156   /* make a new list_store */
157   list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
158
159   /* Fill the list store with data */
160   populate_model (list_store);
161
162   /* Get the first iter in the list */
163   valid = gtk_tree_model_get_iter_first (model, &amp;iter);
164
165   while (valid)
166     {
167       /* Walk through the list, reading each row */
168       gchar *str_data;
169       gint   int_data;
170
171       /* Make sure you terminate calls to gtk_tree_model_get(<!-- -->)
172        * with a '-1' value
173        */
174       gtk_tree_model_get (list_store, &amp;iter, 
175                           STRING_COLUMN, &amp;str_data,
176                           INT_COLUMN, &amp;int_data,
177                           -1);
178
179       /* Do something with the data */
180       g_print ("Row &percnt;d: (&percnt;s,&percnt;d)\n", row_count, str_data, int_data);
181       g_free (str_data);
182
183       row_count ++;
184       valid = gtk_tree_model_iter_next (model, &amp;iter))
185     }
186 }
187 </programlisting>
188 </example>
189 </para>
190
191 <!-- ##### SECTION See_Also ##### -->
192 <para>
193 #GtkTreeView, #GtkTreeStore, #GtkListStore, <link linkend="gtk-GtkTreeView-drag-and-drop">GtkTreeDnd</link>, #GtkTreeSortable
194 </para>
195
196 <!-- ##### STRUCT GtkTreeModel ##### -->
197 <para>
198
199 </para>
200
201
202 <!-- ##### STRUCT GtkTreeIter ##### -->
203 <para>
204 The <structname>GtkTreeIter</structname> is the primary structure for
205 accessing a structure.  Models are expected to put a unique integer in
206 the <structfield>stamp</structfield> member, and put model-specific
207 data in the three <structfield>user_data</structfield> members.
208 </para>
209
210 @stamp: A unique stamp to catch invalid iterators
211 @user_data: Model specific data
212 @user_data2: Model specific data
213 @user_data3: Model specific data
214
215 <!-- ##### STRUCT GtkTreePath ##### -->
216 <para>
217
218 </para>
219
220
221 <!-- ##### STRUCT GtkTreeRowReference ##### -->
222 <para>
223
224 </para>
225
226
227 <!-- ##### STRUCT GtkTreeModelIface ##### -->
228 <para>
229
230 </para>
231
232 @g_iface: 
233 @row_changed: 
234 @row_inserted: 
235 @row_has_child_toggled: 
236 @row_deleted: 
237 @rows_reordered: 
238 @get_flags: 
239 @get_n_columns: 
240 @get_column_type: 
241 @get_iter: 
242 @get_path: 
243 @get_value: 
244 @iter_next: 
245 @iter_children: 
246 @iter_has_child: 
247 @iter_n_children: 
248 @iter_nth_child: 
249 @iter_parent: 
250 @ref_node: 
251 @unref_node: 
252
253 <!-- ##### USER_FUNCTION GtkTreeModelForeachFunc ##### -->
254 <para>
255
256 </para>
257
258 @model: 
259 @path: 
260 @iter: 
261 @data: 
262 @Returns: 
263
264
265 <!-- ##### ENUM GtkTreeModelFlags ##### -->
266 <para>
267 These flags indicate various properties of a #GtkTreeModel.  They are
268 returned by gtk_tree_model_get_flags(), and must be static for the
269 lifetime of the object.  A more complete description of
270 #GTK_TREE_MODEL_ITERS_PERSIST can be found in the overview of this
271 section.
272 </para>
273
274 @GTK_TREE_MODEL_ITERS_PERSIST: Iterators survive all signals emitted by the tree.
275 @GTK_TREE_MODEL_LIST_ONLY: The model is a list only, and never has children
276
277 <!-- ##### FUNCTION gtk_tree_path_new ##### -->
278 <para>
279
280 </para>
281
282 @Returns: 
283
284
285 <!-- ##### FUNCTION gtk_tree_path_new_from_string ##### -->
286 <para>
287
288 </para>
289
290 @path: 
291 @Returns: 
292
293
294 <!-- ##### FUNCTION gtk_tree_path_to_string ##### -->
295 <para>
296
297 </para>
298
299 @path: 
300 @Returns: 
301
302
303 <!-- ##### FUNCTION gtk_tree_path_new_first ##### -->
304 <para>
305
306 </para>
307
308 @Returns: 
309
310
311 <!-- ##### MACRO gtk_tree_path_new_root ##### -->
312 <para>
313 A alternate name for gtk_tree_path_new_root() provided for
314 compatibility reasons; this macro will be deprecated in future
315 versions of GTK+.
316 </para>
317
318 @Returns: A new #GtkTreePath.
319
320
321 <!-- ##### FUNCTION gtk_tree_path_append_index ##### -->
322 <para>
323
324 </para>
325
326 @path: 
327 @index: 
328
329
330 <!-- ##### FUNCTION gtk_tree_path_prepend_index ##### -->
331 <para>
332
333 </para>
334
335 @path: 
336 @index: 
337
338
339 <!-- ##### FUNCTION gtk_tree_path_get_depth ##### -->
340 <para>
341
342 </para>
343
344 @path: 
345 @Returns: 
346
347
348 <!-- ##### FUNCTION gtk_tree_path_get_indices ##### -->
349 <para>
350
351 </para>
352
353 @path: 
354 @Returns: 
355
356
357 <!-- ##### FUNCTION gtk_tree_path_free ##### -->
358 <para>
359
360 </para>
361
362 @path: 
363
364
365 <!-- ##### FUNCTION gtk_tree_path_copy ##### -->
366 <para>
367
368 </para>
369
370 @path: 
371 @Returns: 
372
373
374 <!-- ##### FUNCTION gtk_tree_path_compare ##### -->
375 <para>
376
377 </para>
378
379 @a: 
380 @b: 
381 @Returns: 
382
383
384 <!-- ##### FUNCTION gtk_tree_path_next ##### -->
385 <para>
386
387 </para>
388
389 @path: 
390
391
392 <!-- ##### FUNCTION gtk_tree_path_prev ##### -->
393 <para>
394
395 </para>
396
397 @path: 
398 @Returns: 
399
400
401 <!-- ##### FUNCTION gtk_tree_path_up ##### -->
402 <para>
403
404 </para>
405
406 @path: 
407 @Returns: 
408
409
410 <!-- ##### FUNCTION gtk_tree_path_down ##### -->
411 <para>
412
413 </para>
414
415 @path: 
416
417
418 <!-- ##### FUNCTION gtk_tree_path_is_ancestor ##### -->
419 <para>
420
421 </para>
422
423 @path: 
424 @descendant: 
425 @Returns: 
426
427
428 <!-- ##### FUNCTION gtk_tree_path_is_descendant ##### -->
429 <para>
430
431 </para>
432
433 @path: 
434 @ancestor: 
435 @Returns: 
436
437
438 <!-- ##### FUNCTION gtk_tree_row_reference_new ##### -->
439 <para>
440
441 </para>
442
443 @model: 
444 @path: 
445 @Returns: 
446
447
448 <!-- ##### FUNCTION gtk_tree_row_reference_new_proxy ##### -->
449 <para>
450
451 </para>
452
453 @proxy: 
454 @model: 
455 @path: 
456 @Returns: 
457
458
459 <!-- ##### FUNCTION gtk_tree_row_reference_get_path ##### -->
460 <para>
461
462 </para>
463
464 @reference: 
465 @Returns: 
466
467
468 <!-- ##### FUNCTION gtk_tree_row_reference_valid ##### -->
469 <para>
470
471 </para>
472
473 @reference: 
474 @Returns: 
475
476
477 <!-- ##### FUNCTION gtk_tree_row_reference_free ##### -->
478 <para>
479
480 </para>
481
482 @reference: 
483
484
485 <!-- ##### FUNCTION gtk_tree_row_reference_inserted ##### -->
486 <para>
487
488 </para>
489
490 @proxy: 
491 @path: 
492
493
494 <!-- ##### FUNCTION gtk_tree_row_reference_deleted ##### -->
495 <para>
496
497 </para>
498
499 @proxy: 
500 @path: 
501
502
503 <!-- ##### FUNCTION gtk_tree_row_reference_reordered ##### -->
504 <para>
505
506 </para>
507
508 @proxy: 
509 @path: 
510 @iter: 
511 @new_order: 
512
513
514 <!-- ##### FUNCTION gtk_tree_iter_copy ##### -->
515 <para>
516
517 </para>
518
519 @iter: 
520 @Returns: 
521
522
523 <!-- ##### FUNCTION gtk_tree_iter_free ##### -->
524 <para>
525
526 </para>
527
528 @iter: 
529
530
531 <!-- ##### FUNCTION gtk_tree_model_get_flags ##### -->
532 <para>
533
534 </para>
535
536 @tree_model: 
537 @Returns: 
538
539
540 <!-- ##### FUNCTION gtk_tree_model_get_n_columns ##### -->
541 <para>
542
543 </para>
544
545 @tree_model: 
546 @Returns: 
547
548
549 <!-- ##### FUNCTION gtk_tree_model_get_column_type ##### -->
550 <para>
551
552 </para>
553
554 @tree_model: 
555 @index: 
556 @Returns: 
557
558
559 <!-- ##### FUNCTION gtk_tree_model_get_iter ##### -->
560 <para>
561
562 </para>
563
564 @tree_model: 
565 @iter: 
566 @path: 
567 @Returns: 
568
569
570 <!-- ##### FUNCTION gtk_tree_model_get_iter_from_string ##### -->
571 <para>
572
573 </para>
574
575 @tree_model: 
576 @iter: 
577 @path_string: 
578 @Returns: 
579
580
581 <!-- ##### FUNCTION gtk_tree_model_get_iter_first ##### -->
582 <para>
583
584 </para>
585
586 @tree_model: 
587 @iter: 
588 @Returns: 
589
590
591 <!-- ##### MACRO gtk_tree_model_get_iter_root ##### -->
592 <para>
593 A alternate name for gtk_tree_model_get_iter_root() provided for
594 compatibility reasons; this macro will be deprecated in future
595 versions of GTK+.
596 </para>
597
598 @tree_model:  A #GtkTreeModel.
599 @iter: uninitialized #GtkTreeIter.
600 @Returns:  %TRUE, if @iter was set.
601
602
603 <!-- ##### FUNCTION gtk_tree_model_get_path ##### -->
604 <para>
605
606 </para>
607
608 @tree_model: 
609 @iter: 
610 @Returns: 
611
612
613 <!-- ##### FUNCTION gtk_tree_model_get_value ##### -->
614 <para>
615
616 </para>
617
618 @tree_model: 
619 @iter: 
620 @column: 
621 @value: 
622
623
624 <!-- ##### FUNCTION gtk_tree_model_iter_next ##### -->
625 <para>
626
627 </para>
628
629 @tree_model: 
630 @iter: 
631 @Returns: 
632
633
634 <!-- ##### FUNCTION gtk_tree_model_iter_children ##### -->
635 <para>
636
637 </para>
638
639 @tree_model: 
640 @iter: 
641 @parent: 
642 @Returns: 
643
644
645 <!-- ##### FUNCTION gtk_tree_model_iter_has_child ##### -->
646 <para>
647
648 </para>
649
650 @tree_model: 
651 @iter: 
652 @Returns: 
653
654
655 <!-- ##### FUNCTION gtk_tree_model_iter_n_children ##### -->
656 <para>
657
658 </para>
659
660 @tree_model: 
661 @iter: 
662 @Returns: 
663
664
665 <!-- ##### FUNCTION gtk_tree_model_iter_nth_child ##### -->
666 <para>
667
668 </para>
669
670 @tree_model: 
671 @iter: 
672 @parent: 
673 @n: 
674 @Returns: 
675
676
677 <!-- ##### FUNCTION gtk_tree_model_iter_parent ##### -->
678 <para>
679
680 </para>
681
682 @tree_model: 
683 @iter: 
684 @child: 
685 @Returns: 
686
687
688 <!-- ##### FUNCTION gtk_tree_model_ref_node ##### -->
689 <para>
690
691 </para>
692
693 @tree_model: 
694 @iter: 
695
696
697 <!-- ##### FUNCTION gtk_tree_model_unref_node ##### -->
698 <para>
699
700 </para>
701
702 @tree_model: 
703 @iter: 
704
705
706 <!-- ##### FUNCTION gtk_tree_model_get ##### -->
707 <para>
708
709 </para>
710
711 @tree_model: 
712 @iter: 
713 @Varargs: 
714
715
716 <!-- ##### FUNCTION gtk_tree_model_get_valist ##### -->
717 <para>
718
719 </para>
720
721 @tree_model: 
722 @iter: 
723 @var_args: 
724
725
726 <!-- ##### FUNCTION gtk_tree_model_foreach ##### -->
727 <para>
728
729 </para>
730
731 @model: 
732 @func: 
733 @user_data: 
734
735
736 <!-- ##### FUNCTION gtk_tree_model_row_changed ##### -->
737 <para>
738
739 </para>
740
741 @tree_model: 
742 @path: 
743 @iter: 
744 <!-- # Unused Parameters # -->
745 @start_path: 
746 @start_iter: 
747
748
749 <!-- ##### FUNCTION gtk_tree_model_row_inserted ##### -->
750 <para>
751
752 </para>
753
754 @tree_model: 
755 @path: 
756 @iter: 
757
758
759 <!-- ##### FUNCTION gtk_tree_model_row_has_child_toggled ##### -->
760 <para>
761
762 </para>
763
764 @tree_model: 
765 @path: 
766 @iter: 
767
768
769 <!-- ##### FUNCTION gtk_tree_model_row_deleted ##### -->
770 <para>
771
772 </para>
773
774 @tree_model: 
775 @path: 
776
777
778 <!-- ##### FUNCTION gtk_tree_model_rows_reordered ##### -->
779 <para>
780
781 </para>
782
783 @tree_model: 
784 @path: 
785 @iter: 
786 @new_order: 
787
788
789 <!-- ##### SIGNAL GtkTreeModel::row-changed ##### -->
790 <para>
791
792 </para>
793
794 @treemodel: the object which received the signal.
795 @arg1: 
796 @arg2: 
797
798 <!-- ##### SIGNAL GtkTreeModel::row-deleted ##### -->
799 <para>
800
801 </para>
802
803 @treemodel: the object which received the signal.
804 @arg1: 
805
806 <!-- ##### SIGNAL GtkTreeModel::row-has-child-toggled ##### -->
807 <para>
808
809 </para>
810
811 @treemodel: the object which received the signal.
812 @arg1: 
813 @arg2: 
814
815 <!-- ##### SIGNAL GtkTreeModel::row-inserted ##### -->
816 <para>
817
818 </para>
819
820 @treemodel: the object which received the signal.
821 @arg1: 
822 @arg2: 
823
824 <!-- ##### SIGNAL GtkTreeModel::rows-reordered ##### -->
825 <para>
826
827 </para>
828
829 @treemodel: the object which received the signal.
830 @arg1: 
831 @arg2: 
832 @arg3: 
833