CellRange.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // CellRange.h: interface for the CCellRange class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_CELLRANGE_H__F86EF761_725A_11D1_ABBA_00A0243D1382__INCLUDED_)
  5. #define AFX_CELLRANGE_H__F86EF761_725A_11D1_ABBA_00A0243D1382__INCLUDED_
  6. #if _MSC_VER >= 1000
  7. #pragma once
  8. #endif // _MSC_VER >= 1000
  9. // The code contained in this file is based on the original
  10. // WorldCom Grid control written by Joe Willcoxson,
  11. // mailto:chinajoe@aol.com
  12. // http://users.aol.com/chinajoe
  13. class CCellID
  14. {
  15. // Attributes
  16. public:
  17. int row, col;
  18. // Operations
  19. public:
  20. CCellID(int nRow = -1, int nCol = -1) : row(nRow), col(nCol) {}
  21. int IsValid() const { return (row >= 0 && col >= 0); }
  22. int operator==(const CCellID& rhs) { return (row == rhs.row && col == rhs.col); }
  23. int operator!=(const CCellID& rhs) { return !operator==(rhs); }
  24. };
  25. class CCellRange
  26. {
  27. public:
  28. CCellRange(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1)
  29. {
  30. Set(nMinRow, nMinCol, nMaxRow, nMaxCol);
  31. }
  32. void Set(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1);
  33. int IsValid() const;
  34. int InRange(int row, int col) const;
  35. int InRange(const CCellID& cellID) const;
  36. int Count() { return (m_nMaxRow - m_nMinRow + 1) * (m_nMaxCol - m_nMinCol + 1); }
  37. CCellID GetTopLeft() const;
  38. CCellRange Intersect(const CCellRange& rhs) const;
  39. int GetMinRow() const {return m_nMinRow;}
  40. void SetMinRow(int minRow) {m_nMinRow = minRow;}
  41. int GetMinCol() const {return m_nMinCol;}
  42. void SetMinCol(int minCol) {m_nMinCol = minCol;}
  43. int GetMaxRow() const {return m_nMaxRow;}
  44. void SetMaxRow(int maxRow) {m_nMaxRow = maxRow;}
  45. int GetMaxCol() const {return m_nMaxCol;}
  46. void SetMaxCol(int maxCol) {m_nMaxCol = maxCol;}
  47. int GetRowSpan() const {return m_nMaxRow - m_nMinRow + 1;}
  48. int GetColSpan() const {return m_nMaxCol - m_nMinCol + 1;}
  49. int operator==(const CCellRange& rhs);
  50. int operator!=(const CCellRange& rhs);
  51. protected:
  52. int m_nMinRow;
  53. int m_nMinCol;
  54. int m_nMaxRow;
  55. int m_nMaxCol;
  56. };
  57. inline void CCellRange::Set(int minRow, int minCol, int maxRow, int maxCol)
  58. {
  59. m_nMinRow = minRow;
  60. m_nMinCol = minCol;
  61. m_nMaxRow = maxRow;
  62. m_nMaxCol = maxCol;
  63. }
  64. inline int CCellRange::operator==(const CCellRange& rhs)
  65. {
  66. return ((m_nMinRow == rhs.m_nMinRow) && (m_nMinCol == rhs.m_nMinCol) &&
  67. (m_nMaxRow == rhs.m_nMaxRow) && (m_nMaxCol == rhs.m_nMaxCol));
  68. }
  69. inline int CCellRange::operator!=(const CCellRange& rhs)
  70. {
  71. return !operator==(rhs);
  72. }
  73. inline int CCellRange::IsValid() const
  74. {
  75. return (m_nMinRow >= 0 && m_nMinCol >= 0 && m_nMaxRow >= 0 && m_nMaxCol >= 0 &&
  76. m_nMinRow <= m_nMaxRow && m_nMinCol <= m_nMaxCol);
  77. }
  78. inline int CCellRange::InRange(int row, int col) const
  79. {
  80. return (row >= m_nMinRow && row <= m_nMaxRow && col >= m_nMinCol && col <= m_nMaxCol);
  81. }
  82. inline int CCellRange::InRange(const CCellID& cellID) const
  83. {
  84. return InRange(cellID.row, cellID.col);
  85. }
  86. inline CCellID CCellRange::GetTopLeft() const
  87. {
  88. return CCellID(m_nMinRow, m_nMinCol);
  89. }
  90. inline CCellRange CCellRange::Intersect(const CCellRange& rhs) const
  91. {
  92. return CCellRange(max(m_nMinRow,rhs.m_nMinRow), max(m_nMinCol,rhs.m_nMinCol),
  93. min(m_nMaxRow,rhs.m_nMaxRow), min(m_nMaxCol,rhs.m_nMaxCol));
  94. }
  95. #endif // !defined(AFX_CELLRANGE_H__F86EF761_725A_11D1_ABBA_00A0243D1382__INCLUDED_)