5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.ComponentModel;
29
using System.Windows.Markup;
30
using System.Globalization;
34
[TypeConverter (typeof(SizeValueConverter))]
35
[ValueSerializer (typeof(SizeValueSerializer))]
41
public static readonly Size Zero;
43
public Size (double width, double height)
51
return ((width == 0) && (height == 0));
66
public double Height {
75
public static Size operator + (Size s1, Size s2)
77
return new Size (s1.width + s2.width, s1.height + s2.height);
80
public static Size operator - (Size s1, Size s2)
82
return new Size (s1.width - s2.width, s1.height - s2.height);
85
public static bool operator == (Size s1, Size s2)
87
return (s1.width == s2.width) && (s1.height == s2.height);
90
public static bool operator != (Size s1, Size s2)
92
return (s1.width != s2.width) || (s1.height != s2.height);
95
public static explicit operator Point(Size size)
97
return new Point (size.Width, size.Height);
100
public override bool Equals (object ob)
102
return (ob is Size) && this == (Size)ob;
105
public override int GetHashCode ()
107
return width.GetHashCode () ^ height.GetHashCode ();
110
public override string ToString ()
112
return String.Format ("{{Width={0} Height={1}}}", width.ToString (CultureInfo.InvariantCulture), height.ToString (CultureInfo.InvariantCulture));
116
class SizeValueConverter: TypeConverter
118
public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
120
return destinationType == typeof(string);
123
public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
125
return sourceType == typeof(string);
129
class SizeValueSerializer: ValueSerializer
131
public override bool CanConvertFromString (string value, IValueSerializerContext context)
136
public override bool CanConvertToString (object value, IValueSerializerContext context)
141
public override string ConvertToString (object value, IValueSerializerContext context)
143
Size s = (Size) value;
144
return s.Width.ToString () + "," + s.Height.ToString ();
147
public override object ConvertFromString (string value, IValueSerializerContext context)
149
int i = value.IndexOf (',');
153
if (!double.TryParse (value.Substring (0, i), out w))
155
if (!double.TryParse (value.Substring (i+1), out h))
157
return new Size (w, h);