/
Margins.cs
240 lines (213 loc) · 6.97 KB
/
Margins.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.Serialization;
namespace System.Drawing.Printing
{
/// <summary>
/// Specifies the margins of a printed page.
/// </summary>
[TypeConverter(typeof(MarginsConverter))]
public partial class Margins : ICloneable
{
private int _left;
private int _right;
private int _bottom;
private int _top;
[OptionalField]
private double _doubleLeft;
[OptionalField]
private double _doubleRight;
[OptionalField]
private double _doubleTop;
[OptionalField]
private double _doubleBottom;
/// <summary>
/// Initializes a new instance of a the <see cref='Margins'/> class with one-inch margins.
/// </summary>
public Margins() : this(100, 100, 100, 100)
{
}
/// <summary>
/// Initializes a new instance of a the <see cref='Margins'/> class with the specified left, right, top, and bottom margins.
/// </summary>
public Margins(int left, int right, int top, int bottom)
{
CheckMargin(left, nameof(left));
CheckMargin(right, nameof(right));
CheckMargin(top, nameof(top));
CheckMargin(bottom, nameof(bottom));
_left = left;
_right = right;
_top = top;
_bottom = bottom;
_doubleLeft = (double)left;
_doubleRight = (double)right;
_doubleTop = (double)top;
_doubleBottom = (double)bottom;
}
/// <summary>
/// Gets or sets the left margin, in hundredths of an inch.
/// </summary>
public int Left
{
get => _left;
set
{
CheckMargin(value, nameof(value));
_left = value;
_doubleLeft = (double)value;
}
}
/// <summary>
/// Gets or sets the right margin, in hundredths of an inch.
/// </summary>
public int Right
{
get => _right;
set
{
CheckMargin(value, nameof(value));
_right = value;
_doubleRight = (double)value;
}
}
/// <summary>
/// Gets or sets the top margin, in hundredths of an inch.
/// </summary>
public int Top
{
get => _top;
set
{
CheckMargin(value, nameof(value));
_top = value;
_doubleTop = (double)value;
}
}
/// <summary>
/// Gets or sets the bottom margin, in hundredths of an inch.
/// </summary>
public int Bottom
{
get => _bottom;
set
{
CheckMargin(value, nameof(value));
_bottom = value;
_doubleBottom = (double)value;
}
}
/// <summary>
/// Gets or sets the left margin with double value, in hundredths of an inch.
/// When use the setter, the ranger of setting double value should between
/// 0 to Int.MaxValue;
/// </summary>
internal double DoubleLeft
{
get => _doubleLeft;
set
{
Left = (int)Math.Round(value);
_doubleLeft = value;
}
}
/// <summary>
/// Gets or sets the right margin with double value, in hundredths of an inch.
/// When use the setter, the ranger of setting double value should between
/// 0 to Int.MaxValue;
/// </summary>
internal double DoubleRight
{
get => _doubleRight;
set
{
Right = (int)Math.Round(value);
_doubleRight = value;
}
}
/// <summary>
/// Gets or sets the top margin with double value, in hundredths of an inch.
/// When use the setter, the ranger of setting double value should between
/// 0 to Int.MaxValue;
/// </summary>
internal double DoubleTop
{
get => _doubleTop;
set
{
Top = (int)Math.Round(value);
_doubleTop = value;
}
}
/// <summary>
/// Gets or sets the bottom margin with double value, in hundredths of an inch.
/// When use the setter, the ranger of setting double value should between
/// 0 to Int.MaxValue;
/// </summary>
internal double DoubleBottom
{
get => _doubleBottom;
set
{
Bottom = (int)Math.Round(value);
_doubleBottom = value;
}
}
private static void CheckMargin(int margin, string name)
{
if (margin < 0)
{
throw new ArgumentOutOfRangeException(name, margin, SR.Format(SR.InvalidLowBoundArgumentEx, name, margin, 0));
}
}
/// <summary>
/// Retrieves a duplicate of this object, member by member.
/// </summary>
public object Clone() => MemberwiseClone();
/// <summary>
/// Compares this <see cref='Margins'/> to a specified <see cref='Margins'/> to see whether they
/// are equal.
/// </summary>
public override bool Equals([NotNullWhen(true)] object? obj)
{
if (!(obj is Margins margins))
{
return false;
}
return margins.Left == Left
&& margins.Right == Right
&& margins.Top == Top
&& margins.Bottom == Bottom;
}
/// <summary>
/// Calculates and retrieves a hash code based on the left, right, top, and bottom margins.
/// </summary>
public override int GetHashCode() => HashCode.Combine(Left, Right, Top, Bottom);
/// <summary>
/// Tests whether two <see cref='Margins'/> objects are identical.
/// </summary>
public static bool operator ==(Margins? m1, Margins? m2)
{
if (m1 is null)
{
return m2 is null;
}
if (m2 is null)
{
return false;
}
return m1.Equals(m2);
}
/// <summary>
/// Tests whether two <see cref='Margins'/> objects are different.
/// </summary>
public static bool operator !=(Margins? m1, Margins? m2) => !(m1 == m2);
/// <summary>
/// Provides some interesting information for the Margins in String form.
/// </summary>
public override string ToString() => $"[Margins Left={Left} Right={Right} Top={Top} Bottom={Bottom}]";
}
}