-
Notifications
You must be signed in to change notification settings - Fork 37
/
ksSegmentButtons.pas
223 lines (191 loc) · 5.13 KB
/
ksSegmentButtons.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
unit ksSegmentButtons;
interface
uses
Classes, FMX.Types, FMX.Controls, FMX.Graphics, Types, System.UITypes,
FMX.StdCtrls, System.Generics.Collections;
type
TKsSegmentButton = class(TSpeedButton)
private
FId: string;
protected
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
public
constructor Create(AOwner: TComponent); override;
property ID: string read FId write FId;
end;
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or pidiOSDevice)]
TksSegmentButtons = class(TControl)
private
FButtonIndex: integer;
FGroupID: string;
FItemIndex: integer;
FOnChange: TNotifyEvent;
procedure ResizeButtons;
procedure SetItemIndex(const Value: integer);
function GetSelectedCaption: string;
procedure SetSelectedCaption(const Value: string);
function GetSelectedID: string;
procedure DoButtonClick(Sender: TObject);
function GetButton(index: integer): TKsSegmentButton;
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AddButton(ATitle, AID: string);
procedure Clear;
procedure SelectFirst;
property SelectedCaption: string read GetSelectedCaption write SetSelectedCaption;
property SelectedID: string read GetSelectedID;
property Button[index: integer]: TKsSegmentButton read GetButton; default;
published
property ItemIndex: integer read FItemIndex write SetItemIndex default -1;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property Position;
property Width;
property Height;
property Visible;
end;
procedure Register;
implementation
uses SysUtils;
procedure Register;
begin
RegisterComponents('Kernow Software FMX', [TksSegmentButtons]);
end;
{ TksSegmentButtons }
procedure TksSegmentButtons.AddButton(ATitle, AID: string);
var
ABtn: TKsSegmentButton;
begin
ABtn := TKsSegmentButton.Create(Self);
ABtn.Text := ATitle;
ABtn.StaysPressed := True;
ABtn.ID := AId;
ABtn.GroupName := FGroupID;
ABtn.OnClick := DoButtonClick;
AddObject(ABtn);
if FItemIndex = -1 then
FItemIndex := 0;
ResizeButtons;
end;
procedure TksSegmentButtons.Clear;
var
ICount: integer;
ABtn: TKsSegmentButton;
begin
for ICount := ChildrenCount - 1 downto 0 do
begin
ABtn := Children[ICount] as TKsSegmentButton;
RemoveObject(ABtn);
ABtn.DisposeOf;
end;
FItemIndex := -1;
end;
constructor TksSegmentButtons.Create(AOwner: TComponent);
var
AGuid: TGUID;
begin
inherited Create(AOwner);
CreateGUID(AGuid);
FGroupID := GUIDToString(AGuid);
Width := 200;
Height := 40;
FItemIndex := -1;
Clear;
end;
destructor TksSegmentButtons.Destroy;
begin
inherited;
end;
procedure TksSegmentButtons.DoButtonClick(Sender: TObject);
begin
FItemIndex := Children.IndexOf(Sender as TFmxObject);
if Assigned(FOnChange) then
FOnChange(Self);
end;
function TksSegmentButtons.GetButton(index: integer): TKsSegmentButton;
begin
Result := (Children[index] as TKsSegmentButton);
end;
function TksSegmentButtons.GetSelectedCaption: string;
begin
Result := '';
if ItemIndex > -1 then
Result := (Children[ItemIndex] as TKsSegmentButton).Text;
end;
function TksSegmentButtons.GetSelectedID: string;
begin
Result := '';
if ItemIndex > -1 then
Result := (Children[ItemIndex] as TKsSegmentButton).ID;
end;
procedure TksSegmentButtons.Paint;
begin
inherited;
if csDesigning in ComponentState then
DrawDesignBorder;
end;
procedure TksSegmentButtons.ResizeButtons;
var
ABtnWidth: Single;
ICount: integer;
AXpos: single;
ABtn: TKsSegmentButton;
begin
inherited;
if csDesigning in ComponentState then
Exit;
AXPos := 0;
ABtnWidth := Width / ChildrenCount;
for ICount := 0 to ChildrenCount-1 do
begin
ABtn := Children[ICount] as TKsSegmentButton;
ABtn.Position.X := AXPos;
ABtn.Position.Y := 0;
ABtn.Width := ABtnWidth;
ABtn.Height := Height;
ABtn.IsPressed := FItemIndex = ICount;
ABtn.StyleLookup := 'segmentedbuttonmiddle';
if ICount = 0 then ABtn.StyleLookup := 'segmentedbuttonleft';
if ICount = ChildrenCount-1 then
begin
ABtn.StyleLookup := 'segmentedbuttonright';
ABtn.Position.X := ABtn.Position.X - 1;
end;
AXPos := AXpos + ABtnWidth;
end;
end;
procedure TksSegmentButtons.SelectFirst;
begin
if ChildrenCount > 0 then
ItemIndex := 0;
end;
procedure TksSegmentButtons.SetItemIndex(const Value: integer);
begin
(Children[Value] as TKsSegmentButton).IsPressed := True;
FItemIndex := Value;
ResizeButtons;
end;
procedure TksSegmentButtons.SetSelectedCaption(const Value: string);
var
ICount: integer;
begin
for ICount := 0 to ChildrenCount-1 do
if (Children[ICount] as TKsSegmentButton).Text = Value then
(Children[ICount] as TKsSegmentButton).IsPressed := True;
end;
constructor TKsSegmentButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
StaysPressed := True;
end;
procedure TKsSegmentButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
begin
inherited;
IsPressed := True;
end;
initialization
Classes.RegisterClass(TKsSegmentButton);
end.