[mini] Fix test compiling when running !MOBILE
[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                         string[] points = source.Split(',');
151
152                         if (points.Length<2)
153                                 throw new InvalidOperationException ("source does not contain two numbers");
154                         if (points.Length > 2)
155                                 throw new InvalidOperationException ("source contains too many delimiters");
156
157                         CultureInfo ci = CultureInfo.InvariantCulture;
158                         return new Point (Convert.ToDouble(points[0],ci), Convert.ToDouble(points[1],ci));      
159                 }
160
161                 public override string ToString ()
162                 {
163                         return this.ToString(null, null);
164                 }
165
166                 public string ToString (IFormatProvider provider)
167                 {
168                         return this.ToString(null, provider);
169                 }
170
171                 private string ToString(string format,IFormatProvider formatProvider)
172                 {
173                         CultureInfo ci = (CultureInfo)formatProvider;
174
175                         if (ci == null)
176                                 ci = CultureInfo.CurrentCulture;
177                         string seperator = ci.NumberFormat.NumberDecimalSeparator;
178                         if (seperator.Equals(","))
179                                 seperator = ";";
180                         else
181                                 seperator = ",";
182                         object[] ob = { this._x, seperator, this._y };
183
184                         return string.Format(formatProvider, "{0:" + format + "}{1}{2:" + format + "}", ob);
185                 }
186
187                 string IFormattable.ToString (string format, IFormatProvider formatProvider)
188                 {
189                         return this.ToString(format, formatProvider);
190                 }
191
192                 double _x;
193                 double _y;
194         }
195 }