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