Audio plugin host https://kx.studio/carla
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.

190 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_JUSTIFICATION_H_INCLUDED
  18. #define JUCE_JUSTIFICATION_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Represents a type of justification to be used when positioning graphical items.
  22. e.g. it indicates whether something should be placed top-left, top-right,
  23. centred, etc.
  24. It is used in various places wherever this kind of information is needed.
  25. */
  26. class Justification
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates a Justification object using a combination of flags from the Flags enum. */
  31. Justification (int justificationFlags) noexcept : flags (justificationFlags) {}
  32. /** Creates a copy of another Justification object. */
  33. Justification (const Justification& other) noexcept : flags (other.flags) {}
  34. /** Copies another Justification object. */
  35. Justification& operator= (const Justification& other) noexcept
  36. {
  37. flags = other.flags;
  38. return *this;
  39. }
  40. bool operator== (const Justification& other) const noexcept { return flags == other.flags; }
  41. bool operator!= (const Justification& other) const noexcept { return flags != other.flags; }
  42. //==============================================================================
  43. /** Returns the raw flags that are set for this Justification object. */
  44. inline int getFlags() const noexcept { return flags; }
  45. /** Tests a set of flags for this object.
  46. @returns true if any of the flags passed in are set on this object.
  47. */
  48. inline bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  49. /** Returns just the flags from this object that deal with vertical layout. */
  50. int getOnlyVerticalFlags() const noexcept { return flags & (top | bottom | verticallyCentred); }
  51. /** Returns just the flags from this object that deal with horizontal layout. */
  52. int getOnlyHorizontalFlags() const noexcept { return flags & (left | right | horizontallyCentred | horizontallyJustified); }
  53. //==============================================================================
  54. /** Adjusts the position of a rectangle to fit it into a space.
  55. The (x, y) position of the rectangle will be updated to position it inside the
  56. given space according to the justification flags.
  57. */
  58. template <typename ValueType>
  59. void applyToRectangle (ValueType& x, ValueType& y, ValueType w, ValueType h,
  60. ValueType spaceX, ValueType spaceY, ValueType spaceW, ValueType spaceH) const noexcept
  61. {
  62. x = spaceX;
  63. if ((flags & horizontallyCentred) != 0) x += (spaceW - w) / (ValueType) 2;
  64. else if ((flags & right) != 0) x += spaceW - w;
  65. y = spaceY;
  66. if ((flags & verticallyCentred) != 0) y += (spaceH - h) / (ValueType) 2;
  67. else if ((flags & bottom) != 0) y += spaceH - h;
  68. }
  69. /** Returns the new position of a rectangle that has been justified to fit within a given space.
  70. */
  71. template <typename ValueType>
  72. const Rectangle<ValueType> appliedToRectangle (const Rectangle<ValueType>& areaToAdjust,
  73. const Rectangle<ValueType>& targetSpace) const noexcept
  74. {
  75. ValueType x = areaToAdjust.getX(), y = areaToAdjust.getY();
  76. applyToRectangle (x, y, areaToAdjust.getWidth(), areaToAdjust.getHeight(),
  77. targetSpace.getX(), targetSpace.getY(), targetSpace.getWidth(), targetSpace.getHeight());
  78. return areaToAdjust.withPosition (x, y);
  79. }
  80. //==============================================================================
  81. /** Flag values that can be combined and used in the constructor. */
  82. enum Flags
  83. {
  84. //==============================================================================
  85. /** Indicates that the item should be aligned against the left edge of the available space. */
  86. left = 1,
  87. /** Indicates that the item should be aligned against the right edge of the available space. */
  88. right = 2,
  89. /** Indicates that the item should be placed in the centre between the left and right
  90. sides of the available space. */
  91. horizontallyCentred = 4,
  92. //==============================================================================
  93. /** Indicates that the item should be aligned against the top edge of the available space. */
  94. top = 8,
  95. /** Indicates that the item should be aligned against the bottom edge of the available space. */
  96. bottom = 16,
  97. /** Indicates that the item should be placed in the centre between the top and bottom
  98. sides of the available space. */
  99. verticallyCentred = 32,
  100. //==============================================================================
  101. /** Indicates that lines of text should be spread out to fill the maximum width
  102. available, so that both margins are aligned vertically.
  103. */
  104. horizontallyJustified = 64,
  105. //==============================================================================
  106. /** Indicates that the item should be centred vertically and horizontally.
  107. This is equivalent to (horizontallyCentred | verticallyCentred)
  108. */
  109. centred = 36,
  110. /** Indicates that the item should be centred vertically but placed on the left hand side.
  111. This is equivalent to (left | verticallyCentred)
  112. */
  113. centredLeft = 33,
  114. /** Indicates that the item should be centred vertically but placed on the right hand side.
  115. This is equivalent to (right | verticallyCentred)
  116. */
  117. centredRight = 34,
  118. /** Indicates that the item should be centred horizontally and placed at the top.
  119. This is equivalent to (horizontallyCentred | top)
  120. */
  121. centredTop = 12,
  122. /** Indicates that the item should be centred horizontally and placed at the bottom.
  123. This is equivalent to (horizontallyCentred | bottom)
  124. */
  125. centredBottom = 20,
  126. /** Indicates that the item should be placed in the top-left corner.
  127. This is equivalent to (left | top)
  128. */
  129. topLeft = 9,
  130. /** Indicates that the item should be placed in the top-right corner.
  131. This is equivalent to (right | top)
  132. */
  133. topRight = 10,
  134. /** Indicates that the item should be placed in the bottom-left corner.
  135. This is equivalent to (left | bottom)
  136. */
  137. bottomLeft = 17,
  138. /** Indicates that the item should be placed in the bottom-left corner.
  139. This is equivalent to (right | bottom)
  140. */
  141. bottomRight = 18
  142. };
  143. private:
  144. //==============================================================================
  145. int flags;
  146. };
  147. #endif // JUCE_JUSTIFICATION_H_INCLUDED