source: trunk/Packages/bgracontrols/bgragraphiccontrol.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 6.3 KB
Line 
1unit BGRAGraphicControl;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
9 ExtCtrls, BGRABitmap, BCTypes;
10
11type
12
13 { TBGRAGraphicControl }
14
15 TBGRAGraphicControl = class(TGraphicControl)
16 private
17 { Private declarations }
18 FBGRA: TBGRABitmap;
19 FOnRedraw: TBGRARedrawEvent;
20 FBevelInner, FBevelOuter: TPanelBevel;
21 FBevelWidth: TBevelWidth;
22 FBorderWidth: TBorderWidth;
23 FAlignment: TAlignment;
24 FColorOpacity: byte;
25 procedure SetAlignment(const Value: TAlignment);
26 procedure SetBevelInner(const AValue: TPanelBevel);
27 procedure SetBevelOuter(const AValue: TPanelBevel);
28 procedure SetBevelWidth(const AValue: TBevelWidth);
29 procedure SetBorderWidth(const AValue: TBorderWidth);
30 procedure SetColorOpacity(const AValue: byte);
31 protected
32 { Protected declarations }
33 procedure Paint; override;
34 procedure Resize; override;
35 procedure BGRASetSize(AWidth, AHeight: integer);
36 procedure RedrawBitmapContent; virtual;
37 procedure SetColor(Value: TColor); override;
38 procedure SetEnabled(Value: boolean); override;
39 procedure TextChanged; override;
40 public
41 { Public declarations }
42 constructor Create(TheOwner: TComponent); override;
43 procedure RedrawBitmap;
44 procedure DiscardBitmap;
45 destructor Destroy; override;
46 published
47 { Published declarations }
48 property OnRedraw: TBGRARedrawEvent Read FOnRedraw Write FOnRedraw;
49 property Bitmap: TBGRABitmap Read FBGRA;
50 property BorderWidth: TBorderWidth Read FBorderWidth Write SetBorderWidth default 0;
51 property BevelInner: TPanelBevel Read FBevelInner Write SetBevelInner default bvNone;
52 property BevelOuter: TPanelBevel
53 Read FBevelOuter Write SetBevelOuter default bvRaised;
54 property BevelWidth: TBevelWidth Read FBevelWidth Write SetBevelWidth default 1;
55 property Color;
56 property ColorOpacity: byte Read FColorOpacity Write SetColorOpacity;
57 property Alignment: TAlignment Read FAlignment Write SetAlignment;
58 property OnClick;
59 property OnDblClick;
60 property OnMouseDown;
61 property OnMouseEnter;
62 property OnMouseLeave;
63 property OnMouseMove;
64 property OnMouseUp;
65 property OnPaint;
66 property Caption;
67 end;
68
69procedure Register;
70
71implementation
72
73uses BGRABitmapTypes, Types;
74
75procedure Register;
76begin
77 {$I bgragraphiccontrol_icon.lrs}
78 RegisterComponents('BGRA Controls', [TBGRAGraphicControl]);
79end;
80
81procedure TBGRAGraphicControl.SetAlignment(const Value: TAlignment);
82begin
83 if FAlignment = Value then
84 exit;
85 FAlignment := Value;
86 DiscardBitmap;
87end;
88
89procedure TBGRAGraphicControl.SetBevelInner(const AValue: TPanelBevel);
90begin
91 if FBevelInner = AValue then
92 exit;
93 FBevelInner := AValue;
94 DiscardBitmap;
95end;
96
97procedure TBGRAGraphicControl.SetBevelOuter(const AValue: TPanelBevel);
98begin
99 if FBevelOuter = AValue then
100 exit;
101 FBevelOuter := AValue;
102 DiscardBitmap;
103end;
104
105procedure TBGRAGraphicControl.SetBevelWidth(const AValue: TBevelWidth);
106begin
107 if FBevelWidth = AValue then
108 exit;
109 FBevelWidth := AValue;
110 DiscardBitmap;
111end;
112
113procedure TBGRAGraphicControl.SetBorderWidth(const AValue: TBorderWidth);
114begin
115 if FBorderWidth = AValue then
116 exit;
117 FBorderWidth := AValue;
118 DiscardBitmap;
119end;
120
121procedure TBGRAGraphicControl.SetColorOpacity(const AValue: byte);
122begin
123 if FColorOpacity = AValue then
124 exit;
125 FColorOpacity := AValue;
126 DiscardBitmap;
127end;
128
129procedure TBGRAGraphicControl.Paint;
130begin
131 BGRASetSize(Width, Height);
132 inherited Paint;
133 FBGRA.Draw(Canvas, 0, 0, False);
134end;
135
136procedure TBGRAGraphicControl.Resize;
137begin
138 inherited Resize;
139 DiscardBitmap;
140end;
141
142procedure TBGRAGraphicControl.BGRASetSize(AWidth, AHeight: integer);
143begin
144 if (FBGRA <> nil) and (AWidth <> FBGRA.Width) and (AHeight <> FBGRA.Height) then
145 begin
146 FBGRA.SetSize(AWidth, AHeight);
147 RedrawBitmapContent;
148 end;
149end;
150
151procedure TBGRAGraphicControl.RedrawBitmapContent;
152var
153 ARect: TRect;
154 TS: TTextStyle;
155begin
156 if (FBGRA <> nil) and (FBGRA.NbPixels <> 0) then
157 begin
158 FBGRA.Fill(ColorToBGRA(ColorToRGB(Color), FColorOpacity));
159
160 ARect := GetClientRect;
161
162 // if BevelOuter is set then draw a frame with BevelWidth
163 if (BevelOuter <> bvNone) and (BevelWidth > 0) then
164 FBGRA.CanvasBGRA.Frame3d(ARect, BevelWidth, BevelOuter,
165 BGRA(255, 255, 255, 200), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
166
167 InflateRect(ARect, -BorderWidth, -BorderWidth);
168
169 // if BevelInner is set then skip the BorderWidth and draw a frame with BevelWidth
170 if (BevelInner <> bvNone) and (BevelWidth > 0) then
171 FBGRA.CanvasBGRA.Frame3d(ARect, BevelWidth, BevelInner,
172 BGRA(255, 255, 255, 160), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
173
174 if Caption <> '' then
175 begin
176 FBGRA.CanvasBGRA.Font.Assign(Canvas.Font);
177 TS := Canvas.TextStyle;
178 TS.Alignment := Alignment;
179 TS.Layout := tlCenter;
180 TS.Opaque := False;
181 TS.Clipping := False;
182 TS.SystemFont := Canvas.Font.IsDefault;
183
184 FBGRA.CanvasBGRA.Font.Color := Color xor $FFFFFF;
185 FBGRA.CanvasBGRA.Font.Opacity := 255;
186
187 if not Enabled then
188 FBGRA.CanvasBGRA.Font.Style := [fsStrikeOut]
189 else
190 FBGRA.CanvasBGRA.Font.Style := [];
191
192 FBGRA.CanvasBGRA.TextRect(ARect, ARect.Left, ARect.Top, Caption, TS);
193 end;
194
195 if Assigned(FOnRedraw) then
196 FOnRedraw(self, FBGRA);
197 end;
198end;
199
200procedure TBGRAGraphicControl.SetColor(Value: TColor);
201begin
202 if Value <> Color then
203 DiscardBitmap;
204 inherited SetColor(Value);
205end;
206
207procedure TBGRAGraphicControl.SetEnabled(Value: boolean);
208begin
209 if Value <> Enabled then
210 DiscardBitmap;
211 inherited SetEnabled(Value);
212end;
213
214procedure TBGRAGraphicControl.TextChanged;
215begin
216 DiscardBitmap;
217end;
218
219constructor TBGRAGraphicControl.Create(TheOwner: TComponent);
220begin
221 inherited Create(TheOwner);
222 FBGRA := TBGRABitmap.Create;
223 FBevelWidth := 1;
224 FAlignment := taCenter;
225 Color := clWhite;
226 FColorOpacity := 128;
227 FBevelOuter := bvRaised;
228 FBevelInner := bvNone;
229end;
230
231procedure TBGRAGraphicControl.RedrawBitmap;
232begin
233 RedrawBitmapContent;
234 Repaint;
235end;
236
237procedure TBGRAGraphicControl.DiscardBitmap;
238begin
239 if (FBGRA <> nil) and (FBGRA.NbPixels <> 0) then
240 begin
241 FBGRA.SetSize(0, 0);
242 Invalidate;
243 end;
244end;
245
246destructor TBGRAGraphicControl.Destroy;
247begin
248 FBGRA.Free;
249 inherited Destroy;
250end;
251
252end.
Note: See TracBrowser for help on using the repository browser.