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