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