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
| ///////////////////////////////////////////////////////////////////////////
// //
// 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.Panel.Stack;
interface
uses
Geometry._2d.Integer32.Rectangle,
Geometry._2d.Integer32.Size,
Integer._32,
UserInterface.Control,
UserInterface.Control.Panel;
type
TStackPanel = class(TOpaquePanel)
protected
FInterControlSpace: TInteger32;
procedure BoundsChanged(const OldBounds: TInteger32Rectangle; const NewBounds: TInteger32Rectangle); override;
procedure HasBeenBoundToLayout(); override;
procedure SetInterControlSpace(const Value: TInteger32);
public
function Embellish(): TControl; override;
procedure AddControl(Control: TControl); virtual;
property InterControlSpace: TInteger32 read FInterControlSpace write SetInterControlSpace;
end;
TVerticalStackPanel = class(TStackPanel)
public
procedure LayoutControls(); override;
function GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size; override;
end;
THorizontalStackPanel = class(TStackPanel)
public
procedure LayoutControls(); override;
function GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size; override;
end;
implementation
uses
Geometry._2d.Integer32.Point,
Mathematics,
UserInterface.Control.Margin;
{ TStackPanel }
procedure TStackPanel.AddControl(Control: TControl);
begin
ChildControls.Add(Control.Embellish());
LayoutControls();
end;
function TStackPanel.Embellish(): TControl;
var
Margin: TControlMargin;
begin
Margin := TControlMargin.Create();
Margin.Insets := TInteger32Rectangle.New(4, 4, 4, 4);
Margin.Control := Self;
Result := Margin;
end;
procedure TStackPanel.HasBeenBoundToLayout();
begin
inherited HasBeenBoundToLayout();
LayoutControls();
end;
procedure TStackPanel.SetInterControlSpace(const Value: TInteger32);
begin
if Value <> InterControlSpace then
begin
FInterControlSpace := Value;
LayoutControls();
Invalidate();
end;
end;
procedure TStackPanel.BoundsChanged(const OldBounds: TInteger32Rectangle; const NewBounds: TInteger32Rectangle);
begin
inherited BoundsChanged(OldBounds, NewBounds);
if OldBounds.Size <> NewBounds.Size then
LayoutControls();
end;
{ TVerticalStackPanel }
procedure TVerticalStackPanel.LayoutControls();
var
CurrentTop: TInteger32;
ChildControl: TControl;
PreferredSize: TInteger32Size;
begin
if IsBoundToLayout() then
begin
CurrentTop := 0;
for ChildControl in ChildControls do
begin
PreferredSize := ChildControl.ResolveStyleSize(TInteger32Size.New(TInteger32Size.Maximum.Width, Height));
ChildControl.Bounds := TInteger32Rectangle.New(
TInteger32Point.New(0, CurrentTop),
TInteger32Size.New(Width, PreferredSize.Height)
);
Inc(CurrentTop, ChildControl.Height + InterControlSpace);
end;
end;
end;
function TVerticalStackPanel.GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size;
var
ChildControl: TControl;
AvailableChildSize: TInteger32Size;
PreferredChildSize: TInteger32Size;
begin
AvailableChildSize := TInteger32Size.New(AvailableSize.Width, TInteger32Size.Maximum.Height);
Result := TInteger32Size.New(0, 0);
if ChildControls.Count > 0 then
begin
for ChildControl in ChildControls do
begin
PreferredChildSize := ChildControl.ResolveStyleSize(AvailableChildSize);
Inc(Result.Height, PreferredChildSize.Height);
Result.Width := Maximum(PreferredChildSize.Width, Result.Width);
end;
Inc(Result.Height, (ChildControls.Count - 1) * InterControlSpace);
end;
end;
{ THorizontalStackPanel }
procedure THorizontalStackPanel.LayoutControls();
var
CurrentLeft: TInteger32;
ChildControl: TControl;
PreferredSize: TInteger32Size;
begin
if IsBoundToLayout() then
begin
CurrentLeft := 0;
for ChildControl in ChildControls do
begin
PreferredSize := ChildControl.ResolveStyleSize(TInteger32Size.New(TInteger32Size.Maximum.Width, Height));
ChildControl.Bounds := TInteger32Rectangle.New(
TInteger32Point.New(CurrentLeft, 0),
TInteger32Size.New(PreferredSize.Width, Height)
);
Inc(CurrentLeft, ChildControl.Width + InterControlSpace);
end;
end;
end;
function THorizontalStackPanel.GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size;
var
ChildControl: TControl;
AvailableChildSize: TInteger32Size;
PreferredChildSize: TInteger32Size;
begin
AvailableChildSize := TInteger32Size.New(TInteger32Size.Maximum.Width, AvailableSize.Height);
Result := TInteger32Size.New(0, 0);
if ChildControls.Count > 0 then
begin
for ChildControl in ChildControls do
begin
PreferredChildSize := ChildControl.ResolveStyleSize(AvailableChildSize);
Inc(Result.Width, PreferredChildSize.Width);
Result.Height := Maximum(PreferredChildSize.Height, Result.Height);
end;
Inc(Result.Width, (ChildControls.Count - 1) * InterControlSpace);
end;
end;
end.
|