1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.ListStore;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gobject.Value;
30 private import gtk.BuildableIF;
31 private import gtk.BuildableT;
32 private import gtk.TreeDragDestIF;
33 private import gtk.TreeDragDestT;
34 private import gtk.TreeDragSourceIF;
35 private import gtk.TreeDragSourceT;
36 private import gtk.TreeIter;
37 private import gtk.TreeModelIF;
38 private import gtk.TreeModelT;
39 private import gtk.TreeSortableIF;
40 private import gtk.TreeSortableT;
41 private import gtk.c.functions;
42 public  import gtk.c.types;
43 public  import gtkc.gtktypes;
44 
45 
46 /**
47  * The #GtkListStore object is a list model for use with a #GtkTreeView
48  * widget.  It implements the #GtkTreeModel interface, and consequentialy,
49  * can use all of the methods available there.  It also implements the
50  * #GtkTreeSortable interface so it can be sorted by the view.
51  * Finally, it also implements the tree
52  * [drag and drop][gtk3-GtkTreeView-drag-and-drop]
53  * interfaces.
54  * 
55  * The #GtkListStore can accept most GObject types as a column type, though
56  * it can’t accept all custom types.  Internally, it will keep a copy of
57  * data passed in (such as a string or a boxed pointer).  Columns that
58  * accept #GObjects are handled a little differently.  The
59  * #GtkListStore will keep a reference to the object instead of copying the
60  * value.  As a result, if the object is modified, it is up to the
61  * application writer to call gtk_tree_model_row_changed() to emit the
62  * #GtkTreeModel::row_changed signal.  This most commonly affects lists with
63  * #GdkPixbufs stored.
64  * 
65  * An example for creating a simple list store:
66  * |[<!-- language="C" -->
67  * enum {
68  * COLUMN_STRING,
69  * COLUMN_INT,
70  * COLUMN_BOOLEAN,
71  * N_COLUMNS
72  * };
73  * 
74  * {
75  * GtkListStore *list_store;
76  * GtkTreePath *path;
77  * GtkTreeIter iter;
78  * gint i;
79  * 
80  * list_store = gtk_list_store_new (N_COLUMNS,
81  * G_TYPE_STRING,
82  * G_TYPE_INT,
83  * G_TYPE_BOOLEAN);
84  * 
85  * for (i = 0; i < 10; i++)
86  * {
87  * gchar *some_data;
88  * 
89  * some_data = get_some_data (i);
90  * 
91  * // Add a new row to the model
92  * gtk_list_store_append (list_store, &iter);
93  * gtk_list_store_set (list_store, &iter,
94  * COLUMN_STRING, some_data,
95  * COLUMN_INT, i,
96  * COLUMN_BOOLEAN,  FALSE,
97  * -1);
98  * 
99  * // As the store will keep a copy of the string internally,
100  * // we free some_data.
101  * g_free (some_data);
102  * }
103  * 
104  * // Modify a particular row
105  * path = gtk_tree_path_new_from_string ("4");
106  * gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store),
107  * &iter,
108  * path);
109  * gtk_tree_path_free (path);
110  * gtk_list_store_set (list_store, &iter,
111  * COLUMN_BOOLEAN, TRUE,
112  * -1);
113  * }
114  * ]|
115  * 
116  * # Performance Considerations
117  * 
118  * Internally, the #GtkListStore was implemented with a linked list with
119  * a tail pointer prior to GTK+ 2.6.  As a result, it was fast at data
120  * insertion and deletion, and not fast at random data access.  The
121  * #GtkListStore sets the #GTK_TREE_MODEL_ITERS_PERSIST flag, which means
122  * that #GtkTreeIters can be cached while the row exists.  Thus, if
123  * access to a particular row is needed often and your code is expected to
124  * run on older versions of GTK+, it is worth keeping the iter around.
125  * 
126  * # Atomic Operations
127  * 
128  * It is important to note that only the methods
129  * gtk_list_store_insert_with_values() and gtk_list_store_insert_with_valuesv()
130  * are atomic, in the sense that the row is being appended to the store and the
131  * values filled in in a single operation with regard to #GtkTreeModel signaling.
132  * In contrast, using e.g. gtk_list_store_append() and then gtk_list_store_set()
133  * will first create a row, which triggers the #GtkTreeModel::row-inserted signal
134  * on #GtkListStore. The row, however, is still empty, and any signal handler
135  * connecting to #GtkTreeModel::row-inserted on this particular store should be prepared
136  * for the situation that the row might be empty. This is especially important
137  * if you are wrapping the #GtkListStore inside a #GtkTreeModelFilter and are
138  * using a #GtkTreeModelFilterVisibleFunc. Using any of the non-atomic operations
139  * to append rows to the #GtkListStore will cause the
140  * #GtkTreeModelFilterVisibleFunc to be visited with an empty row first; the
141  * function must be prepared for that.
142  * 
143  * # GtkListStore as GtkBuildable
144  * 
145  * The GtkListStore implementation of the GtkBuildable interface allows
146  * to specify the model columns with a <columns> element that may contain
147  * multiple <column> elements, each specifying one model column. The “type”
148  * attribute specifies the data type for the column.
149  * 
150  * Additionally, it is possible to specify content for the list store
151  * in the UI definition, with the <data> element. It can contain multiple
152  * <row> elements, each specifying to content for one row of the list model.
153  * Inside a <row>, the <col> elements specify the content for individual cells.
154  * 
155  * Note that it is probably more common to define your models in the code,
156  * and one might consider it a layering violation to specify the content of
157  * a list store in a UI definition, data, not presentation, and common wisdom
158  * is to separate the two, as far as possible.
159  * 
160  * An example of a UI Definition fragment for a list store:
161  * |[<!-- language="C" -->
162  * <object class="GtkListStore">
163  * <columns>
164  * <column type="gchararray"/>
165  * <column type="gchararray"/>
166  * <column type="gint"/>
167  * </columns>
168  * <data>
169  * <row>
170  * <col id="0">John</col>
171  * <col id="1">Doe</col>
172  * <col id="2">25</col>
173  * </row>
174  * <row>
175  * <col id="0">Johan</col>
176  * <col id="1">Dahlin</col>
177  * <col id="2">50</col>
178  * </row>
179  * </data>
180  * </object>
181  * ]|
182  */
183 public class ListStore : ObjectG, BuildableIF, TreeDragDestIF, TreeDragSourceIF, TreeModelIF, TreeSortableIF
184 {
185 	/** the main Gtk struct */
186 	protected GtkListStore* gtkListStore;
187 
188 	/** Get the main Gtk struct */
189 	public GtkListStore* getListStoreStruct(bool transferOwnership = false)
190 	{
191 		if (transferOwnership)
192 			ownedRef = false;
193 		return gtkListStore;
194 	}
195 
196 	/** the main Gtk struct as a void* */
197 	protected override void* getStruct()
198 	{
199 		return cast(void*)gtkListStore;
200 	}
201 
202 	/**
203 	 * Sets our main struct and passes it to the parent class.
204 	 */
205 	public this (GtkListStore* gtkListStore, bool ownedRef = false)
206 	{
207 		this.gtkListStore = gtkListStore;
208 		super(cast(GObject*)gtkListStore, ownedRef);
209 	}
210 
211 	// add the Buildable capabilities
212 	mixin BuildableT!(GtkListStore);
213 
214 	// add the TreeDragDest capabilities
215 	mixin TreeDragDestT!(GtkListStore);
216 
217 	// add the TreeDragSource capabilities
218 	mixin TreeDragSourceT!(GtkListStore);
219 
220 	// add the TreeModel capabilities
221 	mixin TreeModelT!(GtkListStore);
222 
223 	// add the TreeSortable capabilities
224 	mixin TreeSortableT!(GtkListStore);
225 
226 	/**
227 	 * Creates a top level iteractor.
228 	 * I don't think lists have but the top level iteractor
229 	 */
230 	TreeIter createIter()
231 	{
232 		GtkTreeIter* iter = new GtkTreeIter;
233 		gtk_list_store_append(getListStoreStruct(), iter);
234 		return new TreeIter(iter);
235 	}
236 
237 	/**
238 	 * sets the values for one row
239 	 * Params:
240 	 *  iter = the row iteractor
241 	 *  columns = an arrays with the columns to set
242 	 *  values = an arrays with the values
243 	 */
244 	void set(TreeIter iter, int[] columns, char*[] values)
245 	{
246 		for ( int i=0 ; i<columns.length && i<values.length; i++ )
247 		{
248 			gtk_list_store_set(
249 				gtkListStore,
250 				iter.getTreeIterStruct(),
251 				columns[i],
252 				values[i],-1);
253 		}
254 	}
255 
256 	/** ditto */
257 	void set(TreeIter iter, int[] columns, string[] values)
258 	{
259 		for ( int i=0 ; i<columns.length && i<values.length; i++ )
260 		{
261 			gtk_list_store_set(
262 				gtkListStore,
263 				iter.getTreeIterStruct(),
264 				columns[i],
265 				Str.toStringz(values[i]),-1);
266 		}
267 	}
268 
269 	/** */
270 	void setValue(TYPE)(TreeIter iter, int column, TYPE value)
271 	{
272 		Value v = new Value(value);
273 		gtk_list_store_set_value(gtkListStore, iter.getTreeIterStruct(), column, v.getValueStruct());
274 	}
275 
276 	/**
277 	 */
278 
279 	/** */
280 	public static GType getType()
281 	{
282 		return gtk_list_store_get_type();
283 	}
284 
285 	/**
286 	 * Non-vararg creation function.  Used primarily by language bindings.
287 	 *
288 	 * Params:
289 	 *     types = an array of #GType types for the columns, from first to last
290 	 *
291 	 * Returns: a new #GtkListStore
292 	 *
293 	 * Throws: ConstructionException GTK+ fails to create the object.
294 	 */
295 	public this(GType[] types)
296 	{
297 		auto p = gtk_list_store_newv(cast(int)types.length, types.ptr);
298 
299 		if(p is null)
300 		{
301 			throw new ConstructionException("null returned by newv");
302 		}
303 
304 		this(cast(GtkListStore*) p, true);
305 	}
306 
307 	/**
308 	 * Appends a new row to @list_store.  @iter will be changed to point to this new
309 	 * row.  The row will be empty after this function is called.  To fill in
310 	 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
311 	 *
312 	 * Params:
313 	 *     iter = An unset #GtkTreeIter to set to the appended row
314 	 */
315 	public void append(out TreeIter iter)
316 	{
317 		GtkTreeIter* outiter = gMalloc!GtkTreeIter();
318 
319 		gtk_list_store_append(gtkListStore, outiter);
320 
321 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
322 	}
323 
324 	/**
325 	 * Removes all rows from the list store.
326 	 */
327 	public void clear()
328 	{
329 		gtk_list_store_clear(gtkListStore);
330 	}
331 
332 	/**
333 	 * Creates a new row at @position.  @iter will be changed to point to this new
334 	 * row.  If @position is -1 or is larger than the number of rows on the list,
335 	 * then the new row will be appended to the list. The row will be empty after
336 	 * this function is called.  To fill in values, you need to call
337 	 * gtk_list_store_set() or gtk_list_store_set_value().
338 	 *
339 	 * Params:
340 	 *     iter = An unset #GtkTreeIter to set to the new row
341 	 *     position = position to insert the new row, or -1 for last
342 	 */
343 	public void insert(out TreeIter iter, int position)
344 	{
345 		GtkTreeIter* outiter = gMalloc!GtkTreeIter();
346 
347 		gtk_list_store_insert(gtkListStore, outiter, position);
348 
349 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
350 	}
351 
352 	/**
353 	 * Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
354 	 * prepended to the beginning of the list. @iter will be changed to point to
355 	 * this new row. The row will be empty after this function is called. To fill
356 	 * in values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
357 	 *
358 	 * Params:
359 	 *     iter = An unset #GtkTreeIter to set to the new row
360 	 *     sibling = A valid #GtkTreeIter, or %NULL
361 	 */
362 	public void insertAfter(out TreeIter iter, TreeIter sibling)
363 	{
364 		GtkTreeIter* outiter = gMalloc!GtkTreeIter();
365 
366 		gtk_list_store_insert_after(gtkListStore, outiter, (sibling is null) ? null : sibling.getTreeIterStruct());
367 
368 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
369 	}
370 
371 	/**
372 	 * Inserts a new row before @sibling. If @sibling is %NULL, then the row will
373 	 * be appended to the end of the list. @iter will be changed to point to this
374 	 * new row. The row will be empty after this function is called. To fill in
375 	 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
376 	 *
377 	 * Params:
378 	 *     iter = An unset #GtkTreeIter to set to the new row
379 	 *     sibling = A valid #GtkTreeIter, or %NULL
380 	 */
381 	public void insertBefore(out TreeIter iter, TreeIter sibling)
382 	{
383 		GtkTreeIter* outiter = gMalloc!GtkTreeIter();
384 
385 		gtk_list_store_insert_before(gtkListStore, outiter, (sibling is null) ? null : sibling.getTreeIterStruct());
386 
387 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
388 	}
389 
390 	/**
391 	 * A variant of gtk_list_store_insert_with_values() which
392 	 * takes the columns and values as two arrays, instead of
393 	 * varargs. This function is mainly intended for
394 	 * language-bindings.
395 	 *
396 	 * Params:
397 	 *     iter = An unset #GtkTreeIter to set to the new row, or %NULL.
398 	 *     position = position to insert the new row, or -1 for last
399 	 *     columns = an array of column numbers
400 	 *     values = an array of GValues
401 	 *
402 	 * Since: 2.6
403 	 */
404 	public void insertWithValuesv(out TreeIter iter, int position, int[] columns, Value[] values)
405 	{
406 		GtkTreeIter* outiter = gMalloc!GtkTreeIter();
407 
408 		GValue[] valuesArray = new GValue[values.length];
409 		for ( int i = 0; i < values.length; i++ )
410 		{
411 			valuesArray[i] = *(values[i].getValueStruct());
412 		}
413 
414 		gtk_list_store_insert_with_valuesv(gtkListStore, outiter, position, columns.ptr, valuesArray.ptr, cast(int)values.length);
415 
416 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
417 	}
418 
419 	/**
420 	 * > This function is slow. Only use it for debugging and/or testing
421 	 * > purposes.
422 	 *
423 	 * Checks if the given iter is a valid iter for this #GtkListStore.
424 	 *
425 	 * Params:
426 	 *     iter = A #GtkTreeIter.
427 	 *
428 	 * Returns: %TRUE if the iter is valid, %FALSE if the iter is invalid.
429 	 *
430 	 * Since: 2.2
431 	 */
432 	public bool iterIsValid(TreeIter iter)
433 	{
434 		return gtk_list_store_iter_is_valid(gtkListStore, (iter is null) ? null : iter.getTreeIterStruct()) != 0;
435 	}
436 
437 	/**
438 	 * Moves @iter in @store to the position after @position. Note that this
439 	 * function only works with unsorted stores. If @position is %NULL, @iter
440 	 * will be moved to the start of the list.
441 	 *
442 	 * Params:
443 	 *     iter = A #GtkTreeIter.
444 	 *     position = A #GtkTreeIter or %NULL.
445 	 *
446 	 * Since: 2.2
447 	 */
448 	public void moveAfter(TreeIter iter, TreeIter position)
449 	{
450 		gtk_list_store_move_after(gtkListStore, (iter is null) ? null : iter.getTreeIterStruct(), (position is null) ? null : position.getTreeIterStruct());
451 	}
452 
453 	/**
454 	 * Moves @iter in @store to the position before @position. Note that this
455 	 * function only works with unsorted stores. If @position is %NULL, @iter
456 	 * will be moved to the end of the list.
457 	 *
458 	 * Params:
459 	 *     iter = A #GtkTreeIter.
460 	 *     position = A #GtkTreeIter, or %NULL.
461 	 *
462 	 * Since: 2.2
463 	 */
464 	public void moveBefore(TreeIter iter, TreeIter position)
465 	{
466 		gtk_list_store_move_before(gtkListStore, (iter is null) ? null : iter.getTreeIterStruct(), (position is null) ? null : position.getTreeIterStruct());
467 	}
468 
469 	/**
470 	 * Prepends a new row to @list_store. @iter will be changed to point to this new
471 	 * row. The row will be empty after this function is called. To fill in
472 	 * values, you need to call gtk_list_store_set() or gtk_list_store_set_value().
473 	 *
474 	 * Params:
475 	 *     iter = An unset #GtkTreeIter to set to the prepend row
476 	 */
477 	public void prepend(out TreeIter iter)
478 	{
479 		GtkTreeIter* outiter = gMalloc!GtkTreeIter();
480 
481 		gtk_list_store_prepend(gtkListStore, outiter);
482 
483 		iter = ObjectG.getDObject!(TreeIter)(outiter, true);
484 	}
485 
486 	/**
487 	 * Removes the given row from the list store.  After being removed,
488 	 * @iter is set to be the next valid row, or invalidated if it pointed
489 	 * to the last row in @list_store.
490 	 *
491 	 * Params:
492 	 *     iter = A valid #GtkTreeIter
493 	 *
494 	 * Returns: %TRUE if @iter is valid, %FALSE if not.
495 	 */
496 	public bool remove(TreeIter iter)
497 	{
498 		return gtk_list_store_remove(gtkListStore, (iter is null) ? null : iter.getTreeIterStruct()) != 0;
499 	}
500 
501 	/**
502 	 * Reorders @store to follow the order indicated by @new_order. Note that
503 	 * this function only works with unsorted stores.
504 	 *
505 	 * Params:
506 	 *     newOrder = an array of integers mapping the new
507 	 *         position of each child to its old position before the re-ordering,
508 	 *         i.e. @new_order`[newpos] = oldpos`. It must have
509 	 *         exactly as many items as the list store’s length.
510 	 *
511 	 * Since: 2.2
512 	 */
513 	public void reorder(int[] newOrder)
514 	{
515 		gtk_list_store_reorder(gtkListStore, newOrder.ptr);
516 	}
517 
518 	/**
519 	 * This function is meant primarily for #GObjects that inherit from #GtkListStore,
520 	 * and should only be used when constructing a new #GtkListStore.  It will not
521 	 * function after a row has been added, or a method on the #GtkTreeModel
522 	 * interface is called.
523 	 *
524 	 * Params:
525 	 *     types = An array length n of #GTypes
526 	 */
527 	public void setColumnTypes(GType[] types)
528 	{
529 		gtk_list_store_set_column_types(gtkListStore, cast(int)types.length, types.ptr);
530 	}
531 
532 	/**
533 	 * See gtk_list_store_set(); this version takes a va_list for use by language
534 	 * bindings.
535 	 *
536 	 * Params:
537 	 *     iter = A valid #GtkTreeIter for the row being modified
538 	 *     varArgs = va_list of column/value pairs
539 	 */
540 	public void setValist(TreeIter iter, void* varArgs)
541 	{
542 		gtk_list_store_set_valist(gtkListStore, (iter is null) ? null : iter.getTreeIterStruct(), varArgs);
543 	}
544 
545 	/**
546 	 * Sets the data in the cell specified by @iter and @column.
547 	 * The type of @value must be convertible to the type of the
548 	 * column.
549 	 *
550 	 * Params:
551 	 *     iter = A valid #GtkTreeIter for the row being modified
552 	 *     column = column number to modify
553 	 *     value = new value for the cell
554 	 */
555 	public void setValue(TreeIter iter, int column, Value value)
556 	{
557 		gtk_list_store_set_value(gtkListStore, (iter is null) ? null : iter.getTreeIterStruct(), column, (value is null) ? null : value.getValueStruct());
558 	}
559 
560 	/**
561 	 * A variant of gtk_list_store_set_valist() which
562 	 * takes the columns and values as two arrays, instead of
563 	 * varargs. This function is mainly intended for
564 	 * language-bindings and in case the number of columns to
565 	 * change is not known until run-time.
566 	 *
567 	 * Params:
568 	 *     iter = A valid #GtkTreeIter for the row being modified
569 	 *     columns = an array of column numbers
570 	 *     values = an array of GValues
571 	 *
572 	 * Since: 2.12
573 	 */
574 	public void setValuesv(TreeIter iter, int[] columns, Value[] values)
575 	{
576 		GValue[] valuesArray = new GValue[values.length];
577 		for ( int i = 0; i < values.length; i++ )
578 		{
579 			valuesArray[i] = *(values[i].getValueStruct());
580 		}
581 
582 		gtk_list_store_set_valuesv(gtkListStore, (iter is null) ? null : iter.getTreeIterStruct(), columns.ptr, valuesArray.ptr, cast(int)values.length);
583 	}
584 
585 	/**
586 	 * Swaps @a and @b in @store. Note that this function only works with
587 	 * unsorted stores.
588 	 *
589 	 * Params:
590 	 *     a = A #GtkTreeIter.
591 	 *     b = Another #GtkTreeIter.
592 	 *
593 	 * Since: 2.2
594 	 */
595 	public void swap(TreeIter a, TreeIter b)
596 	{
597 		gtk_list_store_swap(gtkListStore, (a is null) ? null : a.getTreeIterStruct(), (b is null) ? null : b.getTreeIterStruct());
598 	}
599 }