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.

150 lines
5.6KB

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