forked from bgrabitmap/bgracontrols
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgragraphiccontrol.pas
342 lines (295 loc) · 9.19 KB
/
bgragraphiccontrol.pas
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{
Created by BGRA Controls Team
Dibo, Circular, lainz (007) and contributors.
For detailed information see readme.txt
Site: https://sourceforge.net/p/bgra-controls/
Wiki: http://wiki.lazarus.freepascal.org/BGRAControls
Forum: http://forum.lazarus.freepascal.org/index.php/board,46.0.html
}
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
unit BGRAGraphicControl;
{$I bgracontrols.inc}
interface
uses
Classes, SysUtils, {$IFDEF FPC}LResources,{$ENDIF} Forms, Controls, Graphics, Dialogs, Types,
{$IFNDEF FPC}BGRAGraphics, GraphType, FPImage, {$ENDIF}
BCBaseCtrls, ExtCtrls, BGRABitmap, BCTypes;
type
{ TCustomBGRAGraphicControl }
TCustomBGRAGraphicControl = class(TBGRAGraphicCtrl)
private
{ Private declarations }
FOnRedraw: TBGRARedrawEvent;
FBevelInner, FBevelOuter: TPanelBevel;
FBevelWidth: TBevelWidth;
FBorderWidth: TBorderWidth;
FAlignment: TAlignment;
FColorOpacity: byte;
function GetBitmapHeight: integer;
function GetBitmapScale: double;
function GetBitmapWidth: integer;
procedure SetAlignment(const Value: TAlignment);
procedure SetBevelInner(const AValue: TPanelBevel);
procedure SetBevelOuter(const AValue: TPanelBevel);
procedure SetBevelWidth(const AValue: TBevelWidth);
procedure SetBitmapAutoScale(AValue: boolean);
procedure SetBorderWidth(const AValue: TBorderWidth);
procedure SetColorOpacity(const AValue: byte);
protected
{ Protected declarations }
FBGRA: TBGRABitmap;
FBitmapAutoScale: boolean;
procedure Paint; override;
procedure Resize; override;
procedure BGRASetSize(AWidth, AHeight: integer); virtual;
procedure RedrawBitmapContent; virtual;
procedure SetColor(Value: TColor); override;
procedure SetEnabled(Value: boolean); override;
procedure TextChanged; override;
property BitmapAutoScale: boolean read FBitmapAutoScale write SetBitmapAutoScale default true;
property BitmapScale: double read GetBitmapScale;
property BitmapWidth: integer read GetBitmapWidth;
property BitmapHeight: integer read GetBitmapHeight;
public
{ Public declarations }
constructor Create(TheOwner: TComponent); override;
procedure RedrawBitmap;
procedure DiscardBitmap;
destructor Destroy; override;
property OnRedraw: TBGRARedrawEvent Read FOnRedraw Write FOnRedraw;
property Bitmap: TBGRABitmap Read FBGRA;
property BorderWidth: TBorderWidth Read FBorderWidth Write SetBorderWidth default 0;
property BevelInner: TPanelBevel Read FBevelInner Write SetBevelInner default bvNone;
property BevelOuter: TPanelBevel
Read FBevelOuter Write SetBevelOuter default bvRaised;
property BevelWidth: TBevelWidth Read FBevelWidth Write SetBevelWidth default 1;
property ColorOpacity: byte Read FColorOpacity Write SetColorOpacity;
property Alignment: TAlignment Read FAlignment Write SetAlignment;
end;
TBGRAGraphicControl = class(TCustomBGRAGraphicControl)
published
{ Published declarations }
property Align;
property Anchors;
property OnRedraw;
property Bitmap;
property BitmapAutoscale;
property BitmapScale;
property BorderWidth;
property BevelInner;
property BevelOuter;
property BevelWidth;
property Color;
property ColorOpacity;
property Alignment;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
{$IFDEF FPC}
property OnPaint;
{$ENDIF}
property Caption;
end;
{$IFDEF FPC}procedure Register;{$ENDIF}
implementation
uses BGRABitmapTypes, LazVersion;
{$IFDEF FPC}
procedure Register;
begin
//{$I icons\bgragraphiccontrol_icon.lrs}
RegisterComponents('BGRA Controls', [TBGRAGraphicControl]);
end;
{$ENDIF}
procedure TCustomBGRAGraphicControl.SetAlignment(const Value: TAlignment);
begin
if FAlignment = Value then
exit;
FAlignment := Value;
DiscardBitmap;
end;
function TCustomBGRAGraphicControl.GetBitmapHeight: integer;
begin
result := round(ClientHeight * BitmapScale);
end;
function TCustomBGRAGraphicControl.GetBitmapScale: double;
begin
{$if laz_fullversion >= 2000000}
if not FBitmapAutoScale then
result := GetCanvasScaleFactor
else
result := 1;
{$else}
result := 1;
{$endif}
end;
function TCustomBGRAGraphicControl.GetBitmapWidth: integer;
begin
result := round(ClientWidth * BitmapScale);
end;
procedure TCustomBGRAGraphicControl.SetBevelInner(const AValue: TPanelBevel);
begin
if FBevelInner = AValue then
exit;
FBevelInner := AValue;
DiscardBitmap;
end;
procedure TCustomBGRAGraphicControl.SetBevelOuter(const AValue: TPanelBevel);
begin
if FBevelOuter = AValue then
exit;
FBevelOuter := AValue;
DiscardBitmap;
end;
procedure TCustomBGRAGraphicControl.SetBevelWidth(const AValue: TBevelWidth);
begin
if FBevelWidth = AValue then
exit;
FBevelWidth := AValue;
DiscardBitmap;
end;
procedure TCustomBGRAGraphicControl.SetBitmapAutoScale(AValue: boolean);
begin
if FBitmapAutoScale=AValue then Exit;
FBitmapAutoScale:=AValue;
DiscardBitmap;
end;
procedure TCustomBGRAGraphicControl.SetBorderWidth(const AValue: TBorderWidth);
begin
if FBorderWidth = AValue then
exit;
FBorderWidth := AValue;
DiscardBitmap;
end;
procedure TCustomBGRAGraphicControl.SetColorOpacity(const AValue: byte);
begin
if FColorOpacity = AValue then
exit;
FColorOpacity := AValue;
DiscardBitmap;
end;
procedure TCustomBGRAGraphicControl.Paint;
begin
BGRASetSize(BitmapWidth, BitmapHeight);
inherited Paint;
FBGRA.Draw(Canvas, rect(0, 0, ClientWidth, ClientHeight), False);
end;
procedure TCustomBGRAGraphicControl.Resize;
begin
inherited Resize;
DiscardBitmap;
end;
procedure TCustomBGRAGraphicControl.BGRASetSize(AWidth, AHeight: integer);
begin
if (FBGRA <> nil) and (AWidth <> FBGRA.Width) and (AHeight <> FBGRA.Height) then
begin
FBGRA.SetSize(AWidth, AHeight);
RedrawBitmapContent;
end;
end;
procedure TCustomBGRAGraphicControl.RedrawBitmapContent;
var
ARect: TRect;
TS: TTextStyle;
scale: Double;
begin
if (FBGRA <> nil) and (FBGRA.NbPixels <> 0) then
begin
FBGRA.Fill(ColorToBGRA(ColorToRGB(Color), FColorOpacity));
scale := BitmapScale;
ARect := GetClientRect;
ARect.Left := round(ARect.Left*scale);
ARect.Top := round(ARect.Top*scale);
ARect.Right := round(ARect.Right*scale);
ARect.Bottom := round(ARect.Bottom*scale);
// if BevelOuter is set then draw a frame with BevelWidth
if (BevelOuter <> bvNone) and (BevelWidth > 0) then
FBGRA.CanvasBGRA.Frame3d(ARect, round(BevelWidth*scale), BevelOuter,
BGRA(255, 255, 255, 200), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
InflateRect(ARect, -round(BorderWidth*scale), -round(BorderWidth*scale));
// if BevelInner is set then skip the BorderWidth and draw a frame with BevelWidth
if (BevelInner <> bvNone) and (BevelWidth > 0) then
FBGRA.CanvasBGRA.Frame3d(ARect, round(BevelWidth*scale), BevelInner,
BGRA(255, 255, 255, 160), BGRA(0, 0, 0, 160)); // Note: Frame3D inflates ARect
if Caption <> '' then
begin
FBGRA.CanvasBGRA.Font.Assign(Canvas.Font);
FBGRA.CanvasBGRA.Font.Height:= round(FBGRA.CanvasBGRA.Font.Height*scale);
{$IFDEF FPC}//#
TS := Canvas.TextStyle;
{$ENDIF}
TS.Alignment := Alignment;
TS.Layout := tlCenter;
TS.Opaque := False;
TS.Clipping := False;
{$IFDEF FPC}//#
TS.SystemFont := Canvas.Font.IsDefault;
{$ENDIF}
FBGRA.CanvasBGRA.Font.Color := Color xor $FFFFFF;
FBGRA.CanvasBGRA.Font.Opacity := 255;
if not Enabled then
FBGRA.CanvasBGRA.Font.Style := [fsStrikeOut]
else
FBGRA.CanvasBGRA.Font.Style := [];
FBGRA.CanvasBGRA.TextRect(ARect, ARect.Left, ARect.Top, Caption, TS);
end;
if Assigned(FOnRedraw) then
FOnRedraw(self, FBGRA);
end;
end;
procedure TCustomBGRAGraphicControl.SetColor(Value: TColor);
begin
if Value <> Color then
DiscardBitmap;
inherited SetColor(Value);
end;
procedure TCustomBGRAGraphicControl.SetEnabled(Value: boolean);
begin
if Value <> Enabled then
DiscardBitmap;
inherited SetEnabled(Value);
end;
procedure TCustomBGRAGraphicControl.TextChanged;
begin
DiscardBitmap;
end;
constructor TCustomBGRAGraphicControl.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
FBGRA := TBGRABitmap.Create;
FBitmapAutoScale:= true;
FBevelWidth := 1;
FAlignment := taCenter;
Color := clWhite;
FColorOpacity := 128;
FBevelOuter := bvRaised;
FBevelInner := bvNone;
end;
procedure TCustomBGRAGraphicControl.RedrawBitmap;
begin
RedrawBitmapContent;
Repaint;
end;
procedure TCustomBGRAGraphicControl.DiscardBitmap;
begin
if (FBGRA <> nil) and (FBGRA.NbPixels <> 0) then
begin
FBGRA.SetSize(0, 0);
Invalidate;
end;
end;
destructor TCustomBGRAGraphicControl.Destroy;
begin
FBGRA.Free;
inherited Destroy;
end;
end.