selection.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #pragma once
  24. #include <xlnt/xlnt_config.hpp>
  25. #include <xlnt/cell/cell_reference.hpp>
  26. #include <xlnt/worksheet/pane.hpp>
  27. #include <xlnt/worksheet/range_reference.hpp>
  28. namespace xlnt {
  29. /// <summary>
  30. /// The selected area of a worksheet.
  31. /// </summary>
  32. class XLNT_API selection
  33. {
  34. public:
  35. /// <summary>
  36. /// default ctor
  37. /// </summary>
  38. explicit selection() = default;
  39. /// <summary>
  40. /// ctor when no range selected
  41. /// sqref == active_cell
  42. /// </summary>
  43. explicit selection(pane_corner quadrant, cell_reference active_cell)
  44. : active_cell_(active_cell), sqref_(range_reference(active_cell, active_cell)), pane_(quadrant)
  45. {
  46. }
  47. /// <summary>
  48. /// ctor with selected range
  49. /// sqref must contain active_cell
  50. /// </summary>
  51. explicit selection(pane_corner quadrant, cell_reference active_cell, range_reference selected)
  52. : active_cell_(active_cell), sqref_(selected), pane_(quadrant)
  53. {
  54. }
  55. /// <summary>
  56. /// Returns true if this selection has a defined active cell.
  57. /// </summary>
  58. bool has_active_cell() const
  59. {
  60. return active_cell_.is_set();
  61. }
  62. /// <summary>
  63. /// Returns the cell reference of the active cell.
  64. /// </summary>
  65. cell_reference active_cell() const
  66. {
  67. return active_cell_.get();
  68. }
  69. /// <summary>
  70. /// Sets the active cell to that pointed to by ref.
  71. /// </summary>
  72. void active_cell(const cell_reference &ref)
  73. {
  74. active_cell_ = ref;
  75. }
  76. /// <summary>
  77. /// Returns true if this selection has a defined sqref.
  78. /// </summary>
  79. bool has_sqref() const
  80. {
  81. return sqref_.is_set();
  82. }
  83. /// <summary>
  84. /// Returns the range encompassed by this selection.
  85. /// </summary>
  86. range_reference sqref() const
  87. {
  88. return sqref_.get();
  89. }
  90. /// <summary>
  91. /// Sets the range encompassed by this selection.
  92. /// </summary>
  93. void sqref(const range_reference &ref)
  94. {
  95. sqref_ = ref;
  96. }
  97. /// <summary>
  98. /// Sets the range encompassed by this selection.
  99. /// </summary>
  100. void sqref(const std::string &ref)
  101. {
  102. sqref(range_reference(ref));
  103. }
  104. /// <summary>
  105. /// Returns the sheet quadrant of this selection.
  106. /// </summary>
  107. pane_corner pane() const
  108. {
  109. return pane_;
  110. }
  111. /// <summary>
  112. /// Sets the sheet quadrant of this selection to corner.
  113. /// </summary>
  114. void pane(pane_corner corner)
  115. {
  116. pane_ = corner;
  117. }
  118. /// <summary>
  119. /// Returns true if this selection is equal to rhs based on its active cell,
  120. /// sqref, and pane.
  121. /// </summary>
  122. bool operator==(const selection &rhs) const
  123. {
  124. return active_cell_ == rhs.active_cell_
  125. && sqref_ == rhs.sqref_
  126. && pane_ == rhs.pane_;
  127. }
  128. private:
  129. /// <summary>
  130. /// The last selected cell in the selection
  131. /// </summary>
  132. optional<cell_reference> active_cell_;
  133. /// <summary>
  134. /// The last selected block in the selection
  135. /// contains active_cell_, normally == to active_cell_
  136. /// </summary>
  137. optional<range_reference> sqref_;
  138. /// <summary>
  139. /// The corner of the worksheet that this selection extends to
  140. /// </summary>
  141. pane_corner pane_ = pane_corner::top_left;
  142. };
  143. } // namespace xlnt