Margins.cs 3.7 KB

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