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 gtk.TreeModelIF; 26 27 private import glib.MemorySlice; 28 private import glib.Str; 29 private import glib.c.functions; 30 private import gobject.ObjectG; 31 private import gobject.Signals; 32 private import gobject.Value; 33 private import gtk.TreeIter; 34 private import gtk.TreeModelIF; 35 private import gtk.TreePath; 36 private import gtk.c.functions; 37 public import gtk.c.types; 38 private import std.algorithm; 39 40 41 /** 42 * The tree interface used by GtkTreeView 43 * 44 * The #GtkTreeModel interface defines a generic tree interface for 45 * use by the #GtkTreeView widget. It is an abstract interface, and 46 * is designed to be usable with any appropriate data structure. The 47 * programmer just has to implement this interface on their own data 48 * type for it to be viewable by a #GtkTreeView widget. 49 * 50 * The model is represented as a hierarchical tree of strongly-typed, 51 * columned data. In other words, the model can be seen as a tree where 52 * every node has different values depending on which column is being 53 * queried. The type of data found in a column is determined by using 54 * the GType system (ie. #G_TYPE_INT, #GTK_TYPE_BUTTON, #G_TYPE_POINTER, 55 * etc). The types are homogeneous per column across all nodes. It is 56 * important to note that this interface only provides a way of examining 57 * a model and observing changes. The implementation of each individual 58 * model decides how and if changes are made. 59 * 60 * In order to make life simpler for programmers who do not need to 61 * write their own specialized model, two generic models are provided 62 * — the #GtkTreeStore and the #GtkListStore. To use these, the 63 * developer simply pushes data into these models as necessary. These 64 * models provide the data structure as well as all appropriate tree 65 * interfaces. As a result, implementing drag and drop, sorting, and 66 * storing data is trivial. For the vast majority of trees and lists, 67 * these two models are sufficient. 68 * 69 * Models are accessed on a node/column level of granularity. One can 70 * query for the value of a model at a certain node and a certain 71 * column on that node. There are two structures used to reference a 72 * particular node in a model. They are the [struct@Gtk.TreePath] and 73 * the [struct@Gtk.TreeIter] (“iter” is short for iterator). Most of the 74 * interface consists of operations on a [struct@Gtk.TreeIter]. 75 * 76 * A path is essentially a potential node. It is a location on a model 77 * that may or may not actually correspond to a node on a specific 78 * model. A [struct@Gtk.TreePath] can be converted into either an 79 * array of unsigned integers or a string. The string form is a list 80 * of numbers separated by a colon. Each number refers to the offset 81 * at that level. Thus, the path `0` refers to the root 82 * node and the path `2:4` refers to the fifth child of 83 * the third node. 84 * 85 * By contrast, a [struct@Gtk.TreeIter] is a reference to a specific node on 86 * a specific model. It is a generic struct with an integer and three 87 * generic pointers. These are filled in by the model in a model-specific 88 * way. One can convert a path to an iterator by calling 89 * gtk_tree_model_get_iter(). These iterators are the primary way 90 * of accessing a model and are similar to the iterators used by 91 * #GtkTextBuffer. They are generally statically allocated on the 92 * stack and only used for a short time. The model interface defines 93 * a set of operations using them for navigating the model. 94 * 95 * It is expected that models fill in the iterator with private data. 96 * For example, the #GtkListStore model, which is internally a simple 97 * linked list, stores a list node in one of the pointers. The 98 * #GtkTreeModelSort stores an array and an offset in two of the 99 * pointers. Additionally, there is an integer field. This field is 100 * generally filled with a unique stamp per model. This stamp is for 101 * catching errors resulting from using invalid iterators with a model. 102 * 103 * The lifecycle of an iterator can be a little confusing at first. 104 * Iterators are expected to always be valid for as long as the model 105 * is unchanged (and doesn’t emit a signal). The model is considered 106 * to own all outstanding iterators and nothing needs to be done to 107 * free them from the user’s point of view. Additionally, some models 108 * guarantee that an iterator is valid for as long as the node it refers 109 * to is valid (most notably the #GtkTreeStore and #GtkListStore). 110 * Although generally uninteresting, as one always has to allow for 111 * the case where iterators do not persist beyond a signal, some very 112 * important performance enhancements were made in the sort model. 113 * As a result, the #GTK_TREE_MODEL_ITERS_PERSIST flag was added to 114 * indicate this behavior. 115 * 116 * To help show some common operation of a model, some examples are 117 * provided. The first example shows three ways of getting the iter at 118 * the location `3:2:5`. While the first method shown is 119 * easier, the second is much more common, as you often get paths from 120 * callbacks. 121 * 122 * ## Acquiring a `GtkTreeIter` 123 * 124 * ```c 125 * // Three ways of getting the iter pointing to the location 126 * GtkTreePath *path; 127 * GtkTreeIter iter; 128 * GtkTreeIter parent_iter; 129 * 130 * // get the iterator from a string 131 * gtk_tree_model_get_iter_from_string (model, 132 * &iter, 133 * "3:2:5"); 134 * 135 * // get the iterator from a path 136 * path = gtk_tree_path_new_from_string ("3:2:5"); 137 * gtk_tree_model_get_iter (model, &iter, path); 138 * gtk_tree_path_free (path); 139 * 140 * // walk the tree to find the iterator 141 * gtk_tree_model_iter_nth_child (model, &iter, 142 * NULL, 3); 143 * parent_iter = iter; 144 * gtk_tree_model_iter_nth_child (model, &iter, 145 * &parent_iter, 2); 146 * parent_iter = iter; 147 * gtk_tree_model_iter_nth_child (model, &iter, 148 * &parent_iter, 5); 149 * ``` 150 * 151 * This second example shows a quick way of iterating through a list 152 * and getting a string and an integer from each row. The 153 * populate_model() function used below is not 154 * shown, as it is specific to the #GtkListStore. For information on 155 * how to write such a function, see the #GtkListStore documentation. 156 * 157 * ## Reading data from a `GtkTreeModel` 158 * 159 * ```c 160 * enum 161 * { 162 * STRING_COLUMN, 163 * INT_COLUMN, 164 * N_COLUMNS 165 * }; 166 * 167 * ... 168 * 169 * GtkTreeModel *list_store; 170 * GtkTreeIter iter; 171 * gboolean valid; 172 * int row_count = 0; 173 * 174 * // make a new list_store 175 * list_store = gtk_list_store_new (N_COLUMNS, 176 * G_TYPE_STRING, 177 * G_TYPE_INT); 178 * 179 * // Fill the list store with data 180 * populate_model (list_store); 181 * 182 * // Get the first iter in the list, check it is valid and walk 183 * // through the list, reading each row. 184 * 185 * valid = gtk_tree_model_get_iter_first (list_store, 186 * &iter); 187 * while (valid) 188 * { 189 * char *str_data; 190 * int int_data; 191 * 192 * // Make sure you terminate calls to gtk_tree_model_get() with a “-1” value 193 * gtk_tree_model_get (list_store, &iter, 194 * STRING_COLUMN, &str_data, 195 * INT_COLUMN, &int_data, 196 * -1); 197 * 198 * // Do something with the data 199 * g_print ("Row %d: (%s,%d)\n", 200 * row_count, str_data, int_data); 201 * g_free (str_data); 202 * 203 * valid = gtk_tree_model_iter_next (list_store, 204 * &iter); 205 * row_count++; 206 * } 207 * ``` 208 * 209 * The #GtkTreeModel interface contains two methods for reference 210 * counting: gtk_tree_model_ref_node() and gtk_tree_model_unref_node(). 211 * These two methods are optional to implement. The reference counting 212 * is meant as a way for views to let models know when nodes are being 213 * displayed. #GtkTreeView will take a reference on a node when it is 214 * visible, which means the node is either in the toplevel or expanded. 215 * Being displayed does not mean that the node is currently directly 216 * visible to the user in the viewport. Based on this reference counting 217 * scheme a caching model, for example, can decide whether or not to cache 218 * a node based on the reference count. A file-system based model would 219 * not want to keep the entire file hierarchy in memory, but just the 220 * folders that are currently expanded in every current view. 221 * 222 * When working with reference counting, the following rules must be taken 223 * into account: 224 * 225 * - Never take a reference on a node without owning a reference on its parent. 226 * This means that all parent nodes of a referenced node must be referenced 227 * as well. 228 * 229 * - Outstanding references on a deleted node are not released. This is not 230 * possible because the node has already been deleted by the time the 231 * row-deleted signal is received. 232 * 233 * - Models are not obligated to emit a signal on rows of which none of its 234 * siblings are referenced. To phrase this differently, signals are only 235 * required for levels in which nodes are referenced. For the root level 236 * however, signals must be emitted at all times (however the root level 237 * is always referenced when any view is attached). 238 */ 239 public interface TreeModelIF{ 240 /** Get the main Gtk struct */ 241 public GtkTreeModel* getTreeModelStruct(bool transferOwnership = false); 242 243 /** the main Gtk struct as a void* */ 244 protected void* getStruct(); 245 246 247 /** */ 248 public static GType getType() 249 { 250 return gtk_tree_model_get_type(); 251 } 252 253 /** 254 * Creates a new #GtkTreeModel, with @child_model as the child_model 255 * and @root as the virtual root. 256 * 257 * Params: 258 * root = A #GtkTreePath or %NULL. 259 * 260 * Returns: A new #GtkTreeModel. 261 */ 262 public TreeModelIF filterNew(TreePath root); 263 264 alias foreac = foreach_; 265 /** 266 * Calls func on each node in model in a depth-first fashion. 267 * 268 * If @func returns %TRUE, then the tree ceases to be walked, 269 * and gtk_tree_model_foreach() returns. 270 * 271 * Params: 272 * func = a function to be called on each row 273 * userData = user data to passed to @func 274 */ 275 public void foreach_(GtkTreeModelForeachFunc func, void* userData); 276 277 /** 278 * Returns the type of the column. 279 * 280 * Params: 281 * index = the column index 282 * 283 * Returns: the type of the column 284 */ 285 public GType getColumnType(int index); 286 287 /** 288 * Returns a set of flags supported by this interface. 289 * 290 * The flags are a bitwise combination of #GtkTreeModelFlags. 291 * The flags supported should not change during the lifetime 292 * of the @tree_model. 293 * 294 * Returns: the flags supported by this interface 295 */ 296 public GtkTreeModelFlags getFlags(); 297 298 /** 299 * Sets @iter to a valid iterator pointing to @path. If @path does 300 * not exist, @iter is set to an invalid iterator and %FALSE is returned. 301 * 302 * Params: 303 * iter = the uninitialized #GtkTreeIter-struct 304 * path = the #GtkTreePath-struct 305 * 306 * Returns: %TRUE, if @iter was set 307 */ 308 public bool getIter(out TreeIter iter, TreePath path); 309 310 /** 311 * Initializes @iter with the first iterator in the tree 312 * (the one at the path "0") and returns %TRUE. Returns 313 * %FALSE if the tree is empty. 314 * 315 * Params: 316 * iter = the uninitialized #GtkTreeIter-struct 317 * 318 * Returns: %TRUE, if @iter was set 319 */ 320 public bool getIterFirst(out TreeIter iter); 321 322 /** 323 * Sets @iter to a valid iterator pointing to @path_string, if it 324 * exists. Otherwise, @iter is left invalid and %FALSE is returned. 325 * 326 * Params: 327 * iter = an uninitialized #GtkTreeIter-struct 328 * pathString = a string representation of a #GtkTreePath-struct 329 * 330 * Returns: %TRUE, if @iter was set 331 */ 332 public bool getIterFromString(out TreeIter iter, string pathString); 333 334 /** 335 * Returns the number of columns supported by @tree_model. 336 * 337 * Returns: the number of columns 338 */ 339 public int getNColumns(); 340 341 /** 342 * Returns a newly-created #GtkTreePath-struct referenced by @iter. 343 * 344 * This path should be freed with gtk_tree_path_free(). 345 * 346 * Params: 347 * iter = the #GtkTreeIter-struct 348 * 349 * Returns: a newly-created #GtkTreePath-struct 350 */ 351 public TreePath getPath(TreeIter iter); 352 353 /** 354 * Generates a string representation of the iter. 355 * 356 * This string is a “:” separated list of numbers. 357 * For example, “4:10:0:3” would be an acceptable 358 * return value for this string. 359 * 360 * Params: 361 * iter = a #GtkTreeIter-struct 362 * 363 * Returns: a newly-allocated string. 364 * Must be freed with g_free(). 365 */ 366 public string getStringFromIter(TreeIter iter); 367 368 /** 369 * See gtk_tree_model_get(), this version takes a va_list 370 * for language bindings to use. 371 * 372 * Params: 373 * iter = a row in @tree_model 374 * varArgs = va_list of column/return location pairs 375 */ 376 public void getValist(TreeIter iter, void* varArgs); 377 378 /** 379 * Initializes and sets @value to that at @column. 380 * 381 * When done with @value, g_value_unset() needs to be called 382 * to free any allocated memory. 383 * 384 * Params: 385 * iter = the #GtkTreeIter-struct 386 * column = the column to lookup the value at 387 * value = an empty #GValue to set 388 */ 389 public void getValue(TreeIter iter, int column, out Value value); 390 391 /** 392 * Sets @iter to point to the first child of @parent. 393 * 394 * If @parent has no children, %FALSE is returned and @iter is 395 * set to be invalid. @parent will remain a valid node after this 396 * function has been called. 397 * 398 * If @parent is %NULL returns the first node, equivalent to 399 * `gtk_tree_model_get_iter_first (tree_model, iter);` 400 * 401 * Params: 402 * iter = the new #GtkTreeIter-struct to be set to the child 403 * parent = the #GtkTreeIter-struct, or %NULL 404 * 405 * Returns: %TRUE, if @iter has been set to the first child 406 */ 407 public bool iterChildren(out TreeIter iter, TreeIter parent); 408 409 /** 410 * Returns %TRUE if @iter has children, %FALSE otherwise. 411 * 412 * Params: 413 * iter = the #GtkTreeIter-struct to test for children 414 * 415 * Returns: %TRUE if @iter has children 416 */ 417 public bool iterHasChild(TreeIter iter); 418 419 /** 420 * Returns the number of children that @iter has. 421 * 422 * As a special case, if @iter is %NULL, then the number 423 * of toplevel nodes is returned. 424 * 425 * Params: 426 * iter = the #GtkTreeIter-struct, or %NULL 427 * 428 * Returns: the number of children of @iter 429 */ 430 public int iterNChildren(TreeIter iter); 431 432 /** 433 * Sets @iter to point to the node following it at the current level. 434 * 435 * If there is no next @iter, %FALSE is returned and @iter is set 436 * to be invalid. 437 * 438 * Params: 439 * iter = the #GtkTreeIter-struct 440 * 441 * Returns: %TRUE if @iter has been changed to the next node 442 */ 443 public bool iterNext(TreeIter iter); 444 445 /** 446 * Sets @iter to be the child of @parent, using the given index. 447 * 448 * The first index is 0. If @n is too big, or @parent has no children, 449 * @iter is set to an invalid iterator and %FALSE is returned. @parent 450 * will remain a valid node after this function has been called. As a 451 * special case, if @parent is %NULL, then the @n-th root node 452 * is set. 453 * 454 * Params: 455 * iter = the #GtkTreeIter-struct to set to the nth child 456 * parent = the #GtkTreeIter-struct to get the child from, or %NULL. 457 * n = the index of the desired child 458 * 459 * Returns: %TRUE, if @parent has an @n-th child 460 */ 461 public bool iterNthChild(out TreeIter iter, TreeIter parent, int n); 462 463 /** 464 * Sets @iter to be the parent of @child. 465 * 466 * If @child is at the toplevel, and doesn’t have a parent, then 467 * @iter is set to an invalid iterator and %FALSE is returned. 468 * @child will remain a valid node after this function has been 469 * called. 470 * 471 * @iter will be initialized before the lookup is performed, so @child 472 * and @iter cannot point to the same memory location. 473 * 474 * Params: 475 * iter = the new #GtkTreeIter-struct to set to the parent 476 * child = the #GtkTreeIter-struct 477 * 478 * Returns: %TRUE, if @iter is set to the parent of @child 479 */ 480 public bool iterParent(out TreeIter iter, TreeIter child); 481 482 /** 483 * Sets @iter to point to the previous node at the current level. 484 * 485 * If there is no previous @iter, %FALSE is returned and @iter is 486 * set to be invalid. 487 * 488 * Params: 489 * iter = the #GtkTreeIter-struct 490 * 491 * Returns: %TRUE if @iter has been changed to the previous node 492 */ 493 public bool iterPrevious(TreeIter iter); 494 495 /** 496 * Lets the tree ref the node. 497 * 498 * This is an optional method for models to implement. 499 * To be more specific, models may ignore this call as it exists 500 * primarily for performance reasons. 501 * 502 * This function is primarily meant as a way for views to let 503 * caching models know when nodes are being displayed (and hence, 504 * whether or not to cache that node). Being displayed means a node 505 * is in an expanded branch, regardless of whether the node is currently 506 * visible in the viewport. For example, a file-system based model 507 * would not want to keep the entire file-hierarchy in memory, 508 * just the sections that are currently being displayed by 509 * every current view. 510 * 511 * A model should be expected to be able to get an iter independent 512 * of its reffed state. 513 * 514 * Params: 515 * iter = the #GtkTreeIter-struct 516 */ 517 public void refNode(TreeIter iter); 518 519 /** 520 * Emits the #GtkTreeModel::row-changed signal on @tree_model. 521 * 522 * Params: 523 * path = a #GtkTreePath-struct pointing to the changed row 524 * iter = a valid #GtkTreeIter-struct pointing to the changed row 525 */ 526 public void rowChanged(TreePath path, TreeIter iter); 527 528 /** 529 * Emits the #GtkTreeModel::row-deleted signal on @tree_model. 530 * 531 * This should be called by models after a row has been removed. 532 * The location pointed to by @path should be the location that 533 * the row previously was at. It may not be a valid location anymore. 534 * 535 * Nodes that are deleted are not unreffed, this means that any 536 * outstanding references on the deleted node should not be released. 537 * 538 * Params: 539 * path = a #GtkTreePath-struct pointing to the previous location of 540 * the deleted row 541 */ 542 public void rowDeleted(TreePath path); 543 544 /** 545 * Emits the #GtkTreeModel::row-has-child-toggled signal on 546 * @tree_model. This should be called by models after the child 547 * state of a node changes. 548 * 549 * Params: 550 * path = a #GtkTreePath-struct pointing to the changed row 551 * iter = a valid #GtkTreeIter-struct pointing to the changed row 552 */ 553 public void rowHasChildToggled(TreePath path, TreeIter iter); 554 555 /** 556 * Emits the #GtkTreeModel::row-inserted signal on @tree_model. 557 * 558 * Params: 559 * path = a #GtkTreePath-struct pointing to the inserted row 560 * iter = a valid #GtkTreeIter-struct pointing to the inserted row 561 */ 562 public void rowInserted(TreePath path, TreeIter iter); 563 564 /** 565 * Emits the #GtkTreeModel::rows-reordered signal on @tree_model. 566 * 567 * This should be called by models when their rows have been 568 * reordered. 569 * 570 * Params: 571 * path = a #GtkTreePath-struct pointing to the tree node whose children 572 * have been reordered 573 * iter = a valid #GtkTreeIter-struct pointing to the node whose children 574 * have been reordered, or %NULL if the depth of @path is 0 575 * newOrder = an array of integers mapping the current position of 576 * each child to its old position before the re-ordering, 577 * i.e. @new_order`[newpos] = oldpos` 578 */ 579 public void rowsReordered(TreePath path, TreeIter iter, int* newOrder); 580 581 /** 582 * Emits the #GtkTreeModel::rows-reordered signal on @tree_model. 583 * 584 * This should be called by models when their rows have been 585 * reordered. 586 * 587 * Params: 588 * path = a #GtkTreePath-struct pointing to the tree node whose children 589 * have been reordered 590 * iter = a valid #GtkTreeIter-struct pointing to the node 591 * whose children have been reordered, or %NULL if the depth 592 * of @path is 0 593 * newOrder = an array of integers 594 * mapping the current position of each child to its old 595 * position before the re-ordering, 596 * i.e. @new_order`[newpos] = oldpos` 597 */ 598 public void rowsReorderedWithLength(TreePath path, TreeIter iter, int[] newOrder); 599 600 /** 601 * Lets the tree unref the node. 602 * 603 * This is an optional method for models to implement. 604 * To be more specific, models may ignore this call as it exists 605 * primarily for performance reasons. For more information on what 606 * this means, see gtk_tree_model_ref_node(). 607 * 608 * Please note that nodes that are deleted are not unreffed. 609 * 610 * Params: 611 * iter = the #GtkTreeIter-struct 612 */ 613 public void unrefNode(TreeIter iter); 614 615 /** 616 * This signal is emitted when a row in the model has changed. 617 * 618 * Params: 619 * path = a #GtkTreePath-struct identifying the changed row 620 * iter = a valid #GtkTreeIter-struct pointing to the changed row 621 */ 622 gulong addOnRowChanged(void delegate(TreePath, TreeIter, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 623 624 /** 625 * This signal is emitted when a row has been deleted. 626 * 627 * Note that no iterator is passed to the signal handler, 628 * since the row is already deleted. 629 * 630 * This should be called by models after a row has been removed. 631 * The location pointed to by @path should be the location that 632 * the row previously was at. It may not be a valid location anymore. 633 * 634 * Params: 635 * path = a #GtkTreePath-struct identifying the row 636 */ 637 gulong addOnRowDeleted(void delegate(TreePath, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 638 639 /** 640 * This signal is emitted when a row has gotten the first child 641 * row or lost its last child row. 642 * 643 * Params: 644 * path = a #GtkTreePath-struct identifying the row 645 * iter = a valid #GtkTreeIter-struct pointing to the row 646 */ 647 gulong addOnRowHasChildToggled(void delegate(TreePath, TreeIter, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 648 649 /** 650 * This signal is emitted when a new row has been inserted in 651 * the model. 652 * 653 * Note that the row may still be empty at this point, since 654 * it is a common pattern to first insert an empty row, and 655 * then fill it with the desired values. 656 * 657 * Params: 658 * path = a #GtkTreePath-struct identifying the new row 659 * iter = a valid #GtkTreeIter-struct pointing to the new row 660 */ 661 gulong addOnRowInserted(void delegate(TreePath, TreeIter, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 662 663 /** 664 * This signal is emitted when the children of a node in the 665 * #GtkTreeModel have been reordered. 666 * 667 * Note that this signal is not emitted 668 * when rows are reordered by DND, since this is implemented 669 * by removing and then reinserting the row. 670 * 671 * Params: 672 * path = a #GtkTreePath-struct identifying the tree node whose children 673 * have been reordered 674 * iter = a valid #GtkTreeIter-struct pointing to the node whose children 675 * have been reordered, or %NULL if the depth of @path is 0 676 * newOrder = an array of integers mapping the current position 677 * of each child to its old position before the re-ordering, 678 * i.e. @new_order`[newpos] = oldpos` 679 */ 680 gulong addOnRowsReordered(void delegate(TreePath, TreeIter, void*, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 681 }