]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/tmpl/gtkliststore.sgml
Make 3.0 parallel-installable to 2.x
[~andy/gtk] / docs / reference / gtk / tmpl / gtkliststore.sgml
1 <!-- ##### SECTION Title ##### -->
2 GtkListStore
3
4 <!-- ##### SECTION Short_Description ##### -->
5 A list-like data structure that can be used with the GtkTreeView
6
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
9 The #GtkListStore object is a list model for use with a #GtkTreeView
10 widget.  It implements the #GtkTreeModel interface, and consequentialy,
11 can use all of the methods available there.  It also implements the
12 #GtkTreeSortable interface so it can be sorted by the view.
13 Finally, it also implements the tree <link linkend="gtktreednd">drag and
14 drop</link> interfaces.
15 </para>
16
17 <para>
18 The #GtkListStore can accept most GObject types as a column type, though
19 it can't accept all custom types.  Internally, it will keep a copy of
20 data passed in (such as a string or a boxed pointer).  Columns that
21 accept #GObject<!-- -->s are handled a little differently.  The
22 #GtkListStore will keep a reference to the object instead of copying the
23 value.  As a result, if the object is modified, it is up to the
24 application writer to call @gtk_tree_model_row_changed to emit the
25 "row_changed" signal.  This most commonly affects lists with
26 #GdkPixbuf<!-- -->s stored.
27 </para>
28
29 <example>
30 <title>Creating a simple list store.</title>
31 <programlisting>
32 enum {
33   COLUMN_STRING,
34   COLUMN_INT,
35   COLUMN_BOOLEAN,
36   N_COLUMNS
37 };
38
39 {
40   GtkListStore *list_store;
41   GtkTreePath *path;
42   GtkTreeIter iter;
43   gint i;
44
45   list_store = gtk_list_store_new (N_COLUMNS,
46                                    G_TYPE_STRING,
47                                    G_TYPE_INT,
48                                    G_TYPE_BOOLEAN);
49
50   for (i = 0; i &lt; 10; i++)
51     {
52       gchar *some_data;
53
54       some_data = get_some_data (i);
55
56       /* Add a new row to the model */
57       gtk_list_store_append (list_store, &amp;iter);
58       gtk_list_store_set (list_store, &amp;iter,
59                           COLUMN_STRING, some_data,
60                           COLUMN_INT, i,
61                           COLUMN_BOOLEAN,  FALSE,
62                           -1);
63
64       /* As the store will keep a copy of the string internally, we
65        * free some_data.
66        */
67       g_free (some_data);
68     }
69
70   /* Modify a particular row */
71   path = gtk_tree_path_new_from_string ("4");
72   gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store),
73                            &amp;iter,
74                            path);
75   gtk_tree_path_free (path);
76   gtk_list_store_set (list_store, &amp;iter,
77                       COLUMN_BOOLEAN, TRUE,
78                       -1);
79 }
80 </programlisting>
81 </example>
82
83 <refsect2>
84 <title>Performance Considerations</title>
85 <para>
86 Internally, the #GtkListStore was implemented with a linked list with a
87 tail pointer prior to GTK+ 2.6.  As a result, it was fast at data
88 insertion and deletion, and not fast at random data access.  The
89 #GtkListStore sets the #GTK_TREE_MODEL_ITERS_PERSIST flag, which means
90 that #GtkTreeIter<!-- -->s can be cached while the row exists.  Thus, if
91 access to a particular row is needed often and your code is expected to
92 run on older versions of GTK+, it is worth keeping the iter around.
93 </para>
94 <title>Atomic Operations</title>
95 <para>
96 It is important to note that only the methods 
97 gtk_list_store_insert_with_values() and gtk_list_store_insert_with_valuesv() 
98 are atomic, in the sense that the row is being appended to the store and the 
99 values filled in in a single operation with regard to #GtkTreeModel signaling.
100 In contrast, using e.g. gtk_list_store_append() and then gtk_list_store_set() 
101 will first create a row, which triggers the #GtkTreeModel::row-inserted signal 
102 on #GtkListStore. The row, however, is still empty, and any signal handler 
103 connecting to "row-inserted" on this particular store should be prepared
104 for the situation that the row might be empty. This is especially important 
105 if you are wrapping the #GtkListStore inside a #GtkTreeModelFilter and are
106 using a #GtkTreeModelFilterVisibleFunc. Using any of the non-atomic operations 
107 to append rows to the #GtkListStore will cause the 
108 #GtkTreeModelFilterVisibleFunc to be visited with an empty row first; the 
109 function must be prepared for that.
110 </para>
111 </refsect2>
112 <refsect2 id="GtkListStore-BUILDER-UI">
113 <title>GtkListStore as GtkBuildable</title>
114 <para>
115 The GtkListStore implementation of the GtkBuildable interface allows
116 to specify the model columns with a &lt;columns&gt; element that may
117 contain multiple &lt;column&gt; elements, each specifying one model
118 column. The "type" attribute specifies the data type for the column.
119 </para>
120 <para>
121 Additionally, it is possible to specify content for the list store
122 in the UI definition, with the &lt;data&gt; element. It can contain
123 multiple &lt;row&gt; elements, each specifying to content for one
124 row of the list model. Inside a &lt;row&gt;, the &lt;col&gt; elements
125 specify the content for individual cells.
126 </para>
127 <para>
128 Note that it is probably more common to define your models 
129 in the code, and one might consider it a layering violation 
130 to specify the content of a list store in a UI definition,
131 <emphasis>data</emphasis>, not <emphasis>presentation</emphasis>, 
132 and common wisdom is to separate the two, as far as possible. 
133 <!-- FIXME a bit inconclusive -->
134 </para>
135 <example>
136 <title>A UI Definition fragment for a list store</title>
137 <programlisting><![CDATA[
138 <object class="GtkListStore">
139   <columns>
140     <column type="gchararray"/>
141     <column type="gchararray"/>
142     <column type="gint"/>
143   </columns>
144   <data>
145     <row>
146       <col id="0">John</col>
147       <col id="1">Doe</col>
148       <col id="2">25</col>
149     </row>
150     <row>
151       <col id="0">Johan</col>
152       <col id="1">Dahlin</col>
153       <col id="2">50</col>
154     </row>
155   </data>
156 </object>
157 ]]></programlisting>
158 </example>
159 </refsect2>
160
161 <!-- ##### SECTION See_Also ##### -->
162 <para>
163 #GtkTreeModel, #GtkTreeStore
164 </para>
165
166 <!-- ##### SECTION Stability_Level ##### -->
167
168
169 <!-- ##### SECTION Image ##### -->
170
171
172 <!-- ##### STRUCT GtkListStore ##### -->
173 <para>
174
175 </para>
176
177
178 <!-- ##### FUNCTION gtk_list_store_new ##### -->
179 <para>
180
181 </para>
182
183 @n_columns: 
184 @Varargs: 
185 @Returns: 
186
187
188 <!-- ##### FUNCTION gtk_list_store_newv ##### -->
189 <para>
190
191 </para>
192
193 @n_columns: 
194 @types: 
195 @Returns: 
196
197
198 <!-- ##### FUNCTION gtk_list_store_set_column_types ##### -->
199 <para>
200
201 </para>
202
203 @list_store: 
204 @n_columns: 
205 @types: 
206
207
208 <!-- ##### FUNCTION gtk_list_store_set ##### -->
209 <para>
210
211 </para>
212
213 @list_store: 
214 @iter: 
215 @Varargs: 
216
217
218 <!-- ##### FUNCTION gtk_list_store_set_valist ##### -->
219 <para>
220
221 </para>
222
223 @list_store: 
224 @iter: 
225 @var_args: 
226
227
228 <!-- ##### FUNCTION gtk_list_store_set_value ##### -->
229 <para>
230
231 </para>
232
233 @list_store: 
234 @iter: 
235 @column: 
236 @value: 
237
238
239 <!-- ##### FUNCTION gtk_list_store_set_valuesv ##### -->
240 <para>
241
242 </para>
243
244 @list_store: 
245 @iter: 
246 @columns: 
247 @values: 
248 @n_values: 
249
250
251 <!-- ##### FUNCTION gtk_list_store_remove ##### -->
252 <para>
253
254 </para>
255
256 @list_store: 
257 @iter: 
258 @Returns: 
259
260
261 <!-- ##### FUNCTION gtk_list_store_insert ##### -->
262 <para>
263
264 </para>
265
266 @list_store: 
267 @iter: 
268 @position: 
269
270
271 <!-- ##### FUNCTION gtk_list_store_insert_before ##### -->
272 <para>
273
274 </para>
275
276 @list_store: 
277 @iter: 
278 @sibling: 
279
280
281 <!-- ##### FUNCTION gtk_list_store_insert_after ##### -->
282 <para>
283
284 </para>
285
286 @list_store: 
287 @iter: 
288 @sibling: 
289
290
291 <!-- ##### FUNCTION gtk_list_store_insert_with_values ##### -->
292 <para>
293
294 </para>
295
296 @list_store: 
297 @iter: 
298 @position: 
299 @Varargs: 
300
301
302 <!-- ##### FUNCTION gtk_list_store_insert_with_valuesv ##### -->
303 <para>
304
305 </para>
306
307 @list_store: 
308 @iter: 
309 @position: 
310 @columns: 
311 @values: 
312 @n_values: 
313
314
315 <!-- ##### FUNCTION gtk_list_store_prepend ##### -->
316 <para>
317
318 </para>
319
320 @list_store: 
321 @iter: 
322
323
324 <!-- ##### FUNCTION gtk_list_store_append ##### -->
325 <para>
326
327 </para>
328
329 @list_store: 
330 @iter: 
331
332
333 <!-- ##### FUNCTION gtk_list_store_clear ##### -->
334 <para>
335
336 </para>
337
338 @list_store: 
339
340
341 <!-- ##### FUNCTION gtk_list_store_iter_is_valid ##### -->
342 <para>
343
344 </para>
345
346 @list_store: 
347 @iter: 
348 @Returns: 
349
350
351 <!-- ##### FUNCTION gtk_list_store_reorder ##### -->
352 <para>
353
354 </para>
355
356 @store: 
357 @new_order: 
358
359
360 <!-- ##### FUNCTION gtk_list_store_swap ##### -->
361 <para>
362
363 </para>
364
365 @store: 
366 @a: 
367 @b: 
368
369
370 <!-- ##### FUNCTION gtk_list_store_move_before ##### -->
371 <para>
372
373 </para>
374
375 @store: 
376 @iter: 
377 @position: 
378
379
380 <!-- ##### FUNCTION gtk_list_store_move_after ##### -->
381 <para>
382
383 </para>
384
385 @store: 
386 @iter: 
387 @position: 
388
389