6a4f959d9cfe614cac78a1caa5f9d34b7130993d
[mono.git] / mcs / class / System.Drawing / System.Drawing.Printing / Margins.cs
1 //
2 // System.Drawing.Margins.cs
3 //
4 // Authors:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //   Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.ComponentModel;
33
34 namespace System.Drawing.Printing
35 {
36 #if NET_2_0
37         [Serializable]
38 #endif
39         [TypeConverter (typeof (MarginsConverter))]
40         public class Margins : ICloneable {
41                 int left;
42                 int right;
43                 int top;
44                 int bottom;
45
46                 public Margins ()
47                 {
48                         left = 100;
49                         right = 100;
50                         top = 100;
51                         bottom = 100;
52                 }
53
54                 public Margins (int left, int right, int top, int bottom)
55                 {
56                         Left = left;
57                         Right = right;
58                         Top = top;
59                         Bottom = bottom;
60                 }
61
62                 public int Left {
63                         get {
64                                 return left;
65                         }
66                         set {
67                                 if (value < 0)
68                                         InvalidMargin ("left");
69                                 left = value;
70                         }
71                 }
72
73                 public int Right {
74                         get {
75                                 return right;
76                         }
77                         set {
78                                 if (value < 0)
79                                         InvalidMargin ("right");
80                                 right = value;
81                         }
82                 }
83
84                 public int Top {
85                         get {
86                                 return top;
87                         }
88                         set {
89                                 if (value < 0)
90                                         InvalidMargin ("top");
91                                 top = value;
92                         }
93                 }
94
95                 public int Bottom {
96                         get {
97                                 return bottom;
98                         }
99                         set {
100                                 if (value < 0)
101                                         InvalidMargin ("bottom");
102                                 bottom = value;
103                         }
104                 }
105
106                 private void InvalidMargin (string property)
107                 {
108                         string msg = Locale.GetText ("All Margins must be greater than 0");
109                         throw new System.ArgumentException (msg, property);
110                 }
111                 
112                 public object Clone ()
113                 {
114                         return new Margins (left, right, top, bottom);
115                 }
116
117                 public override bool Equals (object obj)
118                 {       
119                         return Equals (obj as Margins);
120                 }
121
122                 private bool Equals (Margins m)
123                 {
124                         // avoid recursion with == operator
125                         if ((object)m == null)
126                                 return false;
127                         return ((m.Left == left) && (m.Right == right) && (m.Top == top) && (m.Bottom == bottom));
128                 }
129
130                 public override int GetHashCode ()
131                 {
132                         return left | (right << 8) | (right >> 24) | (top << 16) | (top >> 16) | (bottom << 24) | (bottom >> 8);
133                 }
134                 
135                 public override string ToString ()
136                 {
137                         string ret = "[Margins Left={0} Right={1} Top={2} Bottom={3}]";
138                         return String.Format (ret, left, right, top, bottom);
139                 }
140
141 #if NET_2_0
142                 public static bool operator == (Margins m1, Margins m2)
143                 {
144                         // avoid recursion with == operator
145                         if ((object)m1 == null)
146                                 return ((object)m2 == null);
147                         return m1.Equals (m2);
148                 }
149
150                 public static bool operator != (Margins m1, Margins m2)
151                 {
152                         // avoid recursion with == operator
153                         if ((object)m1 == null)
154                                 return ((object)m2 != null);
155                         return !m1.Equals (m2);
156                 }
157 #endif
158         }
159 }