File: Rectangle.cs

package info (click to toggle)
gtk-sharp3 2.99.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,488 kB
  • sloc: xml: 308,885; cs: 38,796; sh: 11,336; perl: 1,295; makefile: 1,099; ansic: 903
file content (258 lines) | stat: -rw-r--r-- 5,835 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// 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 program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General 
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.


using System;
using System.Runtime.InteropServices;

namespace Gdk {

	[StructLayout(LayoutKind.Sequential)]
	public struct Rectangle {

		public int X;
		public int Y;
		public int Width;
		public int Height;

		public Rectangle (int x, int y, int width, int height)
		{
			this.X = x;
			this.Y = y;
			this.Width = width;
			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);
		}

		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);
		}
		
		public static explicit operator GLib.Value (Gdk.Rectangle boxed)
		{
			GLib.Value val = GLib.Value.Empty;
			val.Init (Gdk.Rectangle.GType);
			val.Val = boxed;
			return val;
		}

		public static explicit operator Gdk.Rectangle (GLib.Value val)
		{
			return (Gdk.Rectangle) val.Val;
		}

		public override string ToString ()
		{
			return String.Format ("{0}x{1}+{2}+{3}", Width, Height, X, Y);
		}

		// 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)
		{
			this = Intersect (this, r);
		}

		public static Rectangle Intersect (Rectangle r1, Rectangle r2)
		{
			Rectangle r;
			if (!r1.Intersect (r2, out r))
				return new Rectangle ();
			
			return r;
		}

		// Position/Size
		public int Top {
			get { return Y; }
		}
		public int Bottom {
			get { return Y + Height - 1; }
		}
		public int Right {
			get { return X + Width - 1; }
		}
		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);
		}

		[DllImport(Global.GdkNativeDll, CallingConvention = CallingConvention.Cdecl)]
		static extern IntPtr gdk_rectangle_get_type();

		public static GLib.GType GType { 
			get {
				IntPtr raw_ret = gdk_rectangle_get_type();
				GLib.GType ret = new GLib.GType(raw_ret);
				return ret;
			}
		}

		[DllImport(Global.GdkNativeDll, CallingConvention = CallingConvention.Cdecl)]
		static extern void gdk_rectangle_union (ref Rectangle src1, ref Rectangle src2, out Rectangle dest);

		public Gdk.Rectangle Union (Gdk.Rectangle src)
		{
			Gdk.Rectangle dest;
			gdk_rectangle_union (ref this, ref src, out dest);
			return dest;
		}

		[DllImport(Global.GdkNativeDll, CallingConvention = CallingConvention.Cdecl)]
		static extern bool gdk_rectangle_intersect (ref Rectangle src1, ref Rectangle src2, out Rectangle dest);

		public bool Intersect (Gdk.Rectangle src, out Gdk.Rectangle dest)
		{
			return gdk_rectangle_intersect (ref this, ref src, out dest);
		}

		public static Rectangle New (IntPtr raw)
		{
			return (Gdk.Rectangle) Marshal.PtrToStructure (raw, typeof (Gdk.Rectangle));
		}

		public static Rectangle Zero;
	}
}