[System.Drawing] Add ifdefs to source code used by Xamarin.iOS/Mac to make it compile...
[mono.git] / mcs / class / System.Drawing / System.Drawing / Rectangle.cs
index e2828322bbe9b917bedc82ffb4b2e39e04c668cd..c2ffbc80e5e647b589ed5aec55c96c1fb0703439 100644 (file)
@@ -4,21 +4,47 @@
 // Author:
 //   Mike Kestner (mkestner@speakeasy.net)
 //
-// (C) 2001 Mike Kestner
+// Copyright (C) 2001 Mike Kestner
+// Copyright (C) 2004 Novell, Inc.  http://www.novell.com 
+//
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System;
 using System.Runtime.InteropServices;
 using System.ComponentModel;
 
-namespace System.Drawing {
-
+namespace System.Drawing
+{
        [Serializable]
-       [StructLayout(LayoutKind.Sequential)]
        [ComVisible (true)]
-       [TypeConverter(typeof(RectangleConverter))]
-       public struct Rectangle { 
-               int x, y, width, height;
+#if !MONOTOUCH && !MONOMAC
+       [TypeConverter (typeof (RectangleConverter))]
+#endif
+       public struct Rectangle
+       {
+               private int x, y, width, height;
 
                /// <summary>
                ///     Empty Shared Field
@@ -30,6 +56,7 @@ namespace System.Drawing {
                
                public static readonly Rectangle Empty;
 
+
                /// <summary>
                ///     Ceiling Shared Method
                /// </summary>
@@ -106,12 +133,12 @@ namespace System.Drawing {
                ///     Inflates the Rectangle by a specified Size.
                /// </remarks>
                
-               public void Inflate (Size sz)
+               public void Inflate (Size size)
                {
-                       x -= sz.Width;
-                       y -= sz.Height;
-                       Width += sz.Width * 2;
-                       Height += sz.Height * 2;
+                       x -= size.Width;
+                       y -= size.Height;
+                       Width += size.Width * 2;
+                       Height += size.Height * 2;
                }
 
                /// <summary>
@@ -123,11 +150,18 @@ namespace System.Drawing {
                ///     Rectangles. Returns null if there is no intersection.
                /// </remarks>
                
-               public static Rectangle Intersect (Rectangle r1, Rectangle r2)
+               public static Rectangle Intersect (Rectangle a, Rectangle b)
                {
-                       Rectangle r = new Rectangle (r1.Location, r1.Size);
-                       r.Intersect (r2);
-                       return r;
+                       // MS.NET returns a non-empty rectangle if the two rectangles
+                       // touch each other
+                       if (!a.IntersectsWithInclusive (b))
+                               return Empty;
+
+                       return Rectangle.FromLTRB (
+                               Math.Max (a.Left, b.Left),
+                               Math.Max (a.Top, b.Top),
+                               Math.Min (a.Right, b.Right),
+                               Math.Min (a.Bottom, b.Bottom));
                }
 
                /// <summary>
@@ -139,19 +173,9 @@ namespace System.Drawing {
                ///     and another Rectangle.
                /// </remarks>
                
-               public void Intersect (Rectangle r)
+               public void Intersect (Rectangle rect)
                {
-                       if (!IntersectsWith (r)) {
-                               x = 0;
-                               y = 0;
-                               width = 0;
-                               height = 0;
-                       }
-
-                       x = Math.Max (Left, r.Left);
-                       y = Math.Max (Top, r.Top);
-                       width = Math.Min (Right, r.Right) - X;
-                       height = Math.Min (Bottom, r.Bottom) - Y;
+                       this = Rectangle.Intersect (this, rect);
                }
 
                /// <summary>
@@ -209,12 +233,12 @@ namespace System.Drawing {
                ///     Rectangles.
                /// </remarks>
                
-               public static Rectangle Union (Rectangle r1, Rectangle r2)
+               public static Rectangle Union (Rectangle a, Rectangle b)
                {
-                       return FromLTRB (Math.Min (r1.Left, r2.Left),
-                                        Math.Min (r1.Top, r2.Top),
-                                        Math.Max (r1.Right, r2.Right),
-                                        Math.Max (r1.Bottom, r2.Bottom));
+                       return FromLTRB (Math.Min (a.Left, b.Left),
+                                        Math.Min (a.Top, b.Top),
+                                        Math.Max (a.Right, b.Right),
+                                        Math.Max (a.Bottom, b.Bottom));
                }
 
                /// <summary>
@@ -227,10 +251,10 @@ namespace System.Drawing {
                ///     properties of the two Rectangles.
                /// </remarks>
 
-               public static bool operator == (Rectangle r1, Rectangle r2)
+               public static bool operator == (Rectangle left, Rectangle right)
                {
-                       return ((r1.Location == r2.Location) && 
-                               (r1.Size == r2.Size));
+                       return ((left.Location == right.Location) && 
+                               (left.Size == right.Size));
                }
                
                /// <summary>
@@ -243,10 +267,10 @@ namespace System.Drawing {
                ///     properties of the two Rectangles.
                /// </remarks>
 
-               public static bool operator != (Rectangle r1, Rectangle r2)
+               public static bool operator != (Rectangle left, Rectangle right)
                {
-                       return ((r1.Location != r2.Location) || 
-                               (r1.Size != r2.Size));
+                       return ((left.Location != right.Location) || 
+                               (left.Size != right.Size));
                }
                
 
@@ -262,12 +286,12 @@ namespace System.Drawing {
                ///     Creates a Rectangle from Point and Size values.
                /// </remarks>
                
-               public Rectangle (Point loc, Size sz)
+               public Rectangle (Point location, Size size)
                {
-                       x = loc.X;
-                       y = loc.Y;
-                       width = sz.Width;
-                       height = sz.Height;
+                       x = location.X;
+                       y = location.Y;
+                       width = size.Width;
+                       height = size.Height;
                }
 
                /// <summary>
@@ -328,15 +352,11 @@ namespace System.Drawing {
                ///
                /// <remarks>
                ///     Indicates if the width or height are zero. Read only.
-               /// </remarks>
-               // LAMESPEC: Documentation says "This property returns true if 
-               // the Width, Height, X, and Y properties of this RectangleF all 
-               // have values of zero; otherwise, false.". Reality returns TRUE if
-               // width or height are equal 0          
+               /// </remarks>          
                [Browsable (false)]
                public bool IsEmpty {
                        get {
-                               return ((width == 0) || (height == 0));
+                               return ((x == 0) && (y == 0) && (width == 0) && (height == 0));
                        }
                }
 
@@ -487,8 +507,8 @@ namespace System.Drawing {
                
                public bool Contains (int x, int y)
                {
-                       return ((x >= Left) && (x <= Right) && 
-                               (y >= Top) && (y <= Bottom));
+                       return ((x >= Left) && (x < Right) && 
+                               (y >= Top) && (y < Bottom));
                }
 
                /// <summary>
@@ -526,12 +546,12 @@ namespace System.Drawing {
                ///     Checks equivalence of this Rectangle and another object.
                /// </remarks>
                
-               public override bool Equals (object o)
+               public override bool Equals (object obj)
                {
-                       if (!(o is Rectangle))
+                       if (!(obj is Rectangle))
                                return false;
 
-                       return (this == (Rectangle) o);
+                       return (this == (Rectangle) obj);
                }
 
                /// <summary>
@@ -555,7 +575,13 @@ namespace System.Drawing {
                ///     Checks if a Rectangle intersects with this one.
                /// </remarks>
                
-               public bool IntersectsWith (Rectangle r)
+               public bool IntersectsWith (Rectangle rect)
+               {
+                       return !((Left >= rect.Right) || (Right <= rect.Left) ||
+                           (Top >= rect.Bottom) || (Bottom <= rect.Top));
+               }
+
+               private bool IntersectsWithInclusive (Rectangle r)
                {
                        return !((Left > r.Right) || (Right < r.Left) ||
                            (Top > r.Bottom) || (Bottom < r.Top));
@@ -569,10 +595,10 @@ namespace System.Drawing {
                ///     Moves the Rectangle a specified distance.
                /// </remarks>
 
-               public void Offset (int dx, int dy)
+               public void Offset (int x, int y)
                {
-                       x += dx;
-                       y += dy;
+                       this.x += x;
+                       this.y += y;
                }
                
                /// <summary>
@@ -583,10 +609,10 @@ namespace System.Drawing {
                ///     Moves the Rectangle a specified distance.
                /// </remarks>
 
-               public void Offset (Point pt)
+               public void Offset (Point pos)
                {
-                       x += pt.X;
-                       y += pt.Y;
+                       x += pos.X;
+                       y += pos.Y;
                }
                
                /// <summary>