-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathVcl.StyledCategoryButtons.pas
More file actions
1601 lines (1455 loc) · 52.6 KB
/
Vcl.StyledCategoryButtons.pas
File metadata and controls
1601 lines (1455 loc) · 52.6 KB
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
{******************************************************************************}
{ }
{ StyledCategoryButtons: a Styled CategoryButtons with TStyledButtonItem }
{ Based on TCategoryButtons and TButtonItem }
{ }
{ Copyright (c) 2022-2026 (Ethea S.r.l.) }
{ Author: Carlo Barazzetta }
{ Contributors: }
{ }
{ https://github.com/EtheaDev/StyledComponents }
{ }
{******************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{******************************************************************************}
unit Vcl.StyledCategoryButtons;
interface
{$INCLUDE StyledComponents.inc}
uses
Vcl.ImgList
, System.UITypes
, System.SysUtils
, System.Classes
, System.Math
, Vcl.ToolWin
, Vcl.ComCtrls
, Vcl.StdCtrls
, Vcl.ExtCtrls
, Vcl.Themes
, Vcl.Controls
, Vcl.ActnList
, Vcl.Menus
, Vcl.CategoryButtons
, Winapi.Messages
, Winapi.Windows
, Vcl.StyledButton
, Vcl.ButtonStylesAttributes
, Vcl.StandardButtonStyles
, Vcl.Graphics
;
resourcestring
ERROR_SETTING_CATEGORYBUTTONS_STYLE = 'Error setting CategoryButtons Style: %s/%s/%s not available';
type
/// <summary>Exception class for styled category buttons errors</summary>
EStyledCategoryButtonsError = Exception;
TStyledCategoryButtons = class;
/// <summary>Class reference type for TStyledCategoryButtons descendants</summary>
TStyledCategoryButtonsClass = class of TStyledCategoryButtons;
TStyledButtonItem = class;
/// <summary>Class reference type for TStyledButtonItem descendants</summary>
TStyledButtonItemClass = class of TButtonItem;
TStyledButtonCategory = class;
/// <summary>Class reference type for TStyledButtonCategory descendants</summary>
TStyledButtonCategoryClass = class of TStyledButtonCategory;
TStyledButtonCategories = class;
/// <summary>Class reference type for TStyledButtonCategories descendants</summary>
TStyledButtonCategoriesClass = class of TStyledButtonCategories;
/// <summary>Callback procedure for iterating category button items</summary>
/// <param name="Button">The current button item being processed</param>
TButtonProc = reference to procedure (Button: TStyledButtonItem);
/// <summary>Event handler for retrieving notification badge information</summary>
/// <param name="ACategoryIndex">Index of the category</param>
/// <param name="AButtonItemIndex">Index of the button item within the category</param>
/// <param name="ABadgeContent">Badge text content</param>
/// <param name="ASize">Badge size</param>
/// <param name="APosition">Badge position on the button</param>
/// <param name="AColor">Badge background color</param>
/// <param name="AFontColor">Badge text color</param>
/// <param name="AFontStyle">Badge font style</param>
TGetCategoryButtonsBadgeInfo = procedure (
const ACategoryIndex: Integer;
const AButtonItemIndex: Integer;
var ABadgeContent: string;
var ASize: TNotificationBadgeSize;
var APosition: TNotificationBadgePosition;
var AColor: TColor;
var AFontColor: TColor;
var AFontStyle: TFontStyles) of Object;
/// <summary>Styled button category for organizing button items</summary>
/// <remarks>
/// TStyledButtonCategory represents a group of related buttons within
/// a TStyledCategoryButtons component. Categories can be collapsed/expanded
/// and provide visual grouping with header text.
/// </remarks>
TStyledButtonCategory = class(TButtonCategory)
protected
public
constructor Create(Collection: TCollection); override;
end;
/// <summary>Collection class for styled button categories</summary>
/// <remarks>
/// TStyledButtonCategories is a collection that holds TStyledButtonCategory
/// objects, each containing one or more TStyledButtonItem objects. This
/// provides a two-level hierarchy for organizing buttons.
/// </remarks>
TStyledButtonCategories = class(TButtonCategories)
private
function GetStyledCategoryButtons: TStyledCategoryButtons;
function GetItem(Index: Integer): TStyledButtonCategory;
procedure SetItem(Index: Integer; const AValue: TStyledButtonCategory);
public
constructor Create(const CategoryButtons: TCategoryButtons); override;
function Add: TStyledButtonCategory;
property CategoryButtons: TStyledCategoryButtons read GetStyledCategoryButtons;
property Items[Index: Integer]: TStyledButtonCategory read GetItem write SetItem; default;
end;
/// <summary>Styled button item with individual style support</summary>
/// <remarks>
/// TStyledButtonItem represents a single button within a category.
/// Each item can have its own StyleFamily, StyleClass, StyleAppearance,
/// StyleDrawType, StyleRadius, and StyleRoundedCorners properties,
/// allowing individual buttons to have different appearances.
/// </remarks>
TStyledButtonItem = class(TButtonItem)
private
FCollection: TButtonCollection;
//Styled Attributes
FStyleRadius: Integer;
FStyleRoundedCorners: TRoundedCorners;
FStyleDrawType: TStyledButtonDrawType;
FStyleFamily: TStyledButtonFamily;
FStyleClass: TStyledButtonClass;
FStyleAppearance: TStyledButtonAppearance;
FStyleApplied: Boolean;
procedure InvalidateOwner;
function IsCustomDrawType: Boolean;
function IsCustomRoundedCorners: Boolean;
function IsCustomRadius: Boolean;
function IsStoredStyle: Boolean;
procedure SetStyleFamily(const AValue: TStyledButtonFamily);
procedure SetStyleClass(const AValue: TStyledButtonClass);
procedure SetStyleAppearance(const AValue: TStyledButtonAppearance);
procedure SetStyleDrawType(const AValue: TStyledButtonDrawType);
procedure SetStyleRadius(const AValue: Integer);
procedure SetStyleRoundedCorners(const AValue: TRoundedCorners);
function GetStyledCategoryButtons: TStyledCategoryButtons;
function ApplyButtonStyle: Boolean;
procedure LoadDefaultStyles;
public
procedure Assign(Source: TPersistent); override;
constructor Create(Collection: TCollection); override;
procedure SetButtonStyle(const AStyleFamily: TStyledButtonFamily;
const AStyleClass: TStyledButtonClass;
const AStyleAppearance: TStyledButtonAppearance);
published
property CategoryButtons: TStyledCategoryButtons read GetStyledCategoryButtons;
//StyledComponents Attributes
property StyleRadius: Integer read FStyleRadius write SetStyleRadius stored IsCustomRadius;
property StyleDrawType: TStyledButtonDrawType read FStyleDrawType write SetStyleDrawType stored IsCustomDrawType;
property StyleRoundedCorners: TRoundedCorners read FStyleRoundedCorners write SetStyleRoundedCorners stored IsCustomRoundedCorners;
property StyleFamily: TStyledButtonFamily read FStyleFamily write SetStyleFamily stored IsStoredStyle;
property StyleClass: TStyledButtonClass read FStyleClass write SetStyleClass stored IsStoredStyle;
property StyleAppearance: TStyledButtonAppearance read FStyleAppearance write SetStyleAppearance stored IsStoredStyle;
end;
/// <summary>Styled category buttons component with modern appearance</summary>
/// <remarks>
/// TStyledCategoryButtons is a styled alternative to TCategoryButtons that
/// provides modern visual appearance through the StyledComponents styling
/// system. Organizes buttons into collapsible categories with headers.
/// Supports custom button styles (StyleFamily, StyleClass, StyleAppearance),
/// notification badges via OnGetNotificationBadgeInfo, and all standard
/// category buttons features. Individual items can override the component's
/// default style. Use RegisterDefaultRenderingStyle to set global defaults.
/// </remarks>
[ComponentPlatforms(pidWin32 or pidWin64)]
TStyledCategoryButtons = class(TCategoryButtons)
private
//StyledButton Attributes
FButtonStyleNormal: TStyledButtonAttributes;
FButtonStylePressed: TStyledButtonAttributes;
FButtonStyleSelected: TStyledButtonAttributes;
FButtonStyleHot: TStyledButtonAttributes;
FButtonStyleDisabled: TStyledButtonAttributes;
//Styled Attributes
FStyleRadius: Integer;
FStyleDrawType: TStyledButtonDrawType;
FStyleRoundedCorners: TRoundedCorners;
FStyleFamily: TStyledButtonFamily;
FStyleClass: TStyledButtonClass;
FStyleAppearance: TStyledButtonAppearance;
FCustomDrawType: Boolean;
FStyleApplied: Boolean;
//Notification Badge event handler
FOnGetNotificationBadgeInfo: TGetCategoryButtonsBadgeInfo;
FCaptionAlignment: TAlignment;
FImageAlignment: TImageAlignment;
FImageMargins: TImageMargins;
FSpacing: Integer;
FFlat: Boolean;
FButtonsCursor: TCursor;
FCursor: TCursor;
class var
_DefaultStyleDrawType: TStyledButtonDrawType;
_UseCustomDrawType: Boolean;
_DefaultFamily: TStyledButtonFamily;
_DefaultClass: TStyledButtonClass;
_DefaultAppearance: TStyledButtonAppearance;
_DefaultStyleRadius: Integer;
_DefaultButtonsCursor: TCursor;
procedure ImageMarginsChange(Sender: TObject);
function IsCustomDrawType: Boolean;
function IsCustomRoundedCorners: Boolean;
function IsCustomRadius: Boolean;
function ImageMarginsStored: Boolean;
function IsStoredStyleAppearance: Boolean;
function IsStoredStyleClass: Boolean;
function IsStoredStyleFamily: Boolean;
procedure SetStyleAppearance(const AValue: TStyledButtonAppearance);
procedure SetStyleClass(const AValue: TStyledButtonClass);
procedure SetStyleDrawType(const AValue: TStyledButtonDrawType);
procedure SetStyleFamily(const AValue: TStyledButtonFamily);
procedure SetStyleRadius(const AValue: Integer);
procedure SetStyleRoundedCorners(const AValue: TRoundedCorners);
function ApplyCategoryButtonsStyle: Boolean;
procedure SetStyleApplied(const AValue: Boolean);
function ApplyButtonStyle: Boolean;
function GetActiveStyleName: string;
function AsVCLStyle: Boolean;
function GetAsVCLComponent: Boolean;
procedure SetAsVCLComponent(const AValue: Boolean);
procedure ProcessButtons(AButtonProc: TButtonProc);
procedure SetButtonStyleDisabled(const AValue: TStyledButtonAttributes);
procedure SetButtonStyleHot(const AValue: TStyledButtonAttributes);
procedure SetButtonStyleNormal(const AValue: TStyledButtonAttributes);
procedure SetButtonStylePressed(const AValue: TStyledButtonAttributes);
procedure SetButtonStyleSelected(const AValue: TStyledButtonAttributes);
procedure GetDrawingStyle(const ACanvas: TCanvas;
const AButtonState: TStyledButtonState;
const AItem: TStyledButtonItem);
function GetAttributes(const AMode: TStyledButtonState): TStyledButtonAttributes;
procedure DrawBackgroundAndBorder(const ACanvas: TCanvas;
const ADrawRect, ADropDownRect: TRect;
const AStyleDrawType: TStyledButtonDrawType;
const ARadius: Single; const ARoundedCorners: TRoundedCorners);
procedure DrawCaptionAndImage(const ACanvas: TCanvas; const ASurfaceRect: TRect;
const ACaption: TCaption; const AImageIndex: Integer);
procedure SetCaptionAlignment(const AValue: TAlignment);
function GetImageSize(out AWidth, AHeight: Integer;
out AImageList: TCustomImageList): boolean;
procedure SetSpacing(const AValue: Integer);
procedure SetFlat(const AValue: Boolean);
function IsStyleEnabled: Boolean;
procedure SetCursor(const AValue: TCursor);
procedure SetImageAlignment(const AValue: TImageAlignment);
function GetScaleFactor: Single;
function CalcMaxBorderWidth: Integer;
function GetButtonCategories: TStyledButtonCategories;
procedure SetButtonCategories(const AValue: TStyledButtonCategories);
procedure SetImageMargins(const AValue: TImageMargins);
function IsDefaultImageMargins: Boolean;
procedure CalcDefaultImageMargins(const AValue: TImageAlignment);
{$IFNDEF D10_4+}
function IsCustomStyleActive: Boolean;
{$ENDIF}
//Windows messages
procedure CMStyleChanged(var Message: TMessage); message CM_STYLECHANGED;
protected
{$IFDEF D10_1+}
procedure ChangeScale(M, D: Integer; isDpiChange: Boolean); override;
{$ENDIF}
procedure Loaded; override;
function GetButtonCategoriesClass: TButtonCategoriesClass; override;
function GetButtonCategoryClass: TButtonCategoryClass; override;
function GetButtonItemClass: TButtonItemClass; override;
procedure UpdateStyleElements; override;
procedure DrawButton(const AButton: TButtonItem; ACanvas: TCanvas;
ARect: TRect; AState: TButtonDrawState); override;
procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
public
procedure Assign(Source: TPersistent); override;
function StyledButtonState(const AState: TButtonDrawState): TStyledButtonState;
class procedure RegisterDefaultRenderingStyle(
const ADrawType: TStyledButtonDrawType;
const AFamily: TStyledButtonFamily = DEFAULT_CLASSIC_FAMILY;
const AClass: TStyledButtonClass = DEFAULT_WINDOWS_CLASS;
const AAppearance: TStyledButtonAppearance = DEFAULT_APPEARANCE;
const AStyleRadius: Integer = DEFAULT_RADIUS;
const AButtonsCursor: TCursor = DEFAULT_CURSOR); virtual;
procedure SetCategoryButtonsStyle(const AStyleFamily: TStyledButtonFamily;
const AStyleClass: TStyledButtonClass;
const AStyleAppearance: TStyledButtonAppearance);
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
//Styled constructor
constructor CreateStyled(AOwner: TComponent;
const AFamily: TStyledButtonFamily;
const AClass: TStyledButtonClass;
const AAppearance: TStyledButtonAppearance); virtual;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property AsVCLComponent: Boolean read GetAsVCLComponent write SetAsVCLComponent stored False;
property StyleApplied: Boolean read FStyleApplied write SetStyleApplied;
published
property ButtonsCursor: TCursor read FButtonsCursor write FButtonsCursor default DEFAULT_CURSOR;
property Cursor: TCursor read FCursor write SetCursor default crDefault;
property CaptionAlignment: TAlignment read FCaptionAlignment write SetCaptionAlignment default taLeftJustify;
property Categories: TStyledButtonCategories read GetButtonCategories write SetButtonCategories;
property ImageAlignment: TImageAlignment read FImageAlignment write SetImageAlignment default iaLeft;
property ImageMargins: TImageMargins read FImageMargins write SetImageMargins stored ImageMarginsStored;
property Flat: Boolean read FFlat write SetFlat default False;
property Spacing: Integer read FSpacing write SetSpacing default 4;
//StyledButton Attributes
property ButtonStyleNormal: TStyledButtonAttributes read FButtonStyleNormal write SetButtonStyleNormal;
property ButtonStylePressed: TStyledButtonAttributes read FButtonStylePressed write SetButtonStylePressed;
property ButtonStyleSelected: TStyledButtonAttributes read FButtonStyleSelected write SetButtonStyleSelected;
property ButtonStyleHot: TStyledButtonAttributes read FButtonStyleHot write SetButtonStyleHot;
property ButtonStyleDisabled: TStyledButtonAttributes read FButtonStyleDisabled write SetButtonStyleDisabled;
//StyledComponents Attributes
property StyleRadius: Integer read FStyleRadius write SetStyleRadius stored IsCustomRadius;
property StyleDrawType: TStyledButtonDrawType read FStyleDrawType write SetStyleDrawType stored IsCustomDrawType;
property StyleRoundedCorners: TRoundedCorners read FStyleRoundedCorners write SetStyleRoundedCorners stored IsCustomRoundedCorners;
property StyleFamily: TStyledButtonFamily read FStyleFamily write SetStyleFamily stored IsStoredStyleFamily;
property StyleClass: TStyledButtonClass read FStyleClass write SetStyleClass stored IsStoredStyleClass;
property StyleAppearance: TStyledButtonAppearance read FStyleAppearance write SetStyleAppearance stored IsStoredStyleAppearance;
//Notification Badge Info Event Handler
property OnGetNotificationBadgeInfo: TGetCategoryButtonsBadgeInfo read FOnGetNotificationBadgeInfo write FOnGetNotificationBadgeInfo;
end;
implementation
uses
Vcl.Consts
, Vcl.Forms
, System.Types
, System.RTLConsts
;
const
DEFAULT_IMAGE_HMARGIN = 2;
DEFAULT_IMAGE_VMARGIN = 2;
{ TStyledCategoryButtons }
constructor TStyledCategoryButtons.CreateStyled(AOwner: TComponent;
const AFamily: TStyledButtonFamily; const AClass: TStyledButtonClass;
const AAppearance: TStyledButtonAppearance);
begin
// Assert(Assigned(AOwner));
inherited Create(AOwner);
//new properties for StyledCategoryButtons
FCaptionAlignment := taLeftJustify;
FSpacing := 4;
FFlat := False;
FImageAlignment := iaLeft;
FButtonsCursor := _DefaultButtonsCursor;
FImageMargins := TImageMargins.Create;
FImageMargins.Left := 2;
FImageMargins.OnChange := ImageMarginsChange;
FButtonStyleNormal := TStyledButtonAttributes.Create(Self);
FButtonStyleNormal.Name := 'Normal';
FButtonStylePressed := TStyledButtonAttributes.Create(Self);
FButtonStylePressed.Name := 'Pressed';
FButtonStyleSelected := TStyledButtonAttributes.Create(Self);
FButtonStyleSelected.Name := 'Selected';
FButtonStyleHot := TStyledButtonAttributes.Create(Self);
FButtonStyleHot.Name := 'Hot';
FButtonStyleDisabled := TStyledButtonAttributes.Create(Self);
FButtonStyleDisabled.Name := 'Disabled';
FStyleDrawType := _DefaultStyleDrawType;
FStyleRadius := _DefaultStyleRadius;
FStyleRoundedCorners := ALL_ROUNDED_CORNERS;
FStyleFamily := AFamily;
FStyleClass := AClass;
FStyleAppearance := AAppearance;
end;
procedure TStyledCategoryButtons.CMStyleChanged(var Message: TMessage);
begin
inherited;
ApplyButtonStyle;
end;
{$IFDEF D10_1+}
procedure TStyledCategoryButtons.ChangeScale(M, D: Integer;
isDpiChange: Boolean);
begin
inherited;
{$IFNDEF D10_4+}
//Fixed in Delphi 10.4
ButtonWidth := MulDiv(ButtonWidth, M, D);
ButtonHeight := MulDiv(ButtonHeight, M, D);
Resize;
UpdateAllButtons;
{$ENDIF}
end;
{$ENDIF}
constructor TStyledCategoryButtons.Create(AOwner: TComponent);
begin
CreateStyled(AOwner,
_DefaultFamily,
_DefaultClass,
_DefaultAppearance);
end;
destructor TStyledCategoryButtons.Destroy;
begin
FreeAndNil(FImageMargins);
FreeAndNil(FButtonStyleNormal);
FreeAndNil(FButtonStylePressed);
FreeAndNil(FButtonStyleSelected);
FreeAndNil(FButtonStyleHot);
FreeAndNil(FButtonStyleDisabled);
inherited Destroy;
end;
function TStyledCategoryButtons.GetScaleFactor: Single;
begin
Result := {$IFDEF D10_3+}ScaleFactor{$ELSE}1{$ENDIF};
end;
procedure TStyledCategoryButtons.DrawBackgroundAndBorder(
const ACanvas: TCanvas; const ADrawRect, ADropDownRect: TRect;
const AStyleDrawType: TStyledButtonDrawType;
const ARadius: Single; const ARoundedCorners: TRoundedCorners);
var
LButtonOffset: Integer;
LDropDownRect: TRect;
LScaleFactor: Single;
begin
LScaleFactor := GetScaleFactor;
LDropDownRect := ADropDownRect;
//Draw Button Shape
CanvasDrawshape(ACanvas, ADrawRect, AStyleDrawType,
ARadius*LScaleFactor, ARoundedCorners);
//Draw Bar and Triangle
if LDropDownRect.Width > 0 then
begin
if not (AStyleDrawType in [btRounded, btEllipse]) then
begin
CanvasDrawBar(ACanvas, LDropDownRect,
LScaleFactor,
ACanvas.Pen.Color);
CanvasDrawTriangle(ACanvas, LDropDownRect,
LScaleFactor,
ACanvas.Font.Color);
end
else
begin
LButtonOffset := LDropDownRect.Height div 8;
LDropDownRect.Left := LDropDownRect.Left - LButtonOffset;
LDropDownRect.Right := LDropDownRect.Right - LButtonOffset;
CanvasDrawTriangle(ACanvas, LDropDownRect,
LScaleFactor,
ACanvas.Font.Color);
end;
end;
end;
function TStyledCategoryButtons.GetImageSize(out AWidth, AHeight: Integer;
out AImageList: TCustomImageList): boolean;
begin
AWidth := 0;
AHeight := 0;
//Return True if using ImageList
if Assigned(Images) then
begin
AWidth := Images.Width;
AHeight := Images.Height;
Result := True;
end
else
Result := False;
end;
procedure TStyledCategoryButtons.DrawCaptionAndImage(
const ACanvas: TCanvas; const ASurfaceRect: TRect;
const ACaption: TCaption; const AImageIndex: Integer);
var
LTextFlags: Cardinal;
LImageRect, LTextRect: TRect;
LImageList: TCustomImageList;
LImageWidth, LImageHeight: Integer;
LUseImageList: Boolean;
begin
if boShowCaptions in ButtonOptions then
begin
case FCaptionAlignment of
taLeftJustify: LTextFlags := DT_NOCLIP or DT_LEFT or DT_VCENTER;
taRightJustify: LTextFlags := DT_NOCLIP or DT_RIGHT or DT_VCENTER;
else
LTextFlags := DT_NOCLIP or DT_CENTER or DT_VCENTER;
end;
LTextFlags := DrawTextBiDiModeFlags(LTextFlags);
(*
if FWordWrap then
LTextFlags := LTextFlags or DT_WORDBREAK;
*)
end
else
begin
LTextFlags := DT_NOCLIP or DT_CENTER or DT_VCENTER;
end;
LUseImageList := GetImageSize(LImageWidth, LImageHeight, LImageList);
//Calculate LTextRect and LImageRect using ImageMargins and ImageAlignment
CalcImageAndTextRect(ASurfaceRect, ACaption, LTextRect, LImageRect,
LImageWidth, LImageHeight, FImageAlignment, FImageMargins,
CalcMaxBorderWidth, GetScaleFactor);
if LUseImageList and not Assigned(OnDrawIcon) then
begin
//Uses an ImageList to draw the Icon
Images.Draw(ACanvas, LImageRect.Left, LImageRect.Top,
AImageIndex, Enabled);
end;
if boShowCaptions in ButtonOptions then
DrawButtonText(ACanvas, ACaption, FCaptionAlignment, FSpacing,
CalcMaxBorderWidth, LTextRect, LTextFlags, True);
end;
{$IFNDEF D10_4+}
function TStyledCategoryButtons.IsCustomStyleActive: Boolean;
begin
Result := False;
end;
{$ENDIF}
function TStyledCategoryButtons.StyledButtonState(
const AState: TButtonDrawState): TStyledButtonState;
begin
//Calculate Styled State based on State
if (bdsHot in AState) and not (bdsDown in AState) then
Result := bsmHot
else if bdsDown in AState then
Result := bsmPressed
else if (bdsFocused in AState) or (bdsSelected in AState) then
Result := bsmSelected
else if not Enabled then
Result := bsmDisabled
else
Result := bsmNormal;
end;
procedure TStyledCategoryButtons.DrawButton(const AButton: TButtonItem;
ACanvas: TCanvas; ARect: TRect; AState: TButtonDrawState);
var
LSurfaceRect: TRect;
LOldFontName: TFontName;
LOldFontColor: TColor;
LOldFontStyle: TFontStyles;
LOldParentFont: boolean;
LOldBrushStyle: TBrushStyle;
LOldPenWidth: Integer;
LStyle: TCustomStyleServices;
LState: TStyledButtonState;
LDetails: TThemedElementDetails;
LButtonItem: TStyledButtonItem;
LDropDownRect: TRect;
LColor: TColor;
LBadgeSize: TNotificationBadgeSize;
LBadgePosition: TNotificationBadgePosition;
LBadgeColor: TColor;
LBadgeFontColor: TColor;
LBadgeFontStyle: TFontStyles;
LBadgeContent: string;
begin
//Do not call inherited
LButtonItem := AButton as TStyledButtonItem;
Assert(Assigned(LButtonItem));
if Assigned(OnDrawButton) and (not (csDesigning in ComponentState)) then
OnDrawButton(Self, LButtonItem, ACanvas, ARect, AState)
else
begin
LState := StyledButtonState(AState);
LOldParentFont := ParentFont;
LOldFontName := ACanvas.Font.Name;
LOldFontColor := ACanvas.Font.Color;
LOldFontStyle := ACanvas.Font.Style;
LOldBrushStyle := ACanvas.Brush.Style;
LOldPenWidth := ACanvas.Pen.Width;
try
GetDrawingStyle(ACanvas, LState, LButtonItem);
//At the moment, no DropDown for Buttons
LDropDownRect := TRect.Create(0,0,0,0);
if FFlat then
begin
LStyle := StyleServices{$IFDEF D10_4+}(Self){$ENDIF};
if (LState in [bsmDisabled, bsmNormal]) and IsStyleEnabled then
begin
if (bdsSelected in AState) or (bdsDown in AState) then
LDetails := LStyle.GetElementDetails(tcbButtonSelected)
else if bdsHot in AState then
LDetails := LStyle.GetElementDetails(tcbButtonHot)
else
LDetails := LStyle.GetElementDetails(tcbButtonNormal);
if not (IsCustomStyleActive and not (seFont in StyleElements)) and
LStyle.GetElementColor(LDetails, ecTextColor, LColor) and (LColor <> clNone) then
ACanvas.Font.Color := LColor;
end;
//Don't draw button Face for Flat Normal/disabled Button
if LState in [bsmDisabled, bsmNormal] then
ACanvas.Brush.Style := bsClear;
end;
if Assigned(OnBeforeDrawButton) then
OnBeforeDrawButton(Self, LButtonItem, ACanvas, ARect, AState);
DrawBackgroundAndBorder(ACanvas, ARect, LDropDownRect,
LButtonItem.StyleDrawType, LButtonItem.StyleRadius, LButtonItem.StyleRoundedCorners);
LSurfaceRect := ARect;
if LDropDownRect.Width <> 0 then
Dec(LSurfaceRect.Right, LDropDownRect.Width);
DrawCaptionAndImage(ACanvas, LSurfaceRect, LButtonItem.Caption,
LButtonItem.ImageIndex);
{ Draw the icon - prefer the event }
if Assigned(OnDrawIcon) then
OnDrawIcon(Self, LButtonItem, ACanvas, LSurfaceRect, AState, FSpacing);
LSurfaceRect := ClientRect;
//Get Notification Badge Infos by Event Handler
if Assigned(FOnGetNotificationBadgeInfo) then
begin
LBadgeSize := nbsNormal;
LBadgePosition := nbpTopRight;
LBadgeColor := DEFAULT_BADGE_COLOR;
LBadgeFontColor := DEFAULT_BADGE_FONT_COLOR;
LBadgeContent := '';
LBadgeFontStyle := [fsBold];
FOnGetNotificationBadgeInfo(
AButton.Category.Index, AButton.Index,
LBadgeContent, LBadgeSize, LBadgePosition,
LBadgeColor, LBadgeFontColor, LBadgeFontStyle);
if LBadgeContent <> '' then
begin
DrawButtonNotificationBadge(ACanvas, ARect, GetScaleFactor,
LBadgeContent, LBadgeSize, LBadgePosition,
LBadgeColor, LBadgeFontColor, LBadgeFontStyle);
end;
end;
(*
{ Show insert indications }
if [bdsInsertLeft, bdsInsertTop, bdsInsertRight, bdsInsertBottom] * State <> [] then
begin
ACanvas.Brush.Color := GetShadowColor(EdgeColor);
InsertIndication := Rect;
if bdsInsertLeft in State then
begin
Dec(InsertIndication.Left, 2);
InsertIndication.Right := InsertIndication.Left + 2;
end
else if bdsInsertTop in State then
begin
Dec(InsertIndication.Top);
InsertIndication.Bottom := InsertIndication.Top + 2;
end
else if bdsInsertRight in State then
begin
Inc(InsertIndication.Right, 2);
InsertIndication.Left := InsertIndication.Right - 2;
end
else if bdsInsertBottom in State then
begin
Inc(InsertIndication.Bottom);
InsertIndication.Top := InsertIndication.Bottom - 2;
end;
ACanvas.FillRect(InsertIndication);
ACanvas.Brush.Color := FillColor;
end;
*)
if Assigned(OnAfterDrawButton) then
OnAfterDrawButton(Self, LButtonItem, ACanvas, ARect, AState);
finally
//Restore original values
ACanvas.Font.Name := LOldFontName;
ACanvas.Font.Color := LOldFontColor;
ACanvas.Font.Style := LOldFontStyle;
ACanvas.Brush.Style := LOldBrushStyle;
ACanvas.Pen.Width := LOldPenWidth;
//ACanvas.Brush.Color := Color;
if LOldParentFont then
ParentFont := LOldParentFont;
end;
end;
end;
procedure TStyledCategoryButtons.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited;
end;
procedure TStyledCategoryButtons.ImageMarginsChange(Sender: TObject);
begin
Invalidate;
end;
function TStyledCategoryButtons.IsDefaultImageMargins: Boolean;
begin
//Default image margins: when all margins are zero except for
case FImageAlignment of
iaLeft: Result := (FImageMargins.Left = DEFAULT_IMAGE_HMARGIN) and
(FImageMargins.Top = 0) and (FImageMargins.Right = 0) and (FImageMargins.Bottom = 0);
iaRight: Result := (FImageMargins.Right = DEFAULT_IMAGE_HMARGIN) and
(FImageMargins.Top = 0) and (FImageMargins.Left = 0) and (FImageMargins.Bottom = 0);
iaTop: Result := (FImageMargins.Top = DEFAULT_IMAGE_VMARGIN) and
(FImageMargins.Left = 0) and (FImageMargins.Right = 0) and (FImageMargins.Bottom = 0);
iaBottom: Result := (FImageMargins.Bottom = DEFAULT_IMAGE_VMARGIN) and
(FImageMargins.Left = 0) and (FImageMargins.Right = 0) and (FImageMargins.Top = 0);
iaCenter: Result := (FImageMargins.Bottom = 0) and
(FImageMargins.Left = 0) and (FImageMargins.Right = 0) and (FImageMargins.Top = 0);
else
Result := False;
end;
end;
procedure TStyledCategoryButtons.CalcDefaultImageMargins(const AValue: TImageAlignment);
function AdJustMargin(const AMargin, AOffset: Integer): Integer;
begin
Result := AMargin + Round(AOffset*GetScaleFactor);
end;
begin
if IsDefaultImageMargins then
begin
FImageMargins.Left := 0;
FImageMargins.Right := 0;
FImageMargins.Top := 0;
FImageMargins.Bottom := 0;
case AValue of
iaLeft: FImageMargins.Left := AdJustMargin(FImageMargins.Left, DEFAULT_IMAGE_HMARGIN);
iaRight: FImageMargins.Right := AdJustMargin(FImageMargins.Right, DEFAULT_IMAGE_HMARGIN);
iaTop: FImageMargins.Top := AdJustMargin(FImageMargins.Top, DEFAULT_IMAGE_VMARGIN);
iaBottom: FImageMargins.Bottom := AdJustMargin(FImageMargins.Bottom, DEFAULT_IMAGE_VMARGIN);
end;
end;
end;
function TStyledCategoryButtons.CalcMaxBorderWidth: Integer;
begin
Result := Max(Max(Max(Max(FButtonStyleNormal.BorderWidth,
FButtonStylePressed.BorderWidth),
FButtonStyleSelected.BorderWidth),
FButtonStyleHot.BorderWidth),
FButtonStyleDisabled.BorderWidth);
end;
function TStyledCategoryButtons.ImageMarginsStored: Boolean;
begin
Result := not IsDefaultImageMargins;
end;
function TStyledCategoryButtons.IsCustomDrawType: Boolean;
begin
Result := FCustomDrawType;
end;
function TStyledCategoryButtons.IsCustomRoundedCorners: Boolean;
begin
Result := StyleRoundedCorners <> ALL_ROUNDED_CORNERS;
end;
function TStyledCategoryButtons.IsCustomRadius: Boolean;
begin
Result := StyleRadius <> DEFAULT_RADIUS;
end;
function TStyledCategoryButtons.IsStoredStyleAppearance: Boolean;
var
LClass: TStyledButtonClass;
LAppearance: TStyledButtonAppearance;
LButtonFamily: TButtonFamily;
begin
StyleFamilyCheckAttributes(FStyleFamily, LClass, LAppearance, LButtonFamily);
Result := FStyleAppearance <> LAppearance;
end;
function TStyledCategoryButtons.IsStoredStyleClass: Boolean;
var
LClass: TStyledButtonClass;
LAppearance: TStyledButtonAppearance;
LButtonFamily: TButtonFamily;
begin
StyleFamilyCheckAttributes(FStyleFamily, LClass, LAppearance, LButtonFamily);
if AsVCLStyle then
begin
Result := (FStyleClass <> GetActiveStyleName)
and not SameText(FStyleClass, 'Windows');
end
else
Result := FStyleClass <> LClass;
end;
function TStyledCategoryButtons.IsStoredStyleFamily: Boolean;
begin
Result := FStyleFamily <> DEFAULT_CLASSIC_FAMILY;
end;
procedure TStyledCategoryButtons.Loaded;
begin
inherited;
if not FStyleApplied (*and not HasCustomAttributes*) then
begin
StyleFamilyUpdateAttributes(
FStyleFamily, FStyleClass, FStyleAppearance,
FButtonStyleNormal, FButtonStylePressed, FButtonStyleSelected,
FButtonStyleHot, FButtonStyleDisabled);
StyleApplied := ApplyButtonStyle;
end;
end;
procedure TStyledCategoryButtons.MouseMove(Shift: TShiftState; X, Y: Integer);
var
LButtonItem: TButtonItem;
begin
inherited;
LButtonItem := GetButtonAt(X, Y);
if Assigned(LButtonItem) then
inherited Cursor := FButtonsCursor
else
inherited Cursor := FCursor;
end;
class procedure TStyledCategoryButtons.RegisterDefaultRenderingStyle(
const ADrawType: TStyledButtonDrawType; const AFamily: TStyledButtonFamily;
const AClass: TStyledButtonClass; const AAppearance: TStyledButtonAppearance;
const AStyleRadius: Integer; const AButtonsCursor: TCursor);
begin
_DefaultStyleDrawType := ADrawType;
_UseCustomDrawType := True;
_DefaultFamily := AFamily;
_DefaultClass := AClass;
_DefaultAppearance := AAppearance;
_DefaultStyleRadius := AStyleRadius;
_DefaultButtonsCursor := AButtonsCursor;
end;
function TStyledCategoryButtons.ApplyButtonStyle: Boolean;
var
LButtonFamily: TButtonFamily;
LStyleClass: TStyledButtonClass;
LStyleAppearance: TStyledButtonAppearance;
begin
if AsVCLStyle then
begin
//if StyleElements contains seClient then use
//VCL Style assigned to Button or Global VCL Style
if seBorder in StyleElements then
LStyleAppearance := DEFAULT_APPEARANCE;
LStyleClass := GetActiveStyleName;
end
else
begin
LStyleClass := FStyleClass;
LStyleAppearance := FStyleAppearance;
end;
Result := StyleFamilyCheckAttributes(FStyleFamily,
LStyleClass, LStyleAppearance, LButtonFamily);
if Result (*or (csDesigning in ComponentState)*) then
begin
StyleFamilyUpdateAttributes(
FStyleFamily,
LStyleClass,
LStyleAppearance,
FButtonStyleNormal,
FButtonStylePressed,
FButtonStyleSelected,
FButtonStyleHot,
FButtonStyleDisabled);
end;
StyleClass := LStyleClass;
StyleAppearance := LStyleAppearance;
if Result then
Invalidate;
end;
procedure TStyledCategoryButtons.Assign(Source: TPersistent);
begin
inherited Assign(Source);
if Source is TStyledCategoryButtons then
begin
FStyleFamily := TStyledCategoryButtons(Source).FStyleFamily;
FStyleClass := TStyledCategoryButtons(Source).FStyleClass;
FStyleAppearance := TStyledCategoryButtons(Source).FStyleAppearance;
FStyleRadius := TStyledCategoryButtons(Source).FStyleRadius;
FStyleRoundedCorners := TStyledCategoryButtons(Source).FStyleRoundedCorners;
FStyleDrawType := TStyledCategoryButtons(Source).FStyleDrawType;
FButtonsCursor := TStyledCategoryButtons(Source).FButtonsCursor;
Invalidate;
end;
end;
function TStyledCategoryButtons.AsVCLStyle: Boolean;
begin
Result := (StyleFamily = DEFAULT_CLASSIC_FAMILY) and
(seClient in StyleElements);
end;
function TStyledCategoryButtons.GetAsVCLComponent: Boolean;
begin
Result := (StyleFamily = DEFAULT_CLASSIC_FAMILY) and
(seClient in StyleElements) and
(FStyleClass = GetActiveStyleName);
end;
function TStyledCategoryButtons.GetButtonCategoriesClass: TButtonCategoriesClass;
begin
Result := TStyledButtonCategories;
end;
function TStyledCategoryButtons.GetButtonCategoryClass: TButtonCategoryClass;
begin
Result := TStyledButtonCategory;
end;
function TStyledCategoryButtons.GetButtonItemClass: TButtonItemClass;
begin
Result := TStyledButtonItem;
end;
procedure TStyledCategoryButtons.SetAsVCLComponent(const AValue: Boolean);
begin
if AValue <> GetAsVCLComponent then
begin
if AValue then
begin
FStyleFamily := DEFAULT_CLASSIC_FAMILY;
FStyleClass := DEFAULT_WINDOWS_CLASS;
FStyleAppearance := DEFAULT_APPEARANCE;
StyleElements := StyleElements + [seClient];
FCustomDrawType := False;
end
else if FStyleFamily = DEFAULT_CLASSIC_FAMILY then
begin
StyleElements := StyleElements - [seClient];
end;
UpdateStyleElements;
end;
end;
procedure TStyledCategoryButtons.UpdateStyleElements;
var
LStyleClass: TStyledButtonClass;
begin
if AsVCLStyle then
begin
//if StyleElements contains seClient then Update style
//as VCL Style assigned to CategoryButtons or Global VCL Style
if seBorder in StyleElements then
StyleAppearance := DEFAULT_APPEARANCE;
LStyleClass := GetActiveStyleName;
FStyleClass := LStyleClass;
StyleApplied := ApplyCategoryButtonsStyle;
ProcessButtons(
procedure (ABtn: TStyledButtonItem)
begin
//ABtn.UpdateStyleElements;
ABtn.StyleDrawType := Self.StyleDrawType;