juce_BorderSize.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_BORDERSIZE_H_INCLUDED
  18. #define JUCE_BORDERSIZE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Specifies a set of gaps to be left around the sides of a rectangle.
  22. This is basically the size of the spaces at the top, bottom, left and right of
  23. a rectangle. It's used by various component classes to specify borders.
  24. @see Rectangle
  25. */
  26. template <typename ValueType>
  27. class BorderSize
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a null border.
  32. All sizes are left as 0.
  33. */
  34. BorderSize() noexcept
  35. : top(), left(), bottom(), right()
  36. {
  37. }
  38. /** Creates a copy of another border. */
  39. BorderSize (const BorderSize& other) noexcept
  40. : top (other.top), left (other.left), bottom (other.bottom), right (other.right)
  41. {
  42. }
  43. /** Creates a border with the given gaps. */
  44. BorderSize (ValueType topGap, ValueType leftGap, ValueType bottomGap, ValueType rightGap) noexcept
  45. : top (topGap), left (leftGap), bottom (bottomGap), right (rightGap)
  46. {
  47. }
  48. /** Creates a border with the given gap on all sides. */
  49. explicit BorderSize (ValueType allGaps) noexcept
  50. : top (allGaps), left (allGaps), bottom (allGaps), right (allGaps)
  51. {
  52. }
  53. //==============================================================================
  54. /** Returns the gap that should be left at the top of the region. */
  55. ValueType getTop() const noexcept { return top; }
  56. /** Returns the gap that should be left at the top of the region. */
  57. ValueType getLeft() const noexcept { return left; }
  58. /** Returns the gap that should be left at the top of the region. */
  59. ValueType getBottom() const noexcept { return bottom; }
  60. /** Returns the gap that should be left at the top of the region. */
  61. ValueType getRight() const noexcept { return right; }
  62. /** Returns the sum of the top and bottom gaps. */
  63. ValueType getTopAndBottom() const noexcept { return top + bottom; }
  64. /** Returns the sum of the left and right gaps. */
  65. ValueType getLeftAndRight() const noexcept { return left + right; }
  66. /** Returns true if this border has no thickness along any edge. */
  67. bool isEmpty() const noexcept { return left + right + top + bottom == ValueType(); }
  68. //==============================================================================
  69. /** Changes the top gap. */
  70. void setTop (ValueType newTopGap) noexcept { top = newTopGap; }
  71. /** Changes the left gap. */
  72. void setLeft (ValueType newLeftGap) noexcept { left = newLeftGap; }
  73. /** Changes the bottom gap. */
  74. void setBottom (ValueType newBottomGap) noexcept { bottom = newBottomGap; }
  75. /** Changes the right gap. */
  76. void setRight (ValueType newRightGap) noexcept { right = newRightGap; }
  77. //==============================================================================
  78. /** Returns a rectangle with these borders removed from it. */
  79. Rectangle<ValueType> subtractedFrom (const Rectangle<ValueType>& original) const noexcept
  80. {
  81. return Rectangle<ValueType> (original.getX() + left,
  82. original.getY() + top,
  83. original.getWidth() - (left + right),
  84. original.getHeight() - (top + bottom));
  85. }
  86. /** Removes this border from a given rectangle. */
  87. void subtractFrom (Rectangle<ValueType>& rectangle) const noexcept
  88. {
  89. rectangle = subtractedFrom (rectangle);
  90. }
  91. /** Returns a rectangle with these borders added around it. */
  92. Rectangle<ValueType> addedTo (const Rectangle<ValueType>& original) const noexcept
  93. {
  94. return Rectangle<ValueType> (original.getX() - left,
  95. original.getY() - top,
  96. original.getWidth() + (left + right),
  97. original.getHeight() + (top + bottom));
  98. }
  99. /** Adds this border around a given rectangle. */
  100. void addTo (Rectangle<ValueType>& rectangle) const noexcept
  101. {
  102. rectangle = addedTo (rectangle);
  103. }
  104. //==============================================================================
  105. bool operator== (const BorderSize& other) const noexcept
  106. {
  107. return top == other.top && left == other.left && bottom == other.bottom && right == other.right;
  108. }
  109. bool operator!= (const BorderSize& other) const noexcept
  110. {
  111. return ! operator== (other);
  112. }
  113. private:
  114. //==============================================================================
  115. ValueType top, left, bottom, right;
  116. };
  117. #endif // JUCE_BORDERSIZE_H_INCLUDED