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.

193 lines
8.3KB

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