Margins.cs 3.6 KB

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