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 glib.Node;
26 
27 private import glib.ConstructionException;
28 private import gtkc.glib;
29 public  import gtkc.glibtypes;
30 
31 
32 /**
33  * The #GNode struct represents one node in a [n-ary tree][glib-N-ary-Trees].
34  */
35 public class Node
36 {
37 	/** the main Gtk struct */
38 	protected GNode* gNode;
39 
40 	/** Get the main Gtk struct */
41 	public GNode* getNodeStruct()
42 	{
43 		return gNode;
44 	}
45 
46 	/** the main Gtk struct as a void* */
47 	protected void* getStruct()
48 	{
49 		return cast(void*)gNode;
50 	}
51 
52 	/**
53 	 * Sets our main struct and passes it to the parent class.
54 	 */
55 	public this (GNode* gNode)
56 	{
57 		this.gNode = gNode;
58 	}
59 
60 
61 	/**
62 	 * Gets the position of the first child of a #GNode
63 	 * which contains the given data.
64 	 *
65 	 * Params:
66 	 *     data = the data to find
67 	 *
68 	 * Return: the index of the child of @node which contains
69 	 *     @data, or -1 if the data is not found
70 	 */
71 	public int childIndex(void* data)
72 	{
73 		return g_node_child_index(gNode, data);
74 	}
75 
76 	/**
77 	 * Gets the position of a #GNode with respect to its siblings.
78 	 * @child must be a child of @node. The first child is numbered 0,
79 	 * the second 1, and so on.
80 	 *
81 	 * Params:
82 	 *     child = a child of @node
83 	 *
84 	 * Return: the position of @child with respect to its siblings
85 	 */
86 	public int childPosition(Node child)
87 	{
88 		return g_node_child_position(gNode, (child is null) ? null : child.getNodeStruct());
89 	}
90 
91 	/**
92 	 * Calls a function for each of the children of a #GNode.
93 	 * Note that it doesn't descend beneath the child nodes.
94 	 *
95 	 * Params:
96 	 *     flags = which types of children are to be visited, one of
97 	 *         %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
98 	 *     func = the function to call for each visited node
99 	 *     data = user data to pass to the function
100 	 */
101 	public void childrenForeach(GTraverseFlags flags, GNodeForeachFunc func, void* data)
102 	{
103 		g_node_children_foreach(gNode, flags, func, data);
104 	}
105 
106 	/**
107 	 * Recursively copies a #GNode (but does not deep-copy the data inside the
108 	 * nodes, see g_node_copy_deep() if you need that).
109 	 *
110 	 * Return: a new #GNode containing the same data pointers
111 	 */
112 	public Node copy()
113 	{
114 		auto p = g_node_copy(gNode);
115 		
116 		if(p is null)
117 		{
118 			return null;
119 		}
120 		
121 		return new Node(cast(GNode*) p);
122 	}
123 
124 	/**
125 	 * Recursively copies a #GNode and its data.
126 	 *
127 	 * Params:
128 	 *     copyFunc = the function which is called to copy the data inside each node,
129 	 *         or %NULL to use the original data.
130 	 *     data = data to pass to @copy_func
131 	 *
132 	 * Return: a new #GNode containing copies of the data in @node.
133 	 *
134 	 * Since: 2.4
135 	 */
136 	public Node copyDeep(GCopyFunc copyFunc, void* data)
137 	{
138 		auto p = g_node_copy_deep(gNode, copyFunc, data);
139 		
140 		if(p is null)
141 		{
142 			return null;
143 		}
144 		
145 		return new Node(cast(GNode*) p);
146 	}
147 
148 	/**
149 	 * Gets the depth of a #GNode.
150 	 *
151 	 * If @node is %NULL the depth is 0. The root node has a depth of 1.
152 	 * For the children of the root node the depth is 2. And so on.
153 	 *
154 	 * Return: the depth of the #GNode
155 	 */
156 	public uint depth()
157 	{
158 		return g_node_depth(gNode);
159 	}
160 
161 	/**
162 	 * Removes @root and its children from the tree, freeing any memory
163 	 * allocated.
164 	 */
165 	public void destroy()
166 	{
167 		g_node_destroy(gNode);
168 	}
169 
170 	/**
171 	 * Finds a #GNode in a tree.
172 	 *
173 	 * Params:
174 	 *     order = the order in which nodes are visited - %G_IN_ORDER,
175 	 *         %G_PRE_ORDER, %G_POST_ORDER, or %G_LEVEL_ORDER
176 	 *     flags = which types of children are to be searched, one of
177 	 *         %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
178 	 *     data = the data to find
179 	 *
180 	 * Return: the found #GNode, or %NULL if the data is not found
181 	 */
182 	public Node find(GTraverseType order, GTraverseFlags flags, void* data)
183 	{
184 		auto p = g_node_find(gNode, order, flags, data);
185 		
186 		if(p is null)
187 		{
188 			return null;
189 		}
190 		
191 		return new Node(cast(GNode*) p);
192 	}
193 
194 	/**
195 	 * Finds the first child of a #GNode with the given data.
196 	 *
197 	 * Params:
198 	 *     flags = which types of children are to be searched, one of
199 	 *         %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
200 	 *     data = the data to find
201 	 *
202 	 * Return: the found child #GNode, or %NULL if the data is not found
203 	 */
204 	public Node findChild(GTraverseFlags flags, void* data)
205 	{
206 		auto p = g_node_find_child(gNode, flags, data);
207 		
208 		if(p is null)
209 		{
210 			return null;
211 		}
212 		
213 		return new Node(cast(GNode*) p);
214 	}
215 
216 	/**
217 	 * Gets the first sibling of a #GNode.
218 	 * This could possibly be the node itself.
219 	 *
220 	 * Return: the first sibling of @node
221 	 */
222 	public Node firstSibling()
223 	{
224 		auto p = g_node_first_sibling(gNode);
225 		
226 		if(p is null)
227 		{
228 			return null;
229 		}
230 		
231 		return new Node(cast(GNode*) p);
232 	}
233 
234 	/**
235 	 * Gets the root of a tree.
236 	 *
237 	 * Return: the root of the tree
238 	 */
239 	public Node getRoot()
240 	{
241 		auto p = g_node_get_root(gNode);
242 		
243 		if(p is null)
244 		{
245 			return null;
246 		}
247 		
248 		return new Node(cast(GNode*) p);
249 	}
250 
251 	/**
252 	 * Inserts a #GNode beneath the parent at the given position.
253 	 *
254 	 * Params:
255 	 *     position = the position to place @node at, with respect to its siblings
256 	 *         If position is -1, @node is inserted as the last child of @parent
257 	 *     node = the #GNode to insert
258 	 *
259 	 * Return: the inserted #GNode
260 	 */
261 	public Node insert(int position, Node node)
262 	{
263 		auto p = g_node_insert(gNode, position, (node is null) ? null : node.getNodeStruct());
264 		
265 		if(p is null)
266 		{
267 			return null;
268 		}
269 		
270 		return new Node(cast(GNode*) p);
271 	}
272 
273 	/**
274 	 * Inserts a #GNode beneath the parent after the given sibling.
275 	 *
276 	 * Params:
277 	 *     sibling = the sibling #GNode to place @node after.
278 	 *         If sibling is %NULL, the node is inserted as the first child of @parent.
279 	 *     node = the #GNode to insert
280 	 *
281 	 * Return: the inserted #GNode
282 	 */
283 	public Node insertAfter(Node sibling, Node node)
284 	{
285 		auto p = g_node_insert_after(gNode, (sibling is null) ? null : sibling.getNodeStruct(), (node is null) ? null : node.getNodeStruct());
286 		
287 		if(p is null)
288 		{
289 			return null;
290 		}
291 		
292 		return new Node(cast(GNode*) p);
293 	}
294 
295 	/**
296 	 * Inserts a #GNode beneath the parent before the given sibling.
297 	 *
298 	 * Params:
299 	 *     sibling = the sibling #GNode to place @node before.
300 	 *         If sibling is %NULL, the node is inserted as the last child of @parent.
301 	 *     node = the #GNode to insert
302 	 *
303 	 * Return: the inserted #GNode
304 	 */
305 	public Node insertBefore(Node sibling, Node node)
306 	{
307 		auto p = g_node_insert_before(gNode, (sibling is null) ? null : sibling.getNodeStruct(), (node is null) ? null : node.getNodeStruct());
308 		
309 		if(p is null)
310 		{
311 			return null;
312 		}
313 		
314 		return new Node(cast(GNode*) p);
315 	}
316 
317 	/**
318 	 * Returns %TRUE if @node is an ancestor of @descendant.
319 	 * This is true if node is the parent of @descendant,
320 	 * or if node is the grandparent of @descendant etc.
321 	 *
322 	 * Params:
323 	 *     descendant = a #GNode
324 	 *
325 	 * Return: %TRUE if @node is an ancestor of @descendant
326 	 */
327 	public bool isAncestor(Node descendant)
328 	{
329 		return g_node_is_ancestor(gNode, (descendant is null) ? null : descendant.getNodeStruct()) != 0;
330 	}
331 
332 	/**
333 	 * Gets the last child of a #GNode.
334 	 *
335 	 * Return: the last child of @node, or %NULL if @node has no children
336 	 */
337 	public Node lastChild()
338 	{
339 		auto p = g_node_last_child(gNode);
340 		
341 		if(p is null)
342 		{
343 			return null;
344 		}
345 		
346 		return new Node(cast(GNode*) p);
347 	}
348 
349 	/**
350 	 * Gets the last sibling of a #GNode.
351 	 * This could possibly be the node itself.
352 	 *
353 	 * Return: the last sibling of @node
354 	 */
355 	public Node lastSibling()
356 	{
357 		auto p = g_node_last_sibling(gNode);
358 		
359 		if(p is null)
360 		{
361 			return null;
362 		}
363 		
364 		return new Node(cast(GNode*) p);
365 	}
366 
367 	/**
368 	 * Gets the maximum height of all branches beneath a #GNode.
369 	 * This is the maximum distance from the #GNode to all leaf nodes.
370 	 *
371 	 * If @root is %NULL, 0 is returned. If @root has no children,
372 	 * 1 is returned. If @root has children, 2 is returned. And so on.
373 	 *
374 	 * Return: the maximum height of the tree beneath @root
375 	 */
376 	public uint maxHeight()
377 	{
378 		return g_node_max_height(gNode);
379 	}
380 
381 	/**
382 	 * Gets the number of children of a #GNode.
383 	 *
384 	 * Return: the number of children of @node
385 	 */
386 	public uint nChildren()
387 	{
388 		return g_node_n_children(gNode);
389 	}
390 
391 	/**
392 	 * Gets the number of nodes in a tree.
393 	 *
394 	 * Params:
395 	 *     flags = which types of children are to be counted, one of
396 	 *         %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
397 	 *
398 	 * Return: the number of nodes in the tree
399 	 */
400 	public uint nNodes(GTraverseFlags flags)
401 	{
402 		return g_node_n_nodes(gNode, flags);
403 	}
404 
405 	/**
406 	 * Gets a child of a #GNode, using the given index.
407 	 * The first child is at index 0. If the index is
408 	 * too big, %NULL is returned.
409 	 *
410 	 * Params:
411 	 *     n = the index of the desired child
412 	 *
413 	 * Return: the child of @node at index @n
414 	 */
415 	public Node nthChild(uint n)
416 	{
417 		auto p = g_node_nth_child(gNode, n);
418 		
419 		if(p is null)
420 		{
421 			return null;
422 		}
423 		
424 		return new Node(cast(GNode*) p);
425 	}
426 
427 	/**
428 	 * Inserts a #GNode as the first child of the given parent.
429 	 *
430 	 * Params:
431 	 *     node = the #GNode to insert
432 	 *
433 	 * Return: the inserted #GNode
434 	 */
435 	public Node prepend(Node node)
436 	{
437 		auto p = g_node_prepend(gNode, (node is null) ? null : node.getNodeStruct());
438 		
439 		if(p is null)
440 		{
441 			return null;
442 		}
443 		
444 		return new Node(cast(GNode*) p);
445 	}
446 
447 	/**
448 	 * Reverses the order of the children of a #GNode.
449 	 * (It doesn't change the order of the grandchildren.)
450 	 */
451 	public void reverseChildren()
452 	{
453 		g_node_reverse_children(gNode);
454 	}
455 
456 	/**
457 	 * Traverses a tree starting at the given root #GNode.
458 	 * It calls the given function for each node visited.
459 	 * The traversal can be halted at any point by returning %TRUE from @func.
460 	 *
461 	 * Params:
462 	 *     order = the order in which nodes are visited - %G_IN_ORDER,
463 	 *         %G_PRE_ORDER, %G_POST_ORDER, or %G_LEVEL_ORDER.
464 	 *     flags = which types of children are to be visited, one of
465 	 *         %G_TRAVERSE_ALL, %G_TRAVERSE_LEAVES and %G_TRAVERSE_NON_LEAVES
466 	 *     maxDepth = the maximum depth of the traversal. Nodes below this
467 	 *         depth will not be visited. If max_depth is -1 all nodes in
468 	 *         the tree are visited. If depth is 1, only the root is visited.
469 	 *         If depth is 2, the root and its children are visited. And so on.
470 	 *     func = the function to call for each visited #GNode
471 	 *     data = user data to pass to the function
472 	 */
473 	public void traverse(GTraverseType order, GTraverseFlags flags, int maxDepth, GNodeTraverseFunc func, void* data)
474 	{
475 		g_node_traverse(gNode, order, flags, maxDepth, func, data);
476 	}
477 
478 	/**
479 	 * Unlinks a #GNode from a tree, resulting in two separate trees.
480 	 */
481 	public void unlink()
482 	{
483 		g_node_unlink(gNode);
484 	}
485 
486 	/**
487 	 * Creates a new #GNode containing the given data.
488 	 * Used to create the first node in a tree.
489 	 *
490 	 * Params:
491 	 *     data = the data of the new node
492 	 *
493 	 * Return: a new #GNode
494 	 *
495 	 * Throws: ConstructionException GTK+ fails to create the object.
496 	 */
497 	public this(void* data)
498 	{
499 		auto p = g_node_new(data);
500 		
501 		if(p is null)
502 		{
503 			throw new ConstructionException("null returned by new");
504 		}
505 		
506 		this(cast(GNode*) p);
507 	}
508 }