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