Merge pull request #440 from mono-soc-2012/garyb/resources
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Padding.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) 2005,2006 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //      Daniel Nauck    (dna(at)mono-project(dot)de)
25 //
26
27 using System;
28 using System.ComponentModel;
29 using System.Drawing;
30
31 namespace System.Windows.Forms {
32
33         [Serializable]
34         [TypeConverter(typeof(PaddingConverter))]
35         public struct Padding {
36
37                 //NOTE: "_var" field name is required by serialization.
38                 private int _bottom;
39                 private int _left;
40                 private int _right;
41                 private int _top;
42                 private bool _all;
43
44                 public Padding (int all) {
45                         _left = all;
46                         _right = all;
47                         _top = all;
48                         _bottom = all;
49                         _all = true;
50                 }
51
52                 public Padding (int left, int top, int right, int bottom) {
53                         _left = left;
54                         _right = right;
55                         _top = top;
56                         _bottom = bottom;
57                         _all = (_left == _top) && (_left == _right) && (_left == _bottom);
58                 }
59
60                 public static readonly Padding Empty = new Padding(0);
61
62                 [RefreshProperties(RefreshProperties.All)]
63                 public int All {
64                         get { 
65                                 if(!_all)
66                                         return -1;
67                                 else
68                                         return _top; 
69                         }
70                         set { 
71                                 _all = true;
72                                 _left = _top = _right = _bottom = value;
73                         }
74                 }
75
76                 [RefreshProperties(RefreshProperties.All)]
77                 public int Bottom {
78                         get { return _bottom; }
79                         set { 
80                                 _bottom = value;
81                                 _all = false;
82                         }
83                 }
84
85                 [Browsable(false)]
86                 public int Horizontal {
87                         get { return _left + _right; }
88                 }
89
90                 [RefreshProperties(RefreshProperties.All)]
91                 public int Left {
92                         get { return _left; }
93                         set { 
94                                 _left = value;
95                                 _all = false;
96                         }
97                 }
98
99                 [RefreshProperties(RefreshProperties.All)]
100                 public int Right {
101                         get { return _right; }
102                         set { 
103                                 _right = value;
104                                 _all = false;
105                         }
106                 }
107
108                 [Browsable(false)]
109                 public Size Size {
110                         get { return new Size(Horizontal, Vertical); }
111                 }
112
113                 [RefreshProperties(RefreshProperties.All)]
114                 public int Top {
115                         get { return _top; }
116                         set { 
117                                 _top = value;
118                                 _all = false;
119                         }
120                 }
121
122                 [Browsable(false)]
123                 public int Vertical {
124                         get { return _top + _bottom; }
125                 }
126
127                 public static Padding Add (Padding p1, Padding p2) {
128                         return p1 + p2;
129                 }
130
131                 public override bool Equals (object other) {
132                         if (other is Padding) {
133                                 Padding other_aux = (Padding) other;
134                                 return _left == other_aux.Left &&
135                                         _top == other_aux.Top &&
136                                         _right == other_aux.Right &&
137                                         _bottom == other_aux.Bottom;
138                         }
139                         return false;
140                 }
141
142                 public override int GetHashCode () {
143                         return _top ^ _bottom ^ _left ^ _right;
144                 }
145
146                 public static Padding operator+ (Padding p1, Padding p2) {
147                         return new Padding(p1.Left + p2.Left, p1.Top + p2.Top, p1.Right + p2.Right, p1.Bottom + p2.Bottom);
148                 }
149
150                 public static bool operator== (Padding p1, Padding p2) {
151                         return p1.Equals(p2);
152                 }
153
154                 public static bool operator!= (Padding p1, Padding p2) {
155                         return !(p1.Equals(p2));
156                 }
157
158                 public static Padding operator- (Padding p1, Padding p2) {
159                         return new Padding(p1.Left - p2.Left, p1.Top - p2.Top, p1.Right - p2.Right, p1.Bottom - p2.Bottom);
160                 }
161
162                 public static Padding Subtract (Padding p1, Padding p2) {
163                         return p1 - p2;
164                 }
165
166                 public override string ToString () {
167                         return "{Left=" + Left + ",Top="+ Top + ",Right=" + Right + ",Bottom=" + Bottom + "}"; 
168                 }
169         }
170 }