~ubuntu-branches/ubuntu/jaunty/libgtkada2/jaunty

« back to all changes in this revision

Viewing changes to testgtk/create_icon_view.adb

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-08-11 09:46:51 UTC
  • mfrom: (6.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20080811094651-9mjd6acwa98ffw5c
Tags: 2.12.0-2ubuntu1
Add lpia to supported architectures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-----------------------------------------------------------------------
 
2
--          GtkAda - Ada95 binding for the Gimp Toolkit              --
 
3
--                                                                   --
 
4
--                     Copyright (C) 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
with Gdk.Dnd;                  use Gdk.Dnd;
 
24
with Gdk.Pixbuf;               use Gdk.Pixbuf;
 
25
with Gdk.Types;                use Gdk.Types;
 
26
with Glib;                     use Glib;
 
27
with Glib.Error;               use Glib.Error;
 
28
with Glib.Object;              use Glib.Object;
 
29
with Glib.Properties;          use Glib.Properties;
 
30
with Glib.Values;              use Glib.Values;
 
31
with Gtkada.Handlers;          use Gtkada.Handlers;
 
32
with Gtk.Arguments;            use Gtk.Arguments;
 
33
with Gtk.Cell_Layout;          use Gtk.Cell_Layout;
 
34
with Gtk.Cell_Renderer;        use Gtk.Cell_Renderer;
 
35
with Gtk.Cell_Renderer_Pixbuf; use Gtk.Cell_Renderer_Pixbuf;
 
36
with Gtk.Cell_Renderer_Text;   use Gtk.Cell_Renderer_Text;
 
37
with Gtk.Cell_Renderer_Toggle; use Gtk.Cell_Renderer_Toggle;
 
38
with Gtk.Enums;                use Gtk.Enums;
 
39
with Gtk.Frame;                use Gtk.Frame;
 
40
with Gtk.Icon_View;            use Gtk.Icon_View;
 
41
with Gtk.List_Store;           use Gtk.List_Store;
 
42
with Gtk.Scrolled_Window;      use Gtk.Scrolled_Window;
 
43
with Gtk.Selection;            use Gtk.Selection;
 
44
with Gtk.Tree_Model;           use Gtk.Tree_Model;
 
45
with Interfaces.C.Strings;     use Interfaces.C.Strings;
 
46
 
 
47
package body Create_Icon_View is
 
48
 
 
49
   Item_Targets : constant Target_Entry_Array :=
 
50
     (1 => (Target => New_String ("GTK_TREE_MODEL_ROW"),
 
51
            Flags  => Target_Same_App,
 
52
            Info   => 0));
 
53
 
 
54
   procedure Fill_Model (List : Gtk_List_Store);
 
55
   --  Fill the contents of List
 
56
 
 
57
   procedure Toggled
 
58
     (Model  : access GObject_Record'Class;
 
59
      Params : Glib.Values.GValues);
 
60
   --  Called when one of the toggle buttons is clicked on. This performs the
 
61
   --  actual toggling.
 
62
 
 
63
   ----------
 
64
   -- Help --
 
65
   ----------
 
66
 
 
67
   function Help return String is
 
68
   begin
 
69
      return "A @bGtk_Icon_View@B is a widget which displays a list of icons"
 
70
        & " in a grid. Its data is stored in a @bGtk_Tree_Model@B."
 
71
        & ASCII.LF
 
72
        & "In this demo, @bdrag-and-drop@B is activated so that icons can be"
 
73
        & " moved from one location to another."
 
74
        & ASCII.LF
 
75
        & "This example also shows how to make a toggle button editable within"
 
76
        & " an icon view.";
 
77
   end Help;
 
78
 
 
79
   -------------
 
80
   -- Toggled --
 
81
   -------------
 
82
 
 
83
   procedure Toggled
 
84
     (Model  : access GObject_Record'Class;
 
85
      Params : Glib.Values.GValues)
 
86
   is
 
87
      M    : constant Gtk_List_Store := Gtk_List_Store (Model);
 
88
      Path_String : constant String := To_String (Params, 1);
 
89
      Path        : constant Gtk_Tree_Path := Gtk_New (Path_String);
 
90
      Iter        : constant Gtk_Tree_Iter := Get_Iter (M, Path);
 
91
      Value       : constant Boolean := Get_Boolean (M, Iter, 2);
 
92
   begin
 
93
      Set         (M, Iter, 2, not Value);
 
94
      Path_Free (Path);
 
95
   end Toggled;
 
96
 
 
97
   ----------------
 
98
   -- Fill_Model --
 
99
   ----------------
 
100
 
 
101
   procedure Fill_Model (List : Gtk_List_Store) is
 
102
      Pixbuf : Gdk_Pixbuf;
 
103
      Error  : GError;
 
104
      Iter   : Gtk_Tree_Iter;
 
105
   begin
 
106
      Gdk_New_From_File
 
107
        (Pixbuf, Filename => "gnome-textfile.png", Error => Error);
 
108
 
 
109
      Prepend (List, Iter);
 
110
      Set (List, Iter, 0, Pixbuf);
 
111
      Set (List, Iter, 1, "Really Really" & ASCII.LF
 
112
           & " Really really looooooooong item name");
 
113
      Set (List, Iter, 2, True);
 
114
 
 
115
      for J in 0 .. 9 loop
 
116
         Prepend (List, Iter);
 
117
         Set (List, Iter, 0, Pixbuf);
 
118
         Set (List, Iter, 1, "Icon" & J'Img);
 
119
         Set (List, Iter, 2, True);
 
120
      end loop;
 
121
   end Fill_Model;
 
122
 
 
123
   ---------
 
124
   -- Run --
 
125
   ---------
 
126
 
 
127
   procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
 
128
      Scrolled : Gtk_Scrolled_Window;
 
129
      View     : Gtk_Icon_View;
 
130
      List     : Gtk_List_Store;
 
131
      Text     : Gtk_Cell_Renderer_Text;
 
132
      Toggle   : Gtk_Cell_Renderer_Toggle;
 
133
      Pixbuf   : Gtk_Cell_Renderer_Pixbuf;
 
134
   begin
 
135
      Set_Label (Frame, "Icon View");
 
136
 
 
137
      Gtk_New (Scrolled);
 
138
      Add (Frame, Scrolled);
 
139
 
 
140
      Gtk_New (View);
 
141
      Add (Scrolled, View);
 
142
      Set_Selection_Mode (View, Selection_Multiple);
 
143
 
 
144
      Gtk_New
 
145
        (List,
 
146
         (0 => Gdk.Pixbuf.Get_Type,  --  Associated pixbuf
 
147
          1 => GType_String,         --  Text for the icon
 
148
          2 => GType_Boolean));      --  Toggle activated ?
 
149
      Set_Model (View, Gtk_Tree_Model (List));
 
150
 
 
151
      Fill_Model (List);
 
152
 
 
153
      Gtk_New (Toggle);
 
154
      Pack_Start (+View, Toggle, Expand => False);
 
155
      Set_Property (Toggle, Activatable_Property, True);
 
156
      Add_Attribute (+View, Toggle, "active", 2);
 
157
      Object_Callback.Object_Connect
 
158
        (Toggle, "toggled", Toggled'Access, List);
 
159
 
 
160
      --  Connect "toggled" signal
 
161
 
 
162
      Gtk_New (Pixbuf);
 
163
      Pack_Start (+View, Pixbuf, Expand => False);
 
164
      Set_Property (Pixbuf, Follow_State_Property, True);
 
165
      Add_Attribute (+View, Pixbuf, "pixbuf", 0);
 
166
 
 
167
      Gtk_New (Text);
 
168
      Pack_Start (+View, Text, Expand => False);
 
169
      Set_Property (Text, Editable_Property, True);
 
170
      Set_Property (Text, Xalign_Property, 0.5);
 
171
      --  Set_Property (Text, Wrap_Mode_Property, Wrap_Word_Char);
 
172
      Set_Property (Text, Wrap_Width_Property, 100);
 
173
      Add_Attribute (+View, Text, "text", 1);
 
174
      --  Connect "edited" signal
 
175
 
 
176
      --  Allow drag-and-drop of icons
 
177
      Enable_Model_Drag_Source
 
178
        (View,
 
179
         Start_Button_Mask => Button1_Mask,
 
180
         Targets           => Item_Targets,
 
181
         Actions           => Action_Move);
 
182
      Enable_Model_Drag_Dest
 
183
        (View,
 
184
         Targets           => Item_Targets,
 
185
         Actions           => Action_Move);
 
186
 
 
187
      Show_All (Frame);
 
188
   end Run;
 
189
 
 
190
end Create_Icon_View;