CellPos.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.ComponentModel;
  32. using System.Runtime.InteropServices;
  33. namespace XPTable.Models
  34. {
  35. /// <summary>
  36. /// Represents the position of a Cell in a Table
  37. /// </summary>
  38. [Serializable(),
  39. StructLayout(LayoutKind.Sequential)]
  40. public struct CellPos
  41. {
  42. #region Class Data
  43. /// <summary>
  44. /// Repsesents a null CellPos
  45. /// </summary>
  46. public static readonly CellPos Empty = new CellPos(-1, -1);
  47. /// <summary>
  48. /// The Row index of this CellPos
  49. /// </summary>
  50. private int row;
  51. /// <summary>
  52. /// The Column index of this CellPos
  53. /// </summary>
  54. private int column;
  55. #endregion
  56. #region Constructor
  57. /// <summary>
  58. /// Initializes a new instance of the CellPos class with the specified
  59. /// row index and column index
  60. /// </summary>
  61. /// <param name="row">The Row index of the CellPos</param>
  62. /// <param name="column">The Column index of the CellPos</param>
  63. public CellPos(int row, int column)
  64. {
  65. this.row = row;
  66. this.column = column;
  67. }
  68. #endregion
  69. #region Methods
  70. /// <summary>
  71. /// Translates this CellPos by the specified amount
  72. /// </summary>
  73. /// <param name="rows">The amount to offset the row index</param>
  74. /// <param name="columns">The amount to offset the column index</param>
  75. public void Offset(int rows, int columns)
  76. {
  77. this.row += rows;
  78. this.column += columns;
  79. }
  80. /// <summary>
  81. /// Tests whether obj is a CellPos structure with the same values as
  82. /// this CellPos structure
  83. /// </summary>
  84. /// <param name="obj">The Object to test</param>
  85. /// <returns>This method returns true if obj is a CellPos structure
  86. /// and its Row and Column properties are equal to the corresponding
  87. /// properties of this CellPos structure; otherwise, false</returns>
  88. public override bool Equals(object obj)
  89. {
  90. if (!(obj is CellPos))
  91. {
  92. return false;
  93. }
  94. CellPos cellPos = (CellPos) obj;
  95. if (cellPos.Row == this.Row)
  96. {
  97. return (cellPos.Column == this.Column);
  98. }
  99. return false;
  100. }
  101. /// <summary>
  102. /// Returns the hash code for this CellPos structure
  103. /// </summary>
  104. /// <returns>An integer that represents the hashcode for this
  105. /// CellPos</returns>
  106. public override int GetHashCode()
  107. {
  108. return (this.Row ^ ((this.Column << 13) | (this.Column >> 0x13)));
  109. }
  110. /// <summary>
  111. /// Converts the attributes of this CellPos to a human-readable string
  112. /// </summary>
  113. /// <returns>A string that contains the row and column indexes of this
  114. /// CellPos structure </returns>
  115. public override string ToString()
  116. {
  117. return "CellPos: (" + this.Row + "," + this.Column + ")";
  118. }
  119. #endregion
  120. #region Properties
  121. /// <summary>
  122. /// Gets or sets the Row index of this CellPos
  123. /// </summary>
  124. public int Row
  125. {
  126. get
  127. {
  128. return this.row;
  129. }
  130. set
  131. {
  132. this.row = value;
  133. }
  134. }
  135. /// <summary>
  136. /// Gets or sets the Column index of this CellPos
  137. /// </summary>
  138. public int Column
  139. {
  140. get
  141. {
  142. return this.column;
  143. }
  144. set
  145. {
  146. this.column = value;
  147. }
  148. }
  149. /// <summary>
  150. /// Tests whether any numeric properties of this CellPos have
  151. /// values of -1
  152. /// </summary>
  153. [Browsable(false)]
  154. public bool IsEmpty
  155. {
  156. get
  157. {
  158. return (this.Row == -1 || this.Column == -1);
  159. }
  160. }
  161. #endregion
  162. #region Operators
  163. /// <summary>
  164. /// Tests whether two CellPos structures have equal Row and Column
  165. /// properties
  166. /// </summary>
  167. /// <param name="left">The CellPos structure that is to the left
  168. /// of the equality operator</param>
  169. /// <param name="right">The CellPos structure that is to the right
  170. /// of the equality operator</param>
  171. /// <returns>This operator returns true if the two CellPos structures
  172. /// have equal Row and Column properties</returns>
  173. public static bool operator ==(CellPos left, CellPos right)
  174. {
  175. if (left.Row == right.Row)
  176. {
  177. return (left.Column == right.Column);
  178. }
  179. return false;
  180. }
  181. /// <summary>
  182. /// Tests whether two CellPos structures differ in their Row and
  183. /// Column properties
  184. /// </summary>
  185. /// <param name="left">The CellPos structure that is to the left
  186. /// of the equality operator</param>
  187. /// <param name="right">The CellPos structure that is to the right
  188. /// of the equality operator</param>
  189. /// <returns>This operator returns true if any of the Row and Column
  190. /// properties of the two CellPos structures are unequal; otherwise
  191. /// false</returns>
  192. public static bool operator !=(CellPos left, CellPos right)
  193. {
  194. return !(left == right);
  195. }
  196. #endregion
  197. }
  198. }