// -----------------------------------------------------------------------
//
// Copyright 2015 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using System;
///
/// Defines the valid units for a .
///
public enum GridUnitType
{
///
/// The row or column is auto-sized to fit its content.
///
Auto = 0,
///
/// The row or column is sized in device independent pixels.
///
Pixel = 1,
///
/// The row or column is sized as a weighted proportion of available space.
///
Star = 2,
}
///
/// Holds the width or height of a 's column and row definitions.
///
public struct GridLength : IEquatable
{
private GridUnitType type;
private double value;
///
/// Initializes a new instance of the struct.
///
/// The size of the GridLength in device independent pixels.
public GridLength(double value)
: this(value, GridUnitType.Pixel)
{
}
///
/// Initializes a new instance of the struct.
///
/// The size of the GridLength.
/// The unit of the GridLength.
public GridLength(double value, GridUnitType type)
{
if (value < 0 || double.IsNaN(value) || double.IsInfinity(value))
{
throw new ArgumentException("Invalid value", "value");
}
if (type < GridUnitType.Auto || type > GridUnitType.Star)
{
throw new ArgumentException("Invalid value", "type");
}
this.type = type;
this.value = value;
}
///
/// Gets an instance of that indicates that a row or column should
/// auto-size to fit its content.
///
public static GridLength Auto
{
get { return new GridLength(0, GridUnitType.Auto); }
}
///
/// Gets the unit of the .
///
public GridUnitType GridUnitType
{
get { return this.type; }
}
///
/// Gets a value that indicates whether the has a of Pixel.
///
public bool IsAbsolute
{
get { return this.type == GridUnitType.Pixel; }
}
///
/// Gets a value that indicates whether the has a of Auto.
///
public bool IsAuto
{
get { return this.type == GridUnitType.Auto; }
}
///
/// Gets a value that indicates whether the has a of Star.
///
public bool IsStar
{
get { return this.type == GridUnitType.Star; }
}
///
/// Gets the length.
///
public double Value
{
get { return this.value; }
}
///
/// Compares two GridLength structures for equality.
///
/// The first GridLength.
/// The first GridLength.
/// True if the structures are equal, otherwise false.
public static bool operator ==(GridLength a, GridLength b)
{
return (a.IsAuto && b.IsAuto) || (a.value == b.value && a.type == b.type);
}
///
/// Compares two GridLength structures for inequality.
///
/// The first GridLength.
/// The first GridLength.
/// True if the structures are unequal, otherwise false.
public static bool operator !=(GridLength gl1, GridLength gl2)
{
return !(gl1 == gl2);
}
///
/// Determines whether the is equal to the specified object.
///
/// The object with which to test equality.
/// True if the objects are equal, otherwise false.
public override bool Equals(object o)
{
if (o == null)
{
return false;
}
if (!(o is GridLength))
{
return false;
}
return this == (GridLength)o;
}
///
/// Compares two GridLength structures for equality.
///
/// The structure with which to test equality.
/// True if the structures are equal, otherwise false.
public bool Equals(GridLength gridLength)
{
return this == gridLength;
}
///
/// Gets a hash code for the GridLength.
///
/// The hash code.
public override int GetHashCode()
{
return this.value.GetHashCode() ^ this.type.GetHashCode();
}
///
/// Gets a string representation of the .
///
/// The string representation.
public override string ToString()
{
if (this.IsAuto)
{
return "Auto";
}
string s = this.value.ToString();
return this.IsStar ? s + "*" : s;
}
}
}