1 // 2 // Size.cs 3 // 4 // Author: 5 // Lluis Sanchez <lluis@xamarin.com> 6 // 7 // Copyright (c) 2011 Xamarin Inc 8 // 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: 15 // 16 // The above copyright notice and this permission notice shall be included in 17 // all copies or substantial portions of the Software. 18 // 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 25 // THE SOFTWARE. 26 27 using System; 28 using System.ComponentModel; 29 using System.Globalization; 30 31 namespace Xwt { 32 33 struct Size 34 { 35 double width, height; 36 37 public static readonly Size Zero = new Size (0d, 0d); 38 SizeXwt.Size39 public Size (double width, double height) 40 { 41 this.width = width; 42 this.height = height; 43 } 44 45 public bool IsZero { 46 get { 47 return ((width == 0) && (height == 0)); 48 } 49 } 50 51 [DefaultValue (0d)] 52 public double Width { 53 get { 54 return width; 55 } 56 set { 57 width = value; 58 } 59 } 60 61 [DefaultValue (0d)] 62 public double Height { 63 get { 64 return height; 65 } 66 set { 67 height = value; 68 } 69 } 70 operator +Xwt.Size71 public static Size operator + (Size s1, Size s2) 72 { 73 return new Size (s1.width + s2.width, s1.height + s2.height); 74 } 75 operator -Xwt.Size76 public static Size operator - (Size s1, Size s2) 77 { 78 return new Size (s1.width - s2.width, s1.height - s2.height); 79 } 80 operator ==Xwt.Size81 public static bool operator == (Size s1, Size s2) 82 { 83 return (s1.width == s2.width) && (s1.height == s2.height); 84 } 85 operator !=Xwt.Size86 public static bool operator != (Size s1, Size s2) 87 { 88 return (s1.width != s2.width) || (s1.height != s2.height); 89 } 90 operator PointXwt.Size91 public static explicit operator Point (Size size) 92 { 93 return new Point (size.Width, size.Height); 94 } 95 96 //public static explicit operator Distance (Size size) 97 //{ 98 // return new Distance (size.Width, size.Height); 99 //} 100 EqualsXwt.Size101 public override bool Equals (object ob) 102 { 103 return (ob is Size) && this == (Size)ob; 104 } 105 GetHashCodeXwt.Size106 public override int GetHashCode () 107 { 108 unchecked { 109 return (width.GetHashCode () * 397) ^ height.GetHashCode (); 110 } 111 } 112 ToStringXwt.Size113 public override string ToString () 114 { 115 return String.Format ("{{Width={0} Height={1}}}", width.ToString (CultureInfo.InvariantCulture), height.ToString (CultureInfo.InvariantCulture)); 116 } 117 } 118 } 119