2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / WindowsBase / System.Windows / Size.cs
blobd5345f59d40e61b7fda7fd35bc327a6428f0b9c6
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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)
22 // Authors:
23 // Chris Toshok (toshok@novell.com)
26 using System;
27 using System.ComponentModel;
28 using System.Windows.Converters;
29 using System.Windows.Markup;
31 namespace System.Windows {
33 [Serializable]
34 [ValueSerializer (typeof (SizeValueSerializer))]
35 [TypeConverter (typeof (SizeConverter))]
36 public struct Size : IFormattable
38 public Size (double width, double height)
40 if (width < 0 || height < 0)
41 throw new ArgumentException ("Width and Height must be non-negative.");
43 this.width = width;
44 this.height = height;
47 public bool Equals (Size value)
49 return width == value.Width && height == value.Height;
52 public override bool Equals (object o)
54 if (!(o is Size))
55 return false;
57 return Equals ((Size)o);
60 public static bool Equals (Size size1, Size size2)
62 return size1.Equals (size2);
65 public override int GetHashCode ()
67 throw new NotImplementedException ();
70 public static Size Parse (string source)
72 throw new NotImplementedException ();
75 public override string ToString ()
77 if (IsEmpty)
78 return "Empty";
79 return String.Format ("{0},{1}", width, height);
82 public string ToString (IFormatProvider formatProvider)
84 throw new NotImplementedException ();
87 string IFormattable.ToString (string format, IFormatProvider provider)
89 throw new NotImplementedException ();
92 public bool IsEmpty {
93 get {
94 return (width == Double.NegativeInfinity &&
95 height == Double.NegativeInfinity);
99 public double Height {
100 get { return height; }
101 set {
102 if (IsEmpty)
103 throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
105 if (value < 0)
106 throw new ArgumentException ("height must be non-negative.");
108 height = value;
112 public double Width {
113 get { return width; }
114 set {
115 if (IsEmpty)
116 throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
118 if (value < 0)
119 throw new ArgumentException ("width must be non-negative.");
121 width = value;
125 public static Size Empty {
126 get {
127 Size s = new Size ();
128 s.width = s.height = Double.NegativeInfinity;
129 return s;
133 /* operators */
134 public static explicit operator Point (Size size)
136 return new Point (size.Width, size.Height);
139 public static explicit operator Vector (Size size)
141 return new Vector (size.Width, size.Height);
144 public static bool operator ==(Size size1, Size size2)
146 return size1.Equals (size2);
149 public static bool operator !=(Size size1, Size size2)
151 return !size1.Equals (size2);
154 double width;
155 double height;