[mini] Fix test compiling when running !MOBILE
[mono.git] / mcs / class / WindowsBase / System.Windows / Size.cs
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.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok (toshok@novell.com)
24 //
25
26 using System;
27 using System.ComponentModel;
28 using System.Globalization;
29 using System.Windows.Converters;
30 using System.Windows.Markup;
31
32 namespace System.Windows {
33
34         [Serializable]
35         [ValueSerializer (typeof (SizeValueSerializer))]
36         [TypeConverter (typeof (SizeConverter))]
37         public struct Size : IFormattable
38         {
39                 public Size (double width, double height)
40                 {
41                         if (width < 0 || height < 0)
42                                 throw new ArgumentException ("Width and Height must be non-negative.");
43
44                         this._width = width;
45                         this._height = height;
46                 }
47
48                 public bool Equals (Size value)
49                 {
50                         return _width == value.Width && _height == value.Height;
51                 }
52                 
53                 public override bool Equals (object o)
54                 {
55                         if (!(o is Size))
56                                 return false;
57
58                         return Equals ((Size)o);
59                 }
60
61                 public static bool Equals (Size size1, Size size2)
62                 {
63                         return size1.Equals (size2);
64                 }
65
66                 public override int GetHashCode ()
67                 {
68                         unchecked
69                         {
70                                 return (_width.GetHashCode () * 397) ^ _height.GetHashCode ();
71                         }
72                 }
73
74                 public static Size Parse (string source)
75                 {
76                         if (source == null)
77                                 throw new ArgumentNullException ("source");
78                         Size value;
79                         if (source.Trim () == "Empty")
80                         {
81                                 value = Empty;
82                         }
83                         else
84                         {
85                                 var parts = source.Split (',');
86                                 if (parts.Length != 2)
87                                         throw new FormatException (string.Format ("Invalid Size format: {0}", source));
88                                 double width;
89                                 double height;
90                                 if (double.TryParse (parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out width)
91                                         && double.TryParse (parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out height))
92                                 {
93                                         value = new Size (width, height);
94                                 }
95                                 else
96                                 {
97                                         throw new FormatException (string.Format ("Invalid Size format: {0}", source));
98                                 }
99                         }
100                         return value;
101                 }
102
103                 public override string ToString ()
104                 {
105                         return ConvertToString (null);
106                 }
107
108                 public string ToString (IFormatProvider provider)
109                 {
110                         return ConvertToString (provider);
111                 }
112
113                 string IFormattable.ToString (string format, IFormatProvider provider)
114                 {
115                         return ConvertToString (provider);
116                 }
117
118                 private string ConvertToString (IFormatProvider provider)
119                 {
120                         return IsEmpty ? "Empty" : string.Concat (_width, ",", _height);
121                 }
122
123                 public bool IsEmpty {
124                         get {
125                                 return (_width == Double.NegativeInfinity &&
126                                         _height == Double.NegativeInfinity);
127                         }
128                 }
129
130                 public double Height {
131                         get { return _height; }
132                         set {
133                                 if (IsEmpty)
134                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
135
136                                 if (value < 0)
137                                         throw new ArgumentException ("height must be non-negative.");
138
139                                 _height = value;
140                         }
141                 }
142
143                 public double Width {
144                         get { return _width; }
145                         set {
146                                 if (IsEmpty)
147                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Size.");
148
149                                 if (value < 0)
150                                         throw new ArgumentException ("width must be non-negative.");
151
152                                 _width = value;
153                         }
154                 }
155
156                 public static Size Empty {
157                         get {
158                                 Size s = new Size ();
159                                 s._width = s._height = Double.NegativeInfinity;
160                                 return s;
161                         }
162                 }
163
164                 /* operators */
165                 public static explicit operator Point (Size size)
166                 {
167                         return new Point (size.Width, size.Height);
168                 }
169
170                 public static explicit operator Vector (Size size)
171                 {
172                         return new Vector (size.Width, size.Height);
173                 }
174
175                 public static bool operator ==(Size size1, Size size2)
176                 {
177                         return size1.Equals (size2);
178                 }
179
180                 public static bool operator !=(Size size1, Size size2)
181                 {
182                         return !size1.Equals (size2);
183                 }
184
185                 double _width;
186                 double _height;
187         }
188 }