-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathu_PngComponentCommons.pas
More file actions
1111 lines (918 loc) · 34.5 KB
/
u_PngComponentCommons.pas
File metadata and controls
1111 lines (918 loc) · 34.5 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
unit u_PngComponentCommons;
INTERFACE
{
Author Nedko Ivanov
Based on PngComponents by Uwe Raabe http://cc.embarcadero.com/item/26127
Date 2015-07-27
URL https://github.com/nedich/PngComponents2
License MIT
}
uses
pngimage, Graphics, Types, Buttons, Windows, Controls, Themes, Classes,
UxTheme, PngImageList;
type
TPngOption2 = (
pngBlendOnEnabled, pngGrayscaleOnEnabled,
pngBlendOnDisabled, pngGrayscaleOnDisabled,
pngBlendNotHovering, pngGrayscaleNotHovering
);
TPngOptions2 = set of TPngOption2;
TThemingPreset = (tpCustom, tpButton, tpFlatButton, tpToolButton);
TButtonDrawState = record
bEnabled: Boolean;
bPressed: Boolean;
bHot: Boolean;
bDefault: Boolean;
class operator Equal(a: TButtonDrawState; d: TButtonDrawState): Boolean;
procedure Assign(from: TButtonDrawState);
end;
TThemedButtonDetails = class;
TThemedElementDetailsObject = class(TPersistent)
private
m_owner: TThemedButtonDetails;
m_Element: TThemedElement;
m_Part: Integer;
m_State: Integer;
procedure SetElement(AElement: TThemedElement);
procedure SetPart(const Value: Integer);
procedure SetState(const Value: Integer);
function GetElement: TThemedElement;
function GetPart: Integer;
function GetState: Integer;
procedure Changed;
public
constructor Create(AOwner: TThemedButtonDetails); reintroduce;
procedure Assign(Source : TPersistent); override;
published
property Element: TThemedElement read GetElement write SetElement default teButton;
property Part: Integer read GetPart write SetPart default BP_PUSHBUTTON;
property State: Integer read GetState write SetState default PBS_NORMAL;
end;
IThemedButtonDetails = interface
['{D0171D60-E4F8-4216-A0C4-B2A3BE812A58}']
function GetNormal: TThemedElementDetailsObject;
function GetDisabled: TThemedElementDetailsObject;
function GetHot: TThemedElementDetailsObject;
function GetPressed: TThemedElementDetailsObject;
property Normal: TThemedElementDetailsObject read GetNormal;
property Disabled: TThemedElementDetailsObject read GetDisabled;
property Hot: TThemedElementDetailsObject read GetHot;
property Pressed: TThemedElementDetailsObject read GetPressed;
end;
IButtonControl = interface
['{8B2D2C7E-12A7-40B4-A161-4122AAC9E8DA}']
function GetThemedButtonDetails: IThemedButtonDetails;
function GetDrawState: TButtonDrawState;
function GetLayout: TButtonLayout;
function GetPngOptions: TPngOptions2;
function GetMargin: Integer;
function GetSpacing: Integer;
function GetPngBlendFactor: Integer;
function GetText: string;
function GetControl: TControl;
function GetWidth: Integer;
function GetHeight: Integer;
function GetEnabled: boolean;
function GetClientRect: TRect;
function GetBoundsRect: TRect;
function GetPngImage: TPngImage;
function GetCanvas: TCanvas;
function GetFont: TFont;
function GetWordWrap: Boolean;
function GetImages: TPngImageList;
function GetImageIndex: integer;
function DrawTextBiDiModeFlags(Flags: Longint): Longint;
property Width: Integer read GetWidth;
property Height: Integer read GetHeight;
property Layout: TButtonLayout read GetLayout;
property Caption: string read GetText;
property PngBlendFactor: Integer read GetPngBlendFactor;
property Spacing: Integer read GetSpacing;
property Margin: Integer read GetMargin;
property DrawState: TButtonDrawState read GetDrawState;
property Enabled: boolean read GetEnabled;
property ThemedDetails: IThemedButtonDetails read GetThemedButtonDetails;
property ClientRect: TRect read GetClientRect;
property BoundsRect: TRect read GetBoundsRect;
property PngImage: TPngImage read GetPngImage;
property PngOptions: TPngOptions2 read GetPngOptions;
property Canvas: TCanvas read GetCanvas;
property Font: TFont read GetFont;
property WordWrap: Boolean read GetWordWrap;
property Images: TPngImageList read GetImages;
property ImageIndex: integer read GetImageIndex;
end;
IButtonControlWritable = interface(IButtonControl)
['{99A0C765-C70E-4CA9-9F08-27944601626B}']
procedure SetWidth(value: integer);
procedure SetHeight(value: integer);
procedure SetLayout(value: TButtonLayout);
procedure SetText(value: string);
procedure SetPngBlendFactor(value: integer);
procedure SetSpacing(value: integer);
procedure SetMargin(value: integer);
procedure SetDrawState(value: TButtonDrawState);
procedure SetEnabled(value: boolean);
procedure SetBoundsRect(value: TRect);
procedure SetPngImage(value: TPngImage);
procedure SetPngOptions(value: TPngOptions2);
procedure SetFont(value: TFont);
procedure SetWordWrap(value: Boolean);
procedure SetImages(value: TPngImageList);
procedure SetImageIndex(value: Integer);
property Width: Integer read GetWidth write SetWidth;
property Height: Integer read GetHeight write SetHeight;
property Layout: TButtonLayout read GetLayout write SetLayout;
property Caption: string read GetText write SetText;
property PngBlendFactor: Integer read GetPngBlendFactor write SetPngBlendFactor;
property Spacing: Integer read GetSpacing write SetSpacing;
property Margin: Integer read GetMargin write SetMargin;
property DrawState: TButtonDrawState read GetDrawState write SetDrawState;
property Enabled: boolean read GetEnabled write SetEnabled;
property ThemedDetails: IThemedButtonDetails read GetThemedButtonDetails;
property ClientRect: TRect read GetClientRect;
property BoundsRect: TRect read GetBoundsRect write SetBoundsRect;
property PngImage: TPngImage read GetPngImage write SetPngImage;
property PngOptions: TPngOptions2 read GetPngOptions write SetPngOptions;
property Canvas: TCanvas read GetCanvas;
property Font: TFont read GetFont write SetFont;
property WordWrap: Boolean read GetWordWrap write SetWordWrap;
property Images: TPngImageList read GetImages write SetImages;
property ImageIndex: integer read GetImageIndex write SetImageIndex;
end;
TThemedButtonDetails = class(TInterfacedPersistent, IThemedButtonDetails)
private
m_normal,
m_pressed,
m_hot,
m_disabled: TThemedElementDetailsObject;
m_themingpreset: TThemingPreset;
m_bChanging: Boolean;
FOnChange: TNotifyEvent;
function GetNormal: TThemedElementDetailsObject;
function GetDisabled: TThemedElementDetailsObject;
function GetHot: TThemedElementDetailsObject;
function GetPressed: TThemedElementDetailsObject;
procedure SetDisabled(const Value: TThemedElementDetailsObject);
procedure SetNormal(const Value: TThemedElementDetailsObject);
procedure SetHot(const Value: TThemedElementDetailsObject);
procedure SetPressed(const Value: TThemedElementDetailsObject);
procedure SetThemingPreset(const Value: TThemingPreset);
function GetThemingPreset: TThemingPreset;
public
constructor Create;
destructor Destroy; override;
procedure ThemingDetailsChanged(Sender: TObject);
property OnChange: TNotifyEvent read FOnChange write FOnChange;
procedure Assign(Source: TPersistent); override;
protected
function IsStored: Boolean;
published
property Normal: TThemedElementDetailsObject read GetNormal write SetNormal stored IsStored;
property Disabled: TThemedElementDetailsObject read GetDisabled write SetDisabled stored IsStored;
property Hot: TThemedElementDetailsObject read GetHot write SetHot stored IsStored;
property Pressed: TThemedElementDetailsObject read GetPressed write SetPressed stored IsStored;
property _Kind: TThemingPreset read GetThemingPreset write SetThemingPreset;
end;
TButtonsLib = class
public
class procedure RenderButton(control: IButtonControl); overload;
//class procedure RenderButton(r: TRect; PngImage: TPngImage; DrawState: TButtonDrawState; h: HDC; Caption: string; control: TControl; Layout: TButtonLayout; Margin, Spacing, PngBlendFactor: Integer; Options: TPngOptions2; Font: TFont; bWordWrap: boolean); overload;
class procedure _DrawPNG(Png: TPngImage; Canvas: TCanvas; const ARect: TRect; const Options: TPngOptions2; drawstate: TButtonDrawState; PngBlendFactor: Integer);
class procedure CalcButtonLayout(Canvas: TCanvas; PngImage: TPngImage; const Client:
TRect; Pressed, Down: Boolean; const Caption: string; Layout: TButtonLayout;
Margin, Spacing: Integer; var GlyphPos, TextPos: TPoint; BiDiFlags: LongInt; bWordWrap: boolean);
end;
TPngLib = class
public
class procedure PngDrawOver(base, overlay: TPngImage; Left, top: Integer);
end;
TButtonOverlay = class(TPersistent)
private
m_images: TPngImageList;
m_nImageIndex: Integer;
m_left: Integer;
m_top: Integer;
FOnChange: TNotifyEvent;
procedure SetImageIndex(const Value: Integer);
procedure SetImages(const Value: TPngImageList);
procedure SetLeft(const Value: Integer);
procedure SetTop(const Value: Integer);
procedure Changed;
function GetImages: TPngImageList;
function GetImageIndex: Integer;
function GetLeft: Integer;
function GetTop: Integer;
public
constructor Create;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
published
property Images: TPngImageList read GetImages write SetImages;
property ImageIndex: Integer read GetImageIndex write SetImageIndex default -1;
property Left: Integer read GetLeft write SetLeft;
property Top: Integer read GetTop write SetTop;
end;
{$IF RTLVersion < 23.0 }
type
TThemeServicesHelper = class helper for TThemeServices
private
function GetEnabled: Boolean;
public
function GetElementContentRect(DC: HDC; Details: TThemedElementDetails; const BoundingRect: TRect;
out ContentRect: TRect): Boolean; overload;
property Enabled: Boolean read GetEnabled;
end;
{$IFEND}
procedure Register;
const
SDelphiComponentsPageName = 'PngComponents2';
IMPLEMENTATION
uses
PngFunctions
,SysUtils, Math
{$ifdef nbi},u_Ini {$endif}
,Dialogs;
{$region 'TThemeServicesHelper'}
///From PngBitBtn:
{$IF RTLVersion < 23.0 }
function TThemeServicesHelper.GetElementContentRect(DC: HDC; Details: TThemedElementDetails; const BoundingRect: TRect;
out ContentRect: TRect): Boolean;
begin
ContentRect := Self.ContentRect(DC, Details, BoundingRect);
Result := true;
end;
function TThemeServicesHelper.GetEnabled: Boolean;
begin
Result := ThemesEnabled;
end;
{$IFEND}
{$endregion 'TThemeServicesHelper'}
class procedure TButtonsLib._DrawPNG(Png: TPngImage; Canvas: TCanvas; const ARect: TRect; const Options: TPngOptions2; drawstate: TButtonDrawState; PngBlendFactor: Integer);
{-----------------------------------------------------------------------------
Procedure: _DrawPNG
Author: nbi
Date: 16-Sep-2014
Arguments: Png: TPngImage; Canvas: TCanvas; const ARect: TRect; const Options: TPngOptions2
Result: None
-----------------------------------------------------------------------------}
var
PngCopy: TPngImage;
begin
if Options <> [] then begin
PngCopy := TPngImage.Create;
try
PngCopy.Assign(Png);
if(not drawstate.bEnabled) then begin
/// disabled
if (u_PngComponentCommons.pngGrayscaleOnDisabled in Options) then
MakeImageGrayscale(PngCopy);
if (u_PngComponentCommons.pngBlendOnDisabled in Options) then
MakeImageBlended(PngCopy, PngBlendFactor);
end
else
begin
/// ENABLED
if (pngGrayscaleOnEnabled in Options) then
MakeImageGrayscale(PngCopy);
if (pngBlendOnEnabled in Options) then
MakeImageBlended(PngCopy, PngBlendFactor);
/// not hovered?
if(not drawstate.bHot) and (pngGrayscaleNotHovering in Options) then
MakeImageGrayscale(PngCopy);
if(not drawstate.bHot) and (pngBlendNotHovering in Options) then
if(not drawstate.bPressed) then //dont blend when button is Down
MakeImageBlended(PngCopy, PngBlendFactor);
end;
try
PngCopy.Draw(Canvas, ARect);
except
on e:Exception do begin
raise;
end;
end;
finally
PngCopy.Free;
end;
end
else begin
Png.Draw(Canvas, ARect);
end;
end;
class procedure TButtonsLib.RenderButton(control: IButtonControl);
//begin
// TButtonsLib.RenderButton(control.ClientRect, control.PngImage, control.DrawState,
// control.Canvas.Handle, control.Caption, control.GetControl, control.Layout, control.Margin, control.Spacing,
// control.PngBlendFactor, control.PngOptions, control.Font, control.WordWrap
// );
//end;
//class procedure TButtonsLib.RenderButton(r: TRect; PngImage: TPngImage;
// DrawState: TButtonDrawState; h: HDC; Caption: string; control: TControl; Layout: TButtonLayout;
// Margin, Spacing, PngBlendFactor: integer; Options: TPngOptions2; Font: TFont; bWordWrap: boolean);
var
PngImage: TPngImage;
Canvas: TCanvas;
PaintRect: TRect;
GlyphPos, TextPos: TPoint;
r: TRect;
Caption: string;
DrawState: TButtonDrawState;
procedure DrawTheText;
var
PaintRect: TRect;
flagsDrawText: cardinal;
begin
if Length(Caption) > 0 then begin
//PaintRect := Rect(TextPos.X, TextPos.Y, control.Width-TextPos.X, control.Height-TextPos.Y);
PaintRect := Rect(TextPos.X, TextPos.Y, control.Width, control.Height);
//if() then begin Dec(paintrect.Right, TextPos.X); //no glyph
Canvas.Brush.Style := bsClear;
flagsDrawText := control.DrawTextBiDiModeFlags(0) {or DT_VCENTER} {or DT_CENTER};
if(control.WordWrap) then
flagsDrawText := flagsDrawText or DT_WORDBREAK;
DrawText(Canvas.Handle, PChar(Caption), -1, PaintRect, flagsDrawText or DT_CALCRECT);
//grayed Caption when disabled
if not DrawState.bEnabled then begin
OffsetRect(PaintRect, 1, 1);
Canvas.Font.Color := clBtnHighlight;
DrawText(Canvas.Handle, PChar(Caption), -1, PaintRect, flagsDrawText);
OffsetRect(PaintRect, -1, -1);
Canvas.Font.Color := clBtnShadow;
end;
DrawText(Canvas.Handle, PChar(Caption), -1, PaintRect,
control.DrawTextBiDiModeFlags(0) {or DT_LEFT} {or DT_VCENTER} or DT_WORDBREAK or DT_CENTER);
end;
end;
procedure DrawThemedBorder;
var
a: TThemedElementDetailsObject;
btncontrol: IButtonControl;
begin
if(not supports(control, IButtonControl, btncontrol)) then EXIT;
ASSERT(btncontrol<>nil);
ASSERT(btncontrol.themeddetails<>nil);
if(DrawState.bPressed) then
a := btncontrol.ThemedDetails.Pressed
else
if(not DrawState.bEnabled) then
a := btncontrol.ThemedDetails.Disabled
else
if(DrawState.bHot) then
a := btncontrol.ThemedDetails.Hot
else
a := btncontrol.ThemedDetails.Normal;
//if(a.Part<>0) then
begin
//Paint the background, border, and finally get the inner rect
//Details := ThemeServices.GetElementDetails(TThemedButton(a.Element));
//ThemeServices.DrawParentBackground(0, Canvas.Handle, @Details, True);
DrawThemeParentBackground(0, Canvas.Handle, nil);
//ThemeServices.DrawElement(Canvas.Handle, Details, control.ClientRect);
DrawThemeBackground(ThemeServices.Theme[a.Element], Canvas.Handle, a.Part, a.state, control.ClientRect, nil);
//ThemeServices.GetElementContentRect(Canvas.Handle, Details, control.ClientRect, R);
//r := GetThemeBackgroundContentRect(Theme[Element], DC, Part, State, BoundingRect);
end;
end;
procedure DrawUnthemedBorder;
var
Flags: Cardinal;
begin
//Draw the outer border, when focused
if DrawState.bDefault then begin
Canvas.Pen.Color := clWindowFrame;
Canvas.Pen.Width := 1;
Canvas.Brush.Style := bsClear;
Canvas.Rectangle(R.Left, R.Top, R.Right, R.Bottom);
InflateRect(R, -1, -1);
end;
//Draw the inner border
if DrawState.bPressed then begin
Canvas.Pen.Color := clBtnShadow;
Canvas.Pen.Width := 1;
Canvas.Brush.Color := clBtnFace;
Canvas.Rectangle(R.Left, R.Top, R.Right, R.Bottom);
InflateRect(R, -1, -1);
end
else begin
Flags := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT;
if not DrawState.bEnabled then
Flags := Flags or DFCS_INACTIVE;
DrawFrameControl(Canvas.Handle, R, DFC_BUTTON, Flags);
end;
if DrawState.bPressed then
OffsetRect(R, 1, 1);
end;
begin
Canvas := TCanvas.Create;
r := control.ClientRect;
PngImage := control.PngImage;
Caption := control.Caption;
DrawState := control.DrawState;
try
Canvas.Handle := control.Canvas.Handle;
Canvas.Font.assign(control.Font);
// GetWindowOrgEx(h, LastOrigin);
// SetWindowOrgEx(h, r.left, r.Right, nil);
//
// PerformEraseBackground(Self, h);
//Canvas.Brush.Color := clBlack;
//Canvas.fillrect(ClientRect);
//Draw the border
if ThemeServices.Enabled then begin
DrawThemedBorder;
end
else begin
DrawUnthemedBorder;
end;
//Calculate the position of the PNG glyph
TButtonsLib.CalcButtonLayout(Canvas, PngImage, control.ClientRect, DrawState.bPressed, False, Caption,
control.Layout, control.Margin, control.Spacing,
{var}GlyphPos, {var}TextPos,
control.DrawTextBiDiModeFlags(0), control.WordWrap);
//Draw the image
if (PngImage <> nil) and not PngImage.Empty then begin
PaintRect := Bounds(GlyphPos.X, GlyphPos.Y, PngImage.Width, PngImage.Height);
TButtonsLib._DrawPNG(PngImage, Canvas, PaintRect, control.PngOptions, control.DrawState, control.PngBlendFactor);
end;
//Draw the text
DrawTheText;
finally
Canvas.Free;
end;
end;
{ TButtonDrawState }
procedure TButtonDrawState.Assign(from: TButtonDrawState);
{-----------------------------------------------------------------------------
Procedure: Assign
Author: nbi
Date: 07-Apr-2014
Arguments: from: TButtonDrawState
Result: None
-----------------------------------------------------------------------------}
begin
bEnabled := from.bEnabled;
bPressed := from.bPressed;
bHot := from.bHot;
bDefault := from.bDefault;
end;
class operator TButtonDrawState.Equal(a, d: TButtonDrawState): boolean;
begin
Result := (a.bEnabled<>d.bEnabled) or (a.bPressed<>d.bPressed) or (a.bHot<>d.bHot) or (a.bDefault<>d.bDefault);
Result := not Result;
end;
class procedure TButtonsLib.CalcButtonLayout(Canvas: TCanvas; PngImage: TPngImage; const Client:
TRect; Pressed, Down: Boolean; const Caption: string; Layout: TButtonLayout;
Margin, Spacing: Integer; var GlyphPos, TextPos: TPoint; BiDiFlags: Longint; bWordWrap: boolean);
{-----------------------------------------------------------------------------
Procedure: CalcButtonLayout
Author:
Date: 26-Feb-2014
Arguments: Canvas: TCanvas; PngImage: TPngImage; const Client: TRect; Pressed, Down: Boolean; const Caption: string; Layout: TButtonLayout; Margin, Spacing: Integer; var GlyphPos, TextPos: TPoint; BiDiFlags: LongInt
Result: None
-----------------------------------------------------------------------------}
var
ClientSize, GlyphSize, TextSize, BedSize: TPoint;
TextBounds: TRect;
flags: Cardinal;
//r: TRect;
begin
if (BiDiFlags and DT_RIGHT) = DT_RIGHT then begin
if Layout = blGlyphLeft then
Layout := blGlyphRight
else if Layout = blGlyphRight then
Layout := blGlyphLeft;
end;
//Calculate the item sizes
ClientSize := Point(Client.Right - Client.Left, Client.Bottom - Client.Top);
if PngImage <> nil then
GlyphSize := Point(PngImage.Width, PngImage.Height)
else
GlyphSize := Point(0, 0);
if Length(Caption) > 0 then begin
TextBounds := Rect(0, 0, Client.Right - Client.Left, client.Bottom-client.top-GlyphSize.y);
if(GlyphSize.X<>0) then
TextBounds.Right := TextBounds.Right - GlyphSize.X - Spacing;
if(GlyphSize.Y<>0) then
TextBounds.Bottom := TextBounds.Bottom - GlyphSize.Y - Spacing;
flags := DT_CALCRECT or BiDiFlags;
if(bWordWrap) then
flags := flags or DT_WORDBREAK;
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), TextBounds, flags);
TextSize := Point(TextBounds.Right - TextBounds.Left, TextBounds.Bottom - TextBounds.Top);
end
else begin
TextBounds := Rect(0, 0, 0, 0);
TextSize := Point(0, 0);
end;
//If the layout has the glyph on the right or the left, then both the
//text and the glyph are centered vertically. If the glyph is on the top
//or the bottom, then both the text and the glyph are centered horizontally.
if Layout in [blGlyphLeft, blGlyphRight] then
GlyphPos.Y := (ClientSize.Y - GlyphSize.Y + 1) div 2
else
GlyphPos.X := (ClientSize.X - GlyphSize.X + 1) div 2;
//If there is no text or no bitmap, then Spacing is irrelevant
if (TextSize.X = 0) or (GlyphSize.X = 0) then begin
Spacing := 0;
end;
// bed = glyph + spacing + text
if Layout in [blGlyphLeft, blGlyphRight] then
BedSize := Point(GlyphSize.X + Spacing + TextSize.X, MAX(GlyphSize.Y, TextSize.Y))
else
BedSize := Point(MAX(GlyphSize.X, TextSize.X), GlyphSize.Y + Spacing + TextSize.Y);
if(Margin=-1) then begin
///centered
if Layout in [blGlyphLeft, blGlyphRight] then
Margin := (clientsize.x - bedsize.x) div 2
else
Margin := (clientsize.y - bedsize.y) div 2;
end else begin
// /// margin=0 is not the client.x=0 . we would want to start placing at some margin which is equal to the margin space from top (for left glyph)
// /// thus with margin=0 the placement would look nice
// if Layout in [blGlyphLeft, blGlyphRight] then
// Margin := Margin + (clientsize.y - BedSize.y) div 2;
end;
///position glyph
case Layout of
blGlyphLeft: GlyphPos.X := Margin;
blGlyphRight: GlyphPos.X := ClientSize.X - Margin - GlyphSize.X;
blGlyphTop: GlyphPos.Y := Margin;
blGlyphBottom: GlyphPos.Y := ClientSize.Y - Margin - GlyphSize.Y;
end;
if Layout in [blGlyphLeft, blGlyphRight] then
TextPos.Y := (ClientSize.Y - TextSize.Y) div 2
else
TextPos.X := (ClientSize.X - TextSize.X) div 2;
///position text
case Layout of
blGlyphLeft: TextPos.X := GlyphPos.X + GlyphSize.X + Spacing;
blGlyphRight: TextPos.X := GlyphPos.X - Spacing - TextSize.X;
blGlyphTop: TextPos.Y := GlyphPos.Y + GlyphSize.Y + Spacing;
blGlyphBottom: TextPos.Y := GlyphPos.Y - Spacing - TextSize.Y;
end;
// l(Caption);
// lf('ClientSize %d %d',[ClientSize.x, ClientSize.y]);
// lf('GlyphSize %d %d',[GlyphSize.x, GlyphSize.y]);
// lf('TextSize %d %d',[TextSize.x, TextSize.y]);
// lf('GlyphPos %d %d',[GlyphPos.x, GlyphPos.y]);
// lf('TextPos %d %d',[TextPos.x, TextPos.y]);
// l;
//
// r := rect(0,0, TextSize.x, TextSize.y);
// offsetrect(r, textpos.x, textpos.y);
// Canvas.FillRect(r);
// Canvas.Pen.Color := clred;
// Canvas.Ellipse(textpos.x, textpos.y, textpos.x+1, textpos.y+1);
///n@2014-04-15 commented out, the default vcl button does not have such offset
//n@2014-07-10 uncommented, it does not look good when it does not sink
//Fixup the result variables
with GlyphPos do begin
Inc(X, Integer(Pressed or Down));
Inc(Y, Integer(Pressed or Down));
end;
with TextPos do begin
Inc(X, Integer(Pressed or Down));
Inc(Y, Integer(Pressed or Down));
end;
end;
{ TThemedButtonDetails }
procedure TThemedButtonDetails.Assign(Source: TPersistent);
begin
inherited;
end;
constructor TThemedButtonDetails.Create;//(AOwner: TControl);
{-----------------------------------------------------------------------------
Procedure: Create
Author: nbi
Date: 08-Apr-2014
Arguments: AOwner: TControl
Result: None
-----------------------------------------------------------------------------}
begin
m_bChanging := False;
m_disabled := TThemedElementDetailsObject.Create(self);
m_hot := TThemedElementDetailsObject.Create(self);
m_normal := TThemedElementDetailsObject.Create(self);
m_pressed := TThemedElementDetailsObject.Create(self);
// m_pressed.SetSubComponent(True);
// m_hot.SetSubComponent(True);
// m_normal.SetSubComponent(True);
// m_disabled.SetSubComponent(True);
_Kind := tpButton;
inherited Create;//(TComponent(aowner));
end;
destructor TThemedButtonDetails.Destroy;
{-----------------------------------------------------------------------------
Procedure: Destroy
Author: nbi
Date: 08-Apr-2014
Arguments: None
Result: None
-----------------------------------------------------------------------------}
begin
m_pressed.Free;
m_normal.Free;
m_hot.Free;
m_disabled.Free;
inherited;
end;
function TThemedButtonDetails.GetDisabled: TThemedElementDetailsObject;
begin
Result := m_disabled;
end;
function TThemedButtonDetails.GetHot: TThemedElementDetailsObject;
begin
Result := m_hot;
end;
function TThemedButtonDetails.GetNormal: TThemedElementDetailsObject;
begin
Result := m_normal;
end;
function TThemedButtonDetails.GetPressed: TThemedElementDetailsObject;
begin
Result := m_pressed;
end;
function TThemedButtonDetails.GetThemingPreset: TThemingPreset;
{-----------------------------------------------------------------------------
Procedure: GetThemingPreset
Author: nbi
Date: 15-May-2014
Arguments: None
Result: TThemingPreset
-----------------------------------------------------------------------------}
begin
Result := m_themingpreset;
end;
function TThemedButtonDetails.IsStored: Boolean;
begin
Result := m_themingpreset=tpCustom;
end;
procedure TThemedButtonDetails.ThemingDetailsChanged(Sender: TObject);
{-----------------------------------------------------------------------------
Procedure: OnThemingDetailsChanged
Author: nbi
Date: 08-Apr-2014
Arguments: Sender: TObject
Result: None
-----------------------------------------------------------------------------}
begin
//if(Owner<>nil) then
//(Owner as IButtonControl).GetControl.Invalidate;
if(Assigned(FOnChange)) then
FOnChange(Self);
end;
procedure TThemedButtonDetails.SetDisabled(const Value: TThemedElementDetailsObject);
begin
m_disabled.assign(value);
end;
procedure TThemedButtonDetails.SetHot(const Value: TThemedElementDetailsObject);
begin
m_hot.assign(value);
end;
procedure TThemedButtonDetails.SetNormal(const Value: TThemedElementDetailsObject);
begin
m_normal.assign(value);
end;
procedure TThemedButtonDetails.SetPressed(const Value: TThemedElementDetailsObject);
begin
m_pressed.assign(value);
end;
procedure TThemedButtonDetails.SetThemingPreset(const Value: TThemingPreset);
{-----------------------------------------------------------------------------
Procedure: SetThemingPreset
Author: nbi
Date: 08-Apr-2014
Arguments: const Value: TThemingPreset
Result: None
-----------------------------------------------------------------------------}
begin
if(m_bChanging) then EXIT;//avoid reentrancy
m_bChanging := True;
case Value of
tpCustom: ;
tpButton: begin
m_pressed.m_Element := teButton;
m_pressed.m_Part := BP_PUSHBUTTON;
m_pressed.m_state := PBS_PRESSED;
m_hot.m_Element := teButton;
m_hot.m_Part := BP_PUSHBUTTON;
m_hot.m_state := PBS_HOT;
m_disabled.m_Element := teButton;
m_disabled.m_Part := BP_PUSHBUTTON;
m_disabled.m_state := PBS_DISABLED;
m_normal.m_Element := teButton;
m_normal.m_Part := BP_PUSHBUTTON;
m_normal.m_state := PBS_NORMAL;
end;
tpFlatButton: begin
m_pressed.m_Element := teButton;
m_pressed.m_Part := BP_PUSHBUTTON;
m_pressed.m_state := PBS_PRESSED;
m_hot.m_Element := teButton;
m_hot.m_Part := BP_PUSHBUTTON;
m_hot.m_state := PBS_HOT;
m_disabled.m_Element := teButton;
m_disabled.m_Part := BP_PUSHBUTTON;
m_disabled.m_state := PBS_DISABLED;
m_normal.m_Element := teToolbar;
m_normal.m_Part := TP_BUTTON;
m_normal.m_state := TS_NORMAL;
end;
tpToolButton: begin
m_pressed.m_Element := teToolbar;
m_pressed.m_Part := TP_BUTTON;
m_pressed.m_state := TS_PRESSED;
m_hot.m_Element := teToolbar;
m_hot.m_Part := TP_BUTTON;
m_hot.m_state := TS_HOT;
m_disabled.m_Element := teToolbar;
m_disabled.m_Part := TP_BUTTON;
m_disabled.m_state := TS_DISABLED;
m_normal.m_Element := teToolbar;
m_normal.m_Part := TP_BUTTON;
m_normal.m_state := TS_NORMAL;
end;
end;
m_themingpreset := value;
ThemingDetailsChanged(self);
m_bChanging := False;
end;
{ TThemedElementDetailsObject }
procedure TThemedElementDetailsObject.Assign(Source: TPersistent);
{-----------------------------------------------------------------------------
Procedure: Assign
Author: nbi
Date: 08-Apr-2014
Arguments: Source: TPersistent
Result: None
-----------------------------------------------------------------------------}
begin
ASSERT(Source is TThemedElementDetailsObject);
m_Element := TThemedElementDetailsObject(Source).element;
m_Part := TThemedElementDetailsObject(Source).Part;
m_State := TThemedElementDetailsObject(Source).State;
end;
procedure TThemedElementDetailsObject.Changed;
{-----------------------------------------------------------------------------
Procedure: Changed
Author: nbi
Date: 08-Apr-2014
Arguments: None
Result: None
-----------------------------------------------------------------------------}
begin
m_owner.ThemingDetailsChanged(Self);
m_owner._Kind := tpCustom;
end;
constructor TThemedElementDetailsObject.Create(AOwner: TThemedButtonDetails);
{-----------------------------------------------------------------------------
Procedure: Create
Author: nbi
Date: 08-Apr-2014
Arguments: AOwner: TControl
Result: None
-----------------------------------------------------------------------------}
begin
m_owner := aowner;
inherited Create;//(TComponent(aowner));
end;
function TThemedElementDetailsObject.GetElement: TThemedElement;
begin
Result := m_Element;
end;
function TThemedElementDetailsObject.GetPart: Integer;
begin
Result := m_Part;
end;
function TThemedElementDetailsObject.GetState: Integer;
begin
Result := m_State;
end;
procedure TThemedElementDetailsObject.SetElement(AElement: TThemedElement);
begin
m_Element := AElement;
Changed;
end;
procedure TThemedElementDetailsObject.SetPart(const Value: Integer);
begin
m_Part := value;
Changed;
end;
procedure TThemedElementDetailsObject.SetState(const Value: Integer);
begin
m_State := value;
Changed;
end;
procedure Register;
begin
end;
{ TButtonOverlay }
procedure TButtonOverlay.Changed;
begin
if(Assigned(FOnChange)) then try
ASSERT(Self<>nil);
FOnChange(Self);
except
beep;
end;
end;
constructor TButtonOverlay.Create;
begin
inherited;
m_images := nil;
m_nImageIndex := -1;
m_left := 0;
m_top := 0;
end;
function TButtonOverlay.GetImageIndex: Integer;
begin
Result := m_nImageIndex;
end;
function TButtonOverlay.GetImages: TPngImageList;