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