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