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")
83 var tokenizer = new NumericListTokenizer (source, CultureInfo.InvariantCulture);
86 if (!double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out width) ||
87 !double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out height))
89 throw new FormatException (string.Format ("Invalid Size format: {0}", source));
91 if (!tokenizer.HasNoMoreTokens ())
93 throw new InvalidOperationException ("Invalid Size format: " + source);
95 return new Size(width, height);
98 public override string ToString ()
100 return ConvertToString (null, null);
103 public string ToString (IFormatProvider provider)
105 return ConvertToString (null, provider);
108 string IFormattable.ToString (string format, IFormatProvider provider)
110 return ConvertToString (format, provider);
113 private string ConvertToString (string format, IFormatProvider provider)
118 if (provider == null)
119 provider = CultureInfo.CurrentCulture;
121 format = string.Empty;
122 var separator = NumericListTokenizer.GetSeparator (provider);
123 var vectorFormat = string.Format ("{{0:{0}}}{1}{{1:{0}}}", format, separator);
124 return string.Format (provider, vectorFormat, _width, _height);
127 public bool IsEmpty {
129 return (_width == Double.NegativeInfinity &&
130 _height == Double.NegativeInfinity);
134 public double Height {
135 get { return _height; }
138 throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
141 throw new ArgumentException ("height must be non-negative.");
147 public double Width {
148 get { return _width; }
151 throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
154 throw new ArgumentException ("width must be non-negative.");
160 public static Size Empty {
162 Size s = new Size ();
163 s._width = s._height = Double.NegativeInfinity;
169 public static explicit operator Point (Size size)
171 return new Point (size.Width, size.Height);
174 public static explicit operator Vector (Size size)
176 return new Vector (size.Width, size.Height);
179 public static bool operator ==(Size size1, Size size2)
181 return size1.Equals (size2);
184 public static bool operator !=(Size size1, Size size2)
186 return !size1.Equals (size2);