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.

149 lines
5.6KB

  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 copy of another border. */
  38. BorderSize (const BorderSize&) = default;
  39. /** Creates a border with the given gaps. */
  40. BorderSize (ValueType topGap, ValueType leftGap, ValueType bottomGap, ValueType rightGap) noexcept
  41. : top (topGap), left (leftGap), bottom (bottomGap), right (rightGap)
  42. {
  43. }
  44. /** Creates a border with the given gap on all sides. */
  45. explicit BorderSize (ValueType allGaps) noexcept
  46. : top (allGaps), left (allGaps), bottom (allGaps), right (allGaps)
  47. {
  48. }
  49. //==============================================================================
  50. /** Returns the gap that should be left at the top of the region. */
  51. ValueType getTop() const noexcept { return top; }
  52. /** Returns the gap that should be left at the left of the region. */
  53. ValueType getLeft() const noexcept { return left; }
  54. /** Returns the gap that should be left at the bottom of the region. */
  55. ValueType getBottom() const noexcept { return bottom; }
  56. /** Returns the gap that should be left at the right of the region. */
  57. ValueType getRight() const noexcept { return right; }
  58. /** Returns the sum of the top and bottom gaps. */
  59. ValueType getTopAndBottom() const noexcept { return top + bottom; }
  60. /** Returns the sum of the left and right gaps. */
  61. ValueType getLeftAndRight() const noexcept { return left + right; }
  62. /** Returns true if this border has no thickness along any edge. */
  63. bool isEmpty() const noexcept { return left + right + top + bottom == ValueType(); }
  64. //==============================================================================
  65. /** Changes the top gap. */
  66. void setTop (ValueType newTopGap) noexcept { top = newTopGap; }
  67. /** Changes the left gap. */
  68. void setLeft (ValueType newLeftGap) noexcept { left = newLeftGap; }
  69. /** Changes the bottom gap. */
  70. void setBottom (ValueType newBottomGap) noexcept { bottom = newBottomGap; }
  71. /** Changes the right gap. */
  72. void setRight (ValueType newRightGap) noexcept { right = newRightGap; }
  73. //==============================================================================
  74. /** Returns a rectangle with these borders removed from it. */
  75. Rectangle<ValueType> subtractedFrom (const Rectangle<ValueType>& original) const noexcept
  76. {
  77. return Rectangle<ValueType> (original.getX() + left,
  78. original.getY() + top,
  79. original.getWidth() - (left + right),
  80. original.getHeight() - (top + bottom));
  81. }
  82. /** Removes this border from a given rectangle. */
  83. void subtractFrom (Rectangle<ValueType>& rectangle) const noexcept
  84. {
  85. rectangle = subtractedFrom (rectangle);
  86. }
  87. /** Returns a rectangle with these borders added around it. */
  88. Rectangle<ValueType> addedTo (const Rectangle<ValueType>& original) const noexcept
  89. {
  90. return Rectangle<ValueType> (original.getX() - left,
  91. original.getY() - top,
  92. original.getWidth() + (left + right),
  93. original.getHeight() + (top + bottom));
  94. }
  95. /** Adds this border around a given rectangle. */
  96. void addTo (Rectangle<ValueType>& rectangle) const noexcept
  97. {
  98. rectangle = addedTo (rectangle);
  99. }
  100. //==============================================================================
  101. bool operator== (const BorderSize& other) const noexcept
  102. {
  103. return top == other.top && left == other.left && bottom == other.bottom && right == other.right;
  104. }
  105. bool operator!= (const BorderSize& other) const noexcept
  106. {
  107. return ! operator== (other);
  108. }
  109. private:
  110. //==============================================================================
  111. ValueType top{}, left{}, bottom{}, right{};
  112. };
  113. } // namespace juce