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