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