@ -1,9 +1,12 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System ;
using Perspex.Controls.Primitives ;
using Perspex.Controls.Templates ;
using Perspex.Layout ;
using Perspex.LogicalTree ;
using Perspex.Media ;
namespace Perspex.Controls.Presenters
{
@ -12,6 +15,24 @@ namespace Perspex.Controls.Presenters
/// </summary>
public class ContentPresenter : Control , IContentPresenter
{
/// <summary>
/// Defines the <see cref="Background"/> property.
/// </summary>
public static readonly StyledProperty < Brush > BackgroundProperty =
Border . BackgroundProperty . AddOwner < ContentPresenter > ( ) ;
/// <summary>
/// Defines the <see cref="BorderBrush"/> property.
/// </summary>
public static readonly PerspexProperty < Brush > BorderBrushProperty =
Border . BorderBrushProperty . AddOwner < ContentPresenter > ( ) ;
/// <summary>
/// Defines the <see cref="BorderThickness"/> property.
/// </summary>
public static readonly StyledProperty < double > BorderThicknessProperty =
Border . BorderThicknessProperty . AddOwner < ContentPresenter > ( ) ;
/// <summary>
/// Defines the <see cref="Child"/> property.
/// </summary>
@ -26,6 +47,30 @@ namespace Perspex.Controls.Presenters
public static readonly StyledProperty < object > ContentProperty =
ContentControl . ContentProperty . AddOwner < ContentPresenter > ( ) ;
/// <summary>
/// Defines the <see cref="CornerRadius"/> property.
/// </summary>
public static readonly StyledProperty < float > CornerRadiusProperty =
Border . CornerRadiusProperty . AddOwner < ContentPresenter > ( ) ;
/// <summary>
/// Defines the <see cref="HorizontalContentAlignment"/> property.
/// </summary>
public static readonly StyledProperty < HorizontalAlignment > HorizontalContentAlignmentProperty =
ContentControl . HorizontalContentAlignmentProperty . AddOwner < ContentPresenter > ( ) ;
/// <summary>
/// Defines the <see cref="VerticalContentAlignment"/> property.
/// </summary>
public static readonly StyledProperty < VerticalAlignment > VerticalContentAlignmentProperty =
ContentControl . VerticalContentAlignmentProperty . AddOwner < ContentPresenter > ( ) ;
/// <summary>
/// Defines the <see cref="Padding"/> property.
/// </summary>
public static readonly StyledProperty < Thickness > PaddingProperty =
Border . PaddingProperty . AddOwner < ContentPresenter > ( ) ;
private IControl _ child ;
private bool _ createdChild ;
@ -38,6 +83,33 @@ namespace Perspex.Controls.Presenters
TemplatedParentProperty . Changed . AddClassHandler < ContentPresenter > ( x = > x . TemplatedParentChanged ) ;
}
/// <summary>
/// Gets or sets a brush with which to paint the background.
/// </summary>
public Brush Background
{
get { return GetValue ( BackgroundProperty ) ; }
set { SetValue ( BackgroundProperty , value ) ; }
}
/// <summary>
/// Gets or sets a brush with which to paint the border.
/// </summary>
public Brush BorderBrush
{
get { return GetValue ( BorderBrushProperty ) ; }
set { SetValue ( BorderBrushProperty , value ) ; }
}
/// <summary>
/// Gets or sets the thickness of the border.
/// </summary>
public double BorderThickness
{
get { return GetValue ( BorderThicknessProperty ) ; }
set { SetValue ( BorderThicknessProperty , value ) ; }
}
/// <summary>
/// Gets the control displayed by the presenter.
/// </summary>
@ -56,6 +128,42 @@ namespace Perspex.Controls.Presenters
set { SetValue ( ContentProperty , value ) ; }
}
/// <summary>
/// Gets or sets the radius of the border rounded corners.
/// </summary>
public float CornerRadius
{
get { return GetValue ( CornerRadiusProperty ) ; }
set { SetValue ( CornerRadiusProperty , value ) ; }
}
/// <summary>
/// Gets or sets the horizontal alignment of the content within the control.
/// </summary>
public HorizontalAlignment HorizontalContentAlignment
{
get { return GetValue ( HorizontalContentAlignmentProperty ) ; }
set { SetValue ( HorizontalContentAlignmentProperty , value ) ; }
}
/// <summary>
/// Gets or sets the vertical alignment of the content within the control.
/// </summary>
public VerticalAlignment VerticalContentAlignment
{
get { return GetValue ( VerticalContentAlignmentProperty ) ; }
set { SetValue ( VerticalContentAlignmentProperty , value ) ; }
}
/// <summary>
/// Gets or sets the padding to place around the <see cref="Child"/> control.
/// </summary>
public Thickness Padding
{
get { return GetValue ( PaddingProperty ) ; }
set { SetValue ( PaddingProperty , value ) ; }
}
/// <inheritdoc/>
public override sealed void ApplyTemplate ( )
{
@ -120,23 +228,93 @@ namespace Perspex.Controls.Presenters
}
/// <inheritdoc/>
protected override Size MeasureCore ( Size availableSize )
public override void Render ( DrawingContext context )
{
return base . MeasureCore ( availableSize ) ;
var background = Background ;
var borderBrush = BorderBrush ;
var borderThickness = BorderThickness ;
var cornerRadius = CornerRadius ;
var rect = new Rect ( Bounds . Size ) . Deflate ( BorderThickness ) ;
if ( background ! = null )
{
context . FillRectangle ( background , rect , cornerRadius ) ;
}
if ( borderBrush ! = null & & borderThickness > 0 )
{
context . DrawRectangle ( new Pen ( borderBrush , borderThickness ) , rect , cornerRadius ) ;
}
}
/// <inheritdoc/>
protected override Size MeasureOverride ( Size availableSize )
{
var child = Child ;
var padding = Padding + new Thickness ( BorderThickness ) ;
if ( child ! = null )
{
child . Measure ( availableSize ) ;
return child . DesiredSize ;
child . Measure ( availableSize . Deflate ( padding ) ) ;
return child . DesiredSize . Inflate ( padding ) ;
}
else
{
return new Size ( padding . Left + padding . Right , padding . Bottom + padding . Top ) ;
}
}
/// <inheritdoc/>
protected override Size ArrangeOverride ( Size finalSize )
{
var child = Child ;
if ( child ! = null )
{
var padding = Padding + new Thickness ( BorderThickness ) ;
var sizeMinusPadding = finalSize . Deflate ( padding ) ;
var size = sizeMinusPadding ;
var horizontalAlignment = HorizontalContentAlignment ;
var verticalAlignment = VerticalContentAlignment ;
var originX = padding . Left ;
var originY = padding . Top ;
if ( horizontalAlignment ! = HorizontalAlignment . Stretch )
{
size = size . WithWidth ( child . DesiredSize . Width ) ;
}
if ( verticalAlignment ! = VerticalAlignment . Stretch )
{
size = size . WithHeight ( child . DesiredSize . Height ) ;
}
switch ( horizontalAlignment )
{
case HorizontalAlignment . Stretch :
case HorizontalAlignment . Center :
originX + = ( sizeMinusPadding . Width - size . Width ) / 2 ;
break ;
case HorizontalAlignment . Right :
originX = size . Width - child . DesiredSize . Width ;
break ;
}
switch ( verticalAlignment )
{
case VerticalAlignment . Stretch :
case VerticalAlignment . Center :
originY + = ( sizeMinusPadding . Height - size . Height ) / 2 ;
break ;
case VerticalAlignment . Bottom :
originY = size . Height - child . DesiredSize . Height ;
break ;
}
child . Arrange ( new Rect ( originX , originY , size . Width , size . Height ) ) ;
}
return new Size ( ) ;
return finalSize ;
}
/// <summary>