~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/Size.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.Windows.Markup;
 
30
using System.Globalization;
 
31
 
 
32
namespace Xwt {
 
33
        
 
34
        [TypeConverter (typeof(SizeValueConverter))]
 
35
        [ValueSerializer (typeof(SizeValueSerializer))]
 
36
        [Serializable]
 
37
        public struct Size
 
38
        {               
 
39
                double width, height;
 
40
 
 
41
                public static readonly Size Zero;
 
42
 
 
43
                public Size (double width, double height)
 
44
                {
 
45
                        this.width = width;
 
46
                        this.height = height;
 
47
                }
 
48
 
 
49
                public bool IsZero {
 
50
                        get {
 
51
                                return ((width == 0) && (height == 0));
 
52
                        }
 
53
                }
 
54
                
 
55
                [DefaultValue (0d)]
 
56
                public double Width {
 
57
                        get {
 
58
                                return width;
 
59
                        }
 
60
                        set {
 
61
                                width = value;
 
62
                        }
 
63
                }
 
64
 
 
65
                [DefaultValue (0d)]
 
66
                public double Height {
 
67
                        get {
 
68
                                return height;
 
69
                        }
 
70
                        set {
 
71
                                height = value;
 
72
                        }
 
73
                }
 
74
 
 
75
                public static Size operator + (Size s1, Size s2)
 
76
                {
 
77
                        return new Size (s1.width + s2.width, s1.height + s2.height);
 
78
                }
 
79
                
 
80
                public static Size operator - (Size s1, Size s2)
 
81
                {
 
82
                        return new Size (s1.width - s2.width, s1.height - s2.height);
 
83
                }
 
84
                
 
85
                public static bool operator == (Size s1, Size s2)
 
86
                {
 
87
                        return (s1.width == s2.width) && (s1.height == s2.height);
 
88
                }
 
89
                
 
90
                public static bool operator != (Size s1, Size s2)
 
91
                {
 
92
                        return (s1.width != s2.width) || (s1.height != s2.height);
 
93
                }
 
94
                
 
95
                public static explicit operator Point(Size size) 
 
96
                {
 
97
                        return new Point (size.Width, size.Height);
 
98
                }
 
99
                
 
100
                public override bool Equals (object ob)
 
101
                {
 
102
                        return (ob is Size) && this == (Size)ob;
 
103
                }
 
104
 
 
105
                public override int GetHashCode ()
 
106
                {
 
107
                        return width.GetHashCode () ^ height.GetHashCode ();
 
108
                }
 
109
 
 
110
                public override string ToString ()
 
111
                {
 
112
                        return String.Format ("{{Width={0} Height={1}}}", width.ToString (CultureInfo.InvariantCulture), height.ToString (CultureInfo.InvariantCulture));
 
113
                }
 
114
        }
 
115
        
 
116
        class SizeValueConverter: TypeConverter
 
117
        {
 
118
                public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
 
119
                {
 
120
                        return destinationType == typeof(string);
 
121
                }
 
122
                
 
123
                public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
 
124
                {
 
125
                        return sourceType == typeof(string);
 
126
                }
 
127
        }
 
128
        
 
129
        class SizeValueSerializer: ValueSerializer
 
130
        {
 
131
                public override bool CanConvertFromString (string value, IValueSerializerContext context)
 
132
                {
 
133
                        return true;
 
134
                }
 
135
                
 
136
                public override bool CanConvertToString (object value, IValueSerializerContext context)
 
137
                {
 
138
                        return true;
 
139
                }
 
140
                
 
141
                public override string ConvertToString (object value, IValueSerializerContext context)
 
142
                {
 
143
                        Size s = (Size) value;
 
144
                        return s.Width.ToString () + "," + s.Height.ToString ();
 
145
                }
 
146
                
 
147
                public override object ConvertFromString (string value, IValueSerializerContext context)
 
148
                {
 
149
                        int i = value.IndexOf (',');
 
150
                        if (i == -1)
 
151
                                return Size.Zero;
 
152
                        double w, h;
 
153
                        if (!double.TryParse (value.Substring (0, i), out w))
 
154
                                return Size.Zero;
 
155
                        if (!double.TryParse (value.Substring (i+1), out h))
 
156
                                return Size.Zero;
 
157
                        return new Size (w, h);
 
158
                }
 
159
        }
 
160
}