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