~ubuntu-branches/ubuntu/feisty/fpc/feisty

« back to all changes in this revision

Viewing changes to fpcdocs/gtk2ex/digit.pp

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-01-27 20:08:50 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070127200850-9mrptaqqjsx9nwa7
Tags: 2.0.4-5
* Fixed Build-Depends.
* Add myself to Uploaders in debian/control.
* Make sure that the sources are really patched before building them.
* Build unit 'libc' on powerpc too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
unit digit;
 
2
 
 
3
{$mode objfpc}
 
4
{$H+}
 
5
 
 
6
Interface
 
7
 
 
8
uses gtk,gdk,glib;
 
9
 
 
10
Type
 
11
  TPoint = Record
 
12
    X,Y : gint;
 
13
    end;
 
14
 
 
15
  PGtkDigit = ^TGtkDigit;
 
16
  TGtkDigit = Record
 
17
    ParentWidget : TGtkWidget;
 
18
    borderwidth,
 
19
    digit : guint;
 
20
    Corners : Array [1..6] of TPoint;
 
21
  end;
 
22
 
 
23
  PGtkDigitClass = ^TGtkDigitClass;
 
24
  TGtkDigitClass = Record
 
25
    Parent_Class : TGtkWidgetClass;
 
26
  end;
 
27
 
 
28
Function  GtkDigit_get_type : Guint;cdecl;
 
29
Function  GtkDigit_new : PGtkWidget;cdecl;
 
30
Procedure GtkDigit_set_digit (Obj : PGtkDigit; Digit : guint);cdecl;
 
31
Function  GtkDigit_get_digit (Obj : PGtkDigit) : guint;cdecl;
 
32
 
 
33
Type
 
34
  PGtkActiveDigit = ^TGtkActiveDigit;
 
35
  TGtkActiveDigit = Record
 
36
    ParentWidget : TGtkDigit;
 
37
    Button : guint8;
 
38
  end;
 
39
 
 
40
  PGtkActiveDigitClass = ^TGtkActiveDigitClass;
 
41
  TGtkActiveDigitClass = Record
 
42
    Parent_Class : TGtkDigitClass;
 
43
  end;
 
44
 
 
45
Function  GtkActiveDigit_get_type : Guint;cdecl;
 
46
Function  GtkActiveDigit_new : PGtkWidget;cdecl;
 
47
 
 
48
Implementation
 
49
 
 
50
Type
 
51
  TLEDSegment = (lsTop,lsCenter,lsBottom,
 
52
                 lsLeftTop,lsRightTop,
 
53
                 lsLeftBottom, lsRightBottom);
 
54
  TLedSegments = Array[TLedSegment] of boolean;
 
55
  TSegmentCorners = Array [1..2] of Byte;
 
56
 
 
57
Const
 
58
  DigitSegments : Array[0..9] of TLEDSegments =
 
59
    (
 
60
     (true,false,true,true,true,true,true),       // 0
 
61
     (false,false,false,false,true,false,true),   // 1
 
62
     (true,true,true,false,true,true,false),      // 2
 
63
     (true,true,true,false,true,false,true),      // 3
 
64
     (false,true,false,true,true,false,true),     // 4
 
65
     (true,true,true,true,false,false,true),      // 5
 
66
     (true,true,true,true,false,true,true),       // 6
 
67
     (true,false,false,false,true,false,true),    // 7
 
68
     (true,true,true,true,true,true,true),        // 8
 
69
     (true,true,true,true,true,false,true)        // 9
 
70
    );
 
71
 
 
72
  SegmentCorners : Array [TLEDSegment] of TSegmentCorners =
 
73
    (
 
74
     (1,2),
 
75
     (3,4),
 
76
     (5,6),
 
77
     (1,3),
 
78
     (2,4),
 
79
     (3,5),
 
80
     (4,6)
 
81
    );
 
82
 
 
83
Const
 
84
  GtkDigitType : guint = 0;
 
85
 
 
86
Procedure GTKDigitSizeRequest (Widget : PGtkWidget;
 
87
                               Request : PGtkRequisition);cdecl;
 
88
 
 
89
Var BW : guint;
 
90
 
 
91
begin
 
92
  With PGTKDigit(Widget)^ do
 
93
    BW:=BorderWidth;
 
94
  With Request^ do
 
95
    begin
 
96
    Width:=20+2*BW;
 
97
    Height:=40+2*BW;
 
98
    end;
 
99
end;
 
100
 
 
101
Function GTKDigitExpose (Widget : PGTKWidget;
 
102
                         ExposeEvent : PGDKEventExpose) : gint;cdecl;
 
103
 
 
104
 
 
105
Var
 
106
  Segment : TLedSegment;
 
107
 
 
108
begin
 
109
  With PGTKDigit(Widget)^ do
 
110
    For Segment:=lsTop to lsRightBottom do
 
111
      if DigitSegments[Digit][Segment] then
 
112
        gdk_draw_line(widget^.window,
 
113
                  PgtkStyle(widget^.thestyle)^.fg_gc[widget^.state],
 
114
                  Corners[SegmentCorners[Segment][1]].X,
 
115
                  Corners[SegmentCorners[Segment][1]].Y,
 
116
                  Corners[SegmentCorners[Segment][2]].X,
 
117
                  Corners[SegmentCorners[Segment][2]].Y
 
118
                  )
 
119
      else
 
120
        gdk_draw_line(widget^.window,
 
121
                  PgtkStyle(widget^.thestyle)^.bg_gc[widget^.state],
 
122
                  Corners[SegmentCorners[Segment][1]].X,
 
123
                  Corners[SegmentCorners[Segment][1]].Y,
 
124
                  Corners[SegmentCorners[Segment][2]].X,
 
125
                  Corners[SegmentCorners[Segment][2]].Y
 
126
                  );
 
127
 
 
128
end;
 
129
 
 
130
Procedure SetDigitCorners(Digit : PGtkDigit; IgnoreOffset : Boolean);
 
131
 
 
132
Var
 
133
  BW : guint;
 
134
  W,H,SX,SY : gint;
 
135
  i : longint;
 
136
  Widget : PGTKWidget;
 
137
 
 
138
begin
 
139
  Widget:=PGTKWidget(Digit);
 
140
  BW:=Digit^.Borderwidth;
 
141
  If IgnoreOffset then
 
142
    begin
 
143
    SX:=0;
 
144
    SY:=0;
 
145
    end
 
146
  else
 
147
    begin
 
148
    SX:=Widget^.Allocation.x;
 
149
    SY:=Widget^.Allocation.y;
 
150
    end;
 
151
  W:=Widget^.Allocation.Width-2*BW;
 
152
  H:=(Widget^.Allocation.Height-2*BW) div 2;
 
153
  With PGTKDigit(Widget)^ do
 
154
    For I:=1 to 6 do
 
155
      begin
 
156
      // Set X
 
157
      Case I of
 
158
        1,3,5 : Corners[i].X:=SX+BW;
 
159
        2,4,6 : Corners[i].X:=SX+BW+W;
 
160
      end;
 
161
      // Set Y
 
162
      Case I of
 
163
        1,2 : Corners[i].Y:=SY+BW;
 
164
        3,4 : Corners[i].Y:=SY+BW+H;
 
165
        5,6 : Corners[i].Y:=SY+BW+2*H
 
166
      end;
 
167
      end;
 
168
end;
 
169
 
 
170
procedure GTKDigitSizeAllocate(Widget : PGTKWidget;
 
171
                               Allocation : PGTKAllocation);cdecl;
 
172
 
 
173
begin
 
174
  Widget^.Allocation:=Allocation^;
 
175
  SetDigitCorners(PGtkDigit(Widget),False);
 
176
end;
 
177
 
 
178
Procedure GtkDigitClassInit (CObj : PGtkDigitClass);cdecl;
 
179
 
 
180
begin
 
181
  With PGtkWidgetClass(Cobj)^ do
 
182
    begin
 
183
    size_request:=@GTKDigitSizeRequest;
 
184
    expose_event:=@GTKDigitExpose;
 
185
    size_allocate:=@GTKDigitSizeAllocate;
 
186
    end;
 
187
end;
 
188
 
 
189
 
 
190
Procedure GtkDigitInit (Obj : PGtkDigit);cdecl;
 
191
 
 
192
Var I : longint;
 
193
 
 
194
begin
 
195
  gtk_widget_set_flags(pgtkWidget(obj),GTK_NO_WINDOW);
 
196
  With Obj^ do
 
197
    begin
 
198
    Digit:=0;
 
199
    BorderWidth:=2;
 
200
    For I:=1 to 6 do
 
201
    with Corners[i] do
 
202
      begin
 
203
      X:=0;
 
204
      Y:=0;
 
205
      end;
 
206
    end;
 
207
end;
 
208
 
 
209
Function GtkDigit_get_type : Guint;cdecl;
 
210
 
 
211
Const
 
212
  GtkDigitInfo : TGtkTypeInfo =
 
213
    (type_name : 'GtkDigit';
 
214
     object_size : SizeOf(TGtkDigit);
 
215
     class_size : SizeOf(TGtkDigitClass);
 
216
     class_init_func : TGtkClassInitFunc(@GtkDigitClassInit);
 
217
     object_init_func : TGtkObjectInitFunc(@GtkDigitInit);
 
218
     reserved_1 : Nil;
 
219
     reserved_2 : Nil;
 
220
     base_class_init_func : Nil
 
221
    );
 
222
 
 
223
begin
 
224
  if (GtkDigitType=0) then
 
225
    GtkDigitType:=gtk_type_unique(gtk_widget_get_type,@GtkDigitInfo);
 
226
  Result:=GtkDigitType;
 
227
end;
 
228
 
 
229
Function GtkDigit_new : PGtkWidget;cdecl;
 
230
 
 
231
begin
 
232
  Result:=gtk_type_new(GtkDigit_get_type)
 
233
end;
 
234
 
 
235
Procedure GtkDigit_set_digit (Obj : PGtkDigit; Digit : guint);cdecl;
 
236
 
 
237
begin
 
238
  if Digit in [0..9] then
 
239
    begin
 
240
    Obj^.Digit:=Digit;
 
241
    gtk_widget_draw(PGTKWidget(Obj),Nil);
 
242
    end;
 
243
end;
 
244
 
 
245
Function GtkDigit_get_digit (Obj : PGtkDigit) : guint;cdecl;
 
246
 
 
247
begin
 
248
  Result:=Obj^.Digit;
 
249
end;
 
250
 
 
251
{ ---------------------------------------------------------------------
 
252
    GTKActiveDigit
 
253
  ---------------------------------------------------------------------}
 
254
 
 
255
Procedure GtkActiveDigitRealize(widget : PgtkWidget);cdecl;
 
256
 
 
257
Var
 
258
 attr : TGDKWindowAttr;
 
259
 Mask : gint;
 
260
 
 
261
begin
 
262
  GTK_WIDGET_SET_FLAGS(widget,GTK_REALIZED);
 
263
  With Attr do
 
264
    begin
 
265
    x := widget^.allocation.x;
 
266
    y := widget^.allocation.y;
 
267
    width:=widget^.allocation.width;
 
268
    height:=widget^.allocation.height;
 
269
    wclass:=GDK_INPUT_OUTPUT;
 
270
    window_type:=gdk_window_child;
 
271
    event_mask:=gtk_widget_get_events(widget) or GDK_EXPOSURE_MASK or
 
272
                GDK_BUTTON_PRESS_MASK OR GDK_BUTTON_RELEASE_MASK;
 
273
    visual:=gtk_widget_get_visual(widget);
 
274
    colormap:=gtk_widget_get_colormap(widget);
 
275
    end;
 
276
  Mask:=GDK_WA_X or GDK_WA_Y or GDK_WA_VISUAL or GDK_WA_COLORMAP;
 
277
  widget^.Window:=gdk_window_new(widget^.parent^.window,@attr,mask);
 
278
  widget^.thestyle:=gtk_style_attach(widget^.thestyle,widget^.window);
 
279
  gdk_window_set_user_data(widget^.window,widget);
 
280
  gtk_style_set_background(widget^.thestyle,widget^.window,GTK_STATE_ACTIVE);
 
281
end;
 
282
 
 
283
procedure GTKActiveDigitSizeAllocate(Widget : PGTKWidget;
 
284
                               Allocation : PGTKAllocation);cdecl;
 
285
begin
 
286
  Widget^.allocation:=Allocation^;
 
287
  if GTK_WIDGET_REALIZED(widget) then
 
288
    gdk_window_move_resize(widget^.window,
 
289
                           Allocation^.x,
 
290
                           Allocation^.y,
 
291
                           Allocation^.width,
 
292
                           Allocation^.height);
 
293
  SetDigitCorners(PGTKDigit(Widget),True);
 
294
end;
 
295
 
 
296
Function GtkActiveDigitButtonPress(Widget: PGtKWidget;
 
297
                                    Event : PGdkEventButton) : gint;cdecl;
 
298
 
 
299
begin
 
300
  PGTKActiveDigit(Widget)^.Button:=Event^.Button;
 
301
end;
 
302
 
 
303
Function GtkActiveDigitButtonRelease(Widget: PGtKWidget;
 
304
                                      Event : PGdkEventButton) : gint;cdecl;
 
305
 
 
306
Var
 
307
  Digit : PGtkDigit;
 
308
  D : guint;
 
309
 
 
310
begin
 
311
  Digit:=PGTKDigit(Widget);
 
312
  D:=gtkdigit_get_digit(Digit);
 
313
  If PGTKActiveDigit(Digit)^.Button=Event^.Button then
 
314
    begin
 
315
    If Event^.Button=1 then
 
316
      GTKDigit_set_digit(Digit,D+1)
 
317
    else if Event^.Button=3 then
 
318
      GTKDigit_set_digit(Digit,D-1)
 
319
    else
 
320
      GTKDigit_set_digit(Digit,0);
 
321
    end;
 
322
  PGTKActiveDigit(Digit)^.Button:=0;
 
323
end;
 
324
 
 
325
Procedure GtkActiveDigitClassInit (CObj : PGtkActiveDigitClass);cdecl;
 
326
 
 
327
begin
 
328
  With PGtkWidgetClass(Cobj)^ do
 
329
    begin
 
330
    realize := @GtkActiveDigitRealize;
 
331
    size_allocate := @GtkActiveDigitSizeAllocate;
 
332
    button_press_event:=@GtkActiveDigitButtonPress;
 
333
    button_release_event:=@GtkActiveDigitButtonRelease;
 
334
    end;
 
335
end;
 
336
 
 
337
 
 
338
Procedure GtkActiveDigitInit (Obj : PGtkActiveDigit);cdecl;
 
339
 
 
340
Var I : longint;
 
341
 
 
342
begin
 
343
  gtk_widget_unset_flags(pgtkWidget(obj),GTK_NO_WINDOW);
 
344
  With Obj^ do
 
345
    Button:=0;
 
346
end;
 
347
 
 
348
Const
 
349
  GtkActiveDigitType : guint = 0;
 
350
 
 
351
Function  GtkActiveDigit_get_type : Guint;cdecl;
 
352
 
 
353
Const
 
354
  GtkActiveDigitInfo : TGtkTypeInfo =
 
355
    (type_name : 'GtkActiveDigit';
 
356
     object_size : SizeOf(TGtkActiveDigit);
 
357
     class_size : SizeOf(TGtkActiveDigitClass);
 
358
     class_init_func : TGtkClassInitFunc(@GtkActiveDigitClassInit);
 
359
     object_init_func : TGtkObjectInitFunc(@GtkActiveDigitInit);
 
360
     reserved_1 : Nil;
 
361
     reserved_2 : Nil;
 
362
     base_class_init_func : Nil
 
363
    );
 
364
 
 
365
begin
 
366
  if (GtkActiveDigitType=0) then
 
367
    GtkActiveDigitType:=gtk_type_unique(gtkdigit_get_type,@GtkActiveDigitInfo);
 
368
  Result:=GtkActiveDigitType;
 
369
end;
 
370
 
 
371
 
 
372
Function  GtkActiveDigit_new : PGtkWidget;cdecl;
 
373
 
 
374
begin
 
375
  Result:=gtk_type_new(GtkActiveDigit_get_type)
 
376
end;
 
377
 
 
378
end.  
 
 
b'\\ No newline at end of file'