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.

189 lines
8.2KB

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