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