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