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