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.

159 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_BORDERSIZE_JUCEHEADER__
  19. #define __JUCE_BORDERSIZE_JUCEHEADER__
  20. #include "juce_Rectangle.h"
  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. JUCE_LEAK_DETECTOR (BorderSize);
  119. };
  120. #endif // __JUCE_BORDERSIZE_JUCEHEADER__