**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Drawing / System.Drawing / SizeF.cs
blob18598036629ff4758ec7a4c4b9c251899ec86e87
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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 using System;
35 using System.Runtime.InteropServices;
36 using System.ComponentModel;
38 namespace System.Drawing
40 [Serializable]
41 [ComVisible (true)]
42 public struct SizeF
44 // Private height and width fields.
45 private float wd, ht;
47 // -----------------------
48 // Public Shared Members
49 // -----------------------
51 /// <summary>
52 /// Empty Shared Field
53 /// </summary>
54 ///
55 /// <remarks>
56 /// An uninitialized SizeF Structure.
57 /// </remarks>
59 public static readonly SizeF Empty;
61 /// <summary>
62 /// Addition Operator
63 /// </summary>
64 ///
65 /// <remarks>
66 /// Addition of two SizeF structures.
67 /// </remarks>
69 public static SizeF operator + (SizeF sz1, SizeF sz2)
71 return new SizeF (sz1.Width + sz2.Width,
72 sz1.Height + sz2.Height);
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>
85 public static bool operator == (SizeF sz_a, SizeF sz_b)
87 return ((sz_a.Width == sz_b.Width) &&
88 (sz_a.Height == sz_b.Height));
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>
101 public static bool operator != (SizeF sz_a, SizeF sz_b)
103 return ((sz_a.Width != sz_b.Width) ||
104 (sz_a.Height != sz_b.Height));
107 /// <summary>
108 /// Subtraction Operator
109 /// </summary>
111 /// <remarks>
112 /// Subtracts two SizeF structures.
113 /// </remarks>
115 public static SizeF operator - (SizeF sz1, SizeF sz2)
117 return new SizeF (sz1.Width - sz2.Width,
118 sz1.Height - sz2.Height);
121 /// <summary>
122 /// SizeF to PointF Conversion
123 /// </summary>
125 /// <remarks>
126 /// Returns a PointF based on the dimensions of a given
127 /// SizeF. Requires explicit cast.
128 /// </remarks>
130 public static explicit operator PointF (SizeF sz)
132 return new PointF (sz.Width, sz.Height);
136 // -----------------------
137 // Public Constructors
138 // -----------------------
140 /// <summary>
141 /// SizeF Constructor
142 /// </summary>
144 /// <remarks>
145 /// Creates a SizeF from a PointF value.
146 /// </remarks>
148 public SizeF (PointF pt)
150 wd = pt.X;
151 ht = pt.Y;
154 /// <summary>
155 /// SizeF Constructor
156 /// </summary>
158 /// <remarks>
159 /// Creates a SizeF from an existing SizeF value.
160 /// </remarks>
162 public SizeF (SizeF sz)
164 wd = sz.Width;
165 ht = sz.Height;
168 /// <summary>
169 /// SizeF Constructor
170 /// </summary>
172 /// <remarks>
173 /// Creates a SizeF from specified dimensions.
174 /// </remarks>
176 public SizeF (float width, float height)
178 wd = width;
179 ht = height;
182 // -----------------------
183 // Public Instance Members
184 // -----------------------
186 /// <summary>
187 /// IsEmpty Property
188 /// </summary>
190 /// <remarks>
191 /// Indicates if both Width and Height are zero.
192 /// </remarks>
194 [Browsable (false)]
195 public bool IsEmpty {
196 get {
197 return ((wd == 0.0) && (ht == 0.0));
201 /// <summary>
202 /// Width Property
203 /// </summary>
205 /// <remarks>
206 /// The Width coordinate of the SizeF.
207 /// </remarks>
209 public float Width {
210 get {
211 return wd;
213 set {
214 wd = value;
218 /// <summary>
219 /// Height Property
220 /// </summary>
222 /// <remarks>
223 /// The Height coordinate of the SizeF.
224 /// </remarks>
226 public float Height {
227 get {
228 return ht;
230 set {
231 ht = value;
235 /// <summary>
236 /// Equals Method
237 /// </summary>
239 /// <remarks>
240 /// Checks equivalence of this SizeF and another object.
241 /// </remarks>
243 public override bool Equals (object o)
245 if (!(o is SizeF))
246 return false;
248 return (this == (SizeF) o);
251 /// <summary>
252 /// GetHashCode Method
253 /// </summary>
255 /// <remarks>
256 /// Calculates a hashing value.
257 /// </remarks>
259 public override int GetHashCode ()
261 return (int) wd ^ (int) ht;
264 public PointF ToPointF ()
266 return new PointF (wd, ht);
269 public Size ToSize ()
271 int w, h;
272 checked {
273 w = (int) wd;
274 h = (int) ht;
277 return new Size (w, h);
280 /// <summary>
281 /// ToString Method
282 /// </summary>
284 /// <remarks>
285 /// Formats the SizeF as a string in coordinate notation.
286 /// </remarks>
288 public override string ToString ()
290 return String.Format ("{{Width={0}, Height={1}}}", wd, ht);