-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtk_life.adb
315 lines (261 loc) · 8.67 KB
/
gtk_life.adb
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
-- This started off as the scribble program that comes with the
-- libgtkada-doc package
with Glib; use Glib;
with Gdk;
with Gdk.Drawable; use Gdk.Drawable;
with Gdk.Event; use Gdk.Event;
with Gdk.Font; use Gdk.Font;
with Gdk.GC;
with Gdk.Pixmap; use Gdk.Pixmap;
with Gdk.Rectangle; use Gdk.Rectangle;
with Gdk.Types; use Gdk.Types;
with Gdk.Window; use Gdk.Window;
with Gtk; use Gtk;
with Gtk.Box; use Gtk.Box;
with Gtk.Button; use Gtk.Button;
with Gtk.Drawing_Area; use Gtk.Drawing_Area;
with Gtk.Enums; use Gtk.Enums;
with Gtk.Main; use Gtk.Main;
with Gtk.Rc;
with Gtk.Handlers; use Gtk.Handlers;
with Gtk.Style; use Gtk.Style;
with Gtk.Window; use Gtk.Window;
with Life_G;
procedure Gtk_Life is
type Index_X is mod 40;
type Index_Y is mod 40;
package Life is new Life_G (Index_X, Index_Y);
Gtk_Table : Life.Table := Life.Empty_Table;
-- Size in pixels of a block to be drawn in X and Y directions
X_Size, Y_Size : Gint := 5;
Pixmap : Gdk_Pixmap;
Font : Gdk_Font;
Window : Gtk_Window;
package Configured is new Gtk.Handlers.Return_Callback
(Widget_Type => Gtk_Drawing_Area_Record,
Return_Type => Boolean);
package Destroyed is new Gtk.Handlers.Callback
(Widget_Type => Gtk_Window_Record);
package Propagated is new Gtk.Handlers.Callback
(Widget_Type => Gtk_Drawing_Area_Record);
procedure Draw_Life (Drawing_Area : access Gtk_Drawing_Area_Record'Class) is
begin
for Y in Index_Y'range loop
for X in Index_X'range loop
declare
C : Life.Cell := Life.Get (Gtk_Table, X, Y);
GC : Gdk.GC.Gdk_GC;
use Life;
begin
if C = Life.Alive then
GC := Get_Foreground (Get_Style (Drawing_Area), State_Normal);
else
GC := Get_White (Get_Style (Drawing_Area));
end if;
Draw_Rectangle
(Pixmap,
GC,
True,
Gint (X) * X_Size,
Gint (Y) * Y_Size,
X_Size,
Y_Size);
end;
end loop;
end loop;
Draw (Drawing_Area);
end Draw_Life;
---------------------
-- Configure_Event --
---------------------
function Configure_Event
(Drawing_Area : access Gtk_Drawing_Area_Record'Class)
return Boolean
is
Win : Gdk_Window;
Width : Gint;
Height : Gint;
begin
Win := Get_Window (Drawing_Area);
Get_Size (Win, Width, Height);
-- Ensure its at least the minimum size.
if Width < 200 then
Width := 200;
end if;
if Height < 200 then
Height := 200;
end if;
-- Recalc block sizes.
X_Size := Width / (Gint (Index_X'Last) + 1);
Y_Size := Height / (Gint (Index_Y'Last) + 1);
Gdk.Pixmap.Gdk_New (Pixmap, Win, Width, Height, -1);
Draw_Rectangle (Pixmap, Get_White (Get_Style (Drawing_Area)),
True, 0, 0, Width, Height);
Draw_Life (Drawing_Area);
return True;
end Configure_Event;
------------------
-- Expose_Event --
------------------
function Expose_Event
(Drawing_Area : access Gtk_Drawing_Area_Record'Class;
Event : in Gdk.Event.Gdk_Event)
return Boolean
is
Area : Gdk_Rectangle := Get_Area (Event);
begin
-- Ada.Text_Io.Put_Line("exposing..." &
-- Gint'Image(Area.X) &
-- Gint'Image(Area.Y) &
-- Gint'Image(Gint(Area.Width)) &
-- Gint'Image(Gint(Area.Height))&
-- Integer'Image(Bogus));
Draw_Pixmap (Get_Window (Drawing_Area),
Get_Fg_GC (Get_Style (Drawing_Area), State_Normal),
Pixmap, Area.X, Area.Y, Area.X, Area.Y,
Gint (Area.Width), Gint (Area.Height));
return True;
end Expose_Event;
----------------
-- Toggle_Block --
----------------
procedure Toggle_Block
(Widget : access Gtk_Drawing_Area_Record'Class;
X, Y : Gint) is
Cx : Index_X;
Cy : Index_Y;
use Life;
begin
-- Sanity check!
if X < 0 or else
Y < 0 or else
X >= (Gint (Index_X'Last) + 1) * X_Size or else
Y >= (Gint (Index_Y'Last) + 1) * Y_Size then
return;
end if;
Cx := Index_X (X / X_Size);
Cy := Index_Y (Y / Y_Size) ;
if Life.Get (Gtk_Table, Cx, Cy) = Life.Dead then
Life.Set (Gtk_Table, Cx, Cy);
Draw_Rectangle
(Pixmap,
Get_Black (Get_Style (Widget)),
True,
Gint (Cx) * X_Size,
Gint (Cy) * Y_Size,
X_Size,
Y_Size);
else
Life.Set (Gtk_Table, Cx, Cy, Life.Dead);
Draw_Rectangle
(Pixmap,
Get_White (Get_Style (Widget)),
True,
Gint (Cx) * X_Size,
Gint (Cy) * Y_Size,
X_Size,
Y_Size);
end if;
Draw (Widget);
end Toggle_Block;
------------------------
-- Button_Press_Event --
------------------------
function Button_Press_Event
(Widget : access Gtk_Drawing_Area_Record'Class;
Event : in Gdk_Event)
return Boolean
is
use type Gdk.Pixmap.Gdk_Pixmap;
begin
if Get_Button (Event) = 1 and Pixmap /= Null_Pixmap then
Toggle_Block (Widget, Gint (Get_X (Event)), Gint (Get_Y (Event)));
end if;
return True;
end Button_Press_Event;
-------------------------
-- Motion_Notify_Event --
-------------------------
function Motion_Notify_Event
(Widget : access Gtk_Drawing_Area_Record'Class;
Event : in Gdk_Event)
return Boolean
is
X, Y : Gint;
State : Gdk_Modifier_Type;
Result : Gdk_Window;
use type Gdk.Pixmap.Gdk_Pixmap;
begin
if Get_Is_Hint (Event) then
Get_Pointer (Get_Window (Widget), X, Y, State, Result);
else
X := Gint (Get_X (Event));
Y := Gint (Get_Y (Event));
State := Get_State (Event);
end if;
if (State and Button1_Mask) /= 0 and Pixmap /= Null_Pixmap then
Toggle_Block (Widget, X, Y);
end if;
return True;
end Motion_Notify_Event;
---------
-- Bye --
---------
procedure Bye (Window : access Gtk.Window.Gtk_Window_Record'Class) is
pragma Warnings (Off, Window);
begin
Gtk.Main.Main_Quit;
end Bye;
procedure Propagate (Drawing_Area : access Gtk.Drawing_Area.Gtk_Drawing_Area_Record'Class) is
begin
Life.Propagate (Gtk_Table);
Draw_Life (Drawing_Area);
end Propagate;
Button : Gtk_Button;
Vbox : Gtk_Box;
Drawing_Area : Gtk_Drawing_Area;
begin
Gtk.Main.Init;
Gtk.Rc.Parse ("gtk_life");
Gtk_New (Window, Window_Toplevel);
Set_Title (Window, "Gtk Life");
Set_Border_Width (Window, Border_Width => 5);
Destroyed.Connect (Window, "destroy", Destroyed.To_Marshaller (Bye'Access));
Gtk_New_Vbox (Vbox, Homogeneous => False, Spacing => 0);
Add (Window, Vbox);
Show (Vbox);
Gtk_New (Drawing_Area);
Size (Drawing_Area, 200, 200);
Pack_Start (In_Box => Vbox, Child => Drawing_Area);
Show (Drawing_Area);
Unrealize (Drawing_Area);
Set_Events (Drawing_Area, Exposure_Mask or Leave_Notify_Mask or
Button_Press_Mask or Pointer_Motion_Mask or
Pointer_Motion_Hint_Mask);
Configured.Connect (Widget => Drawing_Area,
Name => "expose_event",
Marsh => Configured.To_Marshaller
(Expose_Event'Access));
Configured.Connect (Widget => Drawing_Area,
Name => "motion_notify_event",
Marsh => Configured.To_Marshaller
(Motion_Notify_Event'Access));
Configured.Connect (Widget => Drawing_Area,
Name => "configure_event",
Marsh => Configured.To_Marshaller
(Configure_Event'Access));
Configured.Connect (Widget => Drawing_Area,
Name => "button_press_event",
Marsh => Configured.To_Marshaller
(Button_Press_Event'Access));
Gtk_New (Button, "Propagate");
Pack_Start (Vbox, Button, Expand => False, Fill => False);
Propagated.Object_Connect (Button,
"clicked",
Propagated.To_Marshaller (Propagate'Access),
Slot_Object => Drawing_Area);
Load (Font,
"-adobe-helvetica-medium-o-normal--14-140-75-75-p-78-iso8859-1");
Show_All (Window);
Gtk.Main.Main;
end Gtk_Life;