1. ----------------------------------------------------------------------- 
  2. --              GtkAda - Ada95 binding for Gtk+/Gnome                -- 
  3. --                                                                   -- 
  4. --                Copyright (C) 2001-2007, 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. --  This package implements a specific model to store your data in. It is 
  26. --  basically similar to a small database, in that each field can contain any 
  27. --  number of columns. 
  28. -- 
  29. --  Each column can contain a different type of data, specified when the model 
  30. --  is created. 
  31. -- 
  32. --  Adding new values in the model is done as in the example at the end. 
  33. --  </description> 
  34. --  <c_version>2.8.17</c_version> 
  35. --  <group>Trees and Lists</group> 
  36.  
  37. with Glib.Types; 
  38. with Glib.Values; use Glib.Values; 
  39. with Gtk; 
  40. with Gtk.Tree_Dnd; 
  41. with Gtk.Tree_Model; 
  42. with Gtk.Tree_Sortable; 
  43.  
  44. package Gtk.Tree_Store is 
  45.  
  46.    type Gtk_Tree_Store_Record is 
  47.      new Gtk.Tree_Model.Gtk_Tree_Model_Record with null record; 
  48.    type Gtk_Tree_Store is access all Gtk_Tree_Store_Record'Class; 
  49.  
  50.    procedure Gtk_New 
  51.      (Tree_Store : out Gtk_Tree_Store; 
  52.       Types      : GType_Array); 
  53.    --  Create a new tree store using Types to fill the columns. 
  54.  
  55.    procedure Initialize 
  56.      (Tree_Store : access Gtk_Tree_Store_Record'Class; 
  57.       Types      : GType_Array); 
  58.    --  Internal initialization function. 
  59.    --  See the section "Creating your own widgets" in the documentation. 
  60.  
  61.    function Get_Type return Glib.GType; 
  62.    --  Return the internal value associated with this widget. 
  63.  
  64.    procedure Set_Column_Types 
  65.      (Tree_Store : access Gtk_Tree_Store_Record; 
  66.       Types      : GType_Array); 
  67.    --  This function is meant primarily for GObjects that inherit from 
  68.    --  Gtk_Tree_Store, and should only be used when constructing a new 
  69.    --  Gtk_Tree_Store. It will not function after a row has been added, or a 
  70.    --  method on the Gtk_Tree_Model interface is called. 
  71.  
  72.    procedure Set_Value 
  73.      (Tree_Store : access Gtk_Tree_Store_Record; 
  74.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  75.       Column     : Gint; 
  76.       Value      : Glib.Values.GValue); 
  77.    --  Set a new value in the model. The value is added in the column Column, 
  78.    --  and in the line Iter. 
  79.    --  This is the most general of the Set procedures, since it allows you to 
  80.    --  control what should be done when the cell is freed (useful for instance 
  81.    --  for reference-controlled types). In particular, you would create a 
  82.    --  GValue of a special type derived from Boxed (see Glib.Value.Set_Boxed). 
  83.    -- 
  84.    --  The type of the column must be of the type stored in the GValue itself. 
  85.    --  Referencing the example given for Set_Boxed, this would be the value 
  86.    --  in "Typ". 
  87.  
  88.    generic 
  89.       type Data_Type is private; 
  90.    package Generic_Set is 
  91.       type Data_Type_Access is access all Data_Type; 
  92.  
  93.       procedure Set 
  94.         (Tree_Store : access Gtk_Tree_Store_Record'Class; 
  95.          Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  96.          Column     : Gint; 
  97.          Value      : Data_Type_Access); 
  98.       --  Generic procedure used to store access objects in the model. 
  99.       --  For GObject and all of its descendents (including all widgets), 
  100.       --  you should use the Set procedure below that takes a GObject as 
  101.       --  parameter. 
  102.       -- 
  103.       --  Please see the example at the end for more information on how to 
  104.       --  create your own Set procedures adapted to your model. Also consider 
  105.       --  using Set_Value for complex cases 
  106.  
  107.       function Get 
  108.         (Tree_Store : access Gtk_Tree_Store_Record'Class; 
  109.          Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  110.          Column     : Gint) return Data_Type_Access; 
  111.       --  Generic procedure used to get access objects back from the model. 
  112.       --  For GObject and all of its descendents (including all widgets), 
  113.       --  you should use the Get_Object function defined in Gtk-Tree_Model 
  114.       --  that returns a GObject. 
  115.  
  116.    end Generic_Set; 
  117.  
  118.    procedure Set 
  119.      (Tree_Store : access Gtk_Tree_Store_Record; 
  120.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  121.       Column     : Gint; 
  122.       Value      : UTF8_String); 
  123.    --  Same as Generic_Set, but tailored to use with a string. 
  124.  
  125.    procedure Set 
  126.      (Tree_Store : access Gtk_Tree_Store_Record; 
  127.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  128.       Column     : Gint; 
  129.       Value      : Boolean); 
  130.    --  Same as Generic_Set, but tailored to use with a boolean. 
  131.  
  132.    procedure Set 
  133.      (Tree_Store : access Gtk_Tree_Store_Record; 
  134.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  135.       Column     : Gint; 
  136.       Value      : Gint); 
  137.    --  Same as Generic_Set, but tailored to use with an integer. 
  138.  
  139.    procedure Set 
  140.      (Tree_Store : access Gtk_Tree_Store_Record; 
  141.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  142.       Column     : Gint; 
  143.       Value      : Glib.C_Proxy); 
  144.    --  Same as Generic_Set, but tailored for Gdk types. 
  145.  
  146.    procedure Set 
  147.      (Tree_Store : access Gtk_Tree_Store_Record; 
  148.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  149.       Column     : Gint; 
  150.       Address    : System.Address); 
  151.    --  Same as Generic_Set, for a generic address 
  152.  
  153.    procedure Set 
  154.      (Tree_Store : access Gtk_Tree_Store_Record; 
  155.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  156.       Column     : Gint; 
  157.       Value      : Glib.Object.GObject); 
  158.    --  Same as Generic_Set, but tailored to objects/widgets. 
  159.  
  160.    procedure Remove 
  161.      (Tree_Store : access Gtk_Tree_Store_Record; 
  162.       Iter       : in out Gtk.Tree_Model.Gtk_Tree_Iter); 
  163.    --  Remove Iter from Tree_Store. 
  164.    --  After being removed, Iter is set to Null_Iter. 
  165.  
  166.    procedure Insert 
  167.      (Tree_Store : access Gtk_Tree_Store_Record; 
  168.       Iter       : in out Gtk.Tree_Model.Gtk_Tree_Iter; 
  169.       Parent     : Gtk.Tree_Model.Gtk_Tree_Iter; 
  170.       Position   : Gint); 
  171.    --  Create a new row at Position. 
  172.    --  If parent is non-null, then the row will be made a child of Parent. 
  173.    --  Otherwise, the row will be created at the toplevel. If Position is 
  174.    --  larger than the number of rows at that level, then the new row will be 
  175.    --  inserted to the end of the list. Iter will be changed to point to this 
  176.    --  new row. The row will be empty before this function is called. To fill 
  177.    --  in values, you need to call Set_Value. 
  178.  
  179.    procedure Insert_Before 
  180.      (Tree_Store : access Gtk_Tree_Store_Record; 
  181.       Iter       : in out Gtk.Tree_Model.Gtk_Tree_Iter; 
  182.       Parent     : Gtk.Tree_Model.Gtk_Tree_Iter; 
  183.       Sibling    : Gtk.Tree_Model.Gtk_Tree_Iter); 
  184.    --  Insert a new row before Sibling. 
  185.    --  If Sibling is Null_Iter, then the row will be appended to the beginning 
  186.    --  of the Parent's children. If Parent and Sibling are Null_Iter, then the 
  187.    --  row will be appended to the toplevel. If both Sibling and Parent are 
  188.    --  set, then Parent must be the parent of Sibling. When Sibling is set, 
  189.    --  Parent is optional. Iter will be changed to point to this new row. The 
  190.    --  row will be empty after this function is called. To fill in values, you 
  191.    --  need to call Set_Value. 
  192.  
  193.    procedure Insert_After 
  194.      (Tree_Store : access Gtk_Tree_Store_Record; 
  195.       Iter       : in out Gtk.Tree_Model.Gtk_Tree_Iter; 
  196.       Parent     : Gtk.Tree_Model.Gtk_Tree_Iter; 
  197.       Sibling    : Gtk.Tree_Model.Gtk_Tree_Iter); 
  198.    --  Insert a new row after Sibling. 
  199.    --  If Sibling is Null_Iter, then the row will be prepended to the beginning 
  200.    --  of the Parent's children. If Parent and Sibling are Null_Iter, then the 
  201.    --  row will be prepended to the toplevel. If both Sibling and Parent are 
  202.    --  set, then Parent must be the parent of Sibling. When Sibling is set, 
  203.    --  Parent is optional. Iter will be changed to point to this new row. The 
  204.    --  row will be empty after this function is called. To fill in values, you 
  205.    --  need to call Set_Value. 
  206.  
  207.    procedure Prepend 
  208.      (Tree_Store : access Gtk_Tree_Store_Record; 
  209.       Iter       : in out Gtk.Tree_Model.Gtk_Tree_Iter; 
  210.       Parent     : Gtk.Tree_Model.Gtk_Tree_Iter); 
  211.    --  Prepend a new row to Tree_Store. 
  212.    --  If Parent is non-null, then it will prepend the new row before the first 
  213.    --  child of Parent, otherwise it will prepend a row to the top level. Iter 
  214.    --  will be changed to point to this new row. The row will be empty after 
  215.    --  this function is called. To fill in values, you need to call Set_Value. 
  216.    --  The efficiency of this procedure is O(N). 
  217.  
  218.    procedure Append 
  219.      (Tree_Store : access Gtk_Tree_Store_Record; 
  220.       Iter       : in out Gtk.Tree_Model.Gtk_Tree_Iter; 
  221.       Parent     : Gtk.Tree_Model.Gtk_Tree_Iter); 
  222.    --  Append a new row to Tree_Store. 
  223.    --  If Parent is non-null, then it will append the new row after the last 
  224.    --  child of Parent, otherwise it will append a row to the top level. Iter 
  225.    --  will be changed to point to this new row. The row will be empty after 
  226.    --  this function is called. To fill in values, you need to call Set_Value. 
  227.    --  The efficiency of this procedure is O(N^2). 
  228.  
  229.    function Is_Ancestor 
  230.      (Tree_Store : access Gtk_Tree_Store_Record; 
  231.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  232.       Descendant : Gtk.Tree_Model.Gtk_Tree_Iter) return Boolean; 
  233.    --  Return True if Iter is an ancestor of Descendant. 
  234.    --  That is, Iter is the parent (or grandparent or great-grandparent) of 
  235.    --  Descendant. 
  236.  
  237.    function Iter_Depth 
  238.      (Tree_Store : access Gtk_Tree_Store_Record; 
  239.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter) return Gint; 
  240.    --  Returns the depth of Iter. 
  241.    --  This will be 0 for anything on the root level, 1 for anything down a 
  242.    --  level, etc. 
  243.  
  244.    function Iter_Is_Valid 
  245.      (Tree_Store : access Gtk_Tree_Store_Record; 
  246.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter) 
  247.       return Boolean; 
  248.    --  WARNING: This function is slow. Only use it for debugging and/or testing 
  249.    --  purposes. 
  250.    --  Checks if the given iter is a valid iter for Tree_Store. 
  251.  
  252.    procedure Move_After 
  253.      (Tree_Store : access Gtk_Tree_Store_Record; 
  254.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  255.       Position   : Gtk.Tree_Model.Gtk_Tree_Iter); 
  256.    --  Moves the row pointed to by Iter to the position after Position. Iter 
  257.    --  and Position should be in the same level. Note that this function only 
  258.    --  works with unsorted stores. If Position is Null_Iter, Iter will be 
  259.    --  moved to the start of the level. 
  260.  
  261.    procedure Move_Before 
  262.      (Tree_Store : access Gtk_Tree_Store_Record; 
  263.       Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  264.       Position   : Gtk.Tree_Model.Gtk_Tree_Iter); 
  265.    --  Moves the row pointed to by Iter to the position before Position. Iter 
  266.    --  and Position should be in the same level. Note that this function only 
  267.    --  works with unsorted stores. If Position is Null_Iter, Iter will be 
  268.  
  269.    procedure Clear (Tree_Store : access Gtk_Tree_Store_Record); 
  270.    --  Removes all rows from Tree_Store 
  271.  
  272.    procedure Reorder 
  273.      (Tree_Store : access Gtk_Tree_Store_Record; 
  274.       Parent     : Gtk.Tree_Model.Gtk_Tree_Iter; 
  275.       New_Order  : Glib.Gint_Array); 
  276.    --  Reorders the children of Parent to follow the order indicated by 
  277.    --  New_order. Note that this function only works with unsorted stores. New 
  278.    --  order is an array of integers mapping the new position of each child to 
  279.    --  its old position before the re-ordering, 
  280.    --  i.e. New_order[newpos] = oldpos 
  281.    --  If Parent is Null_Iter, it also reorders the root nodes 
  282.  
  283.    procedure Swap 
  284.      (Tree_Store : access Gtk_Tree_Store_Record; 
  285.       A          : Gtk.Tree_Model.Gtk_Tree_Iter; 
  286.       B          : Gtk.Tree_Model.Gtk_Tree_Iter); 
  287.    --  Swaps the rows pointed to by A and B (in the same level). Note that this 
  288.    --  function only works with unsorted stores. 
  289.  
  290.    --------------------------- 
  291.    -- Sorting Freeze / Thaw -- 
  292.    --------------------------- 
  293.  
  294.    --  Note: the following two functions are not part of the Gtk+ API, but 
  295.    --  are provided by GtkAda. 
  296.  
  297.    function Freeze_Sort 
  298.      (Tree : access Gtk.Tree_Store.Gtk_Tree_Store_Record'Class) 
  299.       return Gint; 
  300.    --  Freeze the sorting in the tree view, and returns the current 
  301.    --  sort_column_id, which should be used when thawing. (See Thaw_Sort) 
  302.  
  303.    procedure Thaw_Sort 
  304.      (Tree      : access Gtk.Tree_Store.Gtk_Tree_Store_Record'Class; 
  305.       Column_Id : Gint); 
  306.    --  Thaw a frozen tree_view. Column_Id should be the value returned by 
  307.    --  the corresponding call to Freeze_Sort. 
  308.  
  309.    ---------------- 
  310.    -- Interfaces -- 
  311.    ---------------- 
  312.    --  This class implements several interfaces. See Glib.Types 
  313.    -- 
  314.    --  - "Gtk_Tree_Sortable" 
  315.    --    This interface allows you to specify your own sort function 
  316.    -- 
  317.    --  - "Gtk_Tree_Drag_Source" 
  318.    --    This interface allows this widget to act as a dnd source 
  319.    -- 
  320.    --  - "Gtk_Tree_Drag_Dest" 
  321.    --    This interface allows this widget to act as a dnd destination 
  322.  
  323.    package Implements_Tree_Sortable is new Glib.Types.Implements 
  324.      (Gtk.Tree_Sortable.Gtk_Tree_Sortable, 
  325.       Gtk_Tree_Store_Record, 
  326.       Gtk_Tree_Store); 
  327.    function "+" 
  328.      (Model : access Gtk_Tree_Store_Record'Class) 
  329.       return Gtk.Tree_Sortable.Gtk_Tree_Sortable 
  330.       renames Implements_Tree_Sortable.To_Interface; 
  331.    function "-" 
  332.      (Sortable : Gtk.Tree_Sortable.Gtk_Tree_Sortable) 
  333.       return Gtk_Tree_Store 
  334.       renames Implements_Tree_Sortable.To_Object; 
  335.    --  Converts to and from the Gtk_Tree_Sortable interface 
  336.  
  337.    package Implements_Drag_Source is new Glib.Types.Implements 
  338.      (Gtk.Tree_Dnd.Gtk_Tree_Drag_Source, 
  339.       Gtk_Tree_Store_Record, 
  340.       Gtk_Tree_Store); 
  341.    function "+" 
  342.      (Model : access Gtk_Tree_Store_Record'Class) 
  343.       return Gtk.Tree_Dnd.Gtk_Tree_Drag_Source 
  344.       renames Implements_Drag_Source.To_Interface; 
  345.    function "-" 
  346.      (Drag_Source : Gtk.Tree_Dnd.Gtk_Tree_Drag_Source) 
  347.       return Gtk_Tree_Store 
  348.       renames Implements_Drag_Source.To_Object; 
  349.    --  Converts to and from the Gtk_Tree_Drag_Source interface 
  350.  
  351.    package Implements_Drag_Dest is new Glib.Types.Implements 
  352.      (Gtk.Tree_Dnd.Gtk_Tree_Drag_Dest, 
  353.       Gtk_Tree_Store_Record, 
  354.       Gtk_Tree_Store); 
  355.    function "+" 
  356.      (Model : access Gtk_Tree_Store_Record'Class) 
  357.       return Gtk.Tree_Dnd.Gtk_Tree_Drag_Dest 
  358.       renames Implements_Drag_Dest.To_Interface; 
  359.    function "-" 
  360.      (Drag_Dest : Gtk.Tree_Dnd.Gtk_Tree_Drag_Dest) 
  361.       return Gtk_Tree_Store 
  362.       renames Implements_Drag_Dest.To_Object; 
  363.    --  Converts to and from the Gtk_Tree_Drag_Source interface 
  364.  
  365. private 
  366.    pragma Import (C, Get_Type, "gtk_tree_store_get_type"); 
  367. end Gtk.Tree_Store; 
  368.  
  369. --  <example> 
  370. --  Adding a new line in the model: 
  371. -- 
  372. --  declare 
  373. --     Iter  : Gtk_Text_Iter; 
  374. --     Value : Glib.Values.GValue; 
  375. --  begin 
  376. --     Append (Model, Iter, Null_Iter); 
  377. -- 
  378. --     --  First method: 
  379. -- 
  380. --     Init (Value, GType_String); 
  381. --     Set_String (Value, "foo"); 
  382. --     Set_Value (Model, Iter, 0, Value); 
  383. --     Unref (Value); 
  384. -- 
  385. --     --  Second method: 
  386. -- 
  387. --     Set (Model, Iter, 0, "foo"); 
  388. --  end; 
  389. -- 
  390. --  </example> 
  391.  
  392. --  <example> 
  393. --  Defining your own Set function for your model: This can be done by directly 
  394. --  importing the C function, with the appropriate number of parameters. 
  395. --  Remember that you are passing data directly to C, thus you need to end 
  396. --  strings with ASCII.NUL 
  397. -- 
  398. --  procedure My_Set 
  399. --     (Tree_Store : access Gtk_Tree_Store_Record'Class; 
  400. --      Iter       : Gtk.Tree_Model.Gtk_Tree_Iter; 
  401. --      Column1 : Gint; Value1 : UTF8_String; 
  402. --      Column2 : Gint; Value2 : Boolean) 
  403. --  is 
  404. --      procedure Set_String 
  405. --        (Tree : System.Address; 
  406. --         Iter : Gtk.Tree_Model.Gtk_Tree_Iter; 
  407. --         Column : Gint; Value : UTF8_String); 
  408. --      pragma Import (C, Set_String, "ada_gtk_tree_store_set_ptr"); 
  409. -- 
  410. --      procedure Set_Int 
  411. --        (Tree : System.Address; 
  412. --         Iter : Gtk.Tree_Model.Gtk_Tree_Iter; 
  413. --         Column : Gint; Value : Gint); 
  414. --      pragma Import (C, Internal, "ada_gtk_tree_store_set_int"); 
  415. --   begin 
  416. --      Internal 
  417. --        (Get_Object (Tree_Store), Iter, Column1, Value1 & ASCII.NUL); 
  418. --      Internal 
  419. --        (Get_Object (Tree_Store), Iter, Column2, Boolean'Pos (Value2)); 
  420. --   end Set; 
  421. -- 
  422. --  </example> 
  423.  
  424. --  No binding: gtk_tree_store_new 
  425. --  No binding: gtk_tree_store_set 
  426. --  No binding: gtk_tree_store_set_valist