-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatbutton.pas
More file actions
132 lines (116 loc) · 3.3 KB
/
Copy pathflatbutton.pas
File metadata and controls
132 lines (116 loc) · 3.3 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
unit flatbutton;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Controls, Buttons, Graphics, Themes, LCLType, Types;
type
TFlatButton = class(TSpeedButton)
private
FOffsetY: integer;
procedure SetOffsetY(AValue: integer);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
published
// Vertical offset for the caption relative to the icon center (0 = default centered)
property OffsetY: integer read FOffsetY write SetOffsetY default 0;
end;
implementation
constructor TFlatButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FOffsetY := 0;
Flat := True; // Always draw as a flat button
end;
procedure TFlatButton.SetOffsetY(AValue: integer);
begin
if FOffsetY = AValue then Exit;
FOffsetY := AValue;
Invalidate;
end;
procedure TFlatButton.Paint;
var
r: TRect;
xIcon, yIcon, xText: Integer;
ts: TTextStyle;
Details: TThemedElementDetails;
imgW, imgH: Integer;
textWidth, totalWidth, xStart: Integer;
gap: Integer;
begin
gap := 4;
// Toolbar theme elements give the native flat look
if Down then
Details := ThemeServices.GetElementDetails(ttbButtonPressed)
else if MouseInClient then
Details := ThemeServices.GetElementDetails(ttbButtonHot)
else
Details := ThemeServices.GetElementDetails(ttbButtonNormal);
// Draw the themed background
ThemeServices.DrawElement(Canvas.Handle, Details, ClientRect);
// Determine icon dimensions from ImageList or Glyph
if (ImageIndex >= 0) and (Images <> nil) then
begin
imgW := Images.Width;
imgH := Images.Height;
end
else if (Glyph <> nil) and (not Glyph.Empty) then
begin
imgW := Glyph.Width;
imgH := Glyph.Height;
end
else
begin
imgW := 0;
imgH := 0;
end;
// Calculate total content width
Canvas.Font.Assign(Font);
textWidth := Canvas.TextWidth(Caption);
if imgW > 0 then
totalWidth := imgW + gap + textWidth
else
totalWidth := textWidth;
// Horizontal alignment of the whole icon+text block
case Alignment of
taRightJustify:
xStart := ClientWidth - totalWidth - gap;
taCenter:
begin
xStart := (ClientWidth - totalWidth) div 2;
if xStart < gap then
xStart := gap;
end;
else // taLeftJustify
xStart := gap;
end;
// Draw the icon vertically centered
if imgW > 0 then
begin
xIcon := xStart;
yIcon := (ClientHeight - imgH) div 2;
if (ImageIndex >= 0) and (Images <> nil) then
Images.Draw(Canvas, xIcon, yIcon, ImageIndex, Enabled)
else if (Glyph <> nil) and (not Glyph.Empty) then
Canvas.Draw(xIcon, yIcon, Glyph);
xText := xIcon + imgW + gap;
end
else
xText := xStart;
// Caption rectangle shifted vertically by OffsetY
case Alignment of
taRightJustify:
r := Rect(xText, FOffsetY, ClientWidth - gap, ClientHeight + FOffsetY);
taCenter:
r := Rect(xText, FOffsetY, ClientWidth - xStart, ClientHeight + FOffsetY);
else // taLeftJustify
r := Rect(xText, FOffsetY, ClientWidth - gap, ClientHeight + FOffsetY);
end;
// Draw caption centered vertically inside the shifted rectangle
ts := Canvas.TextStyle;
ts.Alignment := taLeftJustify; // text always left-aligned inside its rect
ts.Layout := tlCenter;
Canvas.TextRect(r, r.Left, r.Top, Caption, ts);
end;
end.