1 | unit BGRAGraphicControl;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
|
---|
9 | ExtCtrls, BGRABitmap, BCTypes;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
69 | procedure Register;
|
---|
70 |
|
---|
71 | implementation
|
---|
72 |
|
---|
73 | uses BGRABitmapTypes, Types;
|
---|
74 |
|
---|
75 | procedure Register;
|
---|
76 | begin
|
---|
77 | {$I bgragraphiccontrol_icon.lrs}
|
---|
78 | RegisterComponents('BGRA Controls', [TBGRAGraphicControl]);
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure TBGRAGraphicControl.SetAlignment(const Value: TAlignment);
|
---|
82 | begin
|
---|
83 | if FAlignment = Value then
|
---|
84 | exit;
|
---|
85 | FAlignment := Value;
|
---|
86 | DiscardBitmap;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure TBGRAGraphicControl.SetBevelInner(const AValue: TPanelBevel);
|
---|
90 | begin
|
---|
91 | if FBevelInner = AValue then
|
---|
92 | exit;
|
---|
93 | FBevelInner := AValue;
|
---|
94 | DiscardBitmap;
|
---|
95 | end;
|
---|
96 |
|
---|
97 | procedure TBGRAGraphicControl.SetBevelOuter(const AValue: TPanelBevel);
|
---|
98 | begin
|
---|
99 | if FBevelOuter = AValue then
|
---|
100 | exit;
|
---|
101 | FBevelOuter := AValue;
|
---|
102 | DiscardBitmap;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | procedure TBGRAGraphicControl.SetBevelWidth(const AValue: TBevelWidth);
|
---|
106 | begin
|
---|
107 | if FBevelWidth = AValue then
|
---|
108 | exit;
|
---|
109 | FBevelWidth := AValue;
|
---|
110 | DiscardBitmap;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure TBGRAGraphicControl.SetBorderWidth(const AValue: TBorderWidth);
|
---|
114 | begin
|
---|
115 | if FBorderWidth = AValue then
|
---|
116 | exit;
|
---|
117 | FBorderWidth := AValue;
|
---|
118 | DiscardBitmap;
|
---|
119 | end;
|
---|
120 |
|
---|
121 | procedure TBGRAGraphicControl.SetColorOpacity(const AValue: byte);
|
---|
122 | begin
|
---|
123 | if FColorOpacity = AValue then
|
---|
124 | exit;
|
---|
125 | FColorOpacity := AValue;
|
---|
126 | DiscardBitmap;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | procedure TBGRAGraphicControl.Paint;
|
---|
130 | begin
|
---|
131 | BGRASetSize(Width, Height);
|
---|
132 | inherited Paint;
|
---|
133 | FBGRA.Draw(Canvas, 0, 0, False);
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure TBGRAGraphicControl.Resize;
|
---|
137 | begin
|
---|
138 | inherited Resize;
|
---|
139 | DiscardBitmap;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TBGRAGraphicControl.BGRASetSize(AWidth, AHeight: integer);
|
---|
143 | begin
|
---|
144 | if (FBGRA <> nil) and (AWidth <> FBGRA.Width) and (AHeight <> FBGRA.Height) then
|
---|
145 | begin
|
---|
146 | FBGRA.SetSize(AWidth, AHeight);
|
---|
147 | RedrawBitmapContent;
|
---|
148 | end;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | procedure TBGRAGraphicControl.RedrawBitmapContent;
|
---|
152 | var
|
---|
153 | ARect: TRect;
|
---|
154 | TS: TTextStyle;
|
---|
155 | begin
|
---|
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;
|
---|
198 | end;
|
---|
199 |
|
---|
200 | procedure TBGRAGraphicControl.SetColor(Value: TColor);
|
---|
201 | begin
|
---|
202 | if Value <> Color then
|
---|
203 | DiscardBitmap;
|
---|
204 | inherited SetColor(Value);
|
---|
205 | end;
|
---|
206 |
|
---|
207 | procedure TBGRAGraphicControl.SetEnabled(Value: boolean);
|
---|
208 | begin
|
---|
209 | if Value <> Enabled then
|
---|
210 | DiscardBitmap;
|
---|
211 | inherited SetEnabled(Value);
|
---|
212 | end;
|
---|
213 |
|
---|
214 | procedure TBGRAGraphicControl.TextChanged;
|
---|
215 | begin
|
---|
216 | DiscardBitmap;
|
---|
217 | end;
|
---|
218 |
|
---|
219 | constructor TBGRAGraphicControl.Create(TheOwner: TComponent);
|
---|
220 | begin
|
---|
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;
|
---|
229 | end;
|
---|
230 |
|
---|
231 | procedure TBGRAGraphicControl.RedrawBitmap;
|
---|
232 | begin
|
---|
233 | RedrawBitmapContent;
|
---|
234 | Repaint;
|
---|
235 | end;
|
---|
236 |
|
---|
237 | procedure TBGRAGraphicControl.DiscardBitmap;
|
---|
238 | begin
|
---|
239 | if (FBGRA <> nil) and (FBGRA.NbPixels <> 0) then
|
---|
240 | begin
|
---|
241 | FBGRA.SetSize(0, 0);
|
---|
242 | Invalidate;
|
---|
243 | end;
|
---|
244 | end;
|
---|
245 |
|
---|
246 | destructor TBGRAGraphicControl.Destroy;
|
---|
247 | begin
|
---|
248 | FBGRA.Free;
|
---|
249 | inherited Destroy;
|
---|
250 | end;
|
---|
251 |
|
---|
252 | end.
|
---|