6e78072aa7dbb0ed396a83d4b19c0c9cc2dd0802
[mono.git] / mcs / class / System.Drawing / System.Drawing / Size.cs
1 //
2 // System.Drawing.Size.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.Serialization;
36 using System.Runtime.InteropServices;
37 using System.ComponentModel;
38
39 namespace System.Drawing
40 {
41         [Serializable]
42         [ComVisible (true)]
43         [TypeConverter (typeof (SizeConverter))]
44         public struct Size
45         { 
46                 
47                 // Private Height and width fields.
48                 private int width, height;
49
50                 // -----------------------
51                 // Public Shared Members
52                 // -----------------------
53
54                 /// <summary>
55                 ///     Empty Shared Field
56                 /// </summary>
57                 ///
58                 /// <remarks>
59                 ///     An uninitialized Size Structure.
60                 /// </remarks>
61                 
62                 public static readonly Size Empty;
63
64                 /// <summary>
65                 ///     Ceiling Shared Method
66                 /// </summary>
67                 ///
68                 /// <remarks>
69                 ///     Produces a Size structure from a SizeF structure by
70                 ///     taking the ceiling of the Width and Height properties.
71                 /// </remarks>
72                 
73                 public static Size Ceiling (SizeF value)
74                 {
75                         int w, h;
76                         checked {
77                                 w = (int) Math.Ceiling (value.Width);
78                                 h = (int) Math.Ceiling (value.Height);
79                         }
80
81                         return new Size (w, h);
82                 }
83
84                 /// <summary>
85                 ///     Round Shared Method
86                 /// </summary>
87                 ///
88                 /// <remarks>
89                 ///     Produces a Size structure from a SizeF structure by
90                 ///     rounding the Width and Height properties.
91                 /// </remarks>
92                 
93                 public static Size Round (SizeF value)
94                 {
95                         int w, h;
96                         checked {
97                                 w = (int) Math.Round (value.Width);
98                                 h = (int) Math.Round (value.Height);
99                         }
100
101                         return new Size (w, h);
102                 }
103
104                 /// <summary>
105                 ///     Truncate Shared Method
106                 /// </summary>
107                 ///
108                 /// <remarks>
109                 ///     Produces a Size structure from a SizeF structure by
110                 ///     truncating the Width and Height properties.
111                 /// </remarks>
112                 
113                 public static Size Truncate (SizeF value)
114                 {
115                         int w, h;
116                         checked {
117                                 w = (int) value.Width;
118                                 h = (int) value.Height;
119                         }
120
121                         return new Size (w, h);
122                 }
123
124                 /// <summary>
125                 ///     Addition Operator
126                 /// </summary>
127                 ///
128                 /// <remarks>
129                 ///     Addition of two Size structures.
130                 /// </remarks>
131
132                 public static Size operator + (Size sz1, Size sz2)
133                 {
134                         return new Size (sz1.Width + sz2.Width, 
135                                          sz1.Height + sz2.Height);
136                 }
137                 
138                 /// <summary>
139                 ///     Equality Operator
140                 /// </summary>
141                 ///
142                 /// <remarks>
143                 ///     Compares two Size objects. The return value is
144                 ///     based on the equivalence of the Width and Height 
145                 ///     properties of the two Sizes.
146                 /// </remarks>
147
148                 public static bool operator == (Size sz1, Size sz2)
149                 {
150                         return ((sz1.Width == sz2.Width) && 
151                                 (sz1.Height == sz2.Height));
152                 }
153                 
154                 /// <summary>
155                 ///     Inequality Operator
156                 /// </summary>
157                 ///
158                 /// <remarks>
159                 ///     Compares two Size objects. The return value is
160                 ///     based on the equivalence of the Width and Height 
161                 ///     properties of the two Sizes.
162                 /// </remarks>
163
164                 public static bool operator != (Size sz1, Size sz2)
165                 {
166                         return ((sz1.Width != sz2.Width) || 
167                                 (sz1.Height != sz2.Height));
168                 }
169                 
170                 /// <summary>
171                 ///     Subtraction Operator
172                 /// </summary>
173                 ///
174                 /// <remarks>
175                 ///     Subtracts two Size structures.
176                 /// </remarks>
177
178                 public static Size operator - (Size sz1, Size sz2)
179                 {
180                         return new Size (sz1.Width - sz2.Width, 
181                                          sz1.Height - sz2.Height);
182                 }
183                 
184                 /// <summary>
185                 ///     Size to Point Conversion
186                 /// </summary>
187                 ///
188                 /// <remarks>
189                 ///     Returns a Point based on the dimensions of a given 
190                 ///     Size. Requires explicit cast.
191                 /// </remarks>
192
193                 public static explicit operator Point (Size size)
194                 {
195                         return new Point (size.Width, size.Height);
196                 }
197
198                 /// <summary>
199                 ///     Size to SizeF Conversion
200                 /// </summary>
201                 ///
202                 /// <remarks>
203                 ///     Creates a SizeF based on the dimensions of a given 
204                 ///     Size. No explicit cast is required.
205                 /// </remarks>
206
207                 public static implicit operator SizeF (Size p)
208                 {
209                         return new SizeF (p.Width, p.Height);
210                 }
211
212
213                 // -----------------------
214                 // Public Constructors
215                 // -----------------------
216
217                 /// <summary>
218                 ///     Size Constructor
219                 /// </summary>
220                 ///
221                 /// <remarks>
222                 ///     Creates a Size from a Point value.
223                 /// </remarks>
224                 
225                 public Size (Point pt)
226                 {
227                         width = pt.X;
228                         height = pt.Y;
229                 }
230
231                 /// <summary>
232                 ///     Size Constructor
233                 /// </summary>
234                 ///
235                 /// <remarks>
236                 ///     Creates a Size from specified dimensions.
237                 /// </remarks>
238                 
239                 public Size (int width, int height)
240                 {
241                         this.width = width;
242                         this.height = height;
243                 }
244
245                 // -----------------------
246                 // Public Instance Members
247                 // -----------------------
248
249                 /// <summary>
250                 ///     IsEmpty Property
251                 /// </summary>
252                 ///
253                 /// <remarks>
254                 ///     Indicates if both Width and Height are zero.
255                 /// </remarks>
256                 
257                 [Browsable (false)]
258                 public bool IsEmpty {
259                         get {
260                                 return ((width == 0) && (height == 0));
261                         }
262                 }
263
264                 /// <summary>
265                 ///     Width Property
266                 /// </summary>
267                 ///
268                 /// <remarks>
269                 ///     The Width coordinate of the Size.
270                 /// </remarks>
271                 
272                 public int Width {
273                         get {
274                                 return width;
275                         }
276                         set {
277                                 width = value;
278                         }
279                 }
280
281                 /// <summary>
282                 ///     Height Property
283                 /// </summary>
284                 ///
285                 /// <remarks>
286                 ///     The Height coordinate of the Size.
287                 /// </remarks>
288                 
289                 public int Height {
290                         get {
291                                 return height;
292                         }
293                         set {
294                                 height = value;
295                         }
296                 }
297
298                 /// <summary>
299                 ///     Equals Method
300                 /// </summary>
301                 ///
302                 /// <remarks>
303                 ///     Checks equivalence of this Size and another object.
304                 /// </remarks>
305                 
306                 public override bool Equals (object obj)
307                 {
308                         if (!(obj is Size))
309                                 return false;
310
311                         return (this == (Size) obj);
312                 }
313
314                 /// <summary>
315                 ///     GetHashCode Method
316                 /// </summary>
317                 ///
318                 /// <remarks>
319                 ///     Calculates a hashing value.
320                 /// </remarks>
321                 
322                 public override int GetHashCode ()
323                 {
324                         return width^height;
325                 }
326
327                 /// <summary>
328                 ///     ToString Method
329                 /// </summary>
330                 ///
331                 /// <remarks>
332                 ///     Formats the Size as a string in coordinate notation.
333                 /// </remarks>
334                 
335                 public override string ToString ()
336                 {
337                         return String.Format ("{{Width={0}, Height={1}}}", width, height);
338                 }
339
340                 public static Size Add (Size sz1, Size sz2)
341                 {
342                         return new Size (sz1.Width + sz2.Width, 
343                                          sz1.Height + sz2.Height);
344
345                 }
346                 
347                 public static Size Subtract (Size sz1, Size sz2)
348                 {
349                         return new Size (sz1.Width - sz2.Width, 
350                                          sz1.Height - sz2.Height);
351                 }
352
353         }
354 }