1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Chris Toshok (toshok@novell.com)
27 using System.ComponentModel;
28 using System.Globalization;
29 using System.Windows.Converters;
30 using System.Windows.Markup;
32 namespace System.Windows {
35 [ValueSerializer (typeof (SizeValueSerializer))]
36 [TypeConverter (typeof (SizeConverter))]
37 public struct Size : IFormattable
39 public Size (double width, double height)
41 if (width < 0 || height < 0)
42 throw new ArgumentException ("Width and Height must be non-negative.");
45 this._height = height;
48 public bool Equals (Size value)
50 return _width == value.Width && _height == value.Height;
53 public override bool Equals (object o)
58 return Equals ((Size)o);
61 public static bool Equals (Size size1, Size size2)
63 return size1.Equals (size2);
66 public override int GetHashCode ()
70 return (_width.GetHashCode () * 397) ^ _height.GetHashCode ();
74 public static Size Parse (string source)
77 throw new ArgumentNullException ("source");
79 if (source.Trim () == "Empty")
85 var parts = source.Split (',');
86 if (parts.Length != 2)
87 throw new FormatException (string.Format ("Invalid Size format: {0}", source));
90 if (double.TryParse (parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out width)
91 && double.TryParse (parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out height))
93 value = new Size (width, height);
97 throw new FormatException (string.Format ("Invalid Size format: {0}", source));
103 public override string ToString ()
105 return ConvertToString (null);
108 public string ToString (IFormatProvider provider)
110 return ConvertToString (provider);
113 string IFormattable.ToString (string format, IFormatProvider provider)
115 return ConvertToString (provider);
118 private string ConvertToString (IFormatProvider provider)
120 return IsEmpty ? "Empty" : string.Concat (_width, ",", _height);
123 public bool IsEmpty {
125 return (_width == Double.NegativeInfinity &&
126 _height == Double.NegativeInfinity);
130 public double Height {
131 get { return _height; }
134 throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
137 throw new ArgumentException ("height must be non-negative.");
143 public double Width {
144 get { return _width; }
147 throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
150 throw new ArgumentException ("width must be non-negative.");
156 public static Size Empty {
158 Size s = new Size ();
159 s._width = s._height = Double.NegativeInfinity;
165 public static explicit operator Point (Size size)
167 return new Point (size.Width, size.Height);
170 public static explicit operator Vector (Size size)
172 return new Vector (size.Width, size.Height);
175 public static bool operator ==(Size size1, Size size2)
177 return size1.Equals (size2);
180 public static bool operator !=(Size size1, Size size2)
182 return !size1.Equals (size2);