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
| ///////////////////////////////////////////////////////////////////////////
// //
// 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 UserInterface.Selection._2d;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Geometry._2d.Integer32.Point,
Geometry._2d.Integer32.Rectangle,
Geometry._2d.Integer32.Size,
Integer._32,
Integer._32.Interval.List.OrderedDisjoint,
UserInterface.Selection;
type
T2dSelectionDimension = (None, Cells, Rows, Columns);
T2dSelectionDimensions = set of T2dSelectionDimension;
T2dSelectionPart = record
public
Selected: TBoolean;
Dimension: T2dSelectionDimension;
Block: TInteger32Rectangle;
function ContainsRow(Row: TInteger32): TBoolean;
function ContainsColumn(Column: TInteger32): TBoolean;
function ContainsCell(const Cell: TInteger32Point): TBoolean;
function Contains(const Point: TInteger32Point): TBoolean;
function AddRowsTo(const Intervals: TInteger32OrderedDisjointIntervalList): TInteger32OrderedDisjointIntervalList;
function AddColumnsTo(const Intervals: TInteger32OrderedDisjointIntervalList): TInteger32OrderedDisjointIntervalList;
end;
T2dSelectionParts = array of T2dSelectionPart;
T2dSelectionBlocks = record
private
FBlocks: array of TInteger32Rectangle;
function GetBlocks(Index: TInteger32): TInteger32Rectangle;
public
procedure Clear();
function Count(): TInteger32;
procedure AddBlock(const Block: TInteger32Rectangle);
procedure AddBlocks(const BlocksToAdd: T2dSelectionBlocks);
function AddSelectedBlock(const Block: TInteger32Rectangle): T2dSelectionBlocks;
function RemoveSelectedBlock(const Block: TInteger32Rectangle): T2dSelectionBlocks;
function AddOrRemoveBlock(const Block: TInteger32Rectangle; Selected: TBoolean): T2dSelectionBlocks;
function SubtractBlock(Original, Subtract: TInteger32Rectangle): T2dSelectionBlocks;
property Blocks[Index: TInteger32]: TInteger32Rectangle read GetBlocks; default;
end;
T2dSelection = class(TObject)
private
FMode: TSelectionMode;
FStart: TInteger32Point;
FSize: TInteger32Size;
FSelectionType: TBoolean;
FStartDimension: T2dSelectionDimension;
FEndDimension: T2dSelectionDimension;
Selections: T2dSelectionParts;
DigestUpdatesStack: array of TInteger32Rectangle;
procedure SetMode(const Value: TSelectionMode);
function GetSelected(const Point: TInteger32Point): TBoolean;
function GetSelectedRow(Row: TInteger32): TBoolean;
function GetSelectedColumn(Column: TInteger32): TBoolean;
function GetBlock(): TInteger32Rectangle;
procedure SetStart(const Point: TInteger32Point);
procedure SetSize(const Value: TInteger32Size);
function GetArea(): TInteger32;
procedure PushSelectionBlock();
procedure PopSelectionBlock();
function ResultingDimension(): T2dSelectionDimension;
public
constructor Create(); virtual;
procedure Clear();
procedure StartNew(Dimension: T2dSelectionDimension; const Point: TInteger32Point; KeepOld: TBoolean);
procedure Expand(Dimension: T2dSelectionDimension; const Point: TInteger32Point);
function GetSelectedCells(): T2dSelectionBlocks;
function GetSelectedRows(): TInteger32OrderedDisjointIntervalList;
function GetSelectedColumns(): TInteger32OrderedDisjointIntervalList;
function CurrentSelectionPart(): T2dSelectionPart;
property SelectionType: TBoolean read FSelectionType write FSelectionType;
property Start: TInteger32Point read FStart write SetStart;
property Size: TInteger32Size read FSize write SetSize;
property Area: TInteger32 read GetArea;
property Block: TInteger32Rectangle read GetBlock;
property Selected[const Point: TInteger32Point]: TBoolean read GetSelected;
property SelectedRow[Row: TInteger32]: TBoolean read GetSelectedRow;
property SelectedColumn[Column: TInteger32]: TBoolean read GetSelectedColumn;
property Mode: TSelectionMode read FMode write SetMode;
end;
implementation
uses
Event.DistributionCenter,
Geometry._2d.Dimension,
Integer._32.Interval;
{ T2dSelection }
procedure T2dSelection.Clear();
begin
SetLength(Selections, 0);
Size := TInteger32Size.New(0, 0);
SelectionType := True;
FStartDimension := T2dSelectionDimension.Cells;
end;
constructor T2dSelection.Create();
begin
inherited Create();
FMode := TSelectionMode.Single;
StartNew(T2dSelectionDimension.Cells, TInteger32Point.New(0, 0), False);
end;
function T2dSelection.CurrentSelectionPart(): T2dSelectionPart;
begin
Result.Selected := SelectionType;
Result.Block := Block;
Result.Dimension := ResultingDimension;
end;
procedure T2dSelection.Expand(Dimension: T2dSelectionDimension; const Point: TInteger32Point);
begin
Size := TInteger32Size.New(Point.X - Start.X, Point.Y - Start.Y);
FEndDimension := Dimension;
end;
function T2dSelection.GetSelected(const Point: TInteger32Point): TBoolean;
var
Found: TBoolean;
i: TInteger32;
Selected: TBoolean;
CurrentPart: T2dSelectionPart;
begin
CurrentPart := CurrentSelectionPart;
Found := CurrentPart.Contains(Point);
Selected := CurrentPart.Selected;
if System.Length(Selections) > 0 then
begin
i := High(Selections);
while not Found and (i >= 0) do
begin
Found := Selections[i].Contains(Point);
Selected := Selections[i].Selected;
Dec(i);
end;
end;
Result := Found and Selected;
end;
function T2dSelection.GetSelectedCells(): T2dSelectionBlocks;
var
i: TInteger32;
Selection: T2dSelectionPart;
begin
Result.Clear();
if ResultingDimension = T2dSelectionDimension.Cells then
begin
for i := 0 to High(Selections) do
begin
Selection := Selections[i];
Result := Result.AddOrRemoveBlock(Selection.Block, Selection.Selected);
end;
Result := Result.AddOrRemoveBlock(Block, SelectionType);
end;
end;
function T2dSelection.GetSelectedColumns(): TInteger32OrderedDisjointIntervalList;
var
i: TInteger32;
Selection: T2dSelectionPart;
CurrentPart: T2dSelectionPart;
begin
Result := TInteger32OrderedDisjointIntervalList.New();
if ResultingDimension = T2dSelectionDimension.Columns then
begin
for i := 0 to High(Selections) do
begin
Selection := Selections[i];
Result := Selection.AddColumnsTo(Result);
end;
CurrentPart := CurrentSelectionPart();
Result := CurrentPart.AddColumnsTo(Result);
end;
end;
function T2dSelection.GetSelectedRows(): TInteger32OrderedDisjointIntervalList;
var
i: TInteger32;
Selection: T2dSelectionPart;
CurrentPart: T2dSelectionPart;
begin
Result := TInteger32OrderedDisjointIntervalList.New();
if ResultingDimension = T2dSelectionDimension.Rows then
begin
for i := 0 to High(Selections) do
begin
Selection := Selections[i];
Result := Selection.AddRowsTo(Result);
end;
CurrentPart := CurrentSelectionPart();
Result := CurrentPart.AddRowsTo(Result);
end;
end;
function T2dSelection.GetSelectedRow(Row: TInteger32): TBoolean;
var
Found: TBoolean;
i: TInteger32;
Selected: TBoolean;
CurrentPart: T2dSelectionPart;
begin
CurrentPart := CurrentSelectionPart;
Found := CurrentPart.ContainsRow(Row);
Selected := CurrentPart.Selected;
if System.Length(Selections) > 0 then
begin
i := High(Selections);
while not Found and (i >= 0) do
begin
Found := Selections[i].ContainsRow(Row);
Selected := Selections[i].Selected;
Dec(i);
end;
end;
Result := Found and Selected;
end;
function T2dSelection.GetSelectedColumn(Column: TInteger32): TBoolean;
var
Found: TBoolean;
i: TInteger32;
Selected: TBoolean;
CurrentPart: T2dSelectionPart;
begin
CurrentPart := CurrentSelectionPart;
Found := CurrentPart.ContainsColumn(Column);
Selected := CurrentPart.Selected;
if System.Length(Selections) > 0 then
begin
i := High(Selections);
while not Found and (i >= 0) do
begin
Found := Selections[i].ContainsColumn(Column);
Selected := Selections[i].Selected;
Dec(i);
end;
end;
Result := Found and Selected;
end;
function T2dSelection.GetArea(): TInteger32;
var
Bounds: TInteger32Rectangle;
begin
Bounds := Block;
Bounds := Bounds.Absolute();
Result := Bounds.GetArea();
end;
function T2dSelection.GetBlock(): TInteger32Rectangle;
begin
if Size.Height >= 0 then
begin
Result.Top := Start.Y;
Result.Bottom := Start.Y + Size.Height;
end
else
begin
Result.Top := Start.Y + Size.Height;
Result.Bottom := Start.Y;
end;
if Size.Width >= 0 then
begin
Result.Left := Start.X;
Result.Right := Start.X + Size.Width;
end
else
begin
Result.Left := Start.X + Size.Width;
Result.Right := Start.X;
end;
end;
procedure T2dSelection.PopSelectionBlock();
var
OldBlock: TInteger32Rectangle;
NewBlock: TInteger32Rectangle;
begin
NewBlock := Block;
OldBlock := DigestUpdatesStack[High(DigestUpdatesStack)];
System.SetLength(DigestUpdatesStack, System.Length(DigestUpdatesStack) - 1);
if (System.Length(DigestUpdatesStack) = 0) and (OldBlock <> NewBlock) then
TEventDistributionCenter.Instance.PostChanged(Self);
end;
procedure T2dSelection.PushSelectionBlock();
begin
System.SetLength(DigestUpdatesStack, System.Length(DigestUpdatesStack) + 1);
DigestUpdatesStack[High(DigestUpdatesStack)] := Block;
end;
function T2dSelection.ResultingDimension(): T2dSelectionDimension;
begin
if FStartDimension > FEndDimension then
Result := FStartDimension
else
Result := FEndDimension;
end;
procedure T2dSelection.SetMode(const Value: TSelectionMode);
begin
if Value <> Mode then
begin
FMode := Value;
if Mode = TSelectionMode.Single then
Size := TInteger32Size.New(0, 0);
end;
end;
procedure T2dSelection.SetSize(const Value: TInteger32Size);
begin
PushSelectionBlock();
if Mode = TSelectionMode.Single then
FStart := TInteger32Point.New(Start.X + Value.Width, Start.Y + Value.Height)
else
FSize := Value;
PopSelectionBlock();
end;
procedure T2dSelection.SetStart(const Point: TInteger32Point);
begin
PushSelectionBlock();
FStart := Point;
PopSelectionBlock();
end;
procedure T2dSelection.StartNew(Dimension: T2dSelectionDimension; const Point: TInteger32Point; KeepOld: TBoolean);
begin
PushSelectionBlock();
if (Mode = TSelectionMode.MultipleIntervals) and KeepOld then
begin
System.SetLength(Selections, System.Length(Selections) + 1);
Selections[High(Selections)] := CurrentSelectionPart;
SelectionType := not Selected[Point];
end
else
Clear();
Start := Point;
Size := TInteger32Size.New(0, 0);
FStartDimension := Dimension;
FEndDimension := Dimension;
PopSelectionBlock();
end;
{ T2dSelectionPart }
function T2dSelectionPart.AddRowsTo(const Intervals: TInteger32OrderedDisjointIntervalList): TInteger32OrderedDisjointIntervalList;
begin
if Selected then
Result := Intervals + Block.Range[T2dDimension.Vertical]
else
Result := Intervals - Block.Range[T2dDimension.Vertical];
end;
function T2dSelectionPart.AddColumnsTo(const Intervals: TInteger32OrderedDisjointIntervalList): TInteger32OrderedDisjointIntervalList;
begin
if Selected then
Result := Intervals + Block.Range[T2dDimension.Horizontal]
else
Result := Intervals - Block.Range[T2dDimension.Horizontal];
end;
function T2dSelectionPart.Contains(const Point: TInteger32Point): TBoolean;
begin
Result := ContainsCell(Point) or ContainsColumn(Point.X) or ContainsRow(Point.Y);
end;
function T2dSelectionPart.ContainsRow(Row: TInteger32): TBoolean;
var
Range: TInteger32Interval;
begin
Range := Block.Range[T2dDimension.Vertical];
Result := (Dimension = T2dSelectionDimension.Rows) and Range.Contains(Row);
end;
function T2dSelectionPart.ContainsCell(const Cell: TInteger32Point): TBoolean;
begin
Result := (Dimension = T2dSelectionDimension.Cells) and Block.Contains(Cell);
end;
function T2dSelectionPart.ContainsColumn(Column: TInteger32): TBoolean;
var
Range: TInteger32Interval;
begin
Range := Block.Range[T2dDimension.Horizontal];
Result := (Dimension = T2dSelectionDimension.Columns) and Range.Contains(Column);
end;
{ T2dSelectionBlocks }
procedure T2dSelectionBlocks.AddBlock(const Block: TInteger32Rectangle);
begin
System.SetLength(FBlocks, System.Length(FBlocks) + 1);
FBlocks[High(FBlocks)] := Block;
end;
procedure T2dSelectionBlocks.AddBlocks(const BlocksToAdd: T2dSelectionBlocks);
var
i: TInteger32;
BlockCount: TInteger32;
begin
BlockCount := System.Length(FBlocks);
System.SetLength(FBlocks, BlockCount + BlocksToAdd.Count);
for i := 0 to BlocksToAdd.Count - 1 do
FBlocks[BlockCount + i] := BlocksToAdd[i];
end;
function T2dSelectionBlocks.AddOrRemoveBlock(const Block: TInteger32Rectangle; Selected: TBoolean): T2dSelectionBlocks;
begin
if Selected then
Result := AddSelectedBlock(Block)
else
Result := RemoveSelectedBlock(Block);
end;
function T2dSelectionBlocks.AddSelectedBlock(const Block: TInteger32Rectangle): T2dSelectionBlocks;
begin
Result := RemoveSelectedBlock(Block);
Result.AddBlock(Block);
end;
procedure T2dSelectionBlocks.Clear();
begin
SetLength(FBlocks, 0);
end;
function T2dSelectionBlocks.Count(): TInteger32;
begin
Result := Length(FBlocks);
end;
function T2dSelectionBlocks.GetBlocks(Index: TInteger32): TInteger32Rectangle;
begin
Result := FBlocks[Index];
end;
function T2dSelectionBlocks.RemoveSelectedBlock(const Block: TInteger32Rectangle): T2dSelectionBlocks;
var
i: TInteger32;
begin
Result.Clear();
for i := 0 to Count - 1 do
Result.AddBlocks(SubtractBlock(Self[i], Block));
end;
function T2dSelectionBlocks.SubtractBlock(Original, Subtract: TInteger32Rectangle): T2dSelectionBlocks;
var
Intersection: TInteger32Rectangle;
begin
Intersection := Original.Intersect(Subtract);
Result.Clear();
if Intersection.IsValid then
begin
if Intersection.Top > Original.Top then
begin
Result.AddBlock(TInteger32Rectangle.New(Original.Left, Original.Top, Original.Right, Intersection.Top - 1));
Original.Top := Intersection.Top;
end;
if Intersection.Bottom < Original.Bottom then
begin
Result.AddBlock(TInteger32Rectangle.New(Original.Left, Intersection.Bottom + 1, Original.Right, Original.Bottom));
Original.Bottom := Intersection.Bottom;
end;
if Intersection.Left > Original.Left then
begin
Result.AddBlock(TInteger32Rectangle.New(Original.Left, Original.Top, Intersection.Left - 1, Original.Bottom));
Original.Left := Intersection.Left;
end;
if Intersection.Right < Original.Right then
begin
Result.AddBlock(TInteger32Rectangle.New(Intersection.Right + 1, Original.Top, Original.Right, Original.Bottom));
Original.Right := Intersection.Right;
end;
end
else
Result.AddBlock(Original);
end;
end.
|