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