Size.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // System.Drawing.Size.cs
  3. //
  4. // Author:
  5. // Mike Kestner ([email protected])
  6. //
  7. // Copyright (C) 2001 Mike Kestner
  8. // Copyright (C) 2004 Novell, Inc. http://www.novell.com
  9. //
  10. using System;
  11. namespace Terminal {
  12. public struct Size
  13. {
  14. private int width, height;
  15. public static readonly Size Empty;
  16. /// <summary>
  17. /// Addition Operator
  18. /// </summary>
  19. ///
  20. /// <remarks>
  21. /// Addition of two Size structures.
  22. /// </remarks>
  23. public static Size operator + (Size sz1, Size sz2)
  24. {
  25. return new Size (sz1.Width + sz2.Width,
  26. sz1.Height + sz2.Height);
  27. }
  28. /// <summary>
  29. /// Equality Operator
  30. /// </summary>
  31. ///
  32. /// <remarks>
  33. /// Compares two Size objects. The return value is
  34. /// based on the equivalence of the Width and Height
  35. /// properties of the two Sizes.
  36. /// </remarks>
  37. public static bool operator == (Size sz1, Size sz2)
  38. {
  39. return ((sz1.Width == sz2.Width) &&
  40. (sz1.Height == sz2.Height));
  41. }
  42. /// <summary>
  43. /// Inequality Operator
  44. /// </summary>
  45. ///
  46. /// <remarks>
  47. /// Compares two Size objects. The return value is
  48. /// based on the equivalence of the Width and Height
  49. /// properties of the two Sizes.
  50. /// </remarks>
  51. public static bool operator != (Size sz1, Size sz2)
  52. {
  53. return ((sz1.Width != sz2.Width) ||
  54. (sz1.Height != sz2.Height));
  55. }
  56. /// <summary>
  57. /// Subtraction Operator
  58. /// </summary>
  59. ///
  60. /// <remarks>
  61. /// Subtracts two Size structures.
  62. /// </remarks>
  63. public static Size operator - (Size sz1, Size sz2)
  64. {
  65. return new Size (sz1.Width - sz2.Width,
  66. sz1.Height - sz2.Height);
  67. }
  68. /// <summary>
  69. /// Size to Point Conversion
  70. /// </summary>
  71. ///
  72. /// <remarks>
  73. /// Returns a Point based on the dimensions of a given
  74. /// Size. Requires explicit cast.
  75. /// </remarks>
  76. public static explicit operator Point (Size size)
  77. {
  78. return new Point (size.Width, size.Height);
  79. }
  80. /// <summary>
  81. /// Size Constructor
  82. /// </summary>
  83. ///
  84. /// <remarks>
  85. /// Creates a Size from a Point value.
  86. /// </remarks>
  87. public Size (Point pt)
  88. {
  89. width = pt.X;
  90. height = pt.Y;
  91. }
  92. /// <summary>
  93. /// Size Constructor
  94. /// </summary>
  95. ///
  96. /// <remarks>
  97. /// Creates a Size from specified dimensions.
  98. /// </remarks>
  99. public Size (int width, int height)
  100. {
  101. this.width = width;
  102. this.height = height;
  103. }
  104. /// <summary>
  105. /// IsEmpty Property
  106. /// </summary>
  107. ///
  108. /// <remarks>
  109. /// Indicates if both Width and Height are zero.
  110. /// </remarks>
  111. public bool IsEmpty {
  112. get {
  113. return ((width == 0) && (height == 0));
  114. }
  115. }
  116. /// <summary>
  117. /// Width Property
  118. /// </summary>
  119. ///
  120. /// <remarks>
  121. /// The Width coordinate of the Size.
  122. /// </remarks>
  123. public int Width {
  124. get {
  125. return width;
  126. }
  127. set {
  128. width = value;
  129. }
  130. }
  131. /// <summary>
  132. /// Height Property
  133. /// </summary>
  134. ///
  135. /// <remarks>
  136. /// The Height coordinate of the Size.
  137. /// </remarks>
  138. public int Height {
  139. get {
  140. return height;
  141. }
  142. set {
  143. height = value;
  144. }
  145. }
  146. /// <summary>
  147. /// Equals Method
  148. /// </summary>
  149. ///
  150. /// <remarks>
  151. /// Checks equivalence of this Size and another object.
  152. /// </remarks>
  153. public override bool Equals (object obj)
  154. {
  155. if (!(obj is Size))
  156. return false;
  157. return (this == (Size) obj);
  158. }
  159. /// <summary>
  160. /// GetHashCode Method
  161. /// </summary>
  162. ///
  163. /// <remarks>
  164. /// Calculates a hashing value.
  165. /// </remarks>
  166. public override int GetHashCode ()
  167. {
  168. return width^height;
  169. }
  170. /// <summary>
  171. /// ToString Method
  172. /// </summary>
  173. ///
  174. /// <remarks>
  175. /// Formats the Size as a string in coordinate notation.
  176. /// </remarks>
  177. public override string ToString ()
  178. {
  179. return String.Format ("{{Width={0}, Height={1}}}", width, height);
  180. }
  181. public static Size Add (Size sz1, Size sz2)
  182. {
  183. return new Size (sz1.Width + sz2.Width,
  184. sz1.Height + sz2.Height);
  185. }
  186. public static Size Subtract (Size sz1, Size sz2)
  187. {
  188. return new Size (sz1.Width - sz2.Width,
  189. sz1.Height - sz2.Height);
  190. }
  191. }
  192. }