-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
bcbutton.pas
2119 lines (1908 loc) · 59.4 KB
/
bcbutton.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{ Customizable component which using BGRABitmap for drawing. Control mostly rendered
using framework.
Functionality:
- Gradients
- Double gradients
- Rounding
- Drop down list
- Glyph
- States (normal, hover, clicked)
- Caption with shadow
- Full alpha and antialias support
originally written in 2012 by Krzysztof Dibowski dibowski at interia.pl
}
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
unit BCButton;
{$I bgracontrols.inc}
interface
uses
Classes, types, {$IFDEF FPC}LCLType, LResources, {$ENDIF} Controls, Dialogs,
ActnList, ImgList, Menus, // MORA
Buttons, Graphics,
{$IFNDEF FPC}BGRAGraphics, GraphType, FPImage, {$ENDIF}
BGRABitmap, BGRABitmapTypes, BCThemeManager, BCTypes, Forms, BCBasectrls,
fpjsonrtti, Typinfo, fpjson;
{off $DEFINE DEBUG}
type
TBCButtonMemoryUsage = (bmuLow, bmuMedium, bmuHigh);
TBCButtonState = class;
TBCButtonStyle = (bbtButton, bbtDropDown);
TOnAfterRenderBCButton = procedure(Sender: TObject; const ABGRA: TBGRABitmap;
AState: TBCButtonState; ARect: TRect) of object;
TBCButtonPropertyData = (pdNone, pdUpdateSize);
// MORA: DropDown styles
TBCButtonDropDownStyle = (
bdsSeparate, // DropDown is a separate button (default)
bdsCommon // DropDown is same as main button
);
TBCButtonDropDownPosition = (
bdpLeft, // default
bdpBottom);
{ TBCButtonState }
TBCButtonState = class(TBCProperty)
private
FBackground: TBCBackground;
FBorder: TBCBorder;
FFontEx: TBCFont;
procedure OnChangeFont({%H-}Sender: TObject; {%H-}AData: PtrInt);
procedure OnChangeChildProperty({%H-}Sender: TObject; AData: PtrInt);
procedure SetBackground(AValue: TBCBackground);
procedure SetBorder(AValue: TBCBorder);
procedure SetFontEx(const AValue: TBCFont);
public
constructor Create(AControl: TControl); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure Scale(AScale: single; APreserveDefaultFontHeight: boolean = true);
published
property Background: TBCBackground read FBackground write SetBackground;
property Border: TBCBorder read FBorder write SetBorder;
property FontEx: TBCFont read FFontEx write SetFontEx;
end;
{ TCustomBCButton }
TCustomBCButton = class(TBCStyleGraphicControl)
private
{ Private declarations }
{$IFDEF INDEBUG}
FRenderCount: integer;
{$ENDIF}
FDropDownArrowSize: integer;
FDropDownWidth: integer;
FFlipArrow: boolean;
FActiveButt: TBCButtonStyle;
FBGRANormal, FBGRAHover, FBGRAClick: TBGRABitmapEx;
FCanvasScale: Single;
FCanvasScaleMode: TBCCanvasScaleMode;
FGlyphAlignment: TBCAlignment;
FGlyphOldPlacement: boolean;
FGlyphScale: single;
FInnerMargin: single;
FMemoryUsage: TBCButtonMemoryUsage;
FPreserveGlyphOnAssign: boolean;
FRounding: TBCRounding;
FRoundingDropDown: TBCRounding;
FStateClicked: TBCButtonState;
FStateHover: TBCButtonState;
FStateNormal: TBCButtonState;
FDown: boolean;
FGlyph: TBitmap;
FGlyphMargin: integer;
FButtonState: TBCMouseState;
FDownButtonState: TBCMouseState;
FOnAfterRenderBCButton: TOnAfterRenderBCButton;
FOnButtonClick: TNotifyEvent;
FStaticButton: boolean;
FStyle: TBCButtonStyle;
FGlobalOpacity: byte;
FTextApplyGlobalOpacity: boolean;
AutoSizeExtraY: integer;
AutoSizeExtraX: integer;
FLastBorderWidth: integer;
// MORA
FClickOffset: boolean;
FDropDownArrow: boolean;
FDropDownMenu: TPopupMenu;
FDropDownMenuVisible: boolean;
FDropDownClosingTime: TDateTime;
FDropDownPosition: TBCButtonDropDownPosition;
FDropDownStyle: TBCButtonDropDownStyle;
FImageChangeLink: TChangeLink;
FImageIndex: integer;
FImages: TCustomImageList;
FSaveDropDownClosed: TNotifyEvent;
FShowCaption: boolean;
procedure AssignDefaultStyle;
procedure CalculateGlyphSize(out NeededWidth, NeededHeight: integer);
procedure DropDownClosed(Sender: TObject);
function GetBGRAClick: TBGRABitmapEx;
function GetBGRAHover: TBGRABitmapEx;
function GetBGRANormal: TBGRABitmapEx;
procedure OnRestoreProperty(Sender: TObject; AObject: TObject;
Info: PPropInfo; AValue: TJSONData; var Handled: Boolean);
procedure OnStreamProperty(Sender: TObject; AObject: TObject;
Info: PPropInfo; var Res: TJSONData);
procedure RenderAll(ANow: boolean = False);
function GetButtonRect: TRect;
function GetDropDownWidth(AFull: boolean = True): integer;
function GetDropDownRect(AFull: boolean = True): TRect;
procedure SetBCButtonStateClicked(const AValue: TBCButtonState);
procedure SetBCButtonStateHover(const AValue: TBCButtonState);
procedure SetBCButtonStateNormal(const AValue: TBCButtonState);
procedure SetCanvasScaleMode(AValue: TBCCanvasScaleMode);
procedure SetClickOffset(AValue: boolean);
procedure SetDown(AValue: boolean);
procedure SetDropDownArrow(AValue: boolean);
procedure SetDropDownArrowSize(AValue: integer);
procedure SetDropDownPosition(AValue: TBCButtonDropDownPosition);
procedure SetDropDownWidth(AValue: integer);
procedure SetFlipArrow(AValue: boolean);
procedure SetGlyph(const AValue: TBitmap);
procedure SetGlyphAlignment(AValue: TBCAlignment);
procedure SetGlyphMargin(const AValue: integer);
procedure SetGlyphOldPlacement(AValue: boolean);
procedure SetGlyphScale(AValue: single);
procedure SetImageIndex(AValue: integer);
procedure SetImages(AValue: TCustomImageList);
procedure SetInnerMargin(AValue: single);
procedure SetMemoryUsage(AValue: TBCButtonMemoryUsage);
procedure SetRounding(AValue: TBCRounding);
procedure SetRoundingDropDown(AValue: TBCRounding);
procedure SetShowCaption(AValue: boolean);
procedure SetStaticButton(const AValue: boolean);
procedure SetStyle(const AValue: TBCButtonStyle);
procedure SetGlobalOpacity(const AValue: byte);
procedure SetTextApplyGlobalOpacity(const AValue: boolean);
procedure UpdateSize;
procedure OnChangeGlyph({%H-}Sender: TObject);
procedure OnChangeState({%H-}Sender: TObject; AData: PtrInt);
procedure ImageListChange(ASender: TObject);
function GetGlyph: TBitmap;
protected
{ Protected declarations }
procedure LimitMemoryUsage;
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
{%H-}WithThemeSpace: boolean); override;
class function GetControlClassDefaultSize: TSize; override;
procedure Click; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
procedure MouseEnter; override;
procedure MouseLeave; override;
procedure MouseMove(Shift: TShiftState; X, Y: integer); override;
procedure SetEnabled(Value: boolean); override;
procedure TextChanged; override;
protected
// MORA
procedure ActionChange(Sender: TObject; CheckDefaults: boolean); override;
function GetActionLinkClass: TControlActionLinkClass; override;
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
procedure Render(ABGRA: TBGRABitmapEx; AState: TBCButtonState); virtual;
procedure RenderState(ABGRA: TBGRABitmapEx; AState: TBCButtonState;
const ARect: TRect; ARounding: TBCRounding); virtual;
property ClickOffset: boolean read FClickOffset write SetClickOffset default False;
property DropDownArrow: boolean
read FDropDownArrow write SetDropDownArrow default False;
property DropDownMenu: TPopupMenu read FDropDownMenu write FDropDownMenu;
property DropDownStyle: TBCButtonDropDownStyle
read FDropDownStyle write FDropDownStyle default bdsSeparate;
property DropDownPosition: TBCButtonDropDownPosition
read FDropDownPosition write SetDropDownPosition default bdpLeft;
property Images: TCustomImageList read FImages write SetImages;
property ImageIndex: integer read FImageIndex write SetImageIndex default -1;
property ShowCaption: boolean read FShowCaption write SetShowCaption default True;
protected
{$IFDEF INDEBUG}
function GetDebugText: string; override;
{$ENDIF}
function GetStyleExtension: string; override;
procedure DrawControl; override;
procedure RenderControl; override;
property BGRANormal: TBGRABitmapEx read GetBGRANormal;
property BGRAHover: TBGRABitmapEx read GetBGRAHover;
property BGRAClick: TBGRABitmapEx read GetBGRAClick;
protected
property AutoSizeExtraVertical: integer read AutoSizeExtraY;
property AutoSizeExtraHorizontal: integer read AutoSizeExtraX;
property CanvasScaleMode: TBCCanvasScaleMode read FCanvasScaleMode write SetCanvasScaleMode default csmAuto;
property StateNormal: TBCButtonState read FStateNormal write SetBCButtonStateNormal;
property StateHover: TBCButtonState read FStateHover write SetBCButtonStateHover;
property StateClicked: TBCButtonState read FStateClicked
write SetBCButtonStateClicked;
property Down: boolean read FDown write SetDown default False;
property DropDownWidth: integer read FDropDownWidth write SetDropDownWidth;
property DropDownArrowSize: integer read FDropDownArrowSize
write SetDropDownArrowSize;
property FlipArrow: boolean read FFlipArrow write SetFlipArrow default False;
property Glyph: TBitmap read GetGlyph write SetGlyph;
property GlyphScale: single read FGlyphScale write SetGlyphScale default 1;
property GlyphMargin: integer read FGlyphMargin write SetGlyphMargin default 5;
property GlyphAlignment: TBCAlignment read FGlyphAlignment write SetGlyphAlignment default bcaCenter;
property GlyphOldPlacement: boolean read FGlyphOldPlacement write SetGlyphOldPlacement default true;
property Style: TBCButtonStyle read FStyle write SetStyle default bbtButton;
property StaticButton: boolean
read FStaticButton write SetStaticButton default False;
property GlobalOpacity: byte read FGlobalOpacity write SetGlobalOpacity;
property Rounding: TBCRounding read FRounding write SetRounding;
property RoundingDropDown: TBCRounding read FRoundingDropDown
write SetRoundingDropDown;
property TextApplyGlobalOpacity: boolean
read FTextApplyGlobalOpacity write SetTextApplyGlobalOpacity;
property OnAfterRenderBCButton: TOnAfterRenderBCButton
read FOnAfterRenderBCButton write FOnAfterRenderBCButton;
property OnButtonClick: TNotifyEvent read FOnButtonClick write FOnButtonClick;
property MemoryUsage: TBCButtonMemoryUsage read FMemoryUsage write SetMemoryUsage;
property InnerMargin: single read FInnerMargin write SetInnerMargin;
property PreserveGlyphOnAssign: boolean read FPreserveGlyphOnAssign write FPreserveGlyphOnAssign default True;
public
{ Constructor }
constructor Create(AOwner: TComponent); override;
{ Destructor }
destructor Destroy; override;
{ Assign the properties from Source to this instance }
procedure Assign(Source: TPersistent); override;
{ Set dropdown size and autosize extra padding }
procedure SetSizeVariables(newDropDownWidth, newDropDownArrowSize,
newAutoSizeExtraVertical, newAutoSizeExtraHorizontal: integer);
{ Called by EndUpdate }
procedure UpdateControl; override;
property CanvasScale: single read FCanvasScale;
public
procedure ScaleStyle(AScale: single; APreserveDefaultFontHeight: boolean = true);
{$IFDEF FPC}
{ Save all published settings to file }
procedure SaveToFile(AFileName: string); override;
procedure SaveToJSONFile(AFileName: string);
function SaveToJSON: string;
{ Load and assign all published settings from file }
procedure LoadFromFile(AFileName: string); override;
procedure LoadFromJSONFile(AFileName: string);
procedure LoadFromJSON(AJSON: string);
{ Assign the properties from AFileName to this instance }
procedure AssignFromFile(AFileName: string); override;
procedure AssignFromResource(AResourceName: string);
{$ENDIF}
{ Used by SaveToFile/LoadFromFile }
procedure OnFindClass({%H-}Reader: TReader; const AClassName: string;
var ComponentClass: TComponentClass);
end;
TBCButton = class(TCustomBCButton)
private
FBCThemeManager: TBCThemeManager;
procedure SetFBCThemeManager(AValue: TBCThemeManager);
published
property Action;
property Align;
property Anchors;
{ Click to edit the style. Available when editing only. If you want to stream the style from a file at runtime please use LoadFromFile and SaveToFile methods. }
property AssignStyle;
property AutoSize;
{ The style of the button when pressed. }
property StateClicked;
{ The style of the button when hovered. }
property StateHover;
{ The default style of the button. }
property StateNormal;
property BorderSpacing;
property CanvasScaleMode;
property Caption;
property Color;
property Constraints;
{ Set to True to change the button to always show a StateClicked style that will not change when button is clicked or hovered. }
property Down;
{ The width of the dropdown arrow area. }
property DropDownWidth;
{ The size of the dropdown arrow. }
property DropDownArrowSize;
property Enabled;
{ Changes the direction of the arrow. Default: False. }
property FlipArrow;
{ Set the opacity that will be applied to the whole button. Default: 255. }
property GlobalOpacity;
{ The glyph icon. }
property Glyph;
property GlyphScale;
property GlyphAlignment;
property GlyphOldPlacement;
property PreserveGlyphOnAssign;
{ The margin of the glyph icon. }
property GlyphMargin;
property Hint;
property InnerMargin;
{ Called when the button finish the render. Use it to add your own drawings to the button. }
property OnAfterRenderBCButton;
{ Called when the button part is clicked, not the dropdown. }
property OnButtonClick;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property ParentColor;
property PopupMenu;
{ Change the style of the rounded corners of the button. }
property Rounding;
{ Change the style of the rounded corners of the dropdown part of the button. }
property RoundingDropDown;
{ Set to True to change the button to always show a StateNormal style that will not change when button is clicked or hovered. }
property StaticButton;
property ShowHint;
{ The style of button that will be used. bbtButton or bbtDropDown. }
property Style;
{ Apply the global opacity to rendered text. Default: False. }
property TextApplyGlobalOpacity;
property Visible;
{ -ToDo: Unused property? }
property ClickOffset;
{ Show the dropdown arrow. }
property DropDownArrow;
{ The dropdown menu that will be displayed when the button is pressed. }
property DropDownMenu;
{ The kind of dropdown that will be used. bdsSeparate will show the dropdown down the dropdown arrow side. bdsCommon will show the dropdown down the whole button. }
property DropDownStyle;
{ The position of the dropdown arrow. }
property DropDownPosition;
{ The image list that holds an image to be used with the button ImageIndex property. }
property Images;
{ The index of the image that will be used for the button as glyph icon if glyph property is not set. }
property ImageIndex;
{ Show caption or hides it. Default: True. }
property ShowCaption;
{ Limit memory usage by selecting one of the options. Default: bmuHigh. }
property MemoryUsage;
{ The unique name of the control in the form. }
property Name;
property ThemeManager: TBCThemeManager read FBCThemeManager write SetFBCThemeManager;
end;
{ TBCButtonActionLink }
TBCButtonActionLink = class(TControlActionLink)
protected
procedure AssignClient(AClient: TObject); override;
procedure SetChecked(Value: boolean); override;
procedure SetImageIndex(Value: integer); override;
public
function IsCheckedLinked: boolean; override;
function IsImageIndexLinked: boolean; override;
end;
{$IFDEF FPC}procedure Register;{$ENDIF}
implementation
uses {$IFDEF FPC}LCLIntf, PropEdits, GraphPropEdits, LCLProc, {$ENDIF}Math, BCTools, SysUtils;
const
DropDownReopenDelay = 0.2/(24*60*60);
{$IFDEF FPC}//#
type
TBCButtonImageIndexPropertyEditor = class(TImageIndexPropertyEditor)
protected
function GetImageList: TCustomImageList; override;
end;
{$ENDIF}
{ TBCButton }
procedure TBCButton.SetFBCThemeManager(AValue: TBCThemeManager);
begin
if FBCThemeManager=AValue then Exit;
FBCThemeManager:=AValue;
end;
{$IFDEF FPC}//#
function TBCButtonImageIndexPropertyEditor.GetImageList: TCustomImageList;
var
Component: TPersistent;
begin
Component := GetComponent(0);
if Component is TCustomBCButton then
Result := TCustomBCButton(Component).Images
else
Result := nil;
end;
{$ENDIF}
{$IFDEF FPC}
procedure Register;
begin
{$R images\bgracontrols_images.res}
RegisterComponents('BGRA Button Controls', [TBCButton]);
RegisterPropertyEditor(TypeInfo(integer), TBCButton,
'ImageIndex', TBCButtonImageIndexPropertyEditor);
end;
{$ENDIF}
{ TBCButtonActionLink }
procedure TBCButtonActionLink.AssignClient(AClient: TObject);
begin
inherited AssignClient(AClient);
FClient := AClient as TCustomBCButton;
end;
procedure TBCButtonActionLink.SetChecked(Value: boolean);
begin
if IsCheckedLinked then
TCustomBCButton(FClient).Down := Value;
end;
procedure TBCButtonActionLink.SetImageIndex(Value: integer);
begin
if IsImageIndexLinked then
TCustomBCButton(FClient).ImageIndex := Value;
end;
function TBCButtonActionLink.IsCheckedLinked: boolean;
begin
Result := inherited IsCheckedLinked and (TCustomBCButton(FClient).Down =
(Action as TCustomAction).Checked);
end;
function TBCButtonActionLink.IsImageIndexLinked: boolean;
begin
Result := inherited IsImageIndexLinked and
(TCustomBCButton(FClient).ImageIndex = (Action as TCustomAction).ImageIndex);
end;
{ TBCButtonState }
procedure TBCButtonState.SetFontEx(const AValue: TBCFont);
begin
if FFontEx = AValue then
exit;
FFontEx.Assign(AValue);
Change;
end;
procedure TBCButtonState.OnChangeFont(Sender: TObject; AData: PtrInt);
begin
Change(PtrInt(pdUpdateSize));
end;
procedure TBCButtonState.OnChangeChildProperty(Sender: TObject; AData: PtrInt);
begin
Change(AData);
end;
procedure TBCButtonState.SetBackground(AValue: TBCBackground);
begin
if FBackground = AValue then
Exit;
FBackground.Assign(AValue);
Change;
end;
procedure TBCButtonState.SetBorder(AValue: TBCBorder);
begin
if FBorder = AValue then
Exit;
FBorder.Assign(AValue);
Change;
end;
constructor TBCButtonState.Create(AControl: TControl);
begin
FBackground := TBCBackground.Create(AControl);
FBorder := TBCBorder.Create(AControl);
FFontEx := TBCFont.Create(AControl);
FBackground.OnChange := OnChangeChildProperty;
FBorder.OnChange := OnChangeChildProperty;
FFontEx.OnChange := OnChangeFont;
inherited Create(AControl);
end;
destructor TBCButtonState.Destroy;
begin
FBackground.Free;
FBorder.Free;
FFontEx.Free;
inherited Destroy;
end;
procedure TBCButtonState.Assign(Source: TPersistent);
begin
if Source is TBCButtonState then
begin
FBackground.Assign(TBCButtonState(Source).FBackground);
FBorder.Assign(TBCButtonState(Source).FBorder);
FFontEx.Assign(TBCButtonState(Source).FFontEx);
Change(PtrInt(pdUpdateSize));
end
else
inherited Assign(Source);
end;
procedure TBCButtonState.Scale(AScale: single; APreserveDefaultFontHeight: boolean);
begin
FBackground.Scale(AScale);
FBorder.Scale(AScale);
FFontEx.Scale(AScale, APreserveDefaultFontHeight);
end;
{ TCustomBCButton }
procedure TCustomBCButton.AssignDefaultStyle;
begin
FRounding.RoundX := 12;
FRounding.RoundY := 12;
// Normal
with StateNormal do
begin
Border.Style := bboNone;
FontEx.Color := RGBToColor(230, 230, 255);
FontEx.Style := [fsBold];
FontEx.Shadow := True;
FontEx.ShadowOffsetX := 1;
FontEx.ShadowOffsetY := 1;
FontEx.ShadowRadius := 2;
Background.Gradient1EndPercent := 60;
Background.Style := bbsGradient;
// Gradient1
with Background.Gradient1 do
begin
EndColor := RGBToColor(64, 64, 128);
StartColor := RGBToColor(0, 0, 64);
end;
// Gradient2
with Background.Gradient2 do
begin
EndColor := RGBToColor(0, 0, 64);
GradientType := gtRadial;
Point1XPercent := 50;
Point1YPercent := 100;
Point2YPercent := 0;
StartColor := RGBToColor(64, 64, 128);
end;
end;
// Hover
with StateHover do
begin
Border.Style := bboNone;
FontEx.Color := RGBToColor(255, 255, 255);
FontEx.Style := [fsBold];
FontEx.Shadow := True;
FontEx.ShadowOffsetX := 1;
FontEx.ShadowOffsetY := 1;
FontEx.ShadowRadius := 2;
Background.Gradient1EndPercent := 100;
Background.Style := bbsGradient;
// Gradient1
with Background.Gradient1 do
begin
EndColor := RGBToColor(0, 64, 128);
GradientType := gtRadial;
Point1XPercent := 50;
Point1YPercent := 100;
Point2YPercent := 0;
StartColor := RGBToColor(0, 128, 255);
end;
end;
// Clicked
with StateClicked do
begin
Border.Style := bboNone;
FontEx.Color := RGBToColor(230, 230, 255);
FontEx.Style := [fsBold];
FontEx.Shadow := True;
FontEx.ShadowOffsetX := 1;
FontEx.ShadowOffsetY := 1;
FontEx.ShadowRadius := 2;
Background.Gradient1EndPercent := 100;
Background.Style := bbsGradient;
// Gradient1
with Background.Gradient1 do
begin
EndColor := RGBToColor(0, 0, 64);
GradientType := gtRadial;
Point1XPercent := 50;
Point1YPercent := 100;
Point2YPercent := 0;
StartColor := RGBToColor(0, 64, 128);
end;
end;
end;
procedure TCustomBCButton.CalculateGlyphSize(out NeededWidth, NeededHeight: integer);
begin
if Assigned(FGlyph) and not FGlyph.Empty then
begin
NeededWidth := ceil(FGlyph.Width * FGlyphScale);
NeededHeight := ceil(FGlyph.Height * FGlyphScale);
end
else
if Assigned(FImages) then
begin
NeededWidth := FImages.ResolutionForPPI[FImages.Width, Screen.PixelsPerInch, 1].Width;
NeededHeight := FImages.ResolutionForPPI[FImages.Width, Screen.PixelsPerInch, 1].Height;
end
else
begin
NeededHeight := 0;
NeededWidth := 0;
end;
end;
procedure TCustomBCButton.RenderAll(ANow: boolean);
begin
if (csCreating in ControlState) or IsUpdating then
Exit;
if ANow then
begin
Render(FBGRANormal, FStateNormal);
Render(FBGRAHover, FStateHover);
Render(FBGRAClick, FStateClicked);
end
else
begin
if Assigned(FBGRANormal) then FBGRANormal.NeedRender := True;
if Assigned(FBGRAHover) then FBGRAHover.NeedRender := True;
if Assigned(FBGRAClick) then FBGRAClick.NeedRender := True;
end;
end;
function TCustomBCButton.GetButtonRect: TRect;
begin
Result := GetClientRect;
if FStyle = bbtDropDown then
case FDropDownPosition of
bdpBottom:
Dec(Result.Bottom, GetDropDownWidth(False));
else
// bdpLeft:
Dec(Result.Right, GetDropDownWidth(False));
end;
end;
function TCustomBCButton.GetDropDownWidth(AFull: boolean): integer;
begin
Result := FDropDownWidth + (ifthen(AFull, 2, 1) * FStateNormal.FBorder.Width);
end;
function TCustomBCButton.GetGlyph: TBitmap;
begin
Result := FGlyph as TBitmap;
end;
function TCustomBCButton.GetDropDownRect(AFull: boolean): TRect;
begin
Result := GetClientRect;
case FDropDownPosition of
bdpBottom:
Result.Top := Result.Bottom - GetDropDownWidth(AFull);
else
// bdpLeft:
Result.Left := Result.Right - GetDropDownWidth(AFull);
end;
end;
procedure TCustomBCButton.Render(ABGRA: TBGRABitmapEx; AState: TBCButtonState);
procedure GetActualGlyph(out ABitmap: TBitmap; out AScale: single);
begin
if Assigned(FGlyph) and not FGlyph.Empty then
begin
ABitmap := FGlyph;
AScale := FCanvasScale * FGlyphScale;
end else
if Assigned(FImages) and (FImageIndex > -1) and (FImageIndex < FImages.Count) then
begin
ABitmap := TBitmap.Create;
{$IFDEF FPC}
FImages.ResolutionForPPI[FImages.Width, Screen.PixelsPerInch, FCanvasScale].GetBitmap(FImageIndex, ABitmap);
AScale := 1;
{$ELSE}
FImages.GetBitmapRaw(FImageIndex, result);
ABitmap := AScale;
{$ENDIF}
end else
begin
ABitmap := nil;
AScale := 1;
end;
end;
procedure RenderGlyph(ARect: TRect; AGlyph: TBitmap);
begin
if ARect.IsEmpty or (AGlyph = nil) then exit;
ABGRA.StretchPutImage(ARect, AGlyph, dmLinearBlend);
end;
var
r, r_a, r_g: TRect;
g: TBitmap;
actualCaption: TCaption;
freeScaled: boolean;
scaledState: TBCButtonState;
scaledArrowSize, scaledGlyphMargin, scaledInnerMargin: integer;
scaledRounding, scaledRoundingDropDown: TBCRounding;
gScale: single;
begin
if (csCreating in ControlState) or IsUpdating or (ABGRA = nil) then
Exit;
if FCanvasScale <> 1 then
begin
scaledState := TBCButtonState.Create(nil);
scaledState.Assign(AState);
scaledState.Scale(FCanvasScale, false);
scaledRounding := TBCRounding.Create(nil);
scaledRounding.Assign(Rounding);
scaledRounding.Scale(FCanvasScale);
scaledRoundingDropDown := TBCRounding.Create(nil);
scaledRoundingDropDown.Assign(RoundingDropDown);
scaledRoundingDropDown.Scale(FCanvasScale);
freeScaled := true;
end
else
begin
scaledState := AState;
scaledRounding := Rounding;
scaledRoundingDropDown := RoundingDropDown;
freeScaled := false;
end;
scaledArrowSize := round(DropDownArrowSize * FCanvasScale);
scaledGlyphMargin := round(GlyphMargin * FCanvasScale);
scaledInnerMargin := round(InnerMargin * FCanvasScale);
ABGRA.NeedRender := False;
{ Refreshing size }
ABGRA.SetSize(round(Width * FCanvasScale), round(Height * FCanvasScale));
{ Clearing previous paint }
ABGRA.Fill(BGRAPixelTransparent);
{ Basic body }
r := ScaleRect(GetButtonRect, FCanvasScale);
RenderState(ABGRA, scaledState, r, scaledRounding);
if not GlyphOldPlacement then
r.Inflate(-scaledInnerMargin,-scaledInnerMargin);
{ Calculating rect }
CalculateBorderRect(scaledState.Border, r);
if FStyle = bbtDropDown then
begin
r_a := ScaleRect(GetDropDownRect, FCanvasScale);
RenderState(ABGRA, scaledState, r_a, scaledRoundingDropDown);
CalculateBorderRect(scaledState.Border, r_a);
// Click offset for arrow
if FClickOffset and (AState = FStateClicked) then
r_a.Offset(1,1);
if FFlipArrow then
RenderArrow(TBGRABitmap(ABGRA), r_a, scaledArrowSize, badUp,
scaledState.FontEx.Color)
else
RenderArrow(TBGRABitmap(ABGRA), r_a, scaledArrowSize, badDown,
scaledState.FontEx.Color);
end;
// Click offset for text and glyph
if FClickOffset and (AState = FStateClicked) then
r.Offset(round(1 * FCanvasScale), round(1 * FCanvasScale));
// DropDown arrow
if FDropDownArrow and (FStyle <> bbtDropDown) then
begin
r_a := r;
r_a.Left := r_a.Right - round(FDropDownWidth * FCanvasScale);
if FFlipArrow then
RenderArrow(TBGRABitmap(ABGRA), r_a, scaledArrowSize, badUp,
scaledState.FontEx.Color)
else
RenderArrow(TBGRABitmap(ABGRA), r_a, scaledArrowSize, badDown,
scaledState.FontEx.Color);
Dec(R.Right, round(FDropDownWidth * FCanvasScale));
end;
GetActualGlyph(g, gScale);
if FShowCaption then actualCaption := self.Caption else actualCaption := '';
r_g := ComputeGlyphPosition(r, g, GlyphAlignment, scaledGlyphMargin, actualCaption,
scaledState.FontEx, GlyphOldPlacement, gScale);
if FTextApplyGlobalOpacity then
begin
{ Drawing text }
RenderText(r, scaledState.FontEx, actualCaption, ABGRA, Enabled);
RenderGlyph(r_g, g);
{ Set global opacity }
ABGRA.ApplyGlobalOpacity(FGlobalOpacity);
end
else
begin
{ Set global opacity }
ABGRA.ApplyGlobalOpacity(FGlobalOpacity);
{ Drawing text }
RenderText(r, scaledState.FontEx, actualCaption, ABGRA, Enabled);
RenderGlyph(r_g, g);
end;
if g <> FGlyph then g.Free;
{ Convert to gray if not enabled }
if not Enabled then ABGRA.InplaceGrayscale;
if Assigned(FOnAfterRenderBCButton) then
FOnAfterRenderBCButton(Self, ABGRA, scaledState, r);
if freeScaled then
begin
FreeAndNil(scaledState);
FreeAndNil(scaledRounding);
FreeAndNil(scaledRoundingDropDown);
end;
{$IFDEF INDEBUG}
FRenderCount := FRenderCount +1;
{$ENDIF}
end;
procedure TCustomBCButton.RenderState(ABGRA: TBGRABitmapEx;
AState: TBCButtonState; const ARect: TRect; ARounding: TBCRounding);
begin
RenderBackgroundAndBorder(ARect, AState.FBackground, TBGRABitmap(ABGRA),
ARounding, AState.FBorder, round(FInnerMargin * FCanvasScale));
end;
procedure TCustomBCButton.OnChangeGlyph(Sender: TObject);
begin
RenderControl;
UpdateSize;
Invalidate;
end;
procedure TCustomBCButton.OnChangeState(Sender: TObject; AData: PtrInt);
begin
RenderControl;
if (TBCButtonPropertyData(AData) = pdUpdateSize) or
(FStateNormal.Border.Width <> FLastBorderWidth) then
UpdateSize;
Invalidate;
end;
procedure TCustomBCButton.ImageListChange(ASender: TObject);
begin
if ASender = Images then
begin
RenderControl;
Invalidate;
end;
end;
procedure TCustomBCButton.LimitMemoryUsage;
begin
{$IFNDEF FPC}//# //@ IN DELPHI NEEDRENDER NEED TO BE TRUE. IF FALSE COMPONENT IN BGRANORMAL BE BLACK AFTER INVALIDATE.
if Assigned(FBGRANormal) then FBGRANormal.NeedRender := True;
if Assigned(FBGRAHover) then FBGRAHover.NeedRender := True;
if Assigned(FBGRAClick) then FBGRAClick.NeedRender := True;
{$ENDIF}
if (FMemoryUsage = bmuLow) and Assigned(FBGRANormal) then FBGRANormal.Discard;
if (FMemoryUsage <> bmuHigh) then
begin
if Assigned(FBGRAHover) then FBGRAHover.Discard;
if Assigned(FBGRAClick) then FBGRAClick.Discard;
end;
end;
procedure TCustomBCButton.SetBCButtonStateClicked(const AValue: TBCButtonState);
begin
if FStateClicked = AValue then
exit;
FStateClicked.Assign(AValue);
RenderControl;
Invalidate;
end;
procedure TCustomBCButton.SetBCButtonStateHover(const AValue: TBCButtonState);
begin
if FStateHover = AValue then
exit;
FStateHover.Assign(AValue);
RenderControl;
Invalidate;
end;
procedure TCustomBCButton.SetBCButtonStateNormal(const AValue: TBCButtonState);
begin
if FStateNormal = AValue then
exit;
FStateNormal.Assign(AValue);
RenderControl;
Invalidate;
end;
procedure TCustomBCButton.SetCanvasScaleMode(AValue: TBCCanvasScaleMode);
begin
if FCanvasScaleMode=AValue then Exit;
FCanvasScaleMode:=AValue;
RenderControl;
Invalidate;
end;
procedure TCustomBCButton.SetClickOffset(AValue: boolean);
begin
if FClickOffset = AValue then
Exit;
FClickOffset := AValue;
RenderControl;
end;
procedure TCustomBCButton.SetDown(AValue: boolean);
begin
if FDown = AValue then
exit;
FDown := AValue;
if FDown then
FButtonState := msClicked
else
FButtonState := msNone;
RenderControl;
Invalidate;
end;
procedure TCustomBCButton.SetDropDownArrow(AValue: boolean);
begin
if FDropDownArrow = AValue then
Exit;
FDropDownArrow := AValue;
RenderControl;
Invalidate;
end;
procedure TCustomBCButton.SetDropDownArrowSize(AValue: integer);
begin
if FDropDownArrowSize = AValue then
Exit;
FDropDownArrowSize := AValue;
RenderControl;