Merge pull request #5528 from rodrmoya/fix-mono-profiler-lib
[mono.git] / mcs / class / WindowsBase / System.Windows / Point.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 //
25
26 using System;
27 using System.ComponentModel;
28 using System.Windows.Converters;
29 using System.Windows.Markup;
30 using System.Windows.Media;
31 using System.Globalization;
32
33 namespace System.Windows {
34
35         [Serializable]
36         [TypeConverter (typeof (PointConverter))]
37         [ValueSerializer (typeof (PointValueSerializer))]
38         public struct Point : IFormattable
39         {
40                 public Point (double x, double y)
41                 {
42                         this._x = x;
43                         this._y = y;
44                 }
45
46                 public double X {
47                         get { return _x; }
48                         set { _x = value; }
49                 }
50
51                 public double Y {
52                         get { return _y; }
53                         set { _y = value; }
54                 }
55
56                 public override bool Equals (object o)
57                 {
58                         if (!(o is Point))
59                                 return false;
60                         return Equals ((Point)o);
61                 }
62
63                 public bool Equals (Point value)
64                 {
65                         return _x == value.X && _y == value.Y;
66                 }
67
68                 public override int GetHashCode ()
69                 {
70                     return (_x.GetHashCode() ^ _y.GetHashCode());
71                 }
72
73
74                 public void Offset (double offsetX, double offsetY)
75                 {
76                         _x += offsetX;
77                         _y += offsetY;
78                 }
79
80                 public static Point Add (Point point, Vector vector)
81                 {
82                         return new Point (point.X + vector.X, point.Y + vector.Y);
83                 }
84
85                 public static bool Equals (Point point1, Point point2)
86                 {
87                         return point1.Equals (point2);
88                 }
89
90                 public static Point Multiply (Point point, Matrix matrix)
91                 {
92                         return new Point (point.X * matrix.M11 + point.Y * matrix.M21 + matrix.OffsetX,
93                                           point.X * matrix.M12 + point.Y * matrix.M22 + matrix.OffsetY);
94                 }
95
96                 public static Vector Subtract (Point point1, Point point2)
97                 {
98                         return new Vector (point1.X - point2.X, point1.Y - point2.Y);
99                 }
100
101                 public static Point Subtract (Point point, Vector vector)
102                 {
103                         return new Point (point.X - vector.X, point.Y - vector.Y);
104                 }
105
106                 /* operators */
107
108                 public static Vector operator -(Point point1, Point point2)
109                 {
110                         return Subtract (point1, point2);
111                 }
112
113                 public static Point operator -(Point point, Vector vector)
114                 {
115                         return Subtract (point, vector);
116                 }
117
118                 public static Point operator + (Point point, Vector vector)
119                 {
120                         return Add (point, vector);
121                 }
122
123                 public static Point operator * (Point point, Matrix matrix)
124                 {
125                         return Multiply (point, matrix);
126                 }
127
128                 public static bool operator != (Point point1, Point point2)
129                 {
130                         return !point1.Equals(point2);
131                 }
132
133                 public static bool operator == (Point point1, Point point2)
134                 {
135                         return point1.Equals(point2);
136                 }
137
138                 public static explicit operator Size (Point point)
139                 {
140                         return new Size (point.X, point.Y);
141                 }
142
143                 public static explicit operator Vector (Point point)
144                 {
145                         return new Vector (point.X, point.Y);
146                 }
147
148                 public static Point Parse (string source)
149                 {
150                         if (source == null)
151                                 throw new ArgumentNullException ("source");
152                         var tokenizer = new NumericListTokenizer (source, CultureInfo.InvariantCulture);
153                         double x;
154                         double y;
155                         if (!double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out x) ||
156                             !double.TryParse (tokenizer.GetNextToken (), NumberStyles.Float, CultureInfo.InvariantCulture, out y))
157                         {
158                                 throw new FormatException (string.Format ("Invalid Point format: {0}", source));
159                         }
160                         if (!tokenizer.HasNoMoreTokens ())
161                         {
162                                 throw new InvalidOperationException ("Invalid Point format: " + source);
163                         }
164                         return new Point(x, y);
165                 }
166
167                 public override string ToString ()
168                 {
169                         return this.ToString(null, null);
170                 }
171
172                 public string ToString (IFormatProvider provider)
173                 {
174                         return this.ToString(null, provider);
175                 }
176
177                 private string ToString(string format,IFormatProvider formatProvider)
178                 {
179                         if (formatProvider == null)
180                                 formatProvider = CultureInfo.CurrentCulture;
181                         if (format == null)
182                                 format = string.Empty;
183                         var separator = NumericListTokenizer.GetSeparator (formatProvider);
184                         var pointFormat  = string.Format ("{{0:{0}}}{1}{{1:{0}}}", format, separator);
185                         return string.Format (formatProvider, pointFormat, _x, _y);
186                 }
187
188                 string IFormattable.ToString (string format, IFormatProvider formatProvider)
189                 {
190                         return this.ToString(format, formatProvider);
191                 }
192
193                 double _x;
194                 double _y;
195         }
196 }