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