Merge pull request #5560 from kumpera/wasm-work-p3
[mono.git] / mcs / class / WindowsBase / System.Windows / Rect.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok <toshok@novell.com>
24 //      Sebastien Pouliot  <sebastien@ximian.com>
25 //
26
27 using System.ComponentModel;
28 using System.Globalization;
29 using System.Windows.Converters;
30 using System.Windows.Markup;
31 using System.Windows.Media;
32
33 namespace System.Windows {
34
35         [Serializable]
36         [ValueSerializer (typeof (RectValueSerializer))]
37         [TypeConverter (typeof (RectConverter))]
38         public struct Rect : IFormattable
39         {
40                 public Rect (Size size)
41                 {
42                         _x = _y = 0.0;
43                         _width = size.Width;
44                         _height = size.Height;
45                 }
46
47                 public Rect (Point point, Vector vector) : this (point, Point.Add (point, vector))
48                 { }
49
50                 public Rect (Point point1, Point point2)
51                 {
52                         if (point1.X < point2.X) {
53                                 _x = point1.X;
54                                 _width = point2.X - point1.X;
55                         }
56                         else {
57                                 _x = point2.X;
58                                 _width = point1.X - point2.X;
59                         }
60
61                         if (point1.Y < point2.Y) {
62                                 _y = point1.Y;
63                                 _height = point2.Y - point1.Y;
64                         }
65                         else {
66                                 _y = point2.Y;
67                                 _height = point1.Y - point2.Y;
68                         }
69                 }
70
71                 public Rect (double x, double y, double width, double height)
72                 {
73                         if (width < 0 || height < 0)
74                                 throw new ArgumentException ("width and height must be non-negative.");
75                         this._x = x;
76                         this._y = y;
77                         this._width = width;
78                         this._height = height;
79                 }
80
81                 public Rect (Point location, Size size)
82                 {
83                         _x = location.X;
84                         _y = location.Y;
85                         _width = size.Width;
86                         _height = size.Height;
87                 }
88
89                 public bool Equals (Rect value)
90                 {
91                         return (_x == value.X &&
92                                 _y == value.Y &&
93                                 _width == value.Width &&
94                                 _height == value.Height);
95                 }
96
97                 public static bool operator != (Rect rect1, Rect rect2)
98                 {
99                         return !(rect1.Location == rect2.Location && rect1.Size == rect2.Size);
100                 }
101
102                 public static bool operator == (Rect rect1, Rect rect2)
103                 {
104                         return rect1.Location == rect2.Location && rect1.Size == rect2.Size;
105                 }
106
107                 public override bool Equals (object o)
108                 {
109                         if (!(o is Rect))
110                                 return false;
111
112                         return Equals ((Rect)o);
113                 }
114
115                 public static bool Equals (Rect rect1, Rect rect2)
116                 {
117                         return rect1.Equals (rect2);
118                 }
119
120                 public override int GetHashCode ()
121                 {
122                         unchecked
123                         {
124                                 var hashCode = _x.GetHashCode ();
125                                 hashCode = (hashCode * 397) ^ _y.GetHashCode ();
126                                 hashCode = (hashCode * 397) ^ _width.GetHashCode ();
127                                 hashCode = (hashCode * 397) ^ _height.GetHashCode ();
128                                 return hashCode;
129                         }
130                 }
131
132                 public bool Contains (Rect rect)
133                 {
134                         if (rect.Left < this.Left ||
135                             rect.Right > this.Right)
136                                 return false;
137
138                         if (rect.Top < this.Top ||
139                             rect.Bottom > this.Bottom)
140                                 return false;
141
142                         return true;
143                 }
144
145                 public bool Contains (double x, double y)
146                 {
147                         if (x < Left || x > Right)
148                                 return false;
149                         if (y < Top || y > Bottom)
150                                 return false;
151
152                         return true;
153                 }
154
155                 public bool Contains (Point point)
156                 {
157                         return Contains (point.X, point.Y);
158                 }
159
160                 public static Rect Inflate (Rect rect, double width, double height)
161                 {
162                         if (width < rect.Width * -2)
163                                 return Rect.Empty;
164                         if (height < rect.Height * -2)
165                                 return Rect.Empty;
166
167                         Rect result = rect;
168                         result.Inflate (width, height);
169                         return result;
170                 }
171
172                 public static Rect Inflate (Rect rect, Size size)
173                 {
174                         return Rect.Inflate (rect, size.Width, size.Height);
175                 }
176
177                 public void Inflate (double width, double height)
178                 {
179                         // XXX any error checking like in the static case?
180                         _x -= width;
181                         _y -= height;
182
183                         this._width += 2*width;
184                         this._height += 2*height;
185                 }
186
187                 public void Inflate (Size size)
188                 {
189                         Inflate (size.Width, size.Height);
190                 }
191
192                 public bool IntersectsWith(Rect rect)
193                 {
194                         return !((Left >= rect.Right) || (Right <= rect.Left) ||
195                             (Top >= rect.Bottom) || (Bottom <= rect.Top));
196                 }
197
198                 public void Intersect(Rect rect)
199                 {
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; 
204
205                         if (_width < 0 || _height < 0) {
206                                 this._x = this._y = Double.PositiveInfinity;
207                                 this._width = this._height = Double.NegativeInfinity;
208                         }
209                         else {
210                                 this._x = _x;
211                                 this._y = _y;
212                                 this._width = _width;
213                                 this._height = _height;
214                         }
215                 }
216
217                 public static Rect Intersect(Rect rect1, Rect rect2)
218                 {
219                         Rect result = rect1;
220                         result.Intersect (rect2);
221                         return result;
222                 }
223
224                 public void Offset(double offsetX, double offsetY)
225                 {
226                         _x += offsetX;
227                         _y += offsetY;
228                 }
229
230                 public static Rect Offset(Rect rect, double offsetX, double offsetY)
231                 {
232                         Rect result = rect;
233                         result.Offset (offsetX, offsetY);
234                         return result;
235                 }
236
237                 public void Offset (Vector offsetVector)
238                 {
239                         _x += offsetVector.X;
240                         _y += offsetVector.Y;
241                 }
242
243                 public static Rect Offset (Rect rect, Vector offsetVector)
244                 {
245                         Rect result = rect;
246                         result.Offset (offsetVector);
247                         return result;
248                 }
249
250                 public void Scale(double scaleX, double scaleY)
251                 {
252                         _x *= scaleX;
253                         _y *= scaleY;
254                         _width *= scaleX;
255                         _height *= scaleY;
256                 }
257
258                 public void Transform (Matrix matrix)
259                 {
260                         throw new NotImplementedException ();
261                 }
262
263                 public static Rect Transform (Rect rect, Matrix matrix)
264                 {
265                         Rect result = rect;
266                         result.Transform (matrix);
267                         return result;
268                 }
269
270                 public static Rect Union(Rect rect1, Rect rect2)
271                 {
272                         Rect result = rect1;
273                         result.Union (rect2);
274                         return result;
275                 }
276
277                 public static Rect Union(Rect rect, Point point)
278                 {
279                         Rect result = rect;
280                         result.Union (point);
281                         return result;
282                 }
283                 
284                 public void Union(Rect rect)
285                 {
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);
290                         
291                         _x = left;
292                         _y = top;
293                         _width = right - left;
294                         _height = bottom - top;
295                 }
296
297                 public void Union(Point point)
298                 {
299                         Union (new Rect (point, point));
300                 }
301
302                 public static Rect Parse (string source)
303                 {
304                         if (source == null)
305                                 throw new ArgumentNullException ("source");
306                         Rect value;
307                         if (source.Trim () == "Empty")
308                         {
309                                 value = Empty;
310                         }
311                         else
312                         {
313                                 var tokenizer = new NumericListTokenizer (source, CultureInfo.InvariantCulture);
314                                 double x;
315                                 double y;
316                                 double width;
317                                 double height;
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))
322                                 {
323                                         if (!tokenizer.HasNoMoreTokens ())
324                                         {
325                                                 throw new InvalidOperationException ("Invalid Rect format: " + source);
326                                         }
327                                         value = new Rect (x, y, width, height);
328                                 }
329                                 else
330                                 {
331                                         throw new FormatException (string.Format ("Invalid Rect format: {0}", source));
332                                 }
333                         }
334                         return value;
335                 }
336
337                 public override string ToString ()
338                 {
339                         return ToString (null);
340                 }
341
342                 public string ToString (IFormatProvider provider)
343                 {
344                         return ToString (null, provider);
345                 }
346
347                 string IFormattable.ToString (string format, IFormatProvider provider)
348                 {
349                         return ToString (format, provider);
350                 }
351
352                 private string ToString (string format, IFormatProvider provider)
353                 {
354                         if (IsEmpty)
355                                 return "Empty";
356
357                         if (provider == null)
358                                 provider = CultureInfo.CurrentCulture;
359
360                         if (format == null)
361                                 format = string.Empty;
362
363                         var separator = NumericListTokenizer.GetSeparator (provider);
364
365                         var rectFormat = string.Format (
366                                 "{{0:{0}}}{1}{{1:{0}}}{1}{{2:{0}}}{1}{{3:{0}}}",
367                                 format, separator);
368                         return string.Format (provider, rectFormat,
369                                 _x, _y, _width, _height);
370                 }
371
372                 public static Rect Empty { 
373                         get {
374                                 Rect r = new Rect ();
375                                 r._x = r._y = Double.PositiveInfinity;
376                                 r._width = r._height = Double.NegativeInfinity;
377                                 return r;
378                         } 
379                 }
380                 
381                 public bool IsEmpty { 
382                         get {
383                                 return (_x == Double.PositiveInfinity &&
384                                         _y == Double.PositiveInfinity &&
385                                         _width == Double.NegativeInfinity &&
386                                         _height == Double.NegativeInfinity);
387                         }
388                 }
389                 
390                 public Point Location { 
391                         get {
392                                 return new Point (_x, _y);
393                         }
394                         set {
395                                 if (IsEmpty)
396                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
397
398                                 _x = value.X;
399                                 _y = value.Y;
400                         }
401                 }
402                 
403                 public Size Size { 
404                         get { 
405                                 if (IsEmpty)
406                                         return Size.Empty; 
407                                 return new Size (_width, _height);
408                         }
409                         set {
410                                 if (IsEmpty)
411                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
412
413                                 _width = value.Width;
414                                 _height = value.Height;
415                         }
416                 }
417
418                 public double X {
419                         get { return _x; }
420                         set {
421                                 if (IsEmpty)
422                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
423
424                                 _x = value;
425                         }
426                 }
427
428                 public double Y {
429                         get { return _y; }
430                         set {
431                                 if (IsEmpty)
432                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
433
434                                 _y = value;
435                         }
436                 }
437
438                 public double Width {
439                         get { return _width; }
440                         set {
441                                 if (IsEmpty)
442                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
443
444                                 if (value < 0)
445                                         throw new ArgumentException ("width must be non-negative.");
446
447                                 _width = value;
448                         }
449                 }
450
451                 public double Height {
452                         get { return _height; }
453                         set {
454                                 if (IsEmpty)
455                                         throw new InvalidOperationException ("Cannot modify this property on the Empty Rect.");
456
457                                 if (value < 0)
458                                         throw new ArgumentException ("height must be non-negative.");
459
460                                 _height = value;
461                         }
462                 }
463
464                 public double Left { 
465                         get { return _x; }
466                 }
467
468                 public double Top { 
469                         get { return _y; }
470                 }
471                 
472                 public double Right { 
473                         get { return _x + _width; }
474                 }
475                 
476                 public double Bottom { 
477                         get { return _y + _height; }
478                 }
479                 
480                 public Point TopLeft { 
481                         get { return new Point (Left, Top); }
482                 }
483                 
484                 public Point TopRight { 
485                         get { return new Point (Right, Top); }
486                 }
487                 
488                 public Point BottomLeft { 
489                         get { return new Point (Left, Bottom); }
490                 }
491
492                 public Point BottomRight { 
493                         get { return new Point (Right, Bottom); }
494                 }
495                 
496                 double _x;
497                 double _y;
498                 double _width;
499                 double _height;
500         }
501 }