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.

156 lines
5.8KB

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