4c1c87f29aad44e4bed1beefb00a3ae833418196
[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         [TypeConverter (typeof (SizeFConverter))]
44         public struct SizeF
45         {
46                 // Private height and width fields.
47                 private float width, height;
48
49                 // -----------------------
50                 // Public Shared Members
51                 // -----------------------
52
53                 /// <summary>
54                 ///     Empty Shared Field
55                 /// </summary>
56                 ///
57                 /// <remarks>
58                 ///     An uninitialized SizeF Structure.
59                 /// </remarks>
60                 
61                 public static readonly SizeF Empty;
62
63                 /// <summary>
64                 ///     Addition Operator
65                 /// </summary>
66                 ///
67                 /// <remarks>
68                 ///     Addition of two SizeF structures.
69                 /// </remarks>
70
71                 public static SizeF operator + (SizeF sz1, SizeF sz2)
72                 {
73                         return new SizeF (sz1.Width + sz2.Width, 
74                                           sz1.Height + sz2.Height);
75                 }
76                 
77                 /// <summary>
78                 ///     Equality Operator
79                 /// </summary>
80                 ///
81                 /// <remarks>
82                 ///     Compares two SizeF objects. The return value is
83                 ///     based on the equivalence of the Width and Height 
84                 ///     properties of the two Sizes.
85                 /// </remarks>
86
87                 public static bool operator == (SizeF sz1, SizeF sz2)
88                 {
89                         return ((sz1.Width == sz2.Width) && 
90                                 (sz1.Height == sz2.Height));
91                 }
92                 
93                 /// <summary>
94                 ///     Inequality Operator
95                 /// </summary>
96                 ///
97                 /// <remarks>
98                 ///     Compares two SizeF objects. The return value is
99                 ///     based on the equivalence of the Width and Height 
100                 ///     properties of the two Sizes.
101                 /// </remarks>
102
103                 public static bool operator != (SizeF sz1, SizeF sz2)
104                 {
105                         return ((sz1.Width != sz2.Width) || 
106                                 (sz1.Height != sz2.Height));
107                 }
108                 
109                 /// <summary>
110                 ///     Subtraction Operator
111                 /// </summary>
112                 ///
113                 /// <remarks>
114                 ///     Subtracts two SizeF structures.
115                 /// </remarks>
116
117                 public static SizeF operator - (SizeF sz1, SizeF sz2)
118                 {
119                         return new SizeF (sz1.Width - sz2.Width, 
120                                           sz1.Height - sz2.Height);
121                 }
122                 
123                 /// <summary>
124                 ///     SizeF to PointF Conversion
125                 /// </summary>
126                 ///
127                 /// <remarks>
128                 ///     Returns a PointF based on the dimensions of a given 
129                 ///     SizeF. Requires explicit cast.
130                 /// </remarks>
131
132                 public static explicit operator PointF (SizeF size)
133                 {
134                         return new PointF (size.Width, size.Height);
135                 }
136
137
138                 // -----------------------
139                 // Public Constructors
140                 // -----------------------
141
142                 /// <summary>
143                 ///     SizeF Constructor
144                 /// </summary>
145                 ///
146                 /// <remarks>
147                 ///     Creates a SizeF from a PointF value.
148                 /// </remarks>
149                 
150                 public SizeF (PointF pt)
151                 {
152                         width = pt.X;
153                         height = pt.Y;
154                 }
155
156                 /// <summary>
157                 ///     SizeF Constructor
158                 /// </summary>
159                 ///
160                 /// <remarks>
161                 ///     Creates a SizeF from an existing SizeF value.
162                 /// </remarks>
163                 
164                 public SizeF (SizeF size)
165                 {
166                         width = size.Width;
167                         height = size.Height;
168                 }
169
170                 /// <summary>
171                 ///     SizeF Constructor
172                 /// </summary>
173                 ///
174                 /// <remarks>
175                 ///     Creates a SizeF from specified dimensions.
176                 /// </remarks>
177                 
178                 public SizeF (float width, float height)
179                 {
180                         this.width = width;
181                         this.height = height;
182                 }
183
184                 // -----------------------
185                 // Public Instance Members
186                 // -----------------------
187
188                 /// <summary>
189                 ///     IsEmpty Property
190                 /// </summary>
191                 ///
192                 /// <remarks>
193                 ///     Indicates if both Width and Height are zero.
194                 /// </remarks>
195                 
196                 [Browsable (false)]
197                 public bool IsEmpty {
198                         get {
199                                 return ((width == 0.0) && (height == 0.0));
200                         }
201                 }
202
203                 /// <summary>
204                 ///     Width Property
205                 /// </summary>
206                 ///
207                 /// <remarks>
208                 ///     The Width coordinate of the SizeF.
209                 /// </remarks>
210                 
211                 public float Width {
212                         get {
213                                 return width;
214                         }
215                         set {
216                                 width = value;
217                         }
218                 }
219
220                 /// <summary>
221                 ///     Height Property
222                 /// </summary>
223                 ///
224                 /// <remarks>
225                 ///     The Height coordinate of the SizeF.
226                 /// </remarks>
227                 
228                 public float Height {
229                         get {
230                                 return height;
231                         }
232                         set {
233                                 height = value;
234                         }
235                 }
236
237                 /// <summary>
238                 ///     Equals Method
239                 /// </summary>
240                 ///
241                 /// <remarks>
242                 ///     Checks equivalence of this SizeF and another object.
243                 /// </remarks>
244                 
245                 public override bool Equals (object obj)
246                 {
247                         if (!(obj is SizeF))
248                                 return false;
249
250                         return (this == (SizeF) obj);
251                 }
252
253                 /// <summary>
254                 ///     GetHashCode Method
255                 /// </summary>
256                 ///
257                 /// <remarks>
258                 ///     Calculates a hashing value.
259                 /// </remarks>
260                 
261                 public override int GetHashCode ()
262                 {
263                         return (int) width ^ (int) height;
264                 }
265
266                 public PointF ToPointF ()
267                 {
268                         return new PointF (width, height);
269                 }
270
271                 public Size ToSize ()
272                 {
273                         int w, h;
274                         checked {
275                                 w = (int) width;
276                                 h = (int) height;
277                         }
278
279                         return new Size (w, h);
280                 }
281
282                 /// <summary>
283                 ///     ToString Method
284                 /// </summary>
285                 ///
286                 /// <remarks>
287                 ///     Formats the SizeF as a string in coordinate notation.
288                 /// </remarks>
289                 
290                 public override string ToString ()
291                 {
292                         return string.Format ("{{Width={0}, Height={1}}}", width.ToString (CultureInfo.CurrentCulture),
293                                 height.ToString (CultureInfo.CurrentCulture));
294                 }
295
296                 public static SizeF Add (SizeF sz1, SizeF sz2)
297                 {
298                         return new SizeF (sz1.Width + sz2.Width, 
299                                           sz1.Height + sz2.Height);
300                 }
301                 
302                 public static SizeF Subtract (SizeF sz1, SizeF sz2)
303                 {
304                         return new SizeF (sz1.Width - sz2.Width, 
305                                           sz1.Height - sz2.Height);
306                 }
307         }
308 }