File: create_spin.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 (271 lines) | stat: -rw-r--r-- 9,521 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
-----------------------------------------------------------------------
--          GtkAda - Ada95 binding for the Gimp Toolkit              --
--                                                                   --
--                     Copyright (C) 1998-1999                       --
--        Emmanuel Briot, Joel Brobecker and Arnaud Charlet          --
--                                                                   --
-- 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 Gtk.Adjustment;    use Gtk.Adjustment;
with Gtk.Box;           use Gtk.Box;
with Gtk.Button;        use Gtk.Button;
with Gtk.Check_Button;  use Gtk.Check_Button;
with Gtk.Handlers;      use Gtk.Handlers;
with Gtk.Label;         use Gtk.Label;
with Gtk.Spin_Button;   use Gtk.Spin_Button;
with Gtk.Toggle_Button; use Gtk.Toggle_Button;
with Gtk;               use Gtk;

package body Create_Spin is

   type My_Button_Record is new Gtk_Button_Record with record
      Label : Gtk_Label;
   end record;
   type My_Button is access all My_Button_Record;
   --  This is basic Gtk_Button, except that is has an extra internal
   --  data.

   package Spin_O_Cb is new Handlers.Callback (Gtk_Spin_Button_Record);
   package Spin_Cb is new Handlers.User_Callback
     (Gtk_Toggle_Button_Record, Gtk_Spin_Button);
   package Button_Cb is new Handlers.User_Callback (My_Button_Record, Gint);

   Spinner1 : Gtk_Spin_Button;

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

   function Help return String is
   begin
      return "A @bGtk_Spin_Button@B is a widget that allows the user to"
        & " choose a value from a specific range. You can only associate"
        & " the spin button with numeric values."
        & ASCII.LF
        & "This demo also creates a child of @bGtk_Button@B for the two"
        & " buttons at the bottom. These are basic buttons, but they also"
        & " contain a pointer to the label where to display the value.";
   end Help;

   -------------------
   -- Change_Digits --
   -------------------

   procedure Change_Digits (Spin : access Gtk_Spin_Button_Record'Class) is
   begin
      Set_Digits (Spinner1, Guint (Get_Value_As_Int (Spin)));
   end Change_Digits;

   -----------------
   -- Toggle_Snap --
   -----------------

   procedure Toggle_Snap (Widget : access Gtk_Toggle_Button_Record'Class;
                          Spin : in Gtk_Spin_Button) is
   begin
      Set_Snap_To_Ticks (Spin, Get_Active (Widget));
   end Toggle_Snap;

   --------------------
   -- Toggle_Numeric --
   --------------------

   procedure Toggle_Numeric (Widget : access Gtk_Toggle_Button_Record'Class;
                             Spin : in Gtk_Spin_Button) is
   begin
      Set_Numeric (Spin, Get_Active (Widget));
   end Toggle_Numeric;

   ---------------
   -- Get_Value --
   ---------------

   procedure Get_Value (Widget : access My_Button_Record'Class;
                        Data   : in Gint)
   is
      Spin  : constant Gtk_Spin_Button := Spinner1;
   begin
      if Data = 1 then
         Set_Text (Widget.Label, Gint'Image (Get_Value_As_Int (Spin)));
      else
         Set_Text (Widget.Label, Gdouble'Image (Get_Value (Spin)));
      end if;
   end Get_Value;

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

   procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
      Main_Box : Gtk_Box;
      VBox     : Gtk_Box;
      Hbox     : Gtk_Box;
      Vbox2    : Gtk_Box;
      Label    : Gtk_Label;
      Adj      : Gtk_Adjustment;
      Spinner  : Gtk_Spin_Button;
      Spinner2 : Gtk_Spin_Button;
      Frame2   : Gtk_Frame;
      Check    : Gtk_Check_Button;
      Myb      : My_Button;

   begin
      Set_Label (Frame, "Spin Buttons");

      Gtk_New_Vbox (Main_Box, False, 5);
      Set_Border_Width (Main_Box, 10);
      Add (Frame, Main_Box);

      Gtk_New (Frame2, "Not accelerated");
      Pack_Start (Main_Box, Frame2, False, False, 0);

      Gtk_New_Vbox (VBox, False, 0);
      Set_Border_Width (VBox, 5);
      Add (Frame2, VBox);

      --  Day, month, year spinners
      Gtk_New_Hbox (Hbox, False, 0);
      Pack_Start (VBox, Hbox, False, False, 5);

      Gtk_New_Vbox (Vbox2, False, 0);
      Pack_Start (Hbox, Vbox2, False, False, 5);
      Gtk_New (Label, "Day:");
      Set_Alignment (Label, 0.0, 0.5);
      Pack_Start (Vbox2, Label, False, False, 0);
      Gtk_New (Adj, 1.0, 1.0, 31.0, 1.0, 5.0, 0.0);
      Gtk_New (Spinner, Adj, 0.0, 0);
      Set_Wrap (Spinner, True);
      Pack_Start (Vbox2, Spinner, False, False, 0);

      Gtk_New_Vbox (Vbox2, False, 0);
      Pack_Start (Hbox, Vbox2, False, False, 5);
      Gtk_New (Label, "Month:");
      Set_Alignment (Label, 0.0, 0.5);
      Pack_Start (Vbox2, Label, False, False, 0);
      Gtk_New (Adj, 1.0, 1.0, 12.0, 1.0, 5.0, 0.0);
      Gtk_New (Spinner, Adj, 0.0, 0);
      Set_Wrap (Spinner, True);
      Pack_Start (Vbox2, Spinner, False, False, 0);

      Gtk_New_Vbox (Vbox2, False, 0);
      Pack_Start (Hbox, Vbox2, False, False, 5);
      Gtk_New (Label, "Year:");
      Set_Alignment (Label, 0.0, 0.5);
      Pack_Start (Vbox2, Label, False, False, 0);
      Gtk_New (Adj, 1998.0, 0.0, 2100.0, 1.0, 100.0, 0.0);
      Gtk_New (Spinner, Adj, 0.0, 0);
      Set_Wrap (Spinner, True);
      Set_USize (Spinner, 55, 0);
      Pack_Start (Vbox2, Spinner, False, False, 0);

      Gtk_New (Frame2, "Accelerated");
      Pack_Start (Main_Box, Frame2, False, False, 0);

      Gtk_New_Vbox (VBox, False, 0);
      Set_Border_Width (VBox, 5);
      Add (Frame2, VBox);

      Gtk_New_Hbox (Hbox, False, 0);
      Pack_Start (VBox, Hbox, False, False, 5);

      Gtk_New_Vbox (Vbox2, False, 0);
      Pack_Start (Hbox, Vbox2, False, False, 5);
      Gtk_New (Label, "Value:");
      Set_Alignment (Label, 0.0, 0.5);
      Pack_Start (Vbox2, Label, False, False, 0);
      Gtk_New (Adj, 0.0, -10000.0, 10000.0, 0.5, 100.0, 0.0);
      Gtk_New (Spinner1, Adj, 1.0, 2);
      Set_Wrap (Spinner1, True);
      Set_USize (Spinner1, 100, 0);
      Set_Update_Policy (Spinner1, Update_Always);
      Pack_Start (Vbox2, Spinner1, False, False, 0);

      Gtk_New_Vbox (Vbox2, False, 0);
      Pack_Start (Hbox, Vbox2, False, False, 5);
      Gtk_New (Label, "Digits:");
      Set_Alignment (Label, 0.0, 0.5);
      Pack_Start (Vbox2, Label, False, False, 0);
      Gtk_New (Adj, 2.0, 1.0, 5.0, 1.0, 1.0, 0.0);
      Gtk_New (Spinner2, Adj, 0.0, 0);
      Set_Wrap (Spinner2, True);
      Spin_O_Cb.Object_Connect
        (Adj, "value_changed",
         Spin_O_Cb.To_Marshaller (Change_Digits'Access),
         Slot_Object => Spinner2);

      Pack_Start (Vbox2, Spinner2, False, False, 0);

      Gtk_New_Hbox (Hbox, False, 0);
      Pack_Start (VBox, Hbox, False, False, 5);

      Gtk_New (Check, "Snap to 0.5-ticks");
      Spin_Cb.Connect
        (Check, "clicked",
         Spin_Cb.To_Marshaller (Toggle_Snap'Access), Spinner1);
      Pack_Start (VBox, Check, False, False, 0);
      Set_Active (Check, True);

      Gtk_New (Check, "Snap Numeric only input mode");
      Spin_Cb.Connect
        (Check, "clicked",
         Spin_Cb.To_Marshaller (Toggle_Numeric'Access), Spinner1);
      Pack_Start (VBox, Check, False, False, 0);
      Set_Active (Check, True);

      Gtk_New (Label, "");
      Gtk_New_Hbox (Hbox, False, 0);
      Pack_Start (VBox, Hbox, False, False, 5);

      Myb := new My_Button_Record;
      Initialize (Myb, "Value as Int");
      Myb.Label := Label;
      Button_Cb.Connect (Myb, "clicked",
                         Button_Cb.To_Marshaller (Get_Value'Access), 1);
      Pack_Start (Hbox, Myb, False, False, 5);

      Myb := new My_Button_Record;
      Initialize (Myb, "Value as Float");
      Myb.Label := Label;
      Button_Cb.Connect (Myb, "clicked",
                         Button_Cb.To_Marshaller (Get_Value'Access), 2);
      Pack_Start (Hbox, Myb, False, False, 5);

      Pack_Start (VBox, Label, False, False, 0);
      Set_Text (Label, "0");

      Show_All (Frame);
   end Run;

end Create_Spin;