1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Chris Toshok <toshok@novell.com>
24 // Sebastien Pouliot <sebastien@ximian.com>
28 using System.ComponentModel;
29 using System.Globalization;
31 using System.Windows.Converters;
32 using System.Windows.Markup;
33 using System.Windows.Media;
35 namespace System.Windows {
38 [ValueSerializer (typeof (RectValueSerializer))]
39 [TypeConverter (typeof (RectConverter))]
40 public struct Rect : IFormattable
42 public Rect (Size size)
46 _height = size.Height;
49 public Rect (Point point, Vector vector) : this (point, Point.Add (point, vector))
52 public Rect (Point point1, Point point2)
54 if (point1.X < point2.X) {
56 _width = point2.X - point1.X;
60 _width = point1.X - point2.X;
63 if (point1.Y < point2.Y) {
65 _height = point2.Y - point1.Y;
69 _height = point1.Y - point2.Y;
73 public Rect (double x, double y, double width, double height)
75 if (width < 0 || height < 0)
76 throw new ArgumentException ("width and height must be non-negative.");
80 this._height = height;
83 public Rect (Point location, Size size)
88 _height = size.Height;
91 public bool Equals (Rect value)
93 return (_x == value.X &&
95 _width == value.Width &&
96 _height == value.Height);
99 public static bool operator != (Rect rect1, Rect rect2)
101 return !(rect1.Location == rect2.Location && rect1.Size == rect2.Size);
104 public static bool operator == (Rect rect1, Rect rect2)
106 return rect1.Location == rect2.Location && rect1.Size == rect2.Size;
109 public override bool Equals (object o)
114 return Equals ((Rect)o);
117 public static bool Equals (Rect rect1, Rect rect2)
119 return rect1.Equals (rect2);
122 public override int GetHashCode ()
124 throw new NotImplementedException ();
127 public bool Contains (Rect rect)
129 if (rect.Left < this.Left ||
130 rect.Right > this.Right)
133 if (rect.Top < this.Top ||
134 rect.Bottom > this.Bottom)
140 public bool Contains (double x, double y)
142 if (x < Left || x > Right)
144 if (y < Top || y > Bottom)
150 public bool Contains (Point point)
152 return Contains (point.X, point.Y);
155 public static Rect Inflate (Rect rect, double width, double height)
157 if (width < rect.Width * -2)
159 if (height < rect.Height * -2)
163 result.Inflate (width, height);
167 public static Rect Inflate (Rect rect, Size size)
169 return Rect.Inflate (rect, size.Width, size.Height);
172 public void Inflate (double width, double height)
174 // XXX any error checking like in the static case?
178 this._width += 2*width;
179 this._height += 2*height;
182 public void Inflate (Size size)
184 Inflate (size.Width, size.Height);
187 public bool IntersectsWith(Rect rect)
189 return !((Left >= rect.Right) || (Right <= rect.Left) ||
190 (Top >= rect.Bottom) || (Bottom <= rect.Top));
193 public void Intersect(Rect rect)
195 double _x = Math.Max (this._x, rect._x);
196 double _y = Math.Max (this._y, rect._y);
197 double _width = Math.Min (Right, rect.Right) - _x;
198 double _height = Math.Min (Bottom, rect.Bottom) - _y;
200 if (_width < 0 || _height < 0) {
201 this._x = this._y = Double.PositiveInfinity;
202 this._width = this._height = Double.NegativeInfinity;
207 this._width = _width;
208 this._height = _height;
212 public static Rect Intersect(Rect rect1, Rect rect2)
215 result.Intersect (rect2);
219 public void Offset(double offsetX, double offsetY)
225 public static Rect Offset(Rect rect, double offsetX, double offsetY)
228 result.Offset (offsetX, offsetY);
232 public void Offset (Vector offsetVector)
234 _x += offsetVector.X;
235 _y += offsetVector.Y;
238 public static Rect Offset (Rect rect, Vector offsetVector)
241 result.Offset (offsetVector);
245 public void Scale(double scaleX, double scaleY)
253 public void Transform (Matrix matrix)
255 throw new NotImplementedException ();
258 public static Rect Transform (Rect rect, Matrix matrix)
261 result.Transform (matrix);
265 public static Rect Union(Rect rect1, Rect rect2)
268 result.Union (rect2);
272 public static Rect Union(Rect rect, Point point)
275 result.Union (point);
279 public void Union(Rect rect)
281 var left = Math.Min (Left, rect.Left);
282 var top = Math.Min (Top, rect.Top);
283 var right = Math.Max (Right, rect.Right);
284 var bottom = Math.Max (Bottom, rect.Bottom);
288 _width = right - left;
289 _height = bottom - top;
292 public void Union(Point point)
294 Union (new Rect (point, point));
297 public static Rect Parse (string source)
299 throw new NotImplementedException ();
302 public override string ToString ()
304 return ToString (null);
307 public string ToString (IFormatProvider provider)
309 return ToString (null, provider);
312 string IFormattable.ToString (string format, IFormatProvider provider)
314 return ToString (format, provider);
317 private string ToString (string format, IFormatProvider provider)
322 if (provider == null)
323 provider = CultureInfo.CurrentCulture;
326 format = string.Empty;
328 string separator = ",";
329 NumberFormatInfo numberFormat =
330 provider.GetFormat (typeof (NumberFormatInfo)) as NumberFormatInfo;
331 if (numberFormat != null &&
332 numberFormat.NumberDecimalSeparator == separator)
335 string rectFormat = String.Format (
336 "{{0:{0}}}{1}{{1:{0}}}{1}{{2:{0}}}{1}{{3:{0}}}",
338 return String.Format (provider, rectFormat,
339 _x, _y, _width, _height);
342 public static Rect Empty {
344 Rect r = new Rect ();
345 r._x = r._y = Double.PositiveInfinity;
346 r._width = r._height = Double.NegativeInfinity;
351 public bool IsEmpty {
353 return (_x == Double.PositiveInfinity &&
354 _y == Double.PositiveInfinity &&
355 _width == Double.NegativeInfinity &&
356 _height == Double.NegativeInfinity);
360 public Point Location {
362 return new Point (_x, _y);
366 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
377 return new Size (_width, _height);
381 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
383 _width = value.Width;
384 _height = value.Height;
392 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
402 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
408 public double Width {
409 get { return _width; }
412 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
415 throw new ArgumentException ("width must be non-negative.");
421 public double Height {
422 get { return _height; }
425 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
428 throw new ArgumentException ("height must be non-negative.");
442 public double Right {
443 get { return _x + _width; }
446 public double Bottom {
447 get { return _y + _height; }
450 public Point TopLeft {
451 get { return new Point (Left, Top); }
454 public Point TopRight {
455 get { return new Point (Right, Top); }
458 public Point BottomLeft {
459 get { return new Point (Left, Bottom); }
462 public Point BottomRight {
463 get { return new Point (Right, Bottom); }