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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit CumulativeSizes;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Integer._32;
type
TDeltaSizeMethod = (Distribute, LastColumn, FirstColumn);
TCumulativeSizes = class(TObject)
protected
FFirstVisible: TInteger32;
FLastVisible: TInteger32;
FFixedTotal: TBoolean;
function GetWeight(Index: TInteger32): TInteger32; virtual; abstract;
procedure SetWeight(Index: TInteger32; const Value: TInteger32); virtual; abstract;
function GetCount(): TInteger32; virtual; abstract;
procedure SetCount(const Value: TInteger32); virtual; abstract;
procedure SetVisible(Index: TInteger32; const Value: TBoolean); virtual; abstract;
function IndexWithRunningSum(RunningSum: TInteger32): TInteger32; virtual; abstract;
public
DefaultSize: TInteger32;
procedure Insert(Index: TInteger32; Value: TInteger32); virtual; abstract;
procedure Remove(Index: TInteger32); virtual; abstract;
procedure SetItemSizeAndRedistribute(Item: TInteger32; Size: TInteger32; Redistribute: TBoolean = True); virtual; abstract;
procedure SetItemSize(Item: TInteger32; Size: TInteger32; InPlace: TBoolean); virtual; abstract;
function ValidItemWithPosition(Position: TInteger32): TInteger32;
function ItemWithPosition(Position: TInteger32): TInteger32;
function ItemWithResizePosition(Position: TInteger32; Radius: TInteger32): TInteger32;
function ItemWithInsertPosition(Position: TInteger32): TInteger32;
function Size(Item: TInteger32): TInteger32; virtual; abstract;
function RunningSize(Item: TInteger32): TInteger32; virtual; abstract;
function TotalSize(): TInteger32; virtual; abstract;
procedure SetTotalSize(Value: TInteger32; DeltaSizeMethod: TDeltaSizeMethod); virtual; abstract;
function TotalScrollSize(AvailableSize: TInteger32): TInteger32;
function LowerBound(Item: TInteger32): TInteger32;
function Last(): TInteger32; // TODO: Move to an other object for headers, this is a Sizes object
procedure DetermineVisible(Offset: TInteger32; AvailableSpace: TInteger32);
function HasValidVisibleItems: TBoolean;
function Validate(Index: TInteger32): TInteger32; // TODO: Move to an other object for headers, this is a Sizes object
function IsVisible(Item: TInteger32): TBoolean;
function VisibleCount: TInteger32; deprecated; // Move to Visible interval
procedure ConfigureAsFixedTotalWithRelativeWeights(TotalSize: TInteger32);
property Count: TInteger32 read GetCount write SetCount;
property FirstVisible: TInteger32 read FFirstVisible;
property LastVisible: TInteger32 read FLastVisible;
property FixedTotal: TBoolean read FFixedTotal write FFixedTotal;
property Weight[Index: TInteger32]: TInteger32 read GetWeight write SetWeight;
property Visible[Index: TInteger32]: TBoolean write SetVisible;
end;
implementation
uses
Mathematics;
{ TCumulativeSizes }
procedure TCumulativeSizes.ConfigureAsFixedTotalWithRelativeWeights(TotalSize: TInteger32);
var
i: TInteger32;
begin
FixedTotal := TotalSize = Self.TotalSize();
if FixedTotal then
for i := 0 to Count - 1 do
Weight[i] := Size(i);
end;
procedure TCumulativeSizes.DetermineVisible(Offset: TInteger32; AvailableSpace: TInteger32);
begin
if Count > 0 then
begin
FFirstVisible := Minimum(IndexWithRunningSum(Offset), Count - 1);
FLastVisible := Minimum(IndexWithRunningSum(Offset + AvailableSpace), Count - 1);
end
else
begin
FFirstVisible := 0;
FLastVisible := -1;
end;
end;
function TCumulativeSizes.HasValidVisibleItems: TBoolean;
begin
Result := (FirstVisible <> -1) and (LastVisible <> -1);
end;
function TCumulativeSizes.IsVisible(Item: TInteger32): TBoolean;
begin
Result := (Item >= FirstVisible) and (Item <= LastVisible);
end;
function TCumulativeSizes.ItemWithInsertPosition(Position: TInteger32): TInteger32;
begin
if Position < 0 then
Result := 0
else if Position > TotalSize then
Result := Count
else
begin
Result := IndexWithRunningSum(Position);
if Position - LowerBound(Result) > Size(Result) div 2 then
Inc(Result);
end;
end;
function TCumulativeSizes.ItemWithPosition(Position: TInteger32): TInteger32;
begin
Result := IndexWithRunningSum(Position);
if Result >= Count then
Result := -1;
end;
function TCumulativeSizes.ItemWithResizePosition(Position: TInteger32; Radius: TInteger32): TInteger32;
var
Item: TInteger32;
RunningSize: TInteger32;
begin
Item := ItemWithPosition(Position);
Result := -1;
if Item <> -1 then
begin
RunningSize := Self.RunningSize(Item);
if Position >= RunningSize - Radius then
Result := Item
else if (Item > 0) and (Position <= RunningSize - Size(Item) + Radius) then
Result := Item - 1;
end;
end;
function TCumulativeSizes.Last: TInteger32;
begin
Result := Count - 1;
end;
function TCumulativeSizes.LowerBound(Item: TInteger32): TInteger32;
begin
if Item > 0 then
Result := RunningSize(Item - 1)
else
Result := 0;
end;
function TCumulativeSizes.TotalScrollSize(AvailableSize: TInteger32): TInteger32;
begin
if FixedTotal then
Result := AvailableSize // TODO: We might want to take minimum sizes into account
else
Result := TotalSize;
end;
function TCumulativeSizes.Validate(Index: TInteger32): TInteger32;
begin
if Index >= Count then
Result := Count - 1
else if (Index < 0) and (Count > 0) then
Result := 0
else
Result := Index;
end;
function TCumulativeSizes.ValidItemWithPosition(Position: TInteger32): TInteger32;
begin
Result := IndexWithRunningSum(Position);
if Result >= Count then
Result := Count - 1
else if Result < 0 then
Result := 0;
end;
function TCumulativeSizes.VisibleCount: TInteger32;
begin
Result := LastVisible - FirstVisible + 1;
end;
end.
|