File: create_clist.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 (460 lines) | stat: -rw-r--r-- 15,523 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
-----------------------------------------------------------------------
--          GtkAda - Ada95 binding for the Gimp Toolkit              --
--                                                                   --
--   Copyright (C) 1998-2000 E. Briot, J. Brobecker and A. Charlet   --
--                Copyright (C) 2000-2003 ACT-Europe                 --
--                                                                   --
-- 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 Glib;                use Glib;
with Gdk;                 use Gdk;
with Gdk.Color;           use Gdk.Color;
with Gdk.Pixmap;          use Gdk.Pixmap;
with Gdk.Bitmap;          use Gdk.Bitmap;
with Gtk;                 use Gtk;
with Gtk.Box;             use Gtk.Box;
with Gtk.Button;          use Gtk.Button;
with Gtk.Check_Button;    use Gtk.Check_Button;
with Gtk.Clist;           use Gtk.Clist;
with Gtk.Enums;           use Gtk.Enums;
with Gtk.Label;           use Gtk.Label;
with Gtk.Option_Menu;     use Gtk.Option_Menu;
with Gtk.Scrolled_Window; use Gtk.Scrolled_Window;
with Gtk.Handlers;        use Gtk.Handlers;
with Gtk.Style;           use Gtk.Style;
with Gtk.Widget;          use Gtk.Widget;
with Gtkada.Types;        use Gtkada.Types;
with Pango.Font;          use Pango.Font;
with Common;              use Common;
with Interfaces.C.Strings;

package body Create_Clist is
   package IC renames Interfaces.C;
   package ICS renames Interfaces.C.Strings;

   package Clist_Cb is new Handlers.Callback (Gtk_Clist_Record);
   package Check_Cb is new Handlers.User_Callback
     (Gtk_Check_Button_Record, Gtk_Clist);

   use type Interfaces.C.size_t;

   Clist_Columns      : constant Interfaces.C.size_t := 12;
   Clist_Rows         : Integer := 0;
   Style1             : Gtk_Style;
   Style2             : Gtk_Style;
   Style3             : Gtk_Style;
   Clist_Omenu_Group  : Widget_SList.GSlist;

   Titles : constant Chars_Ptr_Array (1 .. Clist_Columns) :=
     "Auto resize" +
     "Not resizable" +
     "Max width 100" +
     "Min Width 50" +
     "Hide column" +
     "Title 5" +
     "Title 6" +
     "Title 7" +
     "Title 8" +
     "Title 9" +
     "Title 10" +
     "Title 11";
   --  Put at the library level to avoid having to allocate/free the
   --  memory each time "Run" is called...

   Items : constant Chars_Ptr_Array :=
     "Single" + "Browse" + "Multiple" + "Extended";

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

   function Help return String is
   begin
      return "!!!! Consider using a @bGtk_Tree_View@B instead !!!!"
        & ASCII.LF
        & ASCII.LF
        & "An @bGtk_Clist@B is like a @bGtk_List@B, except it shows the"
        & " information on multiple columns. You can have as many columns"
        & " as you want, each with its own information."
        & ASCII.LF
        & "Each line can have its own user_data, although the interface is"
        & " different from the standard interface. Some specific functions"
        & " are provided for this usage. The standard inheritance mechanism"
        & " does not work for rows in a clist, altough of course it does"
        & " work for the @bGtk_Clist@B itself.";
   end Help;

   ----------------
   -- Clear_List --
   ----------------

   procedure Clear_List (List : access Gtk_Clist_Record'Class) is
   begin
      Clear (List);
      Clist_Rows := 0;
   end Clear_List;

   ----------------------
   -- Remove_Selection --
   ----------------------

   procedure Remove_Selection (List : access Gtk_Clist_Record'Class) is
      use Gint_List;
      I : Gint;
   begin
      Freeze (List);
      loop
         exit when Length (Get_Selection (List)) = 0;
         Clist_Rows := Clist_Rows - 1;
         I := Get_Data (First (Get_Selection (List)));
         Remove (List, I);
         exit when Get_Selection_Mode (List) = Selection_Browse;
      end loop;

      Thaw (List);
   end Remove_Selection;

   -------------------
   -- Toggle_Titles --
   -------------------

   procedure Toggle_Titles (Button : access Gtk_Check_Button_Record'Class;
                            List : in Gtk_Clist) is
   begin
      if Get_Active (Button) then
         Column_Titles_Show (List);
      else
         Column_Titles_Hide (List);
      end if;
   end Toggle_Titles;

   ------------------------
   -- Toggle_Reorderable --
   ------------------------

   procedure Toggle_Reorderable (Button : access Gtk_Check_Button_Record'Class;
                                 List : in Gtk_Clist) is
   begin
      Set_Reorderable (List, Get_Active (Button));
   end Toggle_Reorderable;

   -------------
   -- Add1000 --
   -------------

   procedure Add1000 (List : access Gtk_Clist_Record'Class) is
      Pixmap : Gdk.Gdk_Pixmap;
      Mask   : Gdk.Gdk_Bitmap;
      Texts  : Chars_Ptr_Array (0 .. Clist_Columns - 1);
      Row    : Gint;
      Style  : constant Gtk_Style := Get_Style (List);

   begin
      Create_From_Xpm_D (Pixmap, Get_Clist_Window (List),
                         Mask, Get_White (Style),
                         Gtk_Mini_Xpm);
      for I in 4 .. Clist_Columns - 1 loop
         Texts (I) := ICS.New_String ("Column" & IC.size_t'Image (I));
      end loop;
      Texts (0 .. 3) := Null_Ptr + "Right" + "Center" + Null_Ptr;
      Freeze (List);

      for I in 0 .. 999 loop
         ICS.Free (Texts (0));
         Texts (0) := ICS.New_String ("CListRow" & Integer'Image (I));
         Row := Append (List, Texts);
         Set_Pixtext (List, Row, 3, "gtk+", 5, Pixmap, Mask);
      end loop;
      Clist_Rows := Clist_Rows + 1000;

      Free (Texts);

      Thaw (List);
      Gdk.Pixmap.Unref (Pixmap);
      Gdk.Bitmap.Unref (Mask);
   end Add1000;

   --------------
   -- Add10000 --
   --------------

   procedure Add10000 (List : access Gtk_Clist_Record'Class) is
      Texts  : Chars_Ptr_Array (0 .. Clist_Columns - 1);
      Row    : Gint;
      pragma Unreferenced (Row);

   begin
      for I in 3 .. Clist_Columns - 1 loop
         Texts (I) := ICS.New_String ("Column" & IC.size_t'Image (I));
      end loop;

      Texts (0 .. 2) := Null_Ptr + "Right" + "Center";
      Freeze (List);

      for I in 0 .. 9999 loop
         ICS.Free (Texts (0));
         Texts (0) := ICS.New_String ("Row" & Integer'Image (I));
         Row := Append (List, Texts);
      end loop;

      Clist_Rows := Clist_Rows + 10000;
      Free (Texts);
      Thaw (List);
   end Add10000;

   ----------------
   -- Insert_Row --
   ----------------

   procedure Insert_Row (List : access Gtk_Clist_Record'Class) is
      Texts  : Chars_Ptr_Array (0 .. Clist_Columns - 1) :=
        "This" + "is an" + "inserted" + "row" +
        "This" + "is an" + "inserted" + "row" +
        "This" + "is an" + "inserted" + "row";
      Col1 : Gdk_Color;
      Col2 : Gdk_Color;
      Row  : Gint;
      Style : constant Gtk_Style := Get_Style (List);

   begin
      Row := Prepend (List, Texts);
      if Style1 = Null_Style then
         Set_Rgb (Col1, 0, 56000, 0);
         Set_Rgb (Col2, 32000, 0, 56000);

         --  Note that the memory allocated here is never freed in this
         --  small example!
         Style1 := Copy (Style);
         Set_Base (Style1, State_Normal, Col1);
         Set_Base (Style1, State_Selected, Col2);

         Style2 := Copy (Style);
         Set_Foreground (Style2, State_Normal, Col1);
         Set_Foreground (Style2, State_Selected, Col2);

         Style3 := Copy (Style);
         Set_Foreground (Style3, State_Normal, Col1);
         Set_Base (Style3, State_Normal, Col2);

         Set_Font_Description (Style3, From_String ("Courier 120"));
      end if;

      Set_Cell_Style (List, Row, 3, Style1);
      Set_Cell_Style (List, Row, 4, Style2);
      Set_Cell_Style (List, Row, 0, Style3);
      Clist_Rows := Clist_Rows + 1;
      Free (Texts);
   end Insert_Row;

   --------------------
   -- Undo_Selection --
   --------------------

   procedure Undo_Selection (List : access Gtk_Clist_Record'Class) is
   begin
      Gtk.Clist.Undo_Selection (List);
   end Undo_Selection;

   ------------------
   -- Click_Column --
   ------------------

   procedure Click_Column (List   : access Gtk_Clist_Record'Class;
                           Column : Gint) is
   begin
      if Column = 4 then
         Set_Column_Visibility (List, Column, False);
      elsif Column = Get_Sort_Column (List) then
         if Get_Sort_Type (List) = Ascending then
            Set_Sort_Type (List, Descending);
         else
            Set_Sort_Type (List, Ascending);
         end if;
      else
         Set_Sort_Column (List, Column);
      end if;

      Sort (List);
   end Click_Column;

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

   procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
      Texts     : Chars_Ptr_Array (0 .. Clist_Columns - 1);
      VBox, HBox : Gtk_Box;
      Clist     : Gtk_Clist;
      Button    : Gtk_Button;
      Label     : Gtk_Label;
      New_Row   : Gint;
      Scrolled  : Gtk_Scrolled_Window;
      Check     : Gtk_Check_Button;
      Omenu     : Gtk_Option_Menu;
      Col1      : Gdk_Color;
      Col2      : Gdk_Color;
      Style     : Gtk_Style;
      pragma Unreferenced (New_Row);

   begin

      Clist_Rows := 0;
      Set_Label (Frame, "Clist");

      Gtk_New_Vbox (VBox, False, 0);
      Add (Frame, VBox);

      Gtk_New (Scrolled);
      Set_Border_Width (Scrolled, 5);
      Set_Policy (Scrolled, Policy_Automatic, Policy_Automatic);

      Gtk_New (Clist, Gint (Clist_Columns), Titles);
      Add (Scrolled, Clist);
      --  TBD: Callback for click column

      Gtk_New_Hbox (HBox, False, 5);
      Set_Border_Width (HBox, 5);
      Pack_Start (VBox, HBox, False, False, 0);

      Clist_Cb.Connect (Clist, "click_column",
                        Clist_Cb.To_Marshaller (Click_Column'Access));

      Gtk_New (Button, "Insert Row");
      Pack_Start (HBox, Button, True, True, 0);
      Clist_Cb.Object_Connect (Button, "clicked",
                               Clist_Cb.To_Marshaller (Insert_Row'Access),
                               Slot_Object => Clist);

      Gtk_New (Button, "Add 1000 Rows with Pixmaps");
      Pack_Start (HBox, Button, True, True, 0);
      Clist_Cb.Object_Connect (Button, "clicked",
                               Clist_Cb.To_Marshaller (Add1000'Access),
                               Slot_Object => Clist);

      Gtk_New (Button, "Add 10000 Rows");
      Pack_Start (HBox, Button, True, True, 0);
      Clist_Cb.Object_Connect (Button, "clicked",
                               Clist_Cb.To_Marshaller (Add10000'Access),
                               Slot_Object => Clist);

      --  Second layer of buttons
      Gtk_New_Hbox (HBox, False, 5);
      Set_Border_Width (HBox, 5);
      Pack_Start (VBox, HBox, False, False, 0);

      Gtk_New (Button, "Clear List");
      Pack_Start (HBox, Button, True, True, 0);
      Clist_Cb.Object_Connect (Button, "clicked",
                               Clist_Cb.To_Marshaller (Clear_List'Access),
                               Slot_Object => Clist);

      Gtk_New (Button, "Remove Selection");
      Pack_Start (HBox, Button, True, True, 0);
      Clist_Cb.Object_Connect
        (Button, "clicked",
         Clist_Cb.To_Marshaller (Remove_Selection'Access),
         Slot_Object => Clist);

      Gtk_New (Button, "Undo Selection");
      Pack_Start (HBox, Button, True, True, 0);
      Clist_Cb.Object_Connect
        (Button, "clicked",
         Clist_Cb.To_Marshaller (Undo_Selection'Access),
         Slot_Object => Clist);

      --  TBD??? Warning tests button

      --  Third layer of buttons
      Gtk_New_Hbox (HBox, False, 5);
      Set_Border_Width (HBox, 5);
      Pack_Start (VBox, HBox, False, False, 0);

      Gtk_New (Check, "Toggle title Buttons");
      Pack_Start (HBox, Check, True, True, 0);
      Check_Cb.Connect (Check, "clicked",
                        Check_Cb.To_Marshaller (Toggle_Titles'Access),
                        Clist);
      Set_Active (Check, True);

      Gtk_New (Check, "Reorderable");
      Pack_Start (HBox, Check, True, True, 0);
      Check_Cb.Connect
        (Check, "clicked",
         Check_Cb.To_Marshaller (Toggle_Reorderable'Access),
         Clist);
      Set_Active (Check, True);

      Gtk_New (Label, "Selection_Mode :");
      Pack_Start (HBox, Label, False, True, 0);

      Clist_Omenu_Group := Widget_SList.Null_List;
      Build_Option_Menu (Omenu, Clist_Omenu_Group, Items, 0, null);
      --  FIXME: Add the missing callback (instead of null).
      Pack_Start (HBox, Omenu, False, True, 0);

      Pack_Start (VBox, Scrolled, True, True, 0);
      Set_Row_Height (Clist, 18);
      Set_USize (Clist, -1, 300);

      for I in 0 .. Clist_Columns - 1 loop
         Set_Column_Width (Clist, Gint (I), 80);
      end loop;

      Set_Column_Auto_Resize (Clist, 0, True);
      Set_Column_Resizeable (Clist, 1, False);
      Set_Column_Max_Width (Clist, 2, 100);
      Set_Column_Min_Width (Clist, 3, 50);
      Set_Selection_Mode (Clist, Selection_Multiple);
      Set_Column_Justification (Clist, 1, Justify_Right);
      Set_Column_Justification (Clist, 2, Justify_Center);

      for I in 1 .. Clist_Columns - 1 loop
            Texts (I) := ICS.New_String ("Columns " & IC.size_t'Image (I));
      end loop;

      Set_Rgb (Col1, 56000, 0, 0);
      Set_Rgb (Col2, 0, 56000, 32000);

      Gtk_New (Style);
      Set_Foreground (Style, State_Normal, Col1);
      Set_Base (Style, State_Normal, Col2);
      Set_Font_Description (Style, From_String ("Helvetica Bold 14"));

      for I in Gint'(0) .. 9 loop
         Texts (0) :=
           ICS.New_String ("ClistRow " & Integer'Image (Clist_Rows));
         Clist_Rows := Clist_Rows + 1;
         New_Row := Append (Clist, Texts);
         ICS.Free (Texts (0));
         if I mod 4 = 2 then
            Set_Row_Style (Clist, I, Style);
         else
            Set_Cell_Style (Clist, I, I mod 4, Style);
         end if;
      end loop;

      Show_All (Frame);
   end Run;

end Create_Clist;