]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/tmpl/gtktreemodel.sgml
Make 3.0 parallel-installable to 2.x
[~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 stack 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_iter_nth_child (model, &amp;iter, NULL, 3);
125   parent_iter = iter;
126   gtk_tree_model_iter_nth_child (model, &amp;iter, &amp;parent_iter, 2);
127   parent_iter = iter;
128   gtk_tree_model_iter_nth_child (model, &amp;iter, &amp;parent_iter, 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 (list_store, &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 (list_store, &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 <!-- ##### SECTION Stability_Level ##### -->
197
198
199 <!-- ##### SECTION Image ##### -->
200
201
202 <!-- ##### STRUCT GtkTreeModel ##### -->
203 <para>
204
205 </para>
206
207
208 <!-- ##### SIGNAL GtkTreeModel::row-changed ##### -->
209 <para>
210
211 </para>
212
213 @treemodel: the object which received the signal.
214 @arg1: 
215 @arg2: 
216
217 <!-- ##### SIGNAL GtkTreeModel::row-deleted ##### -->
218 <para>
219
220 </para>
221
222 @treemodel: the object which received the signal.
223 @arg1: 
224
225 <!-- ##### SIGNAL GtkTreeModel::row-has-child-toggled ##### -->
226 <para>
227
228 </para>
229
230 @treemodel: the object which received the signal.
231 @arg1: 
232 @arg2: 
233
234 <!-- ##### SIGNAL GtkTreeModel::row-inserted ##### -->
235 <para>
236
237 </para>
238
239 @treemodel: the object which received the signal.
240 @arg1: 
241 @arg2: 
242
243 <!-- ##### SIGNAL GtkTreeModel::rows-reordered ##### -->
244 <para>
245
246 </para>
247
248 @treemodel: the object which received the signal.
249 @arg1: 
250 @arg2: 
251 @arg3: 
252
253 <!-- ##### STRUCT GtkTreeIter ##### -->
254 <para>
255 The <structname>GtkTreeIter</structname> is the primary structure for
256 accessing a structure.  Models are expected to put a unique integer in
257 the <structfield>stamp</structfield> member, and put model-specific
258 data in the three <structfield>user_data</structfield> members.
259 </para>
260
261 @stamp: A unique stamp to catch invalid iterators
262 @user_data: Model specific data
263 @user_data2: Model specific data
264 @user_data3: Model specific data
265
266 <!-- ##### STRUCT GtkTreePath ##### -->
267 <para>
268
269 </para>
270
271
272 <!-- ##### STRUCT GtkTreeRowReference ##### -->
273 <para>
274
275 </para>
276
277
278 <!-- ##### STRUCT GtkTreeModelIface ##### -->
279 <para>
280
281 </para>
282
283 @g_iface: 
284 @row_changed: 
285 @row_inserted: 
286 @row_has_child_toggled: 
287 @row_deleted: 
288 @rows_reordered: 
289 @get_flags: 
290 @get_n_columns: 
291 @get_column_type: 
292 @get_iter: 
293 @get_path: 
294 @get_value: 
295 @iter_next: 
296 @iter_children: 
297 @iter_has_child: 
298 @iter_n_children: 
299 @iter_nth_child: 
300 @iter_parent: 
301 @ref_node: 
302 @unref_node: 
303
304 <!-- ##### USER_FUNCTION GtkTreeModelForeachFunc ##### -->
305 <para>
306
307 </para>
308
309 @model: The #GtkTreeModel currently being iterated
310 @path: The current #GtkTreePath
311 @iter: The current #GtkTreeIter
312 @data: The user data passed to gtk_tree_model_foreach()
313 @Returns: %TRUE to stop iterating, %FALSE to continue.
314
315
316 <!-- ##### ENUM GtkTreeModelFlags ##### -->
317 <para>
318 These flags indicate various properties of a #GtkTreeModel.  They are
319 returned by gtk_tree_model_get_flags(), and must be static for the
320 lifetime of the object.  A more complete description of
321 #GTK_TREE_MODEL_ITERS_PERSIST can be found in the overview of this
322 section.
323 </para>
324
325 @GTK_TREE_MODEL_ITERS_PERSIST: Iterators survive all signals emitted by the tree.
326 @GTK_TREE_MODEL_LIST_ONLY: The model is a list only, and never has children
327
328 <!-- ##### FUNCTION gtk_tree_path_new ##### -->
329 <para>
330
331 </para>
332
333 @void: 
334 @Returns: 
335
336
337 <!-- ##### FUNCTION gtk_tree_path_new_from_string ##### -->
338 <para>
339
340 </para>
341
342 @path: 
343 @Returns: 
344
345
346 <!-- ##### FUNCTION gtk_tree_path_new_from_indices ##### -->
347 <para>
348
349 </para>
350
351 @first_index: 
352 @Varargs: 
353 @Returns: 
354
355
356 <!-- ##### FUNCTION gtk_tree_path_to_string ##### -->
357 <para>
358
359 </para>
360
361 @path: 
362 @Returns: 
363
364
365 <!-- ##### FUNCTION gtk_tree_path_new_first ##### -->
366 <para>
367
368 </para>
369
370 @void: 
371 @Returns: 
372
373
374 <!-- ##### FUNCTION gtk_tree_path_append_index ##### -->
375 <para>
376
377 </para>
378
379 @path: 
380 @index_: 
381
382
383 <!-- ##### FUNCTION gtk_tree_path_prepend_index ##### -->
384 <para>
385
386 </para>
387
388 @path: 
389 @index_: 
390
391
392 <!-- ##### FUNCTION gtk_tree_path_get_depth ##### -->
393 <para>
394
395 </para>
396
397 @path: 
398 @Returns: 
399
400
401 <!-- ##### FUNCTION gtk_tree_path_get_indices ##### -->
402 <para>
403
404 </para>
405
406 @path: 
407 @Returns: 
408
409
410 <!-- ##### FUNCTION gtk_tree_path_get_indices_with_depth ##### -->
411 <para>
412
413 </para>
414
415 @path: 
416 @depth: 
417 @Returns: 
418
419
420 <!-- ##### FUNCTION gtk_tree_path_free ##### -->
421 <para>
422
423 </para>
424
425 @path: 
426
427
428 <!-- ##### FUNCTION gtk_tree_path_copy ##### -->
429 <para>
430
431 </para>
432
433 @path: 
434 @Returns: 
435
436
437 <!-- ##### FUNCTION gtk_tree_path_compare ##### -->
438 <para>
439
440 </para>
441
442 @a: 
443 @b: 
444 @Returns: 
445
446
447 <!-- ##### FUNCTION gtk_tree_path_next ##### -->
448 <para>
449
450 </para>
451
452 @path: 
453
454
455 <!-- ##### FUNCTION gtk_tree_path_prev ##### -->
456 <para>
457
458 </para>
459
460 @path: 
461 @Returns: 
462
463
464 <!-- ##### FUNCTION gtk_tree_path_up ##### -->
465 <para>
466
467 </para>
468
469 @path: 
470 @Returns: 
471
472
473 <!-- ##### FUNCTION gtk_tree_path_down ##### -->
474 <para>
475
476 </para>
477
478 @path: 
479
480
481 <!-- ##### FUNCTION gtk_tree_path_is_ancestor ##### -->
482 <para>
483
484 </para>
485
486 @path: 
487 @descendant: 
488 @Returns: 
489
490
491 <!-- ##### FUNCTION gtk_tree_path_is_descendant ##### -->
492 <para>
493
494 </para>
495
496 @path: 
497 @ancestor: 
498 @Returns: 
499
500
501 <!-- ##### FUNCTION gtk_tree_row_reference_new ##### -->
502 <para>
503
504 </para>
505
506 @model: 
507 @path: 
508 @Returns: 
509
510
511 <!-- ##### FUNCTION gtk_tree_row_reference_new_proxy ##### -->
512 <para>
513
514 </para>
515
516 @proxy: 
517 @model: 
518 @path: 
519 @Returns: 
520
521
522 <!-- ##### FUNCTION gtk_tree_row_reference_get_model ##### -->
523 <para>
524
525 </para>
526
527 @reference: 
528 @Returns: 
529
530
531 <!-- ##### FUNCTION gtk_tree_row_reference_get_path ##### -->
532 <para>
533
534 </para>
535
536 @reference: 
537 @Returns: 
538
539
540 <!-- ##### FUNCTION gtk_tree_row_reference_valid ##### -->
541 <para>
542
543 </para>
544
545 @reference: 
546 @Returns: 
547
548
549 <!-- ##### FUNCTION gtk_tree_row_reference_free ##### -->
550 <para>
551
552 </para>
553
554 @reference: 
555
556
557 <!-- ##### FUNCTION gtk_tree_row_reference_copy ##### -->
558 <para>
559
560 </para>
561
562 @reference: 
563 @Returns: 
564
565
566 <!-- ##### FUNCTION gtk_tree_row_reference_inserted ##### -->
567 <para>
568
569 </para>
570
571 @proxy: 
572 @path: 
573
574
575 <!-- ##### FUNCTION gtk_tree_row_reference_deleted ##### -->
576 <para>
577
578 </para>
579
580 @proxy: 
581 @path: 
582
583
584 <!-- ##### FUNCTION gtk_tree_row_reference_reordered ##### -->
585 <para>
586
587 </para>
588
589 @proxy: 
590 @path: 
591 @iter: 
592 @new_order: 
593
594
595 <!-- ##### FUNCTION gtk_tree_iter_copy ##### -->
596 <para>
597
598 </para>
599
600 @iter: 
601 @Returns: 
602
603
604 <!-- ##### FUNCTION gtk_tree_iter_free ##### -->
605 <para>
606
607 </para>
608
609 @iter: 
610
611
612 <!-- ##### FUNCTION gtk_tree_model_get_flags ##### -->
613 <para>
614
615 </para>
616
617 @tree_model: 
618 @Returns: 
619
620
621 <!-- ##### FUNCTION gtk_tree_model_get_n_columns ##### -->
622 <para>
623
624 </para>
625
626 @tree_model: 
627 @Returns: 
628
629
630 <!-- ##### FUNCTION gtk_tree_model_get_column_type ##### -->
631 <para>
632
633 </para>
634
635 @tree_model: 
636 @index_: 
637 @Returns: 
638
639
640 <!-- ##### FUNCTION gtk_tree_model_get_iter ##### -->
641 <para>
642
643 </para>
644
645 @tree_model: 
646 @iter: 
647 @path: 
648 @Returns: 
649
650
651 <!-- ##### FUNCTION gtk_tree_model_get_iter_from_string ##### -->
652 <para>
653
654 </para>
655
656 @tree_model: 
657 @iter: 
658 @path_string: 
659 @Returns: 
660
661
662 <!-- ##### FUNCTION gtk_tree_model_get_iter_first ##### -->
663 <para>
664
665 </para>
666
667 @tree_model: 
668 @iter: 
669 @Returns: 
670
671
672 <!-- ##### FUNCTION gtk_tree_model_get_path ##### -->
673 <para>
674
675 </para>
676
677 @tree_model: 
678 @iter: 
679 @Returns: 
680
681
682 <!-- ##### FUNCTION gtk_tree_model_get_value ##### -->
683 <para>
684
685 </para>
686
687 @tree_model: 
688 @iter: 
689 @column: 
690 @value: 
691
692
693 <!-- ##### FUNCTION gtk_tree_model_iter_next ##### -->
694 <para>
695
696 </para>
697
698 @tree_model: 
699 @iter: 
700 @Returns: 
701
702
703 <!-- ##### FUNCTION gtk_tree_model_iter_children ##### -->
704 <para>
705
706 </para>
707
708 @tree_model: 
709 @iter: 
710 @parent: 
711 @Returns: 
712
713
714 <!-- ##### FUNCTION gtk_tree_model_iter_has_child ##### -->
715 <para>
716
717 </para>
718
719 @tree_model: 
720 @iter: 
721 @Returns: 
722
723
724 <!-- ##### FUNCTION gtk_tree_model_iter_n_children ##### -->
725 <para>
726
727 </para>
728
729 @tree_model: 
730 @iter: 
731 @Returns: 
732
733
734 <!-- ##### FUNCTION gtk_tree_model_iter_nth_child ##### -->
735 <para>
736
737 </para>
738
739 @tree_model: 
740 @iter: 
741 @parent: 
742 @n: 
743 @Returns: 
744
745
746 <!-- ##### FUNCTION gtk_tree_model_iter_parent ##### -->
747 <para>
748
749 </para>
750
751 @tree_model: 
752 @iter: 
753 @child: 
754 @Returns: 
755
756
757 <!-- ##### FUNCTION gtk_tree_model_get_string_from_iter ##### -->
758 <para>
759
760 </para>
761
762 @tree_model: 
763 @iter: 
764 @Returns: 
765
766
767 <!-- ##### FUNCTION gtk_tree_model_ref_node ##### -->
768 <para>
769
770 </para>
771
772 @tree_model: 
773 @iter: 
774
775
776 <!-- ##### FUNCTION gtk_tree_model_unref_node ##### -->
777 <para>
778
779 </para>
780
781 @tree_model: 
782 @iter: 
783
784
785 <!-- ##### FUNCTION gtk_tree_model_get ##### -->
786 <para>
787
788 </para>
789
790 @tree_model: 
791 @iter: 
792 @Varargs: 
793
794
795 <!-- ##### FUNCTION gtk_tree_model_get_valist ##### -->
796 <para>
797
798 </para>
799
800 @tree_model: 
801 @iter: 
802 @var_args: 
803
804
805 <!-- ##### FUNCTION gtk_tree_model_foreach ##### -->
806 <para>
807
808 </para>
809
810 @model: 
811 @func: 
812 @user_data: 
813
814
815 <!-- ##### FUNCTION gtk_tree_model_row_changed ##### -->
816 <para>
817
818 </para>
819
820 @tree_model: 
821 @path: 
822 @iter: 
823
824
825 <!-- ##### FUNCTION gtk_tree_model_row_inserted ##### -->
826 <para>
827
828 </para>
829
830 @tree_model: 
831 @path: 
832 @iter: 
833
834
835 <!-- ##### FUNCTION gtk_tree_model_row_has_child_toggled ##### -->
836 <para>
837
838 </para>
839
840 @tree_model: 
841 @path: 
842 @iter: 
843
844
845 <!-- ##### FUNCTION gtk_tree_model_row_deleted ##### -->
846 <para>
847
848 </para>
849
850 @tree_model: 
851 @path: 
852
853
854 <!-- ##### FUNCTION gtk_tree_model_rows_reordered ##### -->
855 <para>
856
857 </para>
858
859 @tree_model: 
860 @path: 
861 @iter: 
862 @new_order: 
863
864