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
ClientRectangle = context.DisplayRectangle;
// Reduce by the padding
Rectangle innerRectangle = ClientRectangle;
innerRectangle.X += PreferredPadding.Left;
innerRectangle.Y += PreferredPadding.Top;
innerRectangle.Width -= PreferredPadding.Horizontal;
innerRectangle.Height -= PreferredPadding.Vertical;
// Layout each child centered within this space
foreach (ViewBase child in this)
{
// Only layout visible children
if (child.Visible)
{
// Ask child for it's own preferred size
Size childPreferred = child.GetPreferredSize(context);
// Make sure the child is never bigger than the available space
if (childPreferred.Width > ClientRectangle.Width) childPreferred.Width = ClientWidth;
if (childPreferred.Height > ClientRectangle.Height) childPreferred.Height = ClientHeight;
// Find vertical and horizontal offsets for centering
int xOffset = (innerRectangle.Width - childPreferred.Width) / 2;
int yOffset = (innerRectangle.Height - childPreferred.Height) / 2;
// Create the rectangle that centers the child in our space
context.DisplayRectangle = new Rectangle(innerRectangle.X + xOffset,
innerRectangle.Y + yOffset,
childPreferred.Width,
childPreferred.Height);
// Finally ask the child to layout
child.Layout(context);
}
}
// Put back the original display value now we have finished
context.DisplayRectangle = ClientRectangle;
}