/// <summary>
/// Perform a layout of the elements.
/// </summary>
/// <param name="context">Layout context.</param>
public override void Layout(ViewLayoutContext context)
{
Debug.Assert(context != null);
// Validate incoming reference
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
// We take on all the available display area
ClientRectangle = context.DisplayRectangle;
// How big would the track and position indicator like to be?
Size trackSize = _drawTrack.GetPreferredSize(context);
Size positionSize = _drawPosition.GetPreferredSize(context);
// Grab range and current position from the bar
int min = _drawTrackBar.Minimum;
int max = _drawTrackBar.Maximum;
int range = max - min;
int offset = _drawTrackBar.Value - min;
Rectangle trackRect = ClientRectangle;
Rectangle positionRect = ClientRectangle;
if (_drawTrackBar.Orientation == Orientation.Horizontal)
{
float valueLength = (ClientWidth - positionSize.Width);
if (_drawTrackBar.RightToLeft == RightToLeft.Yes)
{
if (valueLength > 0)
{
positionRect.X = positionRect.Right - positionSize.Width - (int)Math.Round(valueLength / range * offset, 0, MidpointRounding.AwayFromZero);
}
}
else
{
if (valueLength > 0)
{
positionRect.X += (int)Math.Round(valueLength / range * offset, 0, MidpointRounding.AwayFromZero);
}
}
trackRect.Y += (ClientHeight - trackSize.Height) / 2;
trackRect.Height = trackSize.Height;
positionRect.Y += (ClientHeight - positionSize.Height) / 2;
positionRect.Height = positionSize.Height;
positionRect.Width = positionSize.Width;
}
else
{
float valueLength = (ClientHeight - positionSize.Height);
if (valueLength > 0)
{
positionRect.Y = positionRect.Bottom - positionSize.Height - (int)Math.Round(valueLength / range * offset, 0, MidpointRounding.AwayFromZero);
}
trackRect.X += (ClientWidth - trackSize.Width) / 2;
trackRect.Width = trackSize.Width;
positionRect.X += (ClientWidth - positionSize.Width) / 2;
positionRect.Width = positionSize.Width;
positionRect.Height = positionSize.Height;
}
context.DisplayRectangle = trackRect;
_drawTrack.Layout(context);
context.DisplayRectangle = positionRect;
_drawPosition.Layout(context);
context.DisplayRectangle = ClientRectangle;
}