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 8.2KB

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