File: create_combo_box.adb

package info (click to toggle)
libgtkada2 2.8.1-6lenny3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 13,496 kB
  • ctags: 3,886
  • sloc: ada: 103,189; ansic: 45,411; perl: 5,500; sh: 2,812; makefile: 1,169; xml: 19
file content (219 lines) | stat: -rw-r--r-- 8,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
-----------------------------------------------------------------------
--          GtkAda - Ada95 binding for the Gimp Toolkit              --
--                                                                   --
--                     Copyright (C) 2006 AdaCore                    --
--                                                                   --
-- This library is free software; you can redistribute it and/or     --
-- modify it under the terms of the GNU General Public               --
-- License as published by the Free Software Foundation; either      --
-- version 2 of the License, or (at your option) any later version.  --
--                                                                   --
-- This library is distributed in the hope that it will be useful,   --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of    --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU --
-- General Public License for more details.                          --
--                                                                   --
-- You should have received a copy of the GNU General Public         --
-- License along with this library; if not, write to the             --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      --
-- Boston, MA 02111-1307, USA.                                       --
--                                                                   --
-- As a special exception, if other files instantiate generics from  --
-- this unit, or you link this unit with other files to produce an   --
-- executable, this  unit  does not  by itself cause  the resulting  --
-- executable to be covered by the GNU General Public License. This  --
-- exception does not however invalidate any other reasons why the   --
-- executable file  might be covered by the  GNU Public License.     --
-----------------------------------------------------------------------

with Gdk.Color;              use Gdk.Color;
with Gdk.Pixbuf;             use Gdk.Pixbuf;
with Gdk.Rgb;                use Gdk.Rgb;
with Glib;                   use Glib;
with Gtk.Box;                use Gtk.Box;
with Gtk.Frame;              use Gtk.Frame;
with Gtk.Cell_Renderer;      use Gtk.Cell_Renderer;
with Gtk.Cell_Renderer_Pixbuf; use Gtk.Cell_Renderer_Pixbuf;
with Gtk.Cell_Renderer_Text;   use Gtk.Cell_Renderer_Text;
with Gtk.Combo_Box;          use Gtk.Combo_Box;
with Gtk.Cell_Layout;        use Gtk.Cell_Layout;
with Gtk.List_Store;         use Gtk.List_Store;
with Gtk.Tree_Model;         use Gtk.Tree_Model;
with Gtk.Widget;             use Gtk.Widget;

package body Create_Combo_Box is

   Column_0 : constant := 0;
   Column_1 : constant := 1;
   --  The columns in the model

   procedure Append_Color_Pixbuf
     (Model : Gtk_List_Store;
      Color : String);
   --  Append a new pixbuf with Color as its background

   procedure Fill_Pixbuf (Pix : Gdk_Pixbuf; Color : String);
   --  Fill the background of Pix. This is probably not some code you should
   --  copy in your own application, since not very clean.

   procedure Set_Color_Pixbuf
     (Model : Gtk_List_Store; Iter : Gtk_Tree_Iter; Color : String);
   --  Add a pixbuf to the second column of Model

   ----------
   -- Help --
   ----------

   function Help return String is
   begin
      return "A @bGtk_Combo_Box@B is a widget that allows the user to choose"
        & " from a list of valid choices.";
   end Help;

   -----------------
   -- Fill_Pixbuf --
   -----------------

   procedure Fill_Pixbuf (Pix : Gdk_Pixbuf; Color : String) is
      GColor : Gdk_Color;
      Stride  : Gint;
      Num     : Gint;
      Pixels  : Gdk.Rgb.Rgb_Buffer_Access;
   begin
      GColor := Parse (Color);

      --  This code is not clean. It would be better to use cairo, but GtkAda
      --  has no binding for it at the time of this writing. You could also
      --  load the images from XPM data instead.
      Stride := Get_Rowstride (Pix);
      Num    := Get_Width (Pix) * Get_Height (Pix);
      Pixels := Get_Pixels (Pix);

      for N in 0 .. Num - 1 loop
         --  By default, each color occupies 8bits, thus is it easier to
         --  manipulate colors
         Pixels (Integer (N * 3 + 0)) := Guchar (Red   (Gcolor) / 65535 * 255);
         Pixels (Integer (N * 3 + 1)) := Guchar (Green (Gcolor) / 65535 * 255);
         Pixels (Integer (N * 3 + 2)) := Guchar (Blue  (Gcolor) / 65535 * 255);
      end loop;
   end Fill_Pixbuf;

   -------------------------
   -- Append_Color_Pixbuf --
   -------------------------

   procedure Append_Color_Pixbuf
     (Model : Gtk_List_Store;
      Color : String)
   is
      Pix     : Gdk_Pixbuf;
      Iter    : Gtk_Tree_Iter;
   begin
      Pix := Gdk_New (Bits_Per_Sample => 8, Width => 16, Height => 16);
      Fill_Pixbuf (Pix, Color);
      Append (Model, Iter);
      Set (Model, Iter, Column_0, Pix);
      Unref (Pix);
   end Append_Color_Pixbuf;

   ----------------------
   -- Set_Color_Pixbuf --
   ----------------------

   procedure Set_Color_Pixbuf
     (Model : Gtk_List_Store; Iter : Gtk_Tree_Iter; Color : String)
   is
      Pix : Gdk_Pixbuf;
   begin
      Pix := Gdk_New (Bits_Per_Sample => 8, Width => 16, Height => 16);
      Fill_Pixbuf (Pix, Color);
      Set (Model, Iter, Column_1, Pix);
      Unref (Pix);
   end Set_Color_Pixbuf;

   ---------
   -- Run --
   ---------

   procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
      Box        : Gtk_Box;
      Model      : Gtk_List_Store;
      Iter       : Gtk_Tree_Iter;
      Combo      : Gtk_Combo_Box;
      Render     : Gtk_Cell_Renderer_Text;
      Pix        : Gtk_Cell_Renderer_Pixbuf;
   begin
      Set_Label (Frame, "Combo box");

      Gtk_New_Vbox (Box, Homogeneous => False);
      Add (Frame, Box);

      --  A simple text combo

      Gtk_New_Text (Combo);
      Pack_Start (Box, Combo, Expand => False);
      Append_Text (Combo, "Simple Text Combo 1");
      Append_Text (Combo, "Simple Text Combo 2");
      Append_Text (Combo, "Simple Text Combo 3");
      Set_Active (Combo, 0);

      --  Create a model. This is a set of rows, each with two columns in this
      --  specific case.
      Gtk_New (Model, (Column_0 => GType_String,
                       Column_1 => Gdk.Pixbuf.Get_Type));
      Append (Model, Iter);
      Set (Model, Iter, Column_0, "Combo From Model 1");
      Set_Color_Pixbuf (Model, Iter, "red");

      Append (Model, Iter);
      Set (Model, Iter, Column_0, "Combo From Model 2");
      Set_Color_Pixbuf (Model, Iter, "green");

      Append (Model, Iter);
      Set (Model, Iter, Column_0, "Combo From Model 3");
      Set_Color_Pixbuf (Model, Iter, "blue");

      --  Create the combo. We use both columns of the model to display in the
      --  model, but we could display only one, or even have a display that
      --  doesn't come directly from a column (see create_cell_view for
      --  instance)

      Gtk_New_With_Model (Combo, Model);
      Pack_Start (Box, Combo, Expand => False);

      Gtk_New (Pix);
      Pack_Start    (+Combo, Pix, Expand => True);
      Add_Attribute (+Combo, Pix, "pixbuf", Column_1);

      Gtk_New (Render);
      Pack_Start    (+Combo, Render, Expand => True);
      Add_Attribute (+Combo, Render, "text", Column_0);

      Set_Active (Combo, 0);

      --  A matrix combo now
      Gtk_New (Model, (Column_0 => Gdk.Pixbuf.Get_Type));
      Append_Color_Pixbuf (Model, "red");
      Append_Color_Pixbuf (Model, "green");
      Append_Color_Pixbuf (Model, "blue");
      Append_Color_Pixbuf (Model, "yellow");
      Append_Color_Pixbuf (Model, "black");
      Append_Color_Pixbuf (Model, "white");
      Append_Color_Pixbuf (Model, "cyan");
      Append_Color_Pixbuf (Model, "pink");
      Append_Color_Pixbuf (Model, "magenta");

      Gtk_New_With_Model (Combo, Model);
      Pack_Start (Box, Combo, Expand => False);
      Set_Wrap_Width (Combo, 3);  --  Make it a matrix

      Gtk_New (Pix);
      Pack_Start    (+Combo, Pix, Expand => True);
      Add_Attribute (+Combo, Pix, "pixbuf", Column_0);

      Set_Active (Combo, 0);

      Show_All (Frame);
   end Run;

end Create_Combo_Box;