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>
27 using System.ComponentModel;
28 using System.Globalization;
29 using System.Windows.Converters;
30 using System.Windows.Markup;
31 using System.Windows.Media;
33 namespace System.Windows {
36 [ValueSerializer (typeof (RectValueSerializer))]
37 [TypeConverter (typeof (RectConverter))]
38 public struct Rect : IFormattable
40 public Rect (Size size)
44 _height = size.Height;
47 public Rect (Point point, Vector vector) : this (point, Point.Add (point, vector))
50 public Rect (Point point1, Point point2)
52 if (point1.X < point2.X) {
54 _width = point2.X - point1.X;
58 _width = point1.X - point2.X;
61 if (point1.Y < point2.Y) {
63 _height = point2.Y - point1.Y;
67 _height = point1.Y - point2.Y;
71 public Rect (double x, double y, double width, double height)
73 if (width < 0 || height < 0)
74 throw new ArgumentException ("width and height must be non-negative.");
78 this._height = height;
81 public Rect (Point location, Size size)
86 _height = size.Height;
89 public bool Equals (Rect value)
91 return (_x == value.X &&
93 _width == value.Width &&
94 _height == value.Height);
97 public static bool operator != (Rect rect1, Rect rect2)
99 return !(rect1.Location == rect2.Location && rect1.Size == rect2.Size);
102 public static bool operator == (Rect rect1, Rect rect2)
104 return rect1.Location == rect2.Location && rect1.Size == rect2.Size;
107 public override bool Equals (object o)
112 return Equals ((Rect)o);
115 public static bool Equals (Rect rect1, Rect rect2)
117 return rect1.Equals (rect2);
120 public override int GetHashCode ()
124 var hashCode = _x.GetHashCode ();
125 hashCode = (hashCode * 397) ^ _y.GetHashCode ();
126 hashCode = (hashCode * 397) ^ _width.GetHashCode ();
127 hashCode = (hashCode * 397) ^ _height.GetHashCode ();
132 public bool Contains (Rect rect)
134 if (rect.Left < this.Left ||
135 rect.Right > this.Right)
138 if (rect.Top < this.Top ||
139 rect.Bottom > this.Bottom)
145 public bool Contains (double x, double y)
147 if (x < Left || x > Right)
149 if (y < Top || y > Bottom)
155 public bool Contains (Point point)
157 return Contains (point.X, point.Y);
160 public static Rect Inflate (Rect rect, double width, double height)
162 if (width < rect.Width * -2)
164 if (height < rect.Height * -2)
168 result.Inflate (width, height);
172 public static Rect Inflate (Rect rect, Size size)
174 return Rect.Inflate (rect, size.Width, size.Height);
177 public void Inflate (double width, double height)
179 // XXX any error checking like in the static case?
183 this._width += 2*width;
184 this._height += 2*height;
187 public void Inflate (Size size)
189 Inflate (size.Width, size.Height);
192 public bool IntersectsWith(Rect rect)
194 return !((Left >= rect.Right) || (Right <= rect.Left) ||
195 (Top >= rect.Bottom) || (Bottom <= rect.Top));
198 public void Intersect(Rect rect)
200 double _x = Math.Max (this._x, rect._x);
201 double _y = Math.Max (this._y, rect._y);
202 double _width = Math.Min (Right, rect.Right) - _x;
203 double _height = Math.Min (Bottom, rect.Bottom) - _y;
205 if (_width < 0 || _height < 0) {
206 this._x = this._y = Double.PositiveInfinity;
207 this._width = this._height = Double.NegativeInfinity;
212 this._width = _width;
213 this._height = _height;
217 public static Rect Intersect(Rect rect1, Rect rect2)
220 result.Intersect (rect2);
224 public void Offset(double offsetX, double offsetY)
230 public static Rect Offset(Rect rect, double offsetX, double offsetY)
233 result.Offset (offsetX, offsetY);
237 public void Offset (Vector offsetVector)
239 _x += offsetVector.X;
240 _y += offsetVector.Y;
243 public static Rect Offset (Rect rect, Vector offsetVector)
246 result.Offset (offsetVector);
250 public void Scale(double scaleX, double scaleY)
258 public void Transform (Matrix matrix)
260 throw new NotImplementedException ();
263 public static Rect Transform (Rect rect, Matrix matrix)
266 result.Transform (matrix);
270 public static Rect Union(Rect rect1, Rect rect2)
273 result.Union (rect2);
277 public static Rect Union(Rect rect, Point point)
280 result.Union (point);
284 public void Union(Rect rect)
286 var left = Math.Min (Left, rect.Left);
287 var top = Math.Min (Top, rect.Top);
288 var right = Math.Max (Right, rect.Right);
289 var bottom = Math.Max (Bottom, rect.Bottom);
293 _width = right - left;
294 _height = bottom - top;
297 public void Union(Point point)
299 Union (new Rect (point, point));
302 public static Rect Parse (string source)
305 throw new ArgumentNullException ("source");
307 if (source.Trim () == "Empty")
313 var tokenizer = new NumericListTokenizer (source, CultureInfo.InvariantCulture);
318 if (double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out x)
319 && double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out y)
320 && double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out width)
321 && double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out height))
323 if (!tokenizer.HasNoMoreTokens ())
325 throw new InvalidOperationException ("Invalid Rect format: " + source);
327 value = new Rect (x, y, width, height);
331 throw new FormatException (string.Format ("Invalid Rect format: {0}", source));
337 public override string ToString ()
339 return ToString (null);
342 public string ToString (IFormatProvider provider)
344 return ToString (null, provider);
347 string IFormattable.ToString (string format, IFormatProvider provider)
349 return ToString (format, provider);
352 private string ToString (string format, IFormatProvider provider)
357 if (provider == null)
358 provider = CultureInfo.CurrentCulture;
361 format = string.Empty;
363 var separator = NumericListTokenizer.GetSeparator (provider);
365 var rectFormat = string.Format (
366 "{{0:{0}}}{1}{{1:{0}}}{1}{{2:{0}}}{1}{{3:{0}}}",
368 return string.Format (provider, rectFormat,
369 _x, _y, _width, _height);
372 public static Rect Empty {
374 Rect r = new Rect ();
375 r._x = r._y = Double.PositiveInfinity;
376 r._width = r._height = Double.NegativeInfinity;
381 public bool IsEmpty {
383 return (_x == Double.PositiveInfinity &&
384 _y == Double.PositiveInfinity &&
385 _width == Double.NegativeInfinity &&
386 _height == Double.NegativeInfinity);
390 public Point Location {
392 return new Point (_x, _y);
396 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
407 return new Size (_width, _height);
411 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
413 _width = value.Width;
414 _height = value.Height;
422 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
432 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
438 public double Width {
439 get { return _width; }
442 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
445 throw new ArgumentException ("width must be non-negative.");
451 public double Height {
452 get { return _height; }
455 throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
458 throw new ArgumentException ("height must be non-negative.");
472 public double Right {
473 get { return _x + _width; }
476 public double Bottom {
477 get { return _y + _height; }
480 public Point TopLeft {
481 get { return new Point (Left, Top); }
484 public Point TopRight {
485 get { return new Point (Right, Top); }
488 public Point BottomLeft {
489 get { return new Point (Left, Bottom); }
492 public Point BottomRight {
493 get { return new Point (Right, Bottom); }