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.

187 lines
8.2KB

  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. #pragma once
  18. //==============================================================================
  19. /**
  20. Represents a type of justification to be used when positioning graphical items.
  21. e.g. it indicates whether something should be placed top-left, top-right,
  22. centred, etc.
  23. It is used in various places wherever this kind of information is needed.
  24. */
  25. class Justification
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates a Justification object using a combination of flags from the Flags enum. */
  30. Justification (int justificationFlags) noexcept : flags (justificationFlags) {}
  31. /** Creates a copy of another Justification object. */
  32. Justification (const Justification& other) noexcept : flags (other.flags) {}
  33. /** Copies another Justification object. */
  34. Justification& operator= (const Justification& other) noexcept
  35. {
  36. flags = other.flags;
  37. return *this;
  38. }
  39. bool operator== (const Justification& other) const noexcept { return flags == other.flags; }
  40. bool operator!= (const Justification& other) const noexcept { return flags != other.flags; }
  41. //==============================================================================
  42. /** Returns the raw flags that are set for this Justification object. */
  43. inline int getFlags() const noexcept { return flags; }
  44. /** Tests a set of flags for this object.
  45. @returns true if any of the flags passed in are set on this object.
  46. */
  47. inline bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  48. /** Returns just the flags from this object that deal with vertical layout. */
  49. int getOnlyVerticalFlags() const noexcept { return flags & (top | bottom | verticallyCentred); }
  50. /** Returns just the flags from this object that deal with horizontal layout. */
  51. int getOnlyHorizontalFlags() const noexcept { return flags & (left | right | horizontallyCentred | horizontallyJustified); }
  52. //==============================================================================
  53. /** Adjusts the position of a rectangle to fit it into a space.
  54. The (x, y) position of the rectangle will be updated to position it inside the
  55. given space according to the justification flags.
  56. */
  57. template <typename ValueType>
  58. void applyToRectangle (ValueType& x, ValueType& y, ValueType w, ValueType h,
  59. ValueType spaceX, ValueType spaceY, ValueType spaceW, ValueType spaceH) const noexcept
  60. {
  61. x = spaceX;
  62. if ((flags & horizontallyCentred) != 0) x += (spaceW - w) / (ValueType) 2;
  63. else if ((flags & right) != 0) x += spaceW - w;
  64. y = spaceY;
  65. if ((flags & verticallyCentred) != 0) y += (spaceH - h) / (ValueType) 2;
  66. else if ((flags & bottom) != 0) y += spaceH - h;
  67. }
  68. /** Returns the new position of a rectangle that has been justified to fit within a given space.
  69. */
  70. template <typename ValueType>
  71. const Rectangle<ValueType> appliedToRectangle (const Rectangle<ValueType>& areaToAdjust,
  72. const Rectangle<ValueType>& targetSpace) const noexcept
  73. {
  74. ValueType x = areaToAdjust.getX(), y = areaToAdjust.getY();
  75. applyToRectangle (x, y, areaToAdjust.getWidth(), areaToAdjust.getHeight(),
  76. targetSpace.getX(), targetSpace.getY(), targetSpace.getWidth(), targetSpace.getHeight());
  77. return areaToAdjust.withPosition (x, y);
  78. }
  79. //==============================================================================
  80. /** Flag values that can be combined and used in the constructor. */
  81. enum Flags
  82. {
  83. //==============================================================================
  84. /** Indicates that the item should be aligned against the left edge of the available space. */
  85. left = 1,
  86. /** Indicates that the item should be aligned against the right edge of the available space. */
  87. right = 2,
  88. /** Indicates that the item should be placed in the centre between the left and right
  89. sides of the available space. */
  90. horizontallyCentred = 4,
  91. //==============================================================================
  92. /** Indicates that the item should be aligned against the top edge of the available space. */
  93. top = 8,
  94. /** Indicates that the item should be aligned against the bottom edge of the available space. */
  95. bottom = 16,
  96. /** Indicates that the item should be placed in the centre between the top and bottom
  97. sides of the available space. */
  98. verticallyCentred = 32,
  99. //==============================================================================
  100. /** Indicates that lines of text should be spread out to fill the maximum width
  101. available, so that both margins are aligned vertically.
  102. */
  103. horizontallyJustified = 64,
  104. //==============================================================================
  105. /** Indicates that the item should be centred vertically and horizontally.
  106. This is equivalent to (horizontallyCentred | verticallyCentred)
  107. */
  108. centred = 36,
  109. /** Indicates that the item should be centred vertically but placed on the left hand side.
  110. This is equivalent to (left | verticallyCentred)
  111. */
  112. centredLeft = 33,
  113. /** Indicates that the item should be centred vertically but placed on the right hand side.
  114. This is equivalent to (right | verticallyCentred)
  115. */
  116. centredRight = 34,
  117. /** Indicates that the item should be centred horizontally and placed at the top.
  118. This is equivalent to (horizontallyCentred | top)
  119. */
  120. centredTop = 12,
  121. /** Indicates that the item should be centred horizontally and placed at the bottom.
  122. This is equivalent to (horizontallyCentred | bottom)
  123. */
  124. centredBottom = 20,
  125. /** Indicates that the item should be placed in the top-left corner.
  126. This is equivalent to (left | top)
  127. */
  128. topLeft = 9,
  129. /** Indicates that the item should be placed in the top-right corner.
  130. This is equivalent to (right | top)
  131. */
  132. topRight = 10,
  133. /** Indicates that the item should be placed in the bottom-left corner.
  134. This is equivalent to (left | bottom)
  135. */
  136. bottomLeft = 17,
  137. /** Indicates that the item should be placed in the bottom-left corner.
  138. This is equivalent to (right | bottom)
  139. */
  140. bottomRight = 18
  141. };
  142. private:
  143. //==============================================================================
  144. int flags;
  145. };