-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
bclabel.pas
384 lines (331 loc) · 9.45 KB
/
bclabel.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{ Equivalent of standard lazarus TLabel but using BGRA Controls framework for text
render.
Functionality:
- Customizable background (gradients etc.)
- Customizable border (rounding etc.)
- FontEx (shadow, word wrap, etc.)
originally written in 2012 by Krzysztof Dibowski dibowski at interia.pl
}
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
unit BCLabel;
{$I bgracontrols.inc}
interface
uses
Classes, SysUtils,{$IFDEF FPC}LResources,{$ENDIF}
types, Forms, Controls, Graphics, Dialogs,
BCBasectrls, BGRABitmap, BGRABitmapTypes, BCTypes;
type
{ TCustomBCLabel }
TCustomBCLabel = class(TBCStyleGraphicControl)
private
{ Private declarations }
{$IFDEF INDEBUG}
FRenderCount: Integer;
{$ENDIF}
FBackground: TBCBackground;
FBGRA: TBGRABitmapEx;
FBorder: TBCBorder;
FFontEx: TBCFont;
FInnerMargin: single;
FRounding: TBCRounding;
procedure Render;
procedure SetInnerMargin(AValue: single);
procedure SetRounding(AValue: TBCRounding);
procedure UpdateSize;
procedure SetBackground(AValue: TBCBackground);
procedure SetBorder(AValue: TBCBorder);
procedure SetFontEx(AValue: TBCFont);
procedure OnChangeProperty(Sender: TObject; {%H-}Data: BGRAPtrInt);
procedure OnChangeFont({%H-}Sender: TObject; {%H-}AData: BGRAPtrInt);
protected
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
{%H-}WithThemeSpace: boolean); override;
class function GetControlClassDefaultSize: TSize; override;
procedure TextChanged; override;
protected
{$IFDEF INDEBUG}
function GetDebugText: String; override;
{$ENDIF}
procedure DrawControl; override;
procedure RenderControl; override;
function GetStyleExtension: String; override;
protected
{ Protected declarations }
property AutoSize default True;
property Background: TBCBackground read FBackground write SetBackground;
property Border: TBCBorder read FBorder write SetBorder;
property FontEx: TBCFont read FFontEx write SetFontEx;
property Rounding: TBCRounding read FRounding write SetRounding;
property InnerMargin: single read FInnerMargin write SetInnerMargin;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure UpdateControl; override; // Called by EndUpdate
public
{ Streaming }
{$IFDEF FPC}
procedure SaveToFile(AFileName: string); override;
procedure LoadFromFile(AFileName: string); override;
{$ENDIF}
procedure OnFindClass({%H-}Reader: TReader; const AClassName: string;
var ComponentClass: TComponentClass);
end;
{ TBCLabel }
TBCLabel = class(TCustomBCLabel)
published
property Action;
property Align;
property Anchors;
property AssignStyle;
property AutoSize;
property Background;
property Border;
property BorderSpacing;
property Caption;
property Cursor;
property Enabled;
property FontEx;
property Height;
property HelpContext;
property HelpKeyword;
property HelpType;
property Hint;
property InnerMargin;
property Left;
property PopupMenu;
property Rounding;
property ShowHint;
property Tag;
property Top;
property Visible;
property Width;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
end;
{$IFDEF FPC}procedure Register;{$ENDIF}
implementation
uses BCTools;
{$IFDEF FPC}procedure Register;
begin
RegisterComponents('BGRA Controls',[TBCLabel]);
end;
{$ENDIF}
{ TCustomBCLabel }
procedure TCustomBCLabel.Render;
var r: TRect;
begin
if (csCreating in ControlState) or IsUpdating then
Exit;
FBGRA.NeedRender := False;
FBGRA.SetSize(Width, Height);
FBGRA.Fill(BGRAPixelTransparent); // Clear;
r := FBGRA.ClipRect;
CalculateBorderRect(FBorder,r);
RenderBackgroundAndBorder(FBGRA.ClipRect, FBackground, TBGRABitmap(FBGRA), FRounding, FBorder, FInnerMargin);
RenderText(FBGRA.ClipRect, FFontEx, Caption, TBGRABitmap(FBGRA), Enabled);
{$IFDEF INDEBUG}
FRenderCount := FRenderCount +1;
{$ENDIF}
{$IFNDEF FPC}//# //@ IN DELPHI NEEDRENDER NEED TO BE TRUE. IF FALSE COMPONENT IN BGRANORMAL BE BLACK AFTER INVALIDATE.
FBGRA.NeedRender := True;
{$ENDIF}
end;
procedure TCustomBCLabel.SetInnerMargin(AValue: single);
begin
if FInnerMargin=AValue then Exit;
FInnerMargin:=AValue;
RenderControl;
Invalidate;
end;
procedure TCustomBCLabel.SetRounding(AValue: TBCRounding);
begin
if FRounding = AValue then Exit;
FRounding.Assign(AValue);
RenderControl;
Invalidate;
end;
procedure TCustomBCLabel.UpdateSize;
begin
InvalidatePreferredSize;
AdjustSize;
end;
procedure TCustomBCLabel.SetBackground(AValue: TBCBackground);
begin
FBackground.Assign(AValue);
RenderControl;
Invalidate;
end;
procedure TCustomBCLabel.SetBorder(AValue: TBCBorder);
begin
FBorder.Assign(AValue);
RenderControl;
Invalidate;
end;
procedure TCustomBCLabel.SetFontEx(AValue: TBCFont);
begin
FFontEx.Assign(AValue);
RenderControl;
Invalidate;
end;
procedure TCustomBCLabel.OnChangeProperty(Sender: TObject; Data: BGRAPtrInt);
begin
RenderControl;
if (Sender = FBorder) and AutoSize then
UpdateSize;
Invalidate;
end;
procedure TCustomBCLabel.OnChangeFont(Sender: TObject; AData: BGRAPtrInt);
begin
RenderControl;
UpdateSize;
Invalidate;
end;
procedure TCustomBCLabel.CalculatePreferredSize(var PreferredWidth,
PreferredHeight: integer; WithThemeSpace: boolean);
begin
if (Parent = nil) or (not Parent.HandleAllocated) then
Exit;
CalculateTextSize(Caption, FFontEx, PreferredWidth, PreferredHeight);
if AutoSize and (FBorder.Style<>bboNone) then
begin
Inc(PreferredHeight, 2 * FBorder.Width);
Inc(PreferredWidth, 2 * FBorder.Width);
end;
end;
class function TCustomBCLabel.GetControlClassDefaultSize: TSize;
begin
Result.cx := 100;
Result.cy := 25;
end;
procedure TCustomBCLabel.TextChanged;
begin
inherited TextChanged;
RenderControl;
UpdateSize;
Invalidate;
end;
{$IFDEF INDEBUG}
function TCustomBCLabel.GetDebugText: String;
begin
Result := 'R: '+IntToStr(FRenderCount);
end;
{$ENDIF}
procedure TCustomBCLabel.DrawControl;
begin
inherited DrawControl;
if FBGRA.NeedRender then
Render;
FBGRA.Draw(Self.Canvas,0,0,False);
{$IFNDEF FPC}//# //@ IN DELPHI RenderControl NEDD. IF NO RenderControl BE BLACK AFTER INVALIDATE.
FBGRA.NeedRender := True;
{$ENDIF}
end;
procedure TCustomBCLabel.RenderControl;
begin
inherited RenderControl;
if FBGRA<>nil then
FBGRA.NeedRender := True;
end;
function TCustomBCLabel.GetStyleExtension: String;
begin
Result := 'bclbl';
end;
procedure TCustomBCLabel.UpdateControl;
begin
RenderControl;
inherited UpdateControl; // invalidate
end;
{$IFDEF FPC}
procedure TCustomBCLabel.SaveToFile(AFileName: string);
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
WriteComponentAsTextToStream(AStream, Self);
AStream.SaveToFile(AFileName);
finally
AStream.Free;
end;
end;
procedure TCustomBCLabel.LoadFromFile(AFileName: string);
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
AStream.LoadFromFile(AFileName);
ReadComponentFromTextStream(AStream, TComponent(Self), OnFindClass);
finally
AStream.Free;
end;
end;
{$ENDIF}
procedure TCustomBCLabel.OnFindClass(Reader: TReader; const AClassName: string;
var ComponentClass: TComponentClass);
begin
if CompareText(AClassName, 'TBCLabel') = 0 then
ComponentClass := TBCLabel;
end;
constructor TCustomBCLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{$IFDEF INDEBUG}
FRenderCount := 0;
{$ENDIF}
{$IFDEF FPC}
DisableAutoSizing;
Include(FControlState, csCreating);
{$ELSE} //#
{$ENDIF}
BeginUpdate;
try
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
FBGRA := TBGRABitmapEx.Create(Width, Height);
FBackground := TBCBackground.Create(Self);
FBorder := TBCBorder.Create(Self);
FFontEx := TBCFont.Create(Self);
ParentColor := True;
FBackground.OnChange := OnChangeProperty;
FBorder.OnChange := OnChangeProperty;
FFontEx.OnChange := OnChangeFont;
FBackground.Style := bbsClear;
FBorder.Style := bboNone;
FRounding := TBCRounding.Create(Self);
FRounding.OnChange := OnChangeProperty;
AutoSize := True;
finally
{$IFDEF FPC}
EnableAutoSizing;
{$ENDIF}
EndUpdate;
{$IFDEF FPC}
Exclude(FControlState, csCreating);
{$ELSE} //#
{$ENDIF}
end;
end;
destructor TCustomBCLabel.Destroy;
begin
FBGRA.Free;
FBackground.Free;
FBorder.Free;
FFontEx.Free;
FRounding.Free;
inherited Destroy;
end;
end.