Size.cs 4.5 KB

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