The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Specifies a set of gaps to be left around the sides of a rectangle.
  18. This is basically the size of the spaces at the top, bottom, left and right of
  19. a rectangle. It's used by various component classes to specify borders.
  20. @see Rectangle
  21. @tags{Graphics}
  22. */
  23. template <typename ValueType>
  24. class BorderSize
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates a null border.
  29. All sizes are left as 0.
  30. */
  31. BorderSize() = default;
  32. /** Creates a copy of another border. */
  33. BorderSize (const BorderSize&) = default;
  34. /** Creates a border with the given gaps. */
  35. BorderSize (ValueType topGap, ValueType leftGap, ValueType bottomGap, ValueType rightGap) noexcept
  36. : top (topGap), left (leftGap), bottom (bottomGap), right (rightGap)
  37. {
  38. }
  39. /** Creates a border with the given gap on all sides. */
  40. explicit BorderSize (ValueType allGaps) noexcept
  41. : top (allGaps), left (allGaps), bottom (allGaps), right (allGaps)
  42. {
  43. }
  44. //==============================================================================
  45. /** Returns the gap that should be left at the top of the region. */
  46. ValueType getTop() const noexcept { return top; }
  47. /** Returns the gap that should be left at the left of the region. */
  48. ValueType getLeft() const noexcept { return left; }
  49. /** Returns the gap that should be left at the bottom of the region. */
  50. ValueType getBottom() const noexcept { return bottom; }
  51. /** Returns the gap that should be left at the right of the region. */
  52. ValueType getRight() const noexcept { return right; }
  53. /** Returns the sum of the top and bottom gaps. */
  54. ValueType getTopAndBottom() const noexcept { return top + bottom; }
  55. /** Returns the sum of the left and right gaps. */
  56. ValueType getLeftAndRight() const noexcept { return left + right; }
  57. /** Returns true if this border has no thickness along any edge. */
  58. bool isEmpty() const noexcept { return left + right + top + bottom == ValueType(); }
  59. //==============================================================================
  60. /** Changes the top gap. */
  61. void setTop (ValueType newTopGap) noexcept { top = newTopGap; }
  62. /** Changes the left gap. */
  63. void setLeft (ValueType newLeftGap) noexcept { left = newLeftGap; }
  64. /** Changes the bottom gap. */
  65. void setBottom (ValueType newBottomGap) noexcept { bottom = newBottomGap; }
  66. /** Changes the right gap. */
  67. void setRight (ValueType newRightGap) noexcept { right = newRightGap; }
  68. //==============================================================================
  69. /** Returns a rectangle with these borders removed from it. */
  70. Rectangle<ValueType> subtractedFrom (const Rectangle<ValueType>& original) const noexcept
  71. {
  72. return Rectangle<ValueType> (original.getX() + left,
  73. original.getY() + top,
  74. original.getWidth() - (left + right),
  75. original.getHeight() - (top + bottom));
  76. }
  77. /** Removes this border from a given rectangle. */
  78. void subtractFrom (Rectangle<ValueType>& rectangle) const noexcept
  79. {
  80. rectangle = subtractedFrom (rectangle);
  81. }
  82. /** Returns a rectangle with these borders added around it. */
  83. Rectangle<ValueType> addedTo (const Rectangle<ValueType>& original) const noexcept
  84. {
  85. return Rectangle<ValueType> (original.getX() - left,
  86. original.getY() - top,
  87. original.getWidth() + (left + right),
  88. original.getHeight() + (top + bottom));
  89. }
  90. /** Adds this border around a given rectangle. */
  91. void addTo (Rectangle<ValueType>& rectangle) const noexcept
  92. {
  93. rectangle = addedTo (rectangle);
  94. }
  95. //==============================================================================
  96. bool operator== (const BorderSize& other) const noexcept
  97. {
  98. return top == other.top && left == other.left && bottom == other.bottom && right == other.right;
  99. }
  100. bool operator!= (const BorderSize& other) const noexcept
  101. {
  102. return ! operator== (other);
  103. }
  104. private:
  105. //==============================================================================
  106. ValueType top{}, left{}, bottom{}, right{};
  107. };
  108. } // namespace juce