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