2005-10-04 Zoltan Varga <vargaz@freemail.hu>
[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         [TypeConverter (typeof (MarginsConverter))]
40         public class Margins : ICloneable
41         {
42                 int left;
43                 int right;
44                 int top;
45                 int bottom;
46
47                 public Margins()
48                 {
49                         left = 100;
50                         right = 100;
51                         top = 100;
52                         bottom = 100;
53                 }
54
55                 public Margins(int left, int right, int top, int bottom)
56                 {
57                         //Verify parameters
58                         if (left < 0)
59                                 throw new System.ArgumentException("All Margins must be greater than 0", "left");
60                         if (right < 0)
61                                 throw new System.ArgumentException("All Margins must be greater than 0", "right");
62                         if (top < 0)
63                                 throw new System.ArgumentException("All Margins must be greater than 0", "top");
64                         if (bottom < 0)
65                                 throw new System.ArgumentException("All Margins must be greater than 0", "bottom");
66
67                         //Set proprities
68                         this.left = left;
69                         this.right = right;
70                         this.top = top;
71                         this.bottom = bottom;
72                 }
73
74                 public int Left {
75                         get {
76                                 return left;
77                         }
78                         set {
79                                 if (left < 0)
80                                         throw new System.ArgumentException("All Margins must be greater than 0", "left");
81                                 left = value;
82                         }
83                 }
84
85                 public int Right {
86                         get {
87                                 return right;
88                         }
89                         set {
90                                 if (right < 0)
91                                         throw new System.ArgumentException("All Margins must be greater than 0", "left");
92                                 right = value;
93                         }
94                 }
95
96                 public int Top {
97                         get {
98                                 return top;
99                         }
100                         set {
101                                 if (top < 0)
102                                         throw new System.ArgumentException("All Margins must be greater than 0", "left");
103                                 top = value;
104                         }
105                 }
106
107                 public int Bottom {
108                         get {
109                                 return bottom;
110                         }
111                         set {
112                                 if (bottom < 0)
113                                         throw new System.ArgumentException("All Margins must be greater than 0", "left");
114                                 bottom = value;
115                         }
116                 }
117                 
118                 public object Clone()
119                 {
120                         return new Margins (this.Left, this.Right, this.Top, this.Bottom);
121                 }
122
123                 public override bool Equals (object obj)
124                 {       
125                         Margins m = obj as Margins;
126
127                         if (m == null)
128                                 return false;
129                         if (m.Left == left && m.Right == right && m.Top == top && m.Bottom == bottom)
130                                 return true;
131
132                         return false;
133                 }
134
135                 public override int GetHashCode ()
136                 {
137                         // Try to create a somewhat meaningful hash
138                         int hash = left + right * 2^8 + top * 2^16 + bottom * 2^24;
139                         return hash;
140                 }
141                 
142                 public override string ToString()
143                 {
144                         string ret = "[Margins Left={0} Right={1} Top={2} Bottom={3}]";
145                         return String.Format (ret, this.Left, this.Right, this.Top, this.Bottom);
146                 }
147         }
148 }