// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reactive.Linq; using Perspex.Input; using Perspex.Layout; using Perspex.Media; using Perspex.Styling; using Splat; public enum HorizontalAlignment { Stretch, Left, Center, Right, } public enum VerticalAlignment { Stretch, Top, Center, Bottom, } public class Control : Interactive, ILayoutable, ILogical, IStyleable, IStyled { public static readonly PerspexProperty BackgroundProperty = PerspexProperty.Register("Background", inherits: true); public static readonly PerspexProperty BorderBrushProperty = PerspexProperty.Register("BorderBrush"); public static readonly PerspexProperty BorderThicknessProperty = PerspexProperty.Register("BorderThickness"); public static readonly PerspexProperty ForegroundProperty = PerspexProperty.Register("Foreground", new SolidColorBrush(0xff000000), true); public static readonly PerspexProperty HeightProperty = PerspexProperty.Register("Height", double.NaN); public static readonly PerspexProperty IsPointerOverProperty = PerspexProperty.Register("IsPointerOver"); public static readonly PerspexProperty HorizontalAlignmentProperty = PerspexProperty.Register("HorizontalAlignment"); public static readonly PerspexProperty MarginProperty = PerspexProperty.Register("Margin"); public static readonly PerspexProperty MaxHeightProperty = PerspexProperty.Register("MaxHeight", double.PositiveInfinity); public static readonly PerspexProperty MaxWidthProperty = PerspexProperty.Register("MaxWidth", double.PositiveInfinity); public static readonly PerspexProperty MinHeightProperty = PerspexProperty.Register("MinHeight"); public static readonly PerspexProperty MinWidthProperty = PerspexProperty.Register("MinWidth"); public static readonly PerspexProperty ParentProperty = PerspexProperty.Register("Parent"); public static readonly PerspexProperty VerticalAlignmentProperty = PerspexProperty.Register("VerticalAlignment"); public static readonly PerspexProperty WidthProperty = PerspexProperty.Register("Width", double.NaN); public static readonly RoutedEvent PointerPressedEvent = RoutedEvent.Register("PointerPressed", RoutingStrategy.Bubble); public static readonly RoutedEvent PointerReleasedEvent = RoutedEvent.Register("PointerReleased", RoutingStrategy.Bubble); private Classes classes; private string id; private Styles styles; public Control() { this.classes = new Classes(); this.GetObservable(IsPointerOverProperty).Subscribe(x => { if (x) { this.Classes.Add(":pointerover"); } else { this.Classes.Remove(":pointerover"); } }); // Hacky hack hack! this.GetObservable(BackgroundProperty).Skip(1).Subscribe(_ => this.InvalidateMeasure()); } public event EventHandler PointerPressed { add { Contract.Requires(value != null); this.AddHandler(PointerPressedEvent, value); } remove { Contract.Requires(value != null); this.RemoveHandler(PointerPressedEvent, value); } } public event EventHandler PointerReleased { add { Contract.Requires(value != null); this.AddHandler(PointerReleasedEvent, value); } remove { Contract.Requires(value != null); this.RemoveHandler(PointerReleasedEvent, value); } } public Brush Background { get { return this.GetValue(BackgroundProperty); } set { this.SetValue(BackgroundProperty, value); } } public Brush BorderBrush { get { return this.GetValue(BorderBrushProperty); } set { this.SetValue(BorderBrushProperty, value); } } public double BorderThickness { get { return this.GetValue(BorderThicknessProperty); } set { this.SetValue(BorderThicknessProperty, value); } } public Classes Classes { get { return this.classes; } set { if (this.classes != value) { this.classes.Clear(); this.classes.Add(value); } } } public Size? DesiredSize { get; set; } public Brush Foreground { get { return this.GetValue(ForegroundProperty); } set { this.SetValue(ForegroundProperty, value); } } public string Id { get { return this.id; } set { if (this.id != null) { throw new InvalidOperationException("ID already set."); } if (((IVisual)this).VisualParent != null) { throw new InvalidOperationException("Cannot set ID : control already added to tree."); } this.id = value; } } public double Height { get { return this.GetValue(HeightProperty); } set { this.SetValue(HeightProperty, value); } } public bool IsPointerOver { get { return this.GetValue(IsPointerOverProperty); } set { this.SetValue(IsPointerOverProperty, value); } } public HorizontalAlignment HorizontalAlignment { get { return this.GetValue(HorizontalAlignmentProperty); } set { this.SetValue(HorizontalAlignmentProperty, value); } } public Thickness Margin { get { return this.GetValue(MarginProperty); } set { this.SetValue(MarginProperty, value); } } public double MaxHeight { get { return this.GetValue(MaxHeightProperty); } set { this.SetValue(MaxHeightProperty, value); } } public double MaxWidth { get { return this.GetValue(MaxWidthProperty); } set { this.SetValue(MaxWidthProperty, value); } } public double MinHeight { get { return this.GetValue(MinHeightProperty); } set { this.SetValue(MinHeightProperty, value); } } public double MinWidth { get { return this.GetValue(MinWidthProperty); } set { this.SetValue(MinWidthProperty, value); } } public Control Parent { get { return this.GetValue(ParentProperty); } protected set { this.SetValue(ParentProperty, value); } } public Styles Styles { get { if (this.styles == null) { this.styles = new Styles(); } return this.styles; } set { this.styles = value; } } public ITemplatedControl TemplatedParent { get; internal set; } public VerticalAlignment VerticalAlignment { get { return this.GetValue(VerticalAlignmentProperty); } set { this.SetValue(VerticalAlignmentProperty, value); } } public double Width { get { return this.GetValue(WidthProperty); } set { this.SetValue(WidthProperty, value); } } ILogical ILogical.LogicalParent { get { return this.Parent; } set { this.Parent = (Control)value; } } IEnumerable ILogical.LogicalChildren { get { return Enumerable.Empty(); } } public ILayoutRoot GetLayoutRoot() { return this.GetVisualAncestorOrSelf(); } public void Arrange(Rect rect) { this.Bounds = new Rect( rect.Position, this.ArrangeContent(rect.Size.Deflate(this.Margin).Constrain(rect.Size))); } public void Measure(Size availableSize) { availableSize = availableSize.Deflate(this.Margin); this.DesiredSize = this.MeasureContent(availableSize).Constrain(availableSize); } public void InvalidateArrange() { ILayoutRoot root = this.GetLayoutRoot(); if (root != null) { root.LayoutManager.InvalidateArrange(this); } } public void InvalidateMeasure() { ILayoutRoot root = this.GetLayoutRoot(); if (root != null) { root.LayoutManager.InvalidateMeasure(this); } } protected virtual Size ArrangeContent(Size finalSize) { return finalSize; } protected virtual Size MeasureContent(Size availableSize) { return new Size(); } protected override void AttachedToVisualTree() { IStyler styler = Locator.Current.GetService(); styler.ApplyStyles(this); base.AttachedToVisualTree(); } } }