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.

146 lines
5.5KB

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