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.

juce_Justification.h 7.9KB

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