1. ----------------------------------------------------------------------- 
  2. --              GtkAda - Ada95 binding for Gtk+/Gnome                -- 
  3. --                                                                   -- 
  4. --                Copyright (C) 2001-2006 AdaCore                    -- 
  5. --                                                                   -- 
  6. -- This library is free software; you can redistribute it and/or     -- 
  7. -- modify it under the terms of the GNU General Public               -- 
  8. -- License as published by the Free Software Foundation; either      -- 
  9. -- version 2 of the License, or (at your option) any later version.  -- 
  10. --                                                                   -- 
  11. -- This library is distributed in the hope that it will be useful,   -- 
  12. -- but WITHOUT ANY WARRANTY; without even the implied warranty of    -- 
  13. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU -- 
  14. -- General Public License for more details.                          -- 
  15. --                                                                   -- 
  16. -- You should have received a copy of the GNU General Public         -- 
  17. -- License along with this library; if not, write to the             -- 
  18. -- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      -- 
  19. -- Boston, MA 02111-1307, USA.                                       -- 
  20. --                                                                   -- 
  21. -- -- -- -- -- -- -- -- -- -- -- --
  22. ----------------------------------------------------------------------- 
  23.  
  24. --  <description> 
  25. --  The Gtk_Tree_Model_Sort is a model which implements the Gtk_Tree_Sortable 
  26. --  interface. It does not hold any data itself, but rather is created with 
  27. --  child model and proxies its data. It has identical column types to this 
  28. --  child model, and the changes in the child are propagated. The primary 
  29. --  purpose of this model is to provide a way to sort a different model without 
  30. --  modifying it. Note that the sort function used by Gtk_Tree_Model_Sort is 
  31. --  not guaranteed to be stable. 
  32. -- 
  33. --  The use of this is best demonstrated through an example. In the following 
  34. --  sample code we create two Gtk_Tree_View widgets each with a view of the 
  35. --  same data. As the model is wrapped here by a Gtk_Tree_Model_Sort, the two 
  36. --  Gtk_Tree_Views can each sort their view of the data without affecting the 
  37. --  other. By contrast, if we simply put the same model in each widget, then 
  38. --  sorting the first would sort the second. 
  39. -- 
  40. --  declare 
  41. --     Tree_View1, Tree_View2   : Gtk_Tree_View; 
  42. --     Sort_Model1, Sort_Model2 : Gtk_Tree_Model_Sort; 
  43. --     Child_Model              : Gtk_Tree_Model; 
  44. --  begin 
  45. --    Child_Model := Get_My_Model;  --  Your own implementation 
  46. -- 
  47. --    --  Create the first tree 
  48. --    Gtk_New_With_Model (Sort_Model1, Child_Model); 
  49. --    Gtk_New (Tree_View1, Sort_Model1); 
  50. --    Set_Sort_Column_Id (Sort_Model1, COLUMN1, Sort_Ascending); 
  51. -- 
  52. --    --  Create the second tree 
  53. --    Gtk_New_With_Model (Sort_Model2, Child_Model); 
  54. --    Gtk_New (Tree_View2, Sort_Model2); 
  55. --    Set_Sort_Column_Id (Sort_Model2, COLUMN1, Sort_Descending); 
  56. --  end; 
  57. -- 
  58. --  To demonstrate how to access the underlying child model from the sort 
  59. --  model, the next example will be a callback for the Gtk_Tree_Selection 
  60. --  "changed" signal. In this callback, we get a string from COLUMN_1 of the 
  61. --  model. We then modify the string, find the same selected row on the child 
  62. --  model, and change the row there. 
  63. -- 
  64. --  procedure Selection_Changed 
  65. --    (Selection : access Gtk_Tree_Selection_Record'Class) 
  66. --  is 
  67. --     Sort_Model, Child_Model : Gtk_Tree_Model; 
  68. --     Sort_Iter, Child_Iter   : Gtk_Tree_Iter; 
  69. --  begin 
  70. --     --  Get the currently selected row and the model 
  71. --     Get_Selected (Selection, Sort_Model, Sort_Iter); 
  72. --     if Sort_Iter = Null_Iter then 
  73. --        return; 
  74. --     end if; 
  75. -- 
  76. --     --  Lookup the current value on the selected row 
  77. --     declare 
  78. --       Some_Data : constant String := 
  79. --                     Get_String (Sort_Model, Sort_Iter, COLUMN1); 
  80. --     begin 
  81. --        --  Get an iterator on the child model instead of the sort model 
  82. --        Convert_Iter_To_Child_Iter (Sort_Model, Child_Iter, Sort_Iter); 
  83. -- 
  84. --        --  Get the child model and change the value in the row 
  85. --        --  In this example, the model is a Gtk_List_Store, but it could be 
  86. --        --  anything 
  87. --        Child_Model := Get_Model (Gtk_Sort_Model (Sort_Model)); 
  88. --        Set (Ctk_List_Store (Child_Model), Child_Iter, COLUMN1, "data"); 
  89. --     end; 
  90. --  end Selection_Changed; 
  91. --  </description> 
  92. --  <c_version>2.8.17</c_version> 
  93. --  <group>Trees and Lists</group> 
  94.  
  95. with Glib.Properties; 
  96. with Glib.Types; 
  97. with Gtk; 
  98. with Gtk.Tree_Dnd; 
  99. with Gtk.Tree_Model; 
  100. with Gtk.Tree_Sortable; 
  101.  
  102. package Gtk.Tree_Model_Sort is 
  103.  
  104.    type Gtk_Tree_Model_Sort_Record is 
  105.      new Gtk.Tree_Model.Gtk_Tree_Model_Record with null record; 
  106.    type Gtk_Tree_Model_Sort is access all Gtk_Tree_Model_Sort_Record'Class; 
  107.  
  108.    procedure Gtk_New_With_Model 
  109.      (Sort_Model  : out Gtk_Tree_Model_Sort; 
  110.       Child_Model : access Gtk.Tree_Model.Gtk_Tree_Model_Record'Class); 
  111.    procedure Initialize_With_Model 
  112.      (Sort_Model  : access Gtk_Tree_Model_Sort_Record'Class; 
  113.       Child_Model : access Gtk.Tree_Model.Gtk_Tree_Model_Record'Class); 
  114.    --  Creates or initialized a new sortable tree model, with Child_Model as 
  115.    --  the child model. 
  116.    --  Any change in Child_Model is reflected into Sort_Model 
  117.  
  118.    function Get_Type return Glib.GType; 
  119.    --  Return the internal type associated with a Gtk_Tree_Model_Sort. 
  120.  
  121.    function Get_Model 
  122.      (Tree_Model : access Gtk_Tree_Model_Sort_Record) 
  123.       return Gtk.Tree_Model.Gtk_Tree_Model; 
  124.    --  Return the model the Gtk_Tree_Model_Sort is sorting. 
  125.  
  126.    function Convert_Child_Path_To_Path 
  127.      (Tree_Model_Sort : access Gtk_Tree_Model_Sort_Record; 
  128.       Child_Path      : Gtk.Tree_Model.Gtk_Tree_Path) 
  129.       return Gtk.Tree_Model.Gtk_Tree_Path; 
  130.    --  Convert Child_Path to a path relative to Tree_Model_Sort. 
  131.    --  That is, Child_Path points to a path in the child model. 
  132.    --  The returned path will point to the same row in the sorted model. 
  133.    --  If Child_Path isn't a valid path on the child model, then Null 
  134.    --  is returned. 
  135.    --  The returned value must be freed with Path_Free. 
  136.  
  137.    procedure Convert_Child_Iter_To_Iter 
  138.      (Tree_Model_Sort : access Gtk_Tree_Model_Sort_Record; 
  139.       Sort_Iter       : out Gtk.Tree_Model.Gtk_Tree_Iter; 
  140.       Child_Iter      : Gtk.Tree_Model.Gtk_Tree_Iter); 
  141.    --  Set Sort_Iter to point to the row in Tree_Model_Sort that 
  142.    --  corresponds to the row pointed at by Child_Iter. 
  143.  
  144.    function Convert_Path_To_Child_Path 
  145.      (Tree_Model_Sort : access Gtk_Tree_Model_Sort_Record; 
  146.       Sorted_Path     : Gtk.Tree_Model.Gtk_Tree_Path) 
  147.       return Gtk.Tree_Model.Gtk_Tree_Path; 
  148.    --  Convert Sort_Path to a path on the child model of Tree_Model_Sort. 
  149.    --  That is, Sort_Path points ot a location in Tree_Model_Sort. 
  150.    --  The returned path will point to the same location in the model 
  151.    --  not being sorted. 
  152.  
  153.    procedure Convert_Iter_To_Child_Iter 
  154.      (Tree_Model_Sort : access Gtk_Tree_Model_Sort_Record; 
  155.       Child_Iter      : out Gtk.Tree_Model.Gtk_Tree_Iter; 
  156.       Sorted_Iter     : Gtk.Tree_Model.Gtk_Tree_Iter); 
  157.    --  Set Child_Iter to point to the row pointed to by Sorted_Iter. 
  158.  
  159.    procedure Reset_Default_Sort_Func 
  160.      (Tree_Model_Sort : access Gtk_Tree_Model_Sort_Record); 
  161.    --  This resets the default sort function to be in the 'unsorted' state. 
  162.    --  That is, it is in the same order as the child model. It will re-sort the 
  163.    --  model to be in the same order as the child model only if the 
  164.    --  Gtk_Tree_Model_Sort is in 'unsorted' state. 
  165.  
  166.    procedure Clear_Cache (Tree_Model_Sort : access Gtk_Tree_Model_Sort_Record); 
  167.    --  This function should almost never be called. It clears the 
  168.    --  tree_model_sort of any cached iterators that haven't been reffed with 
  169.    --  gtk.tree_model.ref_node. This might be useful if the child model being 
  170.    --  sorted is static (and doesn't change often) and there has been a lot of 
  171.    --  unreffed access to nodes. As a side effect of this function, all 
  172.    --  unreffed iters will be invalid. 
  173.  
  174.    function Iter_Is_Valid 
  175.      (Tree_Model_Sort : access Gtk_Tree_Model_Sort_Record; 
  176.       Iter            : Gtk.Tree_Model.Gtk_Tree_Iter) return Boolean; 
  177.    --  WARNING: this function is slow. Only use if for debugging and/or 
  178.    --  testing purposes. 
  179.    --  Checks if the given iter is a valid iter for this model. 
  180.  
  181.    ---------------- 
  182.    -- Interfaces -- 
  183.    ---------------- 
  184.    --  This class implements several interfaces. See Glib.Types 
  185.    -- 
  186.    --  - "Gtk_Tree_Sortable" 
  187.    --    This interface allows you to specify your own sort function 
  188.    -- 
  189.    --  - "Gtk_Tree_Drag_Source" 
  190.    --    This interface allows this widget to act as a dnd source 
  191.  
  192.    package Implements_Tree_Sortable is new Glib.Types.Implements 
  193.      (Gtk.Tree_Sortable.Gtk_Tree_Sortable, 
  194.       Gtk_Tree_Model_Sort_Record, 
  195.       Gtk_Tree_Model_Sort); 
  196.    function "+" 
  197.      (Model : access Gtk_Tree_Model_Sort_Record'Class) 
  198.       return Gtk.Tree_Sortable.Gtk_Tree_Sortable 
  199.       renames Implements_Tree_Sortable.To_Interface; 
  200.    function "-" 
  201.      (Sortable : Gtk.Tree_Sortable.Gtk_Tree_Sortable) 
  202.       return Gtk_Tree_Model_Sort 
  203.       renames Implements_Tree_Sortable.To_Object; 
  204.    --  Converts to and from the Gtk_Tree_Sortable interface 
  205.  
  206.    package Implements_Drag_Source is new Glib.Types.Implements 
  207.      (Gtk.Tree_Dnd.Gtk_Tree_Drag_Source, 
  208.       Gtk_Tree_Model_Sort_Record, 
  209.       Gtk_Tree_Model_Sort); 
  210.    function "+" 
  211.      (Model : access Gtk_Tree_Model_Sort_Record'Class) 
  212.       return Gtk.Tree_Dnd.Gtk_Tree_Drag_Source 
  213.       renames Implements_Drag_Source.To_Interface; 
  214.    function "-" 
  215.      (Drag_Source : Gtk.Tree_Dnd.Gtk_Tree_Drag_Source) 
  216.       return Gtk_Tree_Model_Sort 
  217.       renames Implements_Drag_Source.To_Object; 
  218.    --  Converts to and from the Gtk_Tree_Drag_Source interface 
  219.  
  220.    ------------- 
  221.    -- Signals -- 
  222.    ------------- 
  223.  
  224.    --  <signals> 
  225.    --  The following new signals are defined for this widget: 
  226.    -- 
  227.    --  </signals> 
  228.  
  229.    ---------------- 
  230.    -- Properties -- 
  231.    ---------------- 
  232.  
  233.    --  <properties> 
  234.    --  The following properties are defined for this widget. See 
  235.    --  Glib.Properties for more information on properties. 
  236.    -- 
  237.    --  Name:  Model_Property 
  238.    --  Type:  Object 
  239.    --  Descr: The model for the TreeModelSort to sort 
  240.    -- 
  241.    --  </properties> 
  242.  
  243.    Model_Property : constant Glib.Properties.Property_Object; 
  244.  
  245. private 
  246.    Model_Property : constant Glib.Properties.Property_Object := 
  247.      Glib.Properties.Build ("model"); 
  248.  
  249.    pragma Import (C, Get_Type, "gtk_tree_model_sort_get_type"); 
  250. end Gtk.Tree_Model_Sort;