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