[System.Drawing] Add ifdefs to source code used by Xamarin.iOS/Mac to make it compile...
[mono.git] / mcs / class / System.Drawing / System.Drawing / SizeF.cs
1 //
2 // System.Drawing.SizeF.cs
3 //
4 // Author:
5 //   Mike Kestner (mkestner@speakeasy.net)
6 //
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Globalization;
36 using System.Runtime.InteropServices;
37 using System.ComponentModel;
38
39 namespace System.Drawing
40 {
41         [Serializable]
42         [ComVisible (true)]
43 #if !MONOTOUCH && !MONOMAC
44         [TypeConverter (typeof (SizeFConverter))]
45 #endif
46         public struct SizeF
47         {
48                 // Private height and width fields.
49                 private float width, height;
50
51                 // -----------------------
52                 // Public Shared Members
53                 // -----------------------
54
55                 /// <summary>
56                 ///     Empty Shared Field
57                 /// </summary>
58                 ///
59                 /// <remarks>
60                 ///     An uninitialized SizeF Structure.
61                 /// </remarks>
62                 
63                 public static readonly SizeF Empty;
64
65                 /// <summary>
66                 ///     Addition Operator
67                 /// </summary>
68                 ///
69                 /// <remarks>
70                 ///     Addition of two SizeF structures.
71                 /// </remarks>
72
73                 public static SizeF operator + (SizeF sz1, SizeF sz2)
74                 {
75                         return new SizeF (sz1.Width + sz2.Width, 
76                                           sz1.Height + sz2.Height);
77                 }
78                 
79                 /// <summary>
80                 ///     Equality Operator
81                 /// </summary>
82                 ///
83                 /// <remarks>
84                 ///     Compares two SizeF objects. The return value is
85                 ///     based on the equivalence of the Width and Height 
86                 ///     properties of the two Sizes.
87                 /// </remarks>
88
89                 public static bool operator == (SizeF sz1, SizeF sz2)
90                 {
91                         return ((sz1.Width == sz2.Width) && 
92                                 (sz1.Height == sz2.Height));
93                 }
94                 
95                 /// <summary>
96                 ///     Inequality Operator
97                 /// </summary>
98                 ///
99                 /// <remarks>
100                 ///     Compares two SizeF objects. The return value is
101                 ///     based on the equivalence of the Width and Height 
102                 ///     properties of the two Sizes.
103                 /// </remarks>
104
105                 public static bool operator != (SizeF sz1, SizeF sz2)
106                 {
107                         return ((sz1.Width != sz2.Width) || 
108                                 (sz1.Height != sz2.Height));
109                 }
110                 
111                 /// <summary>
112                 ///     Subtraction Operator
113                 /// </summary>
114                 ///
115                 /// <remarks>
116                 ///     Subtracts two SizeF structures.
117                 /// </remarks>
118
119                 public static SizeF operator - (SizeF sz1, SizeF sz2)
120                 {
121                         return new SizeF (sz1.Width - sz2.Width, 
122                                           sz1.Height - sz2.Height);
123                 }
124                 
125                 /// <summary>
126                 ///     SizeF to PointF Conversion
127                 /// </summary>
128                 ///
129                 /// <remarks>
130                 ///     Returns a PointF based on the dimensions of a given 
131                 ///     SizeF. Requires explicit cast.
132                 /// </remarks>
133
134                 public static explicit operator PointF (SizeF size)
135                 {
136                         return new PointF (size.Width, size.Height);
137                 }
138
139
140                 // -----------------------
141                 // Public Constructors
142                 // -----------------------
143
144                 /// <summary>
145                 ///     SizeF Constructor
146                 /// </summary>
147                 ///
148                 /// <remarks>
149                 ///     Creates a SizeF from a PointF value.
150                 /// </remarks>
151                 
152                 public SizeF (PointF pt)
153                 {
154                         width = pt.X;
155                         height = pt.Y;
156                 }
157
158                 /// <summary>
159                 ///     SizeF Constructor
160                 /// </summary>
161                 ///
162                 /// <remarks>
163                 ///     Creates a SizeF from an existing SizeF value.
164                 /// </remarks>
165                 
166                 public SizeF (SizeF size)
167                 {
168                         width = size.Width;
169                         height = size.Height;
170                 }
171
172                 /// <summary>
173                 ///     SizeF Constructor
174                 /// </summary>
175                 ///
176                 /// <remarks>
177                 ///     Creates a SizeF from specified dimensions.
178                 /// </remarks>
179                 
180                 public SizeF (float width, float height)
181                 {
182                         this.width = width;
183                         this.height = height;
184                 }
185
186                 // -----------------------
187                 // Public Instance Members
188                 // -----------------------
189
190                 /// <summary>
191                 ///     IsEmpty Property
192                 /// </summary>
193                 ///
194                 /// <remarks>
195                 ///     Indicates if both Width and Height are zero.
196                 /// </remarks>
197                 
198                 [Browsable (false)]
199                 public bool IsEmpty {
200                         get {
201                                 return ((width == 0.0) && (height == 0.0));
202                         }
203                 }
204
205                 /// <summary>
206                 ///     Width Property
207                 /// </summary>
208                 ///
209                 /// <remarks>
210                 ///     The Width coordinate of the SizeF.
211                 /// </remarks>
212                 
213                 public float Width {
214                         get {
215                                 return width;
216                         }
217                         set {
218                                 width = value;
219                         }
220                 }
221
222                 /// <summary>
223                 ///     Height Property
224                 /// </summary>
225                 ///
226                 /// <remarks>
227                 ///     The Height coordinate of the SizeF.
228                 /// </remarks>
229                 
230                 public float Height {
231                         get {
232                                 return height;
233                         }
234                         set {
235                                 height = value;
236                         }
237                 }
238
239                 /// <summary>
240                 ///     Equals Method
241                 /// </summary>
242                 ///
243                 /// <remarks>
244                 ///     Checks equivalence of this SizeF and another object.
245                 /// </remarks>
246                 
247                 public override bool Equals (object obj)
248                 {
249                         if (!(obj is SizeF))
250                                 return false;
251
252                         return (this == (SizeF) obj);
253                 }
254
255                 /// <summary>
256                 ///     GetHashCode Method
257                 /// </summary>
258                 ///
259                 /// <remarks>
260                 ///     Calculates a hashing value.
261                 /// </remarks>
262                 
263                 public override int GetHashCode ()
264                 {
265                         return (int) width ^ (int) height;
266                 }
267
268                 public PointF ToPointF ()
269                 {
270                         return new PointF (width, height);
271                 }
272
273                 public Size ToSize ()
274                 {
275                         int w, h;
276                         checked {
277                                 w = (int) width;
278                                 h = (int) height;
279                         }
280
281                         return new Size (w, h);
282                 }
283
284                 /// <summary>
285                 ///     ToString Method
286                 /// </summary>
287                 ///
288                 /// <remarks>
289                 ///     Formats the SizeF as a string in coordinate notation.
290                 /// </remarks>
291                 
292                 public override string ToString ()
293                 {
294                         return string.Format ("{{Width={0}, Height={1}}}", width.ToString (CultureInfo.CurrentCulture),
295                                 height.ToString (CultureInfo.CurrentCulture));
296                 }
297
298                 public static SizeF Add (SizeF sz1, SizeF sz2)
299                 {
300                         return new SizeF (sz1.Width + sz2.Width, 
301                                           sz1.Height + sz2.Height);
302                 }
303                 
304                 public static SizeF Subtract (SizeF sz1, SizeF sz2)
305                 {
306                         return new SizeF (sz1.Width - sz2.Width, 
307                                           sz1.Height - sz2.Height);
308                 }
309         }
310 }