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