forked from zsdwxm/KernowSoftwareFMX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksSegmentButtons.pas
422 lines (365 loc) · 12.4 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
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
{*******************************************************************************
* *
* TksSegmentButtons - Segment button selection component *
* *
* https://github.com/gmurt/KernowSoftwareFMX *
* *
* Copyright 2015 Graham Murt *
* *
* email: [email protected] *
* *
* 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 ksSegmentButtons;
interface
{$I ksComponents.inc}
uses
Classes, FMX.Types, FMX.Controls, FMX.Graphics, Types, System.UITypes,
FMX.StdCtrls, System.Generics.Collections, FMX.Objects, FMX.Effects,
System.UIConsts, ksSpeedButton, ksTypes;
type
TksSegmentButton = class;
TksSegmentButtons = class;
TksSelectSegmentButtonEvent = procedure(Sender: TObject; AIndex: integer; AButton: TksSegmentButton) of object;
TksSegmentSpeedButton = class(TksSpeedButton)
public
constructor Create(AOwner: TComponent); override;
end;
TKsSegmentButton = class(TCollectionItem)
private
FButton: TksSegmentSpeedButton;
FID: string;
FText: string;
procedure SetText(const Value: string);
function GetBadgeValue: integer;
procedure SetBadgeValue(const Value: integer);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
property ID: string read FID write FID;
property Text: string read FText write SetText;
property BadgeValue: integer read GetBadgeValue write SetBadgeValue;
end;
TksSegmentButtonCollection = class(TCollection)
private
FSegmentButtons: TKsSegmentButtons;
protected
function GetOwner: TPersistent; override;
function GetItem(Index: Integer): TKsSegmentButton;
procedure SetItem(Index: Integer; Value: TKsSegmentButton);
procedure Update(Item: TCollectionItem); override;
public
constructor Create(AButtons: TKsSegmentButtons);
function Add: TKsSegmentButton;
function Insert( Index: Integer ): TKsSegmentButton;
property Items[index: Integer]: TKsSegmentButton read GetItem write SetItem; default; // default - Added by Fenistil
end;
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or
{$IFDEF XE8_OR_NEWER} pidiOSDevice32 or pidiOSDevice64
{$ELSE} pidiOSDevice {$ENDIF} or pidiOSSimulator or pidAndroid)]
TksSegmentButtons = class(TksControl)
private
FGroupID: string;
FItemIndex: integer;
FBtnWidth: single;
FOnChange: TNotifyEvent;
FSegments: TksSegmentButtonCollection;
FTintColor: TAlphaColor;
FBackgroundColor: TAlphaColor;
FOnSelectSegment: TksSelectSegmentButtonEvent;
procedure UpdateButtons;
procedure SetItemIndex(const Value: integer);
procedure SetSegments(const Value: TksSegmentButtonCollection);
procedure SetBackgroundColor(const Value: TAlphaColor);
procedure SetTintColor(const Value: TAlphaColor);
function GetSelected: TKsSegmentButton;
function GetSelectedID: string;
procedure SetSelectedID(const Value: string);
protected
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
property Selected: TKsSegmentButton read GetSelected;
property SelectedID: string read GetSelectedID write SetSelectedID;
published
property Align;
property ItemIndex: integer read FItemIndex write SetItemIndex default -1;
property Margins;
property Position;
property Width;
property TintColor: TAlphaColor read FTintColor write SetTintColor default claNull;
property BackgroundColor: TAlphaColor read FBackgroundColor write SetBackgroundColor default claNull;
property Segments: TksSegmentButtonCollection read FSegments write SetSegments;
property Size;
property Height;
property Visible;
// events
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnSelectSegment: TksSelectSegmentButtonEvent read FOnSelectSegment write FOnSelectSegment;
end;
{$R *.dcr}
procedure Register;
implementation
uses SysUtils, Math;
procedure Register;
begin
RegisterComponents('Kernow Software FMX', [TksSegmentButtons]);
end;
{ TKsSegmentButton }
procedure TKsSegmentButton.Assign(Source: TPersistent);
begin
if (Source is TKsSegmentButton) then
begin
FID := (Source as TKsSegmentButton).ID;
FText := (Source as TKsSegmentButton).Text;
end;
end;
constructor TKsSegmentButton.Create(Collection: TCollection);
begin
inherited Create(Collection);
FButton := TksSegmentSpeedButton.Create((Collection as TksSegmentButtonCollection).FSegmentButtons);
end;
destructor TKsSegmentButton.Destroy;
begin
{$IFDEF NEXTGEN}
FButton.DisposeOf;
{$ELSE}
FButton.Free;
{$ENDIF}
inherited;
end;
function TKsSegmentButton.GetBadgeValue: integer;
begin
Result := FButton.Badge.Value;
end;
procedure TKsSegmentButton.SetBadgeValue(const Value: integer);
begin
FButton.Badge.Value := Value;
end;
procedure TKsSegmentButton.SetText(const Value: string);
begin
FText := Value;
(Collection as TksSegmentButtonCollection).FSegmentButtons.UpdateButtons;
end;
{ TksSegmentButtons }
procedure TksSegmentButtons.Assign(Source: TPersistent);
begin
if (Source is TksSegmentButtons) then
begin
FGroupID := (Source as TksSegmentButtons).FGroupID;
FItemIndex := (Source as TksSegmentButtons).FItemIndex;
FBtnWidth := (Source as TksSegmentButtons).FBtnWidth;
FOnChange := (Source as TksSegmentButtons).FOnChange;
FTintColor := (Source as TksSegmentButtons).FTintColor;
FBackgroundColor := (Source as TksSegmentButtons).FBackgroundColor;
FSegments.Assign((Source as TksSegmentButtons).Segments);
end;
end;
constructor TksSegmentButtons.Create(AOwner: TComponent);
var
AGuid: TGUID;
begin
inherited;
FSegments := TksSegmentButtonCollection.Create(Self);
SetAcceptsControls(False);
CreateGUID(AGuid);
FGroupID := AGuid.ToString;
FGroupID := StringReplace(FGroupID, '{', '', [rfReplaceAll]);
FGroupID := StringReplace(FGroupID, '-', '', [rfReplaceAll]);
FGroupID := StringReplace(FGroupID, '}', '', [rfReplaceAll]);
FBackgroundColor := claNull;
FTintColor := claNull;
Size.Height := 50;
Size.Width := 300;
end;
destructor TksSegmentButtons.Destroy;
begin
FreeAndNil(FSegments);
inherited;
end;
function TksSegmentButtons.GetSelected: TKsSegmentButton;
begin
Result := nil;
if (FItemIndex > -1) and (FItemIndex < FSegments.Count) then
Result := FSegments[FItemIndex];
end;
function TksSegmentButtons.GetSelectedID: string;
begin
Result := '';
if Selected <> nil then
Result := Selected.ID;
end;
procedure TksSegmentButtons.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
inherited;
ItemIndex := Min(Trunc(X / FBtnWidth), Segments.Count-1);
{$IFDEF MSWINDOWS}
Tap(PointF(x, y));
{$ENDIF}
end;
procedure TksSegmentButtons.Paint;
begin
inherited;
if (csDesigning in ComponentState) then
begin
DrawDesignBorder(claDimgray, claDimgray);
if FSegments.Count = 0 then
Canvas.FillText(ClipRect, 'Add "Segments" in the Object Inspector', True, 1, [], TTextAlign.Center);
end;
if FSegments.Count > 0 then
begin
{if Height <> (FSegments[0].FButton.Height + 16) then
begin
Height := FSegments[0].FButton.Height + 16;
UpdateExplicitBounds;
end;}
end;
end;
procedure TksSegmentButtons.Resize;
begin
inherited;
UpdateButtons;
end;
procedure TksSegmentButtons.UpdateButtons;
var
ICount: integer;
begin
if FSegments.Count = 0 then
Exit;
FBtnWidth := (Width-16) / FSegments.Count;
for ICount := 0 to FSegments.Count-1 do
begin
if Assigned(FSegments[ICount].FButton) then
begin
if ContainsObject(FSegments[ICount].FButton) = False then
AddObject(FSegments[ICount].FButton);
with FSegments[ICount].FButton do
begin
StaysPressed := True;
GroupName := FGroupID;
Width := FBtnWidth;
TintColor := FTintColor;
if FItemIndex = ICount then
TextSettings.FontColor := FBackgroundColor
else
TextSettings.FontColor := FTintColor;
IsPressed := (ICount = FItemIndex);
Text := FSegments[ICount].Text;
{$IFDEF ANDROID}
StyleLookup := 'listitembutton';
Height := 30;
{$ELSE}
if ICount = 0 then StyleLookup := 'segmentedbuttonleft';
if ICount > 0 then StyleLookup := 'segmentedbuttonmiddle';
if ICount = FSegments.Count-1 then StyleLookup := 'segmentedbuttonright';
if FSegments.Count = 1 then StyleLookup := 'listitembutton';
{$ENDIF}
Position.Y := (Self.Height - Height) / 2;
Position.X := (ICount * FBtnWidth)+8;
end;
end;
end;
end;
procedure TksSegmentButtons.SetBackgroundColor(const Value: TAlphaColor);
begin
FBackgroundColor := Value;
UpdateButtons;
end;
procedure TksSegmentButtons.SetItemIndex(const Value: integer);
begin
if FItemIndex <> Value then
begin
FItemIndex := Value;
UpdateButtons;
if Assigned(FOnChange) then
FOnChange(Self);
if FItemIndex > -1 then
begin
if Assigned(FOnSelectSegment) then
FOnSelectSegment(Self, FItemIndex, Segments[FItemIndex]);
end;
end;
end;
procedure TksSegmentButtons.SetSegments(const Value: TksSegmentButtonCollection);
begin
FSegments.Assign(Value);
end;
procedure TksSegmentButtons.SetSelectedID(const Value: string);
var
ICount: integer;
begin
for ICount := 0 to Segments.Count-1 do
begin
if Segments[ICount].ID = Value then
begin
ItemIndex := ICount;
Exit;
end;
end;
ItemIndex := -1;
end;
procedure TksSegmentButtons.SetTintColor(const Value: TAlphaColor);
begin
FTintColor := Value;
UpdateButtons;
end;
{ TksSegmentButtonCollection }
function TksSegmentButtonCollection.Add: TKsSegmentButton;
begin
Result := inherited Add as TKsSegmentButton;
FSegmentButtons.UpdateButtons;
end;
constructor TksSegmentButtonCollection.Create(AButtons: TKsSegmentButtons);
begin
inherited Create(TKsSegmentButton);
FSegmentButtons := AButtons;
end;
function TksSegmentButtonCollection.GetItem(Index: Integer): TKsSegmentButton;
begin
Result := inherited Items[index] as TKsSegmentButton;
end;
function TksSegmentButtonCollection.GetOwner: TPersistent;
begin
Result := FSegmentButtons;
end;
function TksSegmentButtonCollection.Insert(Index: Integer): TKsSegmentButton;
begin
Result := inherited insert( index ) as TKsSegmentButton;
FSegmentButtons.UpdateButtons;
end;
procedure TksSegmentButtonCollection.SetItem(Index: Integer; Value: TKsSegmentButton);
begin
inherited SetItem(index, Value);
end;
procedure TksSegmentButtonCollection.Update(Item: TCollectionItem);
begin
inherited;
(Owner as TksSegmentButtons).UpdateButtons;
end;
{ TksSegmentSpeedButton }
constructor TksSegmentSpeedButton.Create(AOwner: TComponent);
begin
inherited;
Locked := True;
Stored := False;
HitTest := False;
StyledSettings := [];
end;
end.