public override void Layout(ViewLayoutContext context)
{
Debug.Assert(context != null);
// Validate incoming reference
if (context == null) throw new ArgumentNullException("context");
// We take on all the available display area
Rectangle original = context.DisplayRectangle;
ClientRectangle = original;
// Layout each child
int offset = 0;
int space = (_orientation == Orientation.Vertical ? ClientHeight : ClientWidth);
for(int i=0; i<Count; i++)
{
ViewBase child = this[i];
// Find length of this item
int length = 0;
// If this is the last item then it takes the remaining space
if (i == (Count - 1))
length = space;
else
{
// Give this item an equal portion of the remainder
length = space / (Count - i);
}
// Ask child for it's own preferred size
Size childPreferred = child.GetPreferredSize(context);
// Size child to our relevant dimension
if (_orientation == Orientation.Vertical)
context.DisplayRectangle = new Rectangle(ClientRectangle.X,
ClientRectangle.Y + offset,
childPreferred.Width,
length);
else
context.DisplayRectangle = new Rectangle(ClientRectangle.X + offset,
ClientRectangle.Y,
length,
ClientRectangle.Height);
// Ask the child to layout
child.Layout(context);
// Adjust running values
offset += length;
space -= length;
}
// Put back the original display value now we have finished
context.DisplayRectangle = original;
}