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.TreeIter;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.Value;
30 private import gtk.TreeIterError;
31 private import gtk.TreeModelIF;
32 private import gtk.TreePath;
33 private import gtk.c.functions;
34 public  import gtk.c.types;
35 public  import gtkc.gtktypes;
36 
37 
38 /**
39  * The #GtkTreeIter is the primary structure
40  * for accessing a #GtkTreeModel. Models are expected to put a unique
41  * integer in the @stamp member, and put
42  * model-specific data in the three @user_data
43  * members.
44  */
45 public class TreeIter
46 {
47 	/** the main Gtk struct */
48 	protected GtkTreeIter* gtkTreeIter;
49 	protected bool ownedRef;
50 
51 	/** Get the main Gtk struct */
52 	public GtkTreeIter* getTreeIterStruct(bool transferOwnership = false)
53 	{
54 		if (transferOwnership)
55 			ownedRef = false;
56 		return gtkTreeIter;
57 	}
58 
59 	/** the main Gtk struct as a void* */
60 	protected void* getStruct()
61 	{
62 		return cast(void*)gtkTreeIter;
63 	}
64 
65 	/**
66 	 * Sets our main struct and passes it to the parent class.
67 	 */
68 	public this (GtkTreeIter* gtkTreeIter, bool ownedRef = false)
69 	{
70 		this.gtkTreeIter = gtkTreeIter;
71 		this.ownedRef = ownedRef;
72 	}
73 
74 	/**
75 	 * this will be set only when the iter
76 	 * is created from the model.
77 	 */
78 	GtkTreeModel* gtkTreeModel;
79 
80 	/** */
81 	public void setModel(GtkTreeModel* gtkTreeModel)
82 	{
83 		this.gtkTreeModel = gtkTreeModel;
84 	}
85 
86 	/** */
87 	public void setModel(TreeModelIF treeModel)
88 	{
89 		this.gtkTreeModel = treeModel.getTreeModelStruct();
90 	}
91 
92 	/**
93 	 * Throws: ConstructionException GTK+ fails to create the object.
94 	 */
95 	public this(TreeModelIF treeModel, string treePath)
96 	{
97 		this(treeModel, new TreePath(treePath));
98 	}
99 
100 	/**
101 	 * Throws: ConstructionException GTK+ fails to create the object.
102 	 */
103 	public this(TreeModelIF treeModel, TreePath treePath)
104 	{
105 		this();
106 		setModel(treeModel);
107 		if ( !gtk_tree_model_get_iter_from_string(
108 			treeModel.getTreeModelStruct(),
109 		getTreeIterStruct(), Str.toStringz(treePath.toString())) )
110 		{
111 			throw new ConstructionException("null returned by gtk_tree_model_get_iter_from_string");
112 		}
113 	}
114 
115 	/**
116 	 * creates a new tree iteractor.
117 	 * used TreeView.createIter and TreeView.append() to create iteractor for a tree or list
118 	 */
119 	this()
120 	{
121 		this(new GtkTreeIter);
122 	}
123 
124 	/**
125 	 * Creates a dynamically allocated tree iterator as a copy of iter.
126 	 */
127 	TreeIter copy(TreeIter iter)
128 	{
129 		TreeIter cp = new TreeIter();
130 		 *(cp.gtkTreeIter) = *(iter.gtkTreeIter);
131 
132 		return cp;
133 	}
134 
135 	/**
136 	 * Get Value
137 	 * Params:
138 	 *  column =
139 	 *  value =
140 	 */
141 	void getValue(int column, Value value)
142 	{
143 		if ( gtkTreeModel  is  null )
144 		{
145 			throw new TreeIterError("getValue","Tree model not set");
146 		}
147 		gtk_tree_model_get_value(gtkTreeModel, gtkTreeIter, column, value.getValueStruct());
148 	}
149 
150 	/**
151 	 * Get the value of a column as a string
152 	 * Params:
153 	 *  column = the column number
154 	 * Returns: a string representing the value of the column
155 	 */
156 	string getValueString(int column)
157 	{
158 		if ( gtkTreeModel  is  null )
159 		{
160 			throw new TreeIterError("getValueString","Tree model not set");
161 		}
162 		Value value = new Value();
163 		gtk_tree_model_get_value(gtkTreeModel, gtkTreeIter, column, value.getValueStruct());
164 		//printf("TreeIter.getValuaString = %.*s\n", value.getString().toString());
165 		return value.getString();
166 	}
167 
168 	/**
169 	 * Get the value of a column as an int
170 	 * Params:
171 	 *  column = the column number
172 	 * Returns: a string representing the value of the column
173 	 */
174 	int getValueInt(int column)
175 	{
176 		if ( gtkTreeModel  is  null )
177 		{
178 			throw new TreeIterError("getValueInt", "Tree model not set");
179 		}
180 		Value value = new Value();
181 		gtk_tree_model_get_value(gtkTreeModel, gtkTreeIter, column, value.getValueStruct());
182 		return value.getInt();
183 	}
184 
185 	/** */
186 	TreePath getTreePath()
187 	{
188 		if ( gtkTreeModel  is  null )
189 		{
190 			throw new TreeIterError("getTreePath","Tree model not set");
191 		}
192 		return new TreePath(gtk_tree_model_get_path(gtkTreeModel, gtkTreeIter));
193 	}
194 
195 	/**
196 	 * This return the path visible to the user.
197 	 */
198 	string getVisiblePath(string separator)
199 	{
200 		string vPath;
201 		if ( gtkTreeModel  is  null )
202 		{
203 			throw new TreeIterError("getVisiblePath", "Tree model not set");
204 		}
205 
206 		vPath = getValueString(0);
207 		TreeIter parent = getParent();
208 		while ( parent !is  null )
209 		{
210 			//printf("TreeIter.getVisiblePath parent = %.*s\n",parent.getValueString(0).toString());
211 			vPath = parent.getValueString(0) ~ separator ~ vPath;
212 			parent = parent.getParent();
213 		}
214 
215 		//printf("TreeIter.getVisiblePath = %.*s\n", vPath.toString());
216 
217 		return vPath;
218 	}
219 
220 	/**
221 	 * Gets the parent of this iter
222 	 * Returns: the parent iter or null if can't get parent or an error occured
223 	 */
224 	TreeIter getParent()
225 	{
226 		if ( gtkTreeModel  is  null )
227 		{
228 			throw new TreeIterError("getParent", "Tree model not set");
229 		}
230 		TreeIter parent = new TreeIter();
231 		bool gotParent = gtk_tree_model_iter_parent(gtkTreeModel, parent.getTreeIterStruct(), gtkTreeIter) == 0 ? false : true;
232 		if ( !gotParent )
233 		{
234 			return null;
235 		}
236 		parent.setModel(gtkTreeModel);
237 		return parent;
238 	}
239 
240 	/** */
241 	TreeIter getGrandParent()
242 	{
243 		if ( gtkTreeModel  is  null )
244 		{
245 			throw new TreeIterError("getGrandParent", "Tree model not set");
246 		}
247 		TreeIter grandParent = this;
248 		TreeIter parent = grandParent.getParent();
249 		while ( parent !is null )
250 		{
251 			grandParent = parent;
252 			parent = grandParent.getParent();
253 		}
254 
255 		return grandParent;
256 	}
257 
258 	/** A unique stamp to catch invalid iterators */
259 	public int stamp()
260 	{
261 		return gtkTreeIter.stamp;
262 	}
263 
264 	/** Ditto */
265 	public void stamp(int stamp)
266 	{
267 		gtkTreeIter.stamp = stamp;
268 	}
269 
270 	/** Model specific data */
271 	public void* userData()
272 	{
273 		return gtkTreeIter.userData;
274 	}
275 
276 	/** Ditto */
277 	public void userData(void* data)
278 	{
279 		gtkTreeIter.userData = data;
280 	}
281 
282 	public struct IterData
283 	{
284 		/// Data fields.
285 		union
286 		{
287 			int     dataInt;
288 			long    dataLong;
289 			double  dataFloat;
290 			double  dataDouble;
291 			string  dataString;
292 
293 			void*   dataUser;
294 		}
295 
296 		TypeInfo type = typeid(void);
297 	}
298 
299 	/**
300 	 * setUserData and getUserData provide simple boxing
301 	 * around the userData field in the TreeIter struct.
302 	 * Throws: TreeIterError for unsupported types or a type mismatch.
303 	 * Example:
304 	 * ---
305 	 * Treeiter iter = new TreeIter();
306 	 *
307 	 * iter.setUserData(20);
308 	 * int i = iter.getUserData!(int)();
309 	 * ---
310 	 */
311 	public void setUserData(T)(T data)
312 	{
313 		IterData* itData = new IterData;
314 		itData.type = typeid(T);
315 
316 		static if(is(T == int))
317 		{
318 			itData.dataInt = data;
319 		}
320 		else static if(is(T == long))
321 		{
322 			itData.dataLong = data;
323 		}
324 		else static if(is(T == float))
325 		{
326 			itData.dataFloat = data;
327 		}
328 		else static if(is(T == double))
329 		{
330 			itData.dataDouble = data;
331 		}
332 		else static if(is(T == string))
333 		{
334 			itData.dataString = data;
335 		}
336 		else static if(is(T == void*))
337 		{
338 			itData.dataUser = data;
339 		}
340 		else
341 		{
342 			pragma(msg, "IterData Type not Supported");
343 
344 			throw new TreeIterError("getUserData", "IterData Type not Supported");
345 		}
346 
347 		gtkTreeIter.userData = itData;
348 	}
349 
350 	/** Ditto */
351 	public T getUserData(T)()
352 	{
353 		IterData* itData = cast(IterData*)gtkTreeIter.userData;
354 
355 		static if(is(T == int))
356 		{
357 			if(itData.type is typeid(T))
358 			{
359 				return itData.dataInt;
360 			}
361 			else
362 			{
363 				throw new TreeIterError("getUserData", "IterData is not: int");
364 			}
365 		}
366 		else static if(is(T == long))
367 		{
368 			if(itData.type is typeid(T))
369 			{
370 				return itData.dataLong;
371 			}
372 			else
373 			{
374 				throw new TreeIterError("getUserData", "IterData is not: long");
375 			}
376 		}
377 		else static if(is(T == float))
378 		{
379 			if(itData.type is typeid(T))
380 			{
381 				return itData.dataFloat;
382 			}
383 			else
384 			{
385 				throw new TreeIterError("getUserData", "IterData is not: float");
386 			}
387 		}
388 		else static if(is(T == double))
389 		{
390 			if(itData.type is typeid(T))
391 			{
392 				return itData.dataDouble;
393 			}
394 			else
395 			{
396 				throw new TreeIterError("getUserData", "IterData is not: double");
397 			}
398 		}
399 		else static if(is(T == string))
400 		{
401 			if(itData.type is typeid(T))
402 			{
403 				return itData.dataString;
404 			}
405 			else
406 			{
407 				throw new TreeIterError("getUserData", "IterData is not: string");
408 			}
409 		}
410 		else static if(is(T == void*))
411 		{
412 			if(itData.type is typeid(T))
413 			{
414 				return itData.dataUser;
415 			}
416 			else
417 			{
418 				throw new TreeIterError("getUserData", "IterData is not: void*");
419 			}
420 		}
421 		else
422 		{
423 			pragma(msg, "IterData Type not Supported");
424 
425 			throw new TreeIterError("getUserData", "IterData Type not Supported");
426 		}
427 	}
428 
429 	/**
430 	 */
431 
432 	/** */
433 	public static GType getType()
434 	{
435 		return gtk_tree_iter_get_type();
436 	}
437 }