[Gtk-sharp-list] [PATCH] Extra APIs for Point and Rectangle
Ben Maurer
bmaurer@ximian.com
Sun, 06 Feb 2005 20:50:30 -0500
--=-fYScq9pCYcDm4FaWswEe
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Hey,
When I was using Gdk's Point and Rectangle, I noticed the class to be a
bit lacking in terms of helper methods. This patch provides many of the
same api's as with System.Drawing for Gdk's classes. I generally stuck
to the S.D API, though made a few additions (for example, I added a few
overloads of Offset and one of Inflate).
I have not written docs, as I want to get comments on the API first. But
I will write stuff before it is checked in.
-- Ben
--=-fYScq9pCYcDm4FaWswEe
Content-Disposition: attachment; filename=gtk-gdk-point-rect-apis.patch
Content-Type: text/x-patch; name=gtk-gdk-point-rect-apis.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit
Index: Point.custom
===================================================================
--- Point.custom (revision 40225)
+++ Point.custom (working copy)
@@ -1,10 +1,14 @@
// Gdk.Point.custom - Gdk Point class customizations
//
-// Author: Jasper van Putten <Jaspervp@gmx.net>
-// Author: Martin Willemoes Hansen <mwh@sysrq.dk>
+// Authors:
+// Jasper van Putten <Jaspervp@gmx.net>
+// Martin Willemoes Hansen <mwh@sysrq.dk>
+// Ben Maurer <bmaurer@novell.com>
+// Contains lots of c&p from System.Drawing
//
// Copyright (c) 2002 Jasper van Putten
// Copyright (c) 2003 Martin Willemoes Hansen
+// Copyright (c) 2005 Novell, Inc
//
// This code is inserted after the automatically generated code.
//
@@ -22,30 +26,69 @@
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
-/// <summary>
-/// ToString method
-/// </summary>
-///
-/// <remarks>
-/// returns a string representation of this point
-/// </remarks>
-
public override string ToString ()
{
return String.Format ("({0},{1})", X, Y);
}
-/// <summary>
-/// Point Constructor
-/// </summary>
-///
-/// <remarks>
-/// Constructs a new Point with the specified
-/// coordinates.
-/// </remarks>
-
public Point (int x, int y)
{
- this.X = x;
- this.Y = y;
+ this.X = x;
+ this.Y = y;
}
+
+public Point (Size sz)
+{
+ this.X = sz.Width;
+ this.Y = sz.Height;
+}
+
+public override bool Equals (object o)
+{
+ if (!(o is Point))
+ return false;
+
+ return (this == (Point) o);
+}
+
+public override int GetHashCode ()
+{
+ return X ^ Y;
+}
+
+public void Offset (int dx, int dy)
+{
+ X += dx;
+ Y += dy;
+}
+
+public bool IsEmpty {
+ get {
+ return ((X == 0) && (Y == 0));
+ }
+}
+
+public static explicit operator Size (Point pt)
+{
+ return new Size (pt.X, pt.Y);
+}
+
+public static Point operator + (Point pt, Size sz)
+{
+ return new Point (pt.X + sz.Width, pt.Y + sz.Height);
+}
+
+public static Point operator - (Point pt, Size sz)
+{
+ return new Point (pt.X - sz.Width, pt.Y - sz.Height);
+}
+
+public static bool operator == (Point pt_a, Point pt_b)
+{
+ return ((pt_a.X == pt_b.X) && (pt_a.Y == pt_b.Y));
+}
+
+public static bool operator != (Point pt_a, Point pt_b)
+{
+ return ((pt_a.X != pt_b.X) || (pt_a.Y != pt_b.Y));
+}
Index: Rectangle.custom
===================================================================
--- Rectangle.custom (revision 40225)
+++ Rectangle.custom (working copy)
@@ -1,8 +1,12 @@
// Gdk.Point.Rectangle - Gdk Rectangle class customizations
//
-// Author: Jasper van Putten <Jaspervp@gmx.net>
+// Authors:
+// Jasper van Putten <Jaspervp@gmx.net>
+// Ben Maurer <bmaurer@novell.com>
+// Contains lots of c&p from System.Drawing
//
// Copyright (c) 2002 Jasper van Putten
+// Copyright (c) 2005 Novell, Inc
//
// This code is inserted after the automatically generated code.
//
@@ -21,20 +25,12 @@
// Boston, MA 02111-1307, USA.
-/// <summary>
-/// ToString method
-/// </summary>
-///
-/// <remarks>
-/// returns a string representation of this Rectangle
-///
-/// </remarks>
-
public override string ToString ()
{
return String.Format ("{0}x{1}+{2}+{3}", Width, Height, X, Y);
}
+// constructors
public Rectangle (int x, int y, int width, int height)
{
this.X = x;
@@ -43,3 +39,174 @@
this.Height = height;
}
+public Rectangle (Point loc, Size sz) : this (loc.X, loc.Y, sz.Width, sz.Height) {}
+
+public static Rectangle FromLTRB (int left, int top, int right, int bottom)
+{
+ return new Rectangle (left, top, right - left,
+ bottom - top);
+}
+
+// Equality
+public override bool Equals (object o)
+{
+ if (!(o is Rectangle))
+ return false;
+
+ return (this == (Rectangle) o);
+}
+
+public override int GetHashCode ()
+{
+ return (Height + Width) ^ X + Y;
+}
+
+public static bool operator == (Rectangle r1, Rectangle r2)
+{
+ return ((r1.Location == r2.Location) && (r1.Size == r2.Size));
+}
+
+public static bool operator != (Rectangle r1, Rectangle r2)
+{
+ return !(r1 == r2);
+}
+
+// Hit Testing / Intersection / Union
+public bool Contains (Rectangle rect)
+{
+ return (rect == Intersect (this, rect));
+}
+
+public bool Contains (Point pt)
+{
+ return Contains (pt.X, pt.Y);
+}
+
+public bool Contains (int x, int y)
+{
+ return ((x >= Left) && (x <= Right) &&
+ (y >= Top) && (y <= Bottom));
+}
+
+public bool IntersectsWith (Rectangle r)
+{
+ return !((Left > r.Right) || (Right < r.Left) ||
+ (Top > r.Bottom) || (Bottom < r.Top));
+}
+
+public static Rectangle Union (Rectangle r1, Rectangle r2)
+{
+ 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));
+}
+
+public void Intersect (Rectangle r)
+{
+ 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;
+}
+
+public static Rectangle Intersect (Rectangle r1, Rectangle r2)
+{
+ Rectangle r = r1;
+ r.Intersect (r2);
+ return r;
+}
+
+// Position/Size
+public int Top {
+ get { return Y; }
+}
+public int Bottom {
+ get { return Y + Height; }
+}
+public int Right {
+ get { return X + Width; }
+}
+public int Left {
+ get { return X; }
+}
+
+public bool IsEmpty {
+ get { return (Width == 0) || (Height == 0); }
+}
+
+public Size Size {
+ get {
+ return new Size (Width, Height);
+ }
+ set {
+ Width = value.Width;
+ Height = value.Height;
+ }
+}
+
+public Point Location {
+ get {
+ return new Point (X, Y);
+ }
+ set {
+ X = value.X;
+ Y = value.Y;
+ }
+}
+
+// Inflate and Offset
+public void Inflate (Size sz)
+{
+ Inflate (sz.Width, sz.Height);
+}
+
+public void Inflate (int width, int height)
+{
+ X -= width;
+ Y -= height;
+ Width += width * 2;
+ Height += height * 2;
+}
+
+public static Rectangle Inflate (Rectangle rect, int x, int y)
+{
+ Rectangle r = rect;
+ r.Inflate (x, y);
+ return r;
+}
+
+public static Rectangle Inflate (Rectangle rect, Size sz)
+{
+ return Inflate (rect, sz.Width, sz.Height);
+}
+
+public void Offset (int dx, int dy)
+{
+ X += dx;
+ Y += dy;
+}
+
+public void Offset (Point dr)
+{
+ Offset (dr.X, dr.Y);
+}
+
+public static Rectangle Offset (Rectangle rect, int dx, int dy)
+{
+ Rectangle r = rect;
+ r.Offset (dx, dy);
+ return r;
+}
+
+public static Rectangle Offset (Rectangle rect, Point dr)
+{
+ return Offset (rect, dr.X, dr.Y);
+}
--=-fYScq9pCYcDm4FaWswEe--