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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
| ///////////////////////////////////////////////////////////////////////////
// //
// 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.Control.Scroll.Panel;
interface
uses
Boolean,
Collection.List,
Event,
Geometry._2d.Integer32.Point,
Geometry._2d.Integer32.Size,
Integer._32,
Text,
UserInterface.Control,
UserInterface.Control.Panel,
UserInterface.Control.Scroll.Bar,
UserInterface.Control.Scroll.Model,
UserInterface.Control.ViewPort,
UserInterface.Keyboard.Modifiers,
UserInterface.Navigation;
type
TScrollBarsVisibility = record
public
VerticalVisible: TBoolean;
HorizontalVisible: TBoolean;
end;
TScrollPanel = class(TPanel)
private
FHorizontalScrollBar: TScrollBar;
FVerticalScrollBar: TScrollBar;
FVerticalScrollPolicy: TScrollPolicy;
FHorizontalScrollPolicy: TScrollPolicy;
FGridSize: TInteger32Size;
FMainPanel: TInteger32Point;
FPanels: TList<TList<TControl>>;
procedure SetVerticalScrollPolicy(const Value: TScrollPolicy);
procedure SetHorizontalScrollPolicy(const Value: TScrollPolicy);
procedure SetMainPanel(const Value: TInteger32Point);
procedure SetGridSize(const Size: TInteger32Size);
function GetPanel(const Position: TInteger32Point): TControl;
procedure SetPanel(const Location: TInteger32Point; Control: TControl);
private
LayingOutControls: TBoolean;
FocusedPanel: TControl;
procedure ValidateFocusedControl();
function DetermineVisibleScrollBarsForSize(AvailableSize: TInteger32Size): TScrollBarsVisibility;
procedure UpdateStyleFromMainPanel();
protected
procedure ReallocatePanels();
function GetCaption(): TText; override;
function GetCaptionVisible(): TBoolean; override;
procedure SetCaptionVisible(const Value: TBoolean); override;
protected
procedure ChildRethinksScrollBars(Child: TControl); override;
procedure DescendantFiresEvent(Descendant: TControl; Event: TEvent); override;
protected
function MainControl(): TControl;
function ViewPort(): TViewport;
protected
function ControlCanBeEntered(Control: TControl): TBoolean;
function ControlAbove(Location: TInteger32Point): TControl;
function ControlBelow(Location: TInteger32Point): TControl;
function ControlAtLeft(Location: TInteger32Point): TControl;
function ControlAtRight(Location: TInteger32Point): TControl;
protected
function Navigate(Direction: TNavigationDirection; KeyModifiers: TKeyModifiers): TBoolean; override;
function GetFocusedPanelLocation(): TInteger32Point;
procedure DescendantRequestsFocusInComposition(Descendant: TControl); override;
protected
function GetPreferredColumnWidth(Column: TInteger32): TInteger32;
function GetPreferredRowHeight(Row: TInteger32): TInteger32;
function GetFixedSize(): TInteger32Size;
function GetAvailableSize(): TInteger32Size;
procedure DetermineVisibleScrollBars();
procedure RepositionScrollBars();
procedure SetViewPortSize(const Size: TInteger32Size);
procedure AdjustScrollModel(Model: TScrollModel; AvailableSize: TInteger32; ScrollSize: TInteger32);
procedure UpdateViewPorts(ScrollModel: TScrollModel);
protected
function GetVerticalScrollBarIsVisible(): TBoolean;
procedure SetVerticalScrollBarIsVisible(Value: TBoolean);
function GetHorizontalScrollBarIsVisible(): TBoolean;
procedure SetHorizontalScrollBarIsVisible(Value: TBoolean);
procedure SetScrollBarIsVisible(ScrollBar: TScrollBar; Value: TBoolean);
function NeedsVerticalScrollBar(AvailableSize: TInteger32Size): TBoolean;
function NeedsHorizontalScrollBar(AvailableSize: TInteger32Size): TBoolean;
property VerticalScrollBarIsVisible: TBoolean read GetVerticalScrollBarIsVisible write SetVerticalScrollBarIsVisible;
property HorizontalScrollBarIsVisible: TBoolean read GetHorizontalScrollBarIsVisible write SetHorizontalScrollBarIsVisible;
protected
procedure SetHorizontalViewPortOffsets();
procedure SetVerticalViewPortOffsets();
procedure HandleViewPortMoved(ViewPort: TViewport);
function GetViewPortPanel(ViewPort: TViewport): TInteger32Point;
public
constructor Create(); override;
destructor Destroy(); override;
public
procedure LayoutControls(); override;
procedure SizeChanged(); override;
function HandleEvent(Event: TEvent): TBoolean; override;
public
function DefaultControlToFocus(): TControl; override;
function CompositeControlHasFocus(): TBoolean; override;
function ChildHasCompositionFocus(ChildControl: TControl): TBoolean; override;
function ChildWithCompositionFocus(): TControl; override;
function IsComposition(): TBoolean; override;
function GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size; override;
function NextInKeyboardInputChain(): TControl; override;
public
property GridSize: TInteger32Size read FGridSize write SetGridSize;
property MainPanel: TInteger32Point read FMainPanel write SetMainPanel;
property Panel[const Position: TInteger32Point]: TControl read GetPanel write SetPanel;
property VerticalScrollBar: TScrollBar read FVerticalScrollBar;
property HorizontalScrollBar: TScrollBar read FHorizontalScrollBar;
property VerticalScrollPolicy: TScrollPolicy read FVerticalScrollPolicy write SetVerticalScrollPolicy;
property HorizontalScrollPolicy: TScrollPolicy read FHorizontalScrollPolicy write SetHorizontalScrollPolicy;
end;
implementation
uses
CumulativeSizes.Sizable,
Geometry._2d.Dimension,
Geometry._2d.Integer32.Rectangle,
Mathematics,
Object_.List,
Reference.User,
UserInterface.Control.Event;
{ TScrollPanel }
procedure TScrollPanel.AdjustScrollModel(Model: TScrollModel; AvailableSize: TInteger32; ScrollSize: TInteger32);
var
Position: TInteger32;
begin
Position := Model.Position;
if Position > ScrollSize - AvailableSize then
Position := Maximum(0, ScrollSize - AvailableSize);
Model.SetScrollData(Maximum(0, ScrollSize), AvailableSize, Position);
end;
function TScrollPanel.ChildHasCompositionFocus(ChildControl: TControl): TBoolean;
begin
Result := ChildControl = FocusedPanel;
end;
procedure TScrollPanel.ChildRethinksScrollBars(Child: TControl);
begin
LayoutControls();
end;
function TScrollPanel.ChildWithCompositionFocus(): TControl;
begin
Result := FocusedPanel;
end;
function TScrollPanel.CompositeControlHasFocus(): TBoolean;
var
FocusManager: TKeyboardFocusManager;
FocusedControl: TControl;
begin
FocusManager := GetFocusManager();
if FocusManager <> nil then
begin
FocusedControl := FocusManager.FocusedControl;
Result := (FocusedControl <> nil) and (FocusedControl.GetRootEmbellishment().ParentControl = Self);
end
else
Result := False;
end;
function TScrollPanel.ControlAbove(Location: TInteger32Point): TControl;
begin
Result := nil;
Dec(Location.Y);
while (Result = nil) and (Location.Y >= 0) do
begin
if ControlCanBeEntered(Panel[Location]) then
Result := Panel[Location]
else
Dec(Location.Y);
end;
end;
function TScrollPanel.ControlAtLeft(Location: TInteger32Point): TControl;
begin
Result := nil;
Dec(Location.X);
while (Result = nil) and (Location.X >= 0) do
begin
if ControlCanBeEntered(Panel[Location]) then
Result := Panel[Location]
else
Dec(Location.X);
end;
end;
function TScrollPanel.ControlAtRight(Location: TInteger32Point): TControl;
begin
Result := nil;
Inc(Location.X);
while (Result = nil) and (Location.X < GridSize.Width) do
begin
if ControlCanBeEntered(Panel[Location]) then
Result := Panel[Location]
else
Inc(Location.X);
end;
end;
function TScrollPanel.ControlBelow(Location: TInteger32Point): TControl;
begin
Result := nil;
Inc(Location.Y);
while (Result = nil) and (Location.Y < GridSize.Height) do
begin
if ControlCanBeEntered(Panel[Location]) then
Result := Panel[Location]
else
Inc(Location.Y);
end;
end;
function TScrollPanel.ControlCanBeEntered(Control: TControl): TBoolean;
begin
// TODO: We might want to use an other criterion than control size to
// determine whether it can be entered
Result := (Control <> nil) and (Control.Height > 0) and (Control.Width > 0);
end;
constructor TScrollPanel.Create();
begin
inherited Create();
FVerticalScrollBar := TScrollBar.Create();
FVerticalScrollBar.Dimension := T2dDimension.Vertical;
FHorizontalScrollBar := TScrollBar.Create();
FHorizontalScrollBar.Dimension := T2dDimension.Horizontal;
VerticalScrollBar.Model := TScrollModel.Create();
HorizontalScrollBar.Model := TScrollModel.Create();
VerticalScrollBar.Model.Listeners.Add(Self);
HorizontalScrollBar.Model.Listeners.Add(Self);
FPanels := TObjectList<TList<TControl>>.Create();
GridSize := TInteger32Size.New(1, 1);
MainPanel := TInteger32Point.New(0, 0);
end;
function TScrollPanel.DefaultControlToFocus(): TControl;
begin
if FocusedPanel <> nil then
Result := FocusedPanel
else
Result := MainControl;
Result := Result.DefaultControlToFocus;
end;
procedure TScrollPanel.DescendantFiresEvent(Descendant: TControl; Event: TEvent);
var
ControlEvent: TControlEvent;
Control: TControl;
begin
if Event is TControlEvent then
begin
ControlEvent := Event as TControlEvent;
Control := ControlEvent.GetControl();
if Event is TControlGotFocusEvent then
FocusedPanel := Control
else if Event is TViewportMovedEvent then
HandleViewPortMoved(Descendant as TViewport)
else if Event is TControlStyleChanged then
begin
if (ViewPort <> nil) and (Control = MainControl) then
UpdateStyleFromMainPanel();
end;
end;
inherited DescendantFiresEvent(Descendant, Event);
end;
procedure TScrollPanel.DescendantRequestsFocusInComposition(Descendant: TControl);
var
PanelLosingFocus: TControl;
begin
PanelLosingFocus := FocusedPanel;
FocusedPanel := Descendant;
if FocusedPanel <> nil then
FocusedPanel.GainedFocusInComposition();
if PanelLosingFocus <> nil then
PanelLosingFocus.LostFocusInComposition();
if CompositeControlHasFocus then
Descendant.RequestFocus();
end;
destructor TScrollPanel.Destroy();
begin
FPanels.Free();
ChildControls.Remove(VerticalScrollBar);
ChildControls.Remove(HorizontalScrollBar);
VerticalScrollBar.Model.Free();
HorizontalScrollBar.Model.Free();
VerticalScrollBar.Free();
HorizontalScrollBar.Free();
inherited Destroy();
end;
procedure TScrollPanel.DetermineVisibleScrollBars();
var
ScrollBarsVisibility: TScrollBarsVisibility;
begin
ScrollBarsVisibility := DetermineVisibleScrollBarsForSize(GetAvailableSize());
VerticalScrollBarIsVisible := ScrollBarsVisibility.VerticalVisible;
HorizontalScrollBarIsVisible := ScrollBarsVisibility.HorizontalVisible;
end;
function TScrollPanel.DetermineVisibleScrollBarsForSize(AvailableSize: TInteger32Size): TScrollBarsVisibility;
var
VerticalScrollBarNeeded: TBoolean;
HorizontalScrollBarNeeded: TBoolean;
begin
Result.VerticalVisible := VerticalScrollBarIsVisible;
Result.HorizontalVisible := HorizontalScrollBarIsVisible;
if Result.VerticalVisible and (VerticalScrollPolicy <> spAsNeeded) then
Dec(AvailableSize.Width, VerticalScrollBar.GetPreferredSize(AvailableSize).Width);
if Result.HorizontalVisible and (HorizontalScrollPolicy <> spAsNeeded) then
Dec(AvailableSize.Height, HorizontalScrollBar.GetPreferredSize(AvailableSize).Height);
if (VerticalScrollPolicy = spAsNeeded) and (HorizontalScrollPolicy = spAsNeeded) then
begin
VerticalScrollBarNeeded := NeedsVerticalScrollBar(AvailableSize);
HorizontalScrollBarNeeded := NeedsHorizontalScrollBar(AvailableSize);
if VerticalScrollBarNeeded then
begin
Result.VerticalVisible := True;
Dec(AvailableSize.Width, VerticalScrollBar.GetPreferredSize(AvailableSize).Width);
Result.HorizontalVisible := NeedsHorizontalScrollBar(AvailableSize);
end
else if HorizontalScrollBarNeeded then
begin
Result.HorizontalVisible := True;
Dec(AvailableSize.Height, HorizontalScrollBar.GetPreferredSize(AvailableSize).Height);
Result.VerticalVisible := NeedsVerticalScrollBar(AvailableSize);
end
else
begin
Result.VerticalVisible := False;
Result.HorizontalVisible := False;
end;
end
else if VerticalScrollPolicy = spAsNeeded then
Result.VerticalVisible := NeedsVerticalScrollBar(AvailableSize)
else if HorizontalScrollPolicy = spAsNeeded then
Result.HorizontalVisible := NeedsHorizontalScrollBar(AvailableSize);
VerticalScrollBarIsVisible := Result.VerticalVisible;
HorizontalScrollBarIsVisible := Result.HorizontalVisible;
end;
function TScrollPanel.GetAvailableSize(): TInteger32Size;
begin
Result := Size - GetFixedSize();
end;
function TScrollPanel.GetCaption(): TText;
begin
Result := MainControl.Caption;
end;
function TScrollPanel.GetCaptionVisible(): TBoolean;
begin
Result := MainControl.CaptionVisible;
end;
function TScrollPanel.GetFixedSize(): TInteger32Size;
var
i: TInteger32;
begin
Result := TInteger32Size.New(0, 0);
for i := 0 to MainPanel.Y - 1 do
Inc(Result.Height, GetPreferredRowHeight(i));
for i := MainPanel.Y + 1 to GridSize.Height - 1 do
Inc(Result.Height, GetPreferredRowHeight(i));
for i := 0 to MainPanel.X - 1 do
Inc(Result.Width, GetPreferredColumnWidth(i));
for i := MainPanel.X + 1 to GridSize.Width - 1 do
Inc(Result.Width, GetPreferredColumnWidth(i));
end;
function TScrollPanel.GetFocusedPanelLocation(): TInteger32Point;
var
PanelToFind: TControl;
Position: TInteger32Point;
begin
Result := TInteger32Point.New(-1, -1);
if FocusedPanel <> nil then
begin
PanelToFind := FocusedPanel.GetRootEmbellishment();
Position.Y := 0;
while (Result.X = -1) and (Position.Y < GridSize.Height) do
begin
Position.X := 0;
while (Result.X = -1) and (Position.X < GridSize.Width) do
if Panel[Position] = PanelToFind then
Result := Position
else
Inc(Position.X);
Inc(Position.Y);
end;
end;
end;
function TScrollPanel.GetHorizontalScrollBarIsVisible(): TBoolean;
begin
Result := ChildControls.Contains(HorizontalScrollBar);
end;
function TScrollPanel.GetPanel(const Position: TInteger32Point): TControl;
begin
Result := FPanels[Position.Y][Position.X];
end;
function TScrollPanel.GetPreferredColumnWidth(Column: TInteger32): TInteger32;
var
i: TInteger32;
Control: TControl;
begin
Result := 0;
for i := 0 to GridSize.Height - 1 do
begin
Control := Panel[TInteger32Point.New(Column, i)];
if Control <> nil then
Result := Maximum(Result, Control.GetPreferredSize(TInteger32Size.Maximum).Width);
end;
end;
function TScrollPanel.GetPreferredRowHeight(Row: TInteger32): TInteger32;
var
i: TInteger32;
Control: TControl;
begin
Result := 0;
for i := 0 to GridSize.Width - 1 do
begin
Control := Panel[TInteger32Point.New(i, Row)];
if Control <> nil then
Result := Maximum(Result, Control.GetPreferredSize(TInteger32Size.Maximum).Height);
end;
end;
function TScrollPanel.GetVerticalScrollBarIsVisible(): TBoolean;
begin
Result := ChildControls.Contains(VerticalScrollBar);
end;
function TScrollPanel.GetViewPortPanel(ViewPort: TViewport): TInteger32Point;
var
Position: TInteger32Point;
begin
Result := TInteger32Point.New(-1, -1);
Position.Y := 0;
while (Result.X = -1) and (Position.Y < GridSize.Height) do
begin
Position.X := 0;
while (Result.X = -1) and (Position.X < GridSize.Width) do
begin
if Panel[Position] = ViewPort then
Result := Position
else
Inc(Position.X);
end;
Inc(Position.Y);
end;
end;
function TScrollPanel.HandleEvent(Event: TEvent): TBoolean;
var
ScrollEvent: TScrollEvent;
begin
if Event is TScrollEvent then
begin
ScrollEvent := Event as TScrollEvent;
Result := (ScrollEvent.Model = HorizontalScrollBar.Model) or (ScrollEvent.Model = VerticalScrollBar.Model);
if Result then
UpdateViewPorts(ScrollEvent.Model);
end
else
Result := inherited HandleEvent(Event);
end;
procedure TScrollPanel.HandleViewPortMoved(ViewPort: TViewport);
var
Panel: TInteger32Point;
begin
Panel := GetViewPortPanel(ViewPort);
if Panel.X = MainPanel.X then
HorizontalScrollBar.Model.Position := ViewPort.Offset.X;
if Panel.Y = MainPanel.Y then
VerticalScrollBar.Model.Position := ViewPort.Offset.Y;
end;
function TScrollPanel.IsComposition(): TBoolean;
begin
Result := True;
end;
procedure TScrollPanel.LayoutControls();
begin
if not LayingOutControls then
begin
LayingOutControls := True;
DetermineVisibleScrollBars();
RepositionScrollBars();
LayingOutControls := False;
end;
end;
function TScrollPanel.MainControl(): TControl;
begin
Result := ViewPort.Control;
end;
function TScrollPanel.Navigate(Direction: TNavigationDirection; KeyModifiers: TKeyModifiers): TBoolean;
var
CurrentLocation: TInteger32Point;
NextControl: TControl;
Fast: TBoolean;
begin
Result := Direction in [TNavigationDirection.Up, TNavigationDirection.Down, TNavigationDirection.Right, TNavigationDirection.Left];
if Result then
begin
CurrentLocation := GetFocusedPanelLocation();
NextControl := nil;
case Direction of
TNavigationDirection.Down:
NextControl := ControlBelow(CurrentLocation);
TNavigationDirection.Up:
NextControl := ControlAbove(CurrentLocation);
TNavigationDirection.Right:
NextControl := ControlAtRight(CurrentLocation);
TNavigationDirection.Left:
NextControl := ControlAtLeft(CurrentLocation);
end;
Result := NextControl <> nil;
if Result then
begin
Fast := KeyModifiers = [TKeyModifier.Control];
case Direction of
TNavigationDirection.Down:
NextControl.EnterFromTop(Fast);
TNavigationDirection.Up:
NextControl.EnterFromBottom(Fast);
TNavigationDirection.Right:
NextControl.EnterFromLeft(Fast);
TNavigationDirection.Left:
NextControl.EnterFromRight(Fast);
end;
end;
end;
end;
function TScrollPanel.NeedsHorizontalScrollBar(AvailableSize: TInteger32Size): TBoolean;
begin
Result := ViewPort.GetScrollSize(AvailableSize).Width > AvailableSize.Width;
end;
function TScrollPanel.NeedsVerticalScrollBar(AvailableSize: TInteger32Size): TBoolean;
begin
Result := ViewPort.GetScrollSize(AvailableSize).Height > AvailableSize.Height;
end;
function TScrollPanel.NextInKeyboardInputChain(): TControl;
begin
Result := FocusedPanel;
end;
function TScrollPanel.GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size;
var
FixedSize: TInteger32Size;
ScrollBarSize: TInteger32Size;
begin
FixedSize := GetFixedSize();
Result := Panel[MainPanel].GetPreferredSize(AvailableSize - FixedSize) + FixedSize;
if (Result.Height > AvailableSize.Height) and (AvailableSize.Width > Result.Width) then
begin
ScrollBarSize := VerticalScrollBar.GetPreferredSize(TInteger32Size.New(AvailableSize.Width - Result.Width, AvailableSize.Height));
Inc(Result.Width, ScrollBarSize.Width);
end
else if (Result.Width > AvailableSize.Width) and (AvailableSize.Height > Result.Height) then
begin
ScrollBarSize := HorizontalScrollBar.GetPreferredSize(TInteger32Size.New(AvailableSize.Width, AvailableSize.Height - Result.Height));
Inc(Result.Height, ScrollBarSize.Height);
end;
end;
procedure TScrollPanel.ReallocatePanels();
var
Index: TInteger32;
Row: TList<TControl>;
begin
FPanels.Clear();
FPanels.Capacity := GridSize.Height;
for Index := 1 to GridSize.Height do
begin
Row := TObjectList<TControl>.Create();
Row.ElementManager := TReferenceUser<TControl>.Create();
Row.Count := GridSize.Width;
FPanels.Add(Row);
end;
end;
procedure TScrollPanel.RepositionScrollBars();
var
AvailableSize: TInteger32Size;
ControlSize: TInteger32Size;
ScrollBarWidth: TInteger32;
ScrollBarHeight: TInteger32;
begin
AvailableSize := GetAvailableSize();
if VerticalScrollBarIsVisible then
begin
ScrollBarWidth := VerticalScrollBar.GetPreferredSize(AvailableSize).Width;
AvailableSize.Width := Maximum(0, AvailableSize.Width - ScrollBarWidth);
end
else
ScrollBarWidth := 0;
if HorizontalScrollBarIsVisible then
begin
ScrollBarHeight := HorizontalScrollBar.GetPreferredSize(AvailableSize).Height;
AvailableSize.Height := Maximum(0, AvailableSize.Height - ScrollBarHeight);
end
else
ScrollBarHeight := 0;
ControlSize := MainControl.GetScrollSize(AvailableSize);
if VerticalScrollBarIsVisible then
VerticalScrollBar.Bounds := TInteger32Rectangle.New(TInteger32Point.New(Width - ScrollBarWidth, 0), TInteger32Size.New(ScrollBarWidth, Height - ScrollBarHeight))
else
ControlSize.Height := AvailableSize.Height;
if HorizontalScrollBarIsVisible then
HorizontalScrollBar.Bounds := TInteger32Rectangle.New(TInteger32Point.New(0, Height - ScrollBarHeight), TInteger32Size.New(Width - ScrollBarWidth, ScrollBarHeight))
else
ControlSize.Width := AvailableSize.Width;
SetViewPortSize(AvailableSize);
ControlSize := MainControl.GetScrollSize(AvailableSize);
AdjustScrollModel(HorizontalScrollBar.Model, AvailableSize.Width, ControlSize.Width);
AdjustScrollModel(VerticalScrollBar.Model, AvailableSize.Height, ControlSize.Height);
ViewPort.ValidateOffset();
end;
procedure TScrollPanel.SetCaptionVisible(const Value: TBoolean);
begin
MainControl.CaptionVisible := Value;
end;
procedure TScrollPanel.SetGridSize(const Size: TInteger32Size);
begin
if Size <> GridSize then
begin
FGridSize := Size;
ReallocatePanels();
end;
end;
procedure TScrollPanel.SetHorizontalScrollBarIsVisible(Value: TBoolean);
begin
SetScrollBarIsVisible(HorizontalScrollBar, Value);
end;
procedure TScrollPanel.SetHorizontalScrollPolicy(const Value: TScrollPolicy);
begin
if Value <> HorizontalScrollPolicy then
begin
FHorizontalScrollPolicy := Value;
LayoutControls();
end;
end;
procedure TScrollPanel.SetHorizontalViewPortOffsets();
var
i: TInteger32;
ViewPort: TViewport;
begin
for i := 0 to GridSize.Height - 1 do
begin
ViewPort := Panel[TInteger32Point.New(MainPanel.X, i)] as TViewport;
ViewPort.SetOffset(TInteger32Point.New(HorizontalScrollBar.Model.Position, ViewPort.Offset.Y));
end;
end;
procedure TScrollPanel.SetMainPanel(const Value: TInteger32Point);
begin
FMainPanel := Value;
ValidateFocusedControl();
UpdateStyleFromMainPanel();
end;
procedure TScrollPanel.SetViewPortSize(const Size: TInteger32Size);
var
Rows: TSizableCumulativeSizes;
Columns: TSizableCumulativeSizes;
Row: TInteger32;
Column: TInteger32;
Control: TControl;
ControlPosition: TInteger32Point;
ControlSize: TInteger32Size;
begin
// TODO: Rows should be a member of a TScrollGridPanel and should be the authority on view port sizes
Rows := TSizableCumulativeSizes.Create();
Rows.Count := GridSize.Height;
for Row := 0 to MainPanel.Y - 1 do
Rows.SetItemSize(Row, GetPreferredRowHeight(Row), False);
for Row := MainPanel.Y + 1 to GridSize.Height - 1 do
Rows.SetItemSize(Row, GetPreferredRowHeight(Row), False);
Rows.SetItemSize(MainPanel.Y, Size.Height, False);
// TODO: Columns should be a member of a TScrollGridPanel and should be the authority on view port sizes
Columns := TSizableCumulativeSizes.Create();
Columns.Count := GridSize.Width;
for Column := 0 to MainPanel.X - 1 do
Columns.SetItemSize(Column, GetPreferredColumnWidth(Column), False);
for Column := MainPanel.X + 1 to GridSize.Width - 1 do
Columns.SetItemSize(Column, GetPreferredColumnWidth(Column), False);
Columns.SetItemSize(MainPanel.X, Size.Width, False);
ControlPosition.Y := 0;
for Row := 0 to GridSize.Height - 1 do
begin
ControlSize.Height := Rows.Size(Row);
ControlPosition.X := 0;
for Column := 0 to GridSize.Width - 1 do
begin
ControlSize.Width := Columns.Size(Column);
Control := Panel[TInteger32Point.New(Column, Row)];
if Control <> nil then
begin
Control.Bounds := TInteger32Rectangle.New(ControlPosition, ControlSize);
(Control as TViewport).Control.Size := Control.GetScrollSize(Control.Size);
end;
Inc(ControlPosition.X, ControlSize.Width);
end;
Inc(ControlPosition.Y, ControlSize.Height);
end;
Columns.Free();
Rows.Free();
end;
procedure TScrollPanel.SetPanel(const Location: TInteger32Point; Control: TControl);
var
CurrentControl: TControl;
ViewPort: TViewport;
ControlSize: TInteger32Size;
begin
CurrentControl := Panel[Location];
if CurrentControl <> nil then
ChildControls.Remove(CurrentControl);
ControlSize := Control.Size;
ViewPort := TViewport.Create();
ViewPort.Control := Control;
Control.Size:= ControlSize;
FPanels[Location.Y][Location.X] := ViewPort;
if Control <> nil then
ChildControls.Add(ViewPort);
ValidateFocusedControl();
if Location = MainPanel then
UpdateStyleFromMainPanel();
end;
procedure TScrollPanel.SetScrollBarIsVisible(ScrollBar: TScrollBar; Value: TBoolean);
begin
// TODO: We insert scroll bars at the front to prevent problems when scroll bars are removed during painting
SetChildControlVisible(ScrollBar, Value, TListEndPoint.Head);
if not Value then
ScrollBar.Model.Position := 0;
end;
procedure TScrollPanel.SetVerticalScrollBarIsVisible(Value: TBoolean);
begin
SetScrollBarIsVisible(VerticalScrollBar, Value);
end;
procedure TScrollPanel.SetVerticalScrollPolicy(const Value: TScrollPolicy);
begin
if Value <> VerticalScrollPolicy then
begin
FVerticalScrollPolicy := Value;
LayoutControls();
end;
end;
procedure TScrollPanel.SetVerticalViewPortOffsets();
var
i: TInteger32;
ViewPort: TViewport;
begin
for i := 0 to GridSize.Width - 1 do
begin
ViewPort := Panel[TInteger32Point.New(i, MainPanel.Y)] as TViewport;
ViewPort.SetOffset(TInteger32Point.New(ViewPort.Offset.X, VerticalScrollBar.Model.Position));
end;
end;
procedure TScrollPanel.SizeChanged();
begin
LayoutControls();
inherited SizeChanged();
end;
procedure TScrollPanel.UpdateStyleFromMainPanel();
begin
if ViewPort <> nil then
begin
// TODO: We probably have to take FixedSize into account
Style.Width.Value := MainControl.Style.Width.Value;
Style.Height.Value := MainControl.Style.Height.Value;
end;
end;
procedure TScrollPanel.UpdateViewPorts(ScrollModel: TScrollModel);
begin
if ScrollModel = HorizontalScrollBar.Model then
begin
SetHorizontalViewPortOffsets();
Invalidate();
end
else if ScrollModel = VerticalScrollBar.Model then
begin
SetVerticalViewPortOffsets();
Invalidate();
end;
end;
procedure TScrollPanel.ValidateFocusedControl();
begin
if (FocusedPanel = nil) and (ViewPort <> nil) then
FocusedPanel := MainControl;
end;
function TScrollPanel.ViewPort(): TViewport;
begin
Result := Panel[MainPanel] as TViewport;
end;
end.
|