alternative to assert
[gtkD.git] / src / gtk / TreeIter.d
blobcc5e91d69f519cf6177118d85c12c9d4ba00106b
1 /*
2 * This file is part of duit.
4 * duit is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * duit is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with duit; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile =
26 * outPack = gtk
27 * outFile = TreeIter
28 * strct = GtkTreeIter
29 * realStrct=
30 * ctorStrct=
31 * clss = TreeIter
32 * interf =
33 * class Code: Yes
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - gtk_tree_iter_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * - glib.Str
45 * - gtk.TreeIter
46 * - gtk.TreeModel
47 * - gtk.TreePath
48 * - gtk.TreeIterError
49 * - gobject.Value;
50 * structWrap:
51 * - GtkTreeIter* -> TreeIter
52 * local aliases:
55 module gtk.TreeIter;
57 private import gtk.gtktypes;
59 private import lib.gtk;
61 private import glib.Str;
62 private import gtk.TreeIter;
63 private import gtk.TreeModel;
64 private import gtk.TreePath;
65 private import gtk.TreeIterError;
66 private import gobject.Value;;
68 /**
69 * Description
70 * The GtkTreeModel interface defines a generic tree interface for use by
71 * the GtkTreeView widget. It is an abstract interface, and is designed
72 * to be usable with any appropriate data structure. The programmer just
73 * has to implement this interface on their own data type for it to be
74 * viewable by a GtkTreeView widget.
75 * The model is represented as a hierarchical tree of strongly-typed,
76 * columned data. In other words, the model can be seen as a tree where
77 * every node has different values depending on which column is being
78 * queried. The type of data found in a column is determined by using the
79 * GType system (ie. G_TYPE_INT, GTK_TYPE_BUTTON, G_TYPE_POINTER, etc.).
80 * The types are homogeneous per column across all nodes. It is important
81 * to note that this interface only provides a way of examining a model and
82 * observing changes. The implementation of each individual model decides
83 * how and if changes are made.
84 * In order to make life simpler for programmers who do not need to write
85 * their own specialized model, two generic models are provided the
86 * GtkTreeStore and the GtkListStore. To use these, the developer simply
87 * pushes data into these models as necessary. These models provide the
88 * data structure as well as all appropriate tree interfaces. As a result,
89 * implementing drag and drop, sorting, and storing data is trivial. For
90 * the vast majority of trees and lists, these two models are sufficient.
91 * Models are accessed on a node/column level of granularity. One can
92 * query for the value of a model at a certain node and a certain column
93 * on that node. There are two structures used to reference a particular
94 * node in a model. They are the GtkTreePath and the GtkTreeIter
95 * [4]
96 * Most of the interface consists of operations on a GtkTreeIter.
97 * A path is essentially a potential node. It is a location on a model
98 * that may or may not actually correspond to a node on a specific model.
99 * The GtkTreePath struct can be converted into either an array of
100 * unsigned integers or a string. The string form is a list of numbers
101 * separated by a colon. Each number refers to the offset at that level.
102 * Thus, the path 0 refers to the root node and the path
103 * 2:4 refers to the fifth child of the third node.
104 * By contrast, a GtkTreeIter is a reference to a specific node on a
105 * specific model. It is a generic struct with an integer and three
106 * generic pointers. These are filled in by the model in a model-specific
107 * way. One can convert a path to an iterator by calling
108 * gtk_tree_model_get_iter(). These iterators are the primary way of
109 * accessing a model and are similar to the iterators used by
110 * GtkTextBuffer. They are generally statically allocated on the stack and
111 * only used for a short time. The model interface defines a set of
112 * operations using them for navigating the model.
113 * It is expected that models fill in the iterator with private data. For
114 * example, the GtkListStore model, which is internally a simple linked
115 * list, stores a list node in one of the pointers. The GtkTreeModelSort
116 * stores an array and an offset in two of the pointers. Additionally,
117 * there is an integer field. This field is generally filled with a unique
118 * stamp per model. This stamp is for catching errors resulting from using
119 * invalid iterators with a model.
120 * The lifecycle of an iterator can be a little confusing at first.
121 * Iterators are expected to always be valid for as long as the model is
122 * unchanged (and doesn't emit a signal). The model is considered to own
123 * all outstanding iterators and nothing needs to be done to free them from
124 * the user's point of view. Additionally, some models guarantee that an
125 * iterator is valid for as long as the node it refers to is valid (most
126 * notably the GtkTreeStore and GtkListStore). Although generally
127 * uninteresting, as one always has to allow for the case where iterators
128 * do not persist beyond a signal, some very important performance
129 * enhancements were made in the sort model. As a result, the
130 * GTK_TREE_MODEL_ITERS_PERSIST flag was added to indicate this behavior.
131 * To help show some common operation of a model, some examples are
132 * provided. The first example shows three ways of getting the iter at the
133 * location 3:2:5. While the first method shown is easier,
134 * the second is much more common, as you often get paths from callbacks.
135 * Example1.Acquiring a GtkTreeIter
136 * /+* Three ways of getting the iter pointing to the location
137 * +/
139 * GtkTreePath *path;
140 * GtkTreeIter iter;
141 * GtkTreeIter parent_iter;
142 * /+* get the iterator from a string +/
143 * gtk_tree_model_get_iter_from_string (model, iter, "3:2:5");
144 * /+* get the iterator from a path +/
145 * path = gtk_tree_path_new_from_string ("3:2:5");
146 * gtk_tree_model_get_iter (model, iter, path);
147 * gtk_tree_path_free (path);
148 * /+* walk the tree to find the iterator +/
149 * gtk_tree_model_iter_nth_child (model, iter, NULL, 3);
150 * parent_iter = iter;
151 * gtk_tree_model_iter_nth_child (model, iter, parent_iter, 2);
152 * parent_iter = iter;
153 * gtk_tree_model_iter_nth_child (model, iter, parent_iter, 5);
155 * This second example shows a quick way of iterating through a list and
156 * getting a string and an integer from each row. The
157 * populate_model function used below is not shown, as
158 * it is specific to the GtkListStore. For information on how to write
159 * such a function, see the GtkListStore documentation.
160 * Example2.Reading data from a GtkTreeModel
161 * enum
163 * STRING_COLUMN,
164 * INT_COLUMN,
165 * N_COLUMNS
166 * };
168 * GtkTreeModel *list_store;
169 * GtkTreeIter iter;
170 * gboolean valid;
171 * gint row_count = 0;
172 * /+* make a new list_store +/
173 * list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
174 * /+* Fill the list store with data +/
175 * populate_model (list_store);
176 * /+* Get the first iter in the list +/
177 * valid = gtk_tree_model_get_iter_first (list_store, iter);
178 * while (valid)
180 * /+* Walk through the list, reading each row +/
181 * gchar *str_data;
182 * gint int_data;
183 * /+* Make sure you terminate calls to gtk_tree_model_get()
184 * * with a '-1' value
185 * +/
186 * gtk_tree_model_get (list_store, iter,
187 * STRING_COLUMN, str_data,
188 * INT_COLUMN, int_data,
189 * -1);
190 * /+* Do something with the data +/
191 * g_print ("Row %d: (%s,%d)\n", row_count, str_data, int_data);
192 * g_free (str_data);
193 * row_count ++;
194 * valid = gtk_tree_model_iter_next (list_store, iter);
198 public class TreeIter
201 /** the main Gtk struct */
202 protected GtkTreeIter* gtkTreeIter;
205 public GtkTreeIter* getTreeIterStruct()
207 return gtkTreeIter;
211 /** the main Gtk struct as a void* */
212 protected void* getStruct()
214 return cast(void*)gtkTreeIter;
218 * Sets our main struct and passes it to the parent class
220 public this (GtkTreeIter* gtkTreeIter)
222 this.gtkTreeIter = gtkTreeIter;
226 * this will be set only when the iter
227 * is created from the model.
229 GtkTreeModel *gtkTreeModel;
231 public void setModel(GtkTreeModel *gtkTreeModel)
233 this.gtkTreeModel = gtkTreeModel;
236 public void setModel(TreeModel treeModel)
238 this.gtkTreeModel = treeModel.getTreeModelStruct();
241 public this(TreeModel treeModel, TreePath treePath)
243 this();
244 setModel(treeModel);
245 if ( gtk_tree_model_get_iter_from_string(
246 treeModel.getTreeModelStruct(),
247 getTreeIterStruct(), Str.toStringz(treePath.toString())) )
249 // ???
254 * creates a new tree iteractor.
255 * used TreeView.createIter and TreeView.append() to create iteractor for a tree or list
257 this()
259 this(new GtkTreeIter);
264 * Get Value
265 * @param iter
266 * @param column
267 * @param value
269 void getValue(int column, Value value)
271 if ( gtkTreeModel is null )
273 throw new TreeIterError("getValue","Tree model not set");
275 gtk_tree_model_get_value(gtkTreeModel, gtkTreeIter, column, value.getValueStruct());
279 * Get the value of a column as a string
280 * @para column the column number
281 * @return a string representing the value of the column
283 char[] getValueString(int column)
285 if ( gtkTreeModel is null )
287 throw new TreeIterError("getValueString","Tree model not set");
289 Value value = new Value();
290 gtk_tree_model_get_value(gtkTreeModel, gtkTreeIter, column, value.getValueStruct());
291 //printf("TreeIter.getValuaString = %.*s\n", value.getString().toString());
292 return value.getString();
296 * Get the value of a column as an int
297 * @para column the column number
298 * @return a string representing the value of the column
300 int getValueInt(int column)
302 if ( gtkTreeModel is null )
304 throw new TreeIterError("getValueInt", "Tree model not set");
306 Value value = new Value();
307 gtk_tree_model_get_value(gtkTreeModel, gtkTreeIter, column, value.getValueStruct());
308 return value.getInt();
311 TreePath getTreePath()
313 if ( gtkTreeModel is null )
315 throw new TreeIterError("getTreePath","Tree model not set");
317 return new TreePath(gtk_tree_model_get_path(gtkTreeModel, gtkTreeIter));
321 * This return the path visible to the user.
323 char[] getVisiblePath(char[] separator)
325 char[] vPath;
326 if ( gtkTreeModel is null )
328 throw new TreeIterError("getVisiblePath", "Tree model not set");
331 vPath = getValueString(0);
332 TreeIter parent = getParent();
333 while ( parent !is null )
335 //printf("TreeIter.getVisiblePath parent = %.*s\n",parent.getValueString(0).toString());
336 vPath = parent.getValueString(0) ~ separator ~ vPath;
337 parent = parent.getParent();
340 //printf("TreeIter.getVisiblePath = %.*s\n", vPath.toString());
342 return vPath;
346 * Gets the parent of this iter
347 * @param child
348 * @return the parent iter or null if can't get parent or an error occured
350 TreeIter getParent()
352 if ( gtkTreeModel is null )
354 throw new TreeIterError("getParent", "Tree model not set");
356 TreeIter parent = new TreeIter();
357 bool gotParent = gtk_tree_model_iter_parent(gtkTreeModel, parent.getTreeIterStruct(), gtkTreeIter) == 0 ? false : true;
358 if ( !gotParent )
360 return null;
362 parent.setModel(gtkTreeModel);
363 return parent;
367 TreeIter getGrandParent()
369 if ( gtkTreeModel is null )
371 throw new TreeIterError("getGrandParent", "Tree model not set");
373 TreeIter grandParent = this;
374 TreeIter parent = grandParent.getParent();
375 while ( parent !is null )
377 grandParent = parent;
378 parent = grandParent.getParent();
381 return grandParent;
391 // imports for the signal processing
392 private import gobject.Signals;
393 private import gdk.gdktypes;
394 int[char[]] connectedSignals;
396 void delegate(GtkTreePath*, TreeIter, TreeIter)[] onRowChangedListeners;
397 void addOnRowChanged(void delegate(GtkTreePath*, TreeIter, TreeIter) dlg)
399 if ( !("row-changed" in connectedSignals) )
401 Signals.connectData(
402 getStruct(),
403 "row-changed",
404 cast(GCallback)&callBackRowChanged,
405 this,
406 null,
407 cast(ConnectFlags)0);
408 connectedSignals["row-changed"] = 1;
410 onRowChangedListeners ~= dlg;
412 extern(C) static void callBackRowChanged(GtkTreeModel* treeModelStruct, GtkTreePath* path, GtkTreeIter* iter, TreeIter treeIter)
414 bit consumed = false;
416 foreach ( void delegate(GtkTreePath*, TreeIter, TreeIter) dlg ; treeIter.onRowChangedListeners )
418 dlg(path, new TreeIter(iter), treeIter);
421 return consumed;
424 void delegate(GtkTreePath*, TreeIter)[] onRowDeletedListeners;
425 void addOnRowDeleted(void delegate(GtkTreePath*, TreeIter) dlg)
427 if ( !("row-deleted" in connectedSignals) )
429 Signals.connectData(
430 getStruct(),
431 "row-deleted",
432 cast(GCallback)&callBackRowDeleted,
433 this,
434 null,
435 cast(ConnectFlags)0);
436 connectedSignals["row-deleted"] = 1;
438 onRowDeletedListeners ~= dlg;
440 extern(C) static void callBackRowDeleted(GtkTreeModel* treeModelStruct, GtkTreePath* path, TreeIter treeIter)
442 bit consumed = false;
444 foreach ( void delegate(GtkTreePath*, TreeIter) dlg ; treeIter.onRowDeletedListeners )
446 dlg(path, treeIter);
449 return consumed;
452 void delegate(GtkTreePath*, TreeIter, TreeIter)[] onRowHasChildToggledListeners;
453 void addOnRowHasChildToggled(void delegate(GtkTreePath*, TreeIter, TreeIter) dlg)
455 if ( !("row-has-child-toggled" in connectedSignals) )
457 Signals.connectData(
458 getStruct(),
459 "row-has-child-toggled",
460 cast(GCallback)&callBackRowHasChildToggled,
461 this,
462 null,
463 cast(ConnectFlags)0);
464 connectedSignals["row-has-child-toggled"] = 1;
466 onRowHasChildToggledListeners ~= dlg;
468 extern(C) static void callBackRowHasChildToggled(GtkTreeModel* treeModelStruct, GtkTreePath* path, GtkTreeIter* iter, TreeIter treeIter)
470 bit consumed = false;
472 foreach ( void delegate(GtkTreePath*, TreeIter, TreeIter) dlg ; treeIter.onRowHasChildToggledListeners )
474 dlg(path, new TreeIter(iter), treeIter);
477 return consumed;
480 void delegate(GtkTreePath*, TreeIter, TreeIter)[] onRowInsertedListeners;
481 void addOnRowInserted(void delegate(GtkTreePath*, TreeIter, TreeIter) dlg)
483 if ( !("row-inserted" in connectedSignals) )
485 Signals.connectData(
486 getStruct(),
487 "row-inserted",
488 cast(GCallback)&callBackRowInserted,
489 this,
490 null,
491 cast(ConnectFlags)0);
492 connectedSignals["row-inserted"] = 1;
494 onRowInsertedListeners ~= dlg;
496 extern(C) static void callBackRowInserted(GtkTreeModel* treeModelStruct, GtkTreePath* path, GtkTreeIter* iter, TreeIter treeIter)
498 bit consumed = false;
500 foreach ( void delegate(GtkTreePath*, TreeIter, TreeIter) dlg ; treeIter.onRowInsertedListeners )
502 dlg(path, new TreeIter(iter), treeIter);
505 return consumed;
508 void delegate(GtkTreePath*, TreeIter, gpointer, TreeIter)[] onRowsReorderedListeners;
509 void addOnRowsReordered(void delegate(GtkTreePath*, TreeIter, gpointer, TreeIter) dlg)
511 if ( !("rows-reordered" in connectedSignals) )
513 Signals.connectData(
514 getStruct(),
515 "rows-reordered",
516 cast(GCallback)&callBackRowsReordered,
517 this,
518 null,
519 cast(ConnectFlags)0);
520 connectedSignals["rows-reordered"] = 1;
522 onRowsReorderedListeners ~= dlg;
524 extern(C) static void callBackRowsReordered(GtkTreeModel* treeModelStruct, GtkTreePath* path, GtkTreeIter* iter, gpointer arg3, TreeIter treeIter)
526 bit consumed = false;
528 foreach ( void delegate(GtkTreePath*, TreeIter, gpointer, TreeIter) dlg ; treeIter.onRowsReorderedListeners )
530 dlg(path, new TreeIter(iter), arg3, treeIter);
533 return consumed;
574 * Creates a dynamically allocated tree iterator as a copy of iter. This
575 * function is not intended for use in applications, because you can just copy
576 * the structs by value (GtkTreeIter new_iter = iter;). You
577 * must free this iter with gtk_tree_iter_free().
578 * iter:
579 * A GtkTreeIter.
580 * Returns:
581 * a newly-allocated copy of iter.
583 public TreeIter copy()
585 // GtkTreeIter* gtk_tree_iter_copy (GtkTreeIter *iter);
586 return new TreeIter( gtk_tree_iter_copy(gtkTreeIter) );
590 * Frees an iterator that has been allocated on the heap. This function is
591 * mainly used for language bindings.
592 * iter:
593 * A dynamically allocated tree iterator.
595 public void free()
597 // void gtk_tree_iter_free (GtkTreeIter *iter);
598 gtk_tree_iter_free(gtkTreeIter);