juce_Justification.h 8.2 KB

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