1-----------------------------------------------------------------------
2--          GtkAda - Ada95 binding for the Gimp Toolkit              --
3--                                                                   --
4--               Copyright (C) 2006-2013, 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-- As a special exception, if other files instantiate generics from  --
22-- this unit, or you link this unit with other files to produce an   --
23-- executable, this  unit  does not  by itself cause  the resulting  --
24-- executable to be covered by the GNU General Public License. This  --
25-- exception does not however invalidate any other reasons why the   --
26-- executable file  might be covered by the  GNU Public License.     --
27-----------------------------------------------------------------------
28
29with Glib;                     use Glib;
30with Glib.Object;              use Glib.Object;
31with Glib.Values;              use Glib.Values;
32with Gtk;                      use Gtk;
33with Gtk.Enums;                use Gtk.Enums;
34with Gtk.Scrolled_Window;      use Gtk.Scrolled_Window;
35with Gtk.Cell_Renderer_Text;   use Gtk.Cell_Renderer_Text;
36with Gtk.List_Store;           use Gtk.List_Store;
37with Gtk.Tree_View;            use Gtk.Tree_View;
38with Gtk.Tree_Model;           use Gtk.Tree_Model;
39with Gtk.Tree_Model_Filter;    use Gtk.Tree_Model_Filter;
40with Gtk.Tree_View_Column;     use Gtk.Tree_View_Column;
41with Gtk.Frame;                use Gtk.Frame;
42
43package body Create_Tree_Filter is
44
45   Column_0 : constant := 0;
46
47   function Custom_Filter
48     (Model : access Gtk_Tree_Model_Record'Class;
49      Iter  : Gtk_Tree_Iter) return Boolean;
50   --  Decide whether a row should be made visible or not
51
52   procedure Custom_Appearance
53     (Model  : access Gtk_Tree_Model_Filter_Record'Class;
54      Iter   : Gtk_Tree_Iter;
55      Value  : out GValue;
56      Column : Gint);
57   --  Change the appearance of the view dynamically
58
59   ----------
60   -- Help --
61   ----------
62
63   function Help return String is
64   begin
65      return "This example demonstrates a special tree modeL: it wraps another"
66        & " model, and can be used to filter out lines, or even modify its"
67        & " appearance on the fly." & ASCII.LF
68        & "In this example, we have creates a model that contains the sequence"
69        & " 1, 2, ... 9. Another model is applied on top of it, and filters"
70        & " all odd numbers rows. It also changes the appearance to display"
71        & " some extra text." & ASCII.LF
72        & "The underlying model itself is never modified, and by changing a"
73        & " few properties we can decide to show the whole underlying model"
74        & " itself." & ASCII.LF
75        & "Modifying the appearance on the fly is not efficient. It is"
76        & " generally better to use the functions from @bGtk_Cell_Layout@B to"
77        & " create ""virtual"" columns in the model. See the Cell View demo.";
78   end Help;
79
80   -------------------
81   -- Custom_Filter --
82   -------------------
83
84   function Custom_Filter
85     (Model : access Gtk_Tree_Model_Record'Class;
86      Iter  : Gtk_Tree_Iter) return Boolean
87   is
88      Value : constant Gint := Get_Int (Model, Iter, Column_0);
89   begin
90      return Value mod 2 /= 1;
91   end Custom_Filter;
92
93   -----------------------
94   -- Custom_Appearance --
95   -----------------------
96
97   procedure Custom_Appearance
98     (Model  : access Gtk_Tree_Model_Filter_Record'Class;
99      Iter   : Gtk_Tree_Iter;
100      Value  : out GValue;
101      Column : Gint)
102   is
103      Val        : Gint;
104      Child_Iter : Gtk_Tree_Iter;
105   begin
106      Convert_Iter_To_Child_Iter (Model, Child_Iter, Iter);
107      Val := Get_Int (Get_Model (Model), Child_Iter, Column);
108      Set_String (Value, "This is line" & Gint'Image (Val));
109   end Custom_Appearance;
110
111   ---------
112   -- Run --
113   ---------
114
115   procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
116      Model    : Gtk_List_Store;
117      Filter   : Gtk_Tree_Model_Filter;
118      Tree     : Gtk_Tree_View;
119      Scrolled : Gtk_Scrolled_Window;
120      Col      : Gtk_Tree_View_Column;
121      Num      : Gint;
122      Text     : Gtk_Cell_Renderer_Text;
123      Iter     : Gtk_Tree_Iter;
124      pragma Unreferenced (Num);
125
126   begin
127      Set_Label (Frame, "Tree Model Filter");
128
129      Gtk_New (Scrolled);
130      Add (Frame, Scrolled);
131      Set_Policy (Scrolled, Policy_Automatic, Policy_Automatic);
132
133      --  Create the model that contains the actual data. This model will
134      --  never be modified
135
136      Gtk_New (Model, (Column_0 => GType_Int));
137
138      for N in 1 .. 10 loop
139         Append (Model, Iter);
140         Set (Model, Iter, Column_0, Gint (N));
141      end loop;
142
143      --  Now creates a filter around this model. We filter through a custom
144      --  function, but that could be a simple row in the model as well. The
145      --  function is slightly more flexible, though.
146
147      Gtk_New (Filter, Model);
148      Set_Visible_Func (Filter, Custom_Filter'Access);
149      Set_Modify_Func  (Filter, (0 => GType_String), Custom_Appearance'Access);
150
151      --  And now a view that displays the filter. A single column is displayed
152
153      Gtk_New (Tree, Filter);
154      Add (Scrolled, Tree);
155      Set_Headers_Visible (Tree, False);
156
157      Gtk_New (Text);
158
159      Gtk_New (Col);
160      Num := Append_Column (Tree, Col);
161      Pack_Start (Col, Text, True);
162      Add_Attribute (Col, Text, "text", Column_0);
163
164      Show_All (Frame);
165   end Run;
166
167end Create_Tree_Filter;
168