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.

154 lines
5.7KB

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