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