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.

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