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.

190 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_JUSTIFICATION_JUCEHEADER__
  19. #define __JUCE_JUSTIFICATION_JUCEHEADER__
  20. #include "../geometry/juce_Rectangle.h"
  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 JUCE_API Justification
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a Justification object using a combination of flags. */
  33. inline Justification (int flags_) noexcept : flags (flags_) {}
  34. /** Creates a copy of another Justification object. */
  35. Justification (const Justification& other) noexcept;
  36. /** Copies another Justification object. */
  37. Justification& operator= (const Justification& other) noexcept;
  38. bool operator== (const Justification& other) const noexcept { return flags == other.flags; }
  39. bool operator!= (const Justification& other) const noexcept { return flags != other.flags; }
  40. //==============================================================================
  41. /** Returns the raw flags that are set for this Justification object. */
  42. inline int getFlags() const noexcept { return flags; }
  43. /** Tests a set of flags for this object.
  44. @returns true if any of the flags passed in are set on this object.
  45. */
  46. inline bool testFlags (int flagsToTest) const noexcept { return (flags & flagsToTest) != 0; }
  47. /** Returns just the flags from this object that deal with vertical layout. */
  48. int getOnlyVerticalFlags() const noexcept;
  49. /** Returns just the flags from this object that deal with horizontal layout. */
  50. int getOnlyHorizontalFlags() const noexcept;
  51. //==============================================================================
  52. /** Adjusts the position of a rectangle to fit it into a space.
  53. The (x, y) position of the rectangle will be updated to position it inside the
  54. given space according to the justification flags.
  55. */
  56. template <typename ValueType>
  57. void applyToRectangle (ValueType& x, ValueType& y, ValueType w, ValueType h,
  58. ValueType spaceX, ValueType spaceY, ValueType spaceW, ValueType spaceH) const noexcept
  59. {
  60. x = spaceX;
  61. if ((flags & horizontallyCentred) != 0) x += (spaceW - w) / (ValueType) 2;
  62. else if ((flags & right) != 0) x += spaceW - w;
  63. y = spaceY;
  64. if ((flags & verticallyCentred) != 0) y += (spaceH - h) / (ValueType) 2;
  65. else if ((flags & bottom) != 0) y += spaceH - h;
  66. }
  67. /** Returns the new position of a rectangle that has been justified to fit within a given space.
  68. */
  69. template <typename ValueType>
  70. const Rectangle<ValueType> appliedToRectangle (const Rectangle<ValueType>& areaToAdjust,
  71. const Rectangle<ValueType>& targetSpace) const noexcept
  72. {
  73. ValueType x = areaToAdjust.getX(), y = areaToAdjust.getY();
  74. applyToRectangle (x, y, areaToAdjust.getWidth(), areaToAdjust.getHeight(),
  75. targetSpace.getX(), targetSpace.getY(), targetSpace.getWidth(), targetSpace.getHeight());
  76. return areaToAdjust.withPosition (x, y);
  77. }
  78. //==============================================================================
  79. /** Flag values that can be combined and used in the constructor. */
  80. enum
  81. {
  82. //==============================================================================
  83. /** Indicates that the item should be aligned against the left edge of the available space. */
  84. left = 1,
  85. /** Indicates that the item should be aligned against the right edge of the available space. */
  86. right = 2,
  87. /** Indicates that the item should be placed in the centre between the left and right
  88. sides of the available space. */
  89. horizontallyCentred = 4,
  90. //==============================================================================
  91. /** Indicates that the item should be aligned against the top edge of the available space. */
  92. top = 8,
  93. /** Indicates that the item should be aligned against the bottom edge of the available space. */
  94. bottom = 16,
  95. /** Indicates that the item should be placed in the centre between the top and bottom
  96. sides of the available space. */
  97. verticallyCentred = 32,
  98. //==============================================================================
  99. /** Indicates that lines of text should be spread out to fill the maximum width
  100. available, so that both margins are aligned vertically.
  101. */
  102. horizontallyJustified = 64,
  103. //==============================================================================
  104. /** Indicates that the item should be centred vertically and horizontally.
  105. This is equivalent to (horizontallyCentred | verticallyCentred)
  106. */
  107. centred = 36,
  108. /** Indicates that the item should be centred vertically but placed on the left hand side.
  109. This is equivalent to (left | verticallyCentred)
  110. */
  111. centredLeft = 33,
  112. /** Indicates that the item should be centred vertically but placed on the right hand side.
  113. This is equivalent to (right | verticallyCentred)
  114. */
  115. centredRight = 34,
  116. /** Indicates that the item should be centred horizontally and placed at the top.
  117. This is equivalent to (horizontallyCentred | top)
  118. */
  119. centredTop = 12,
  120. /** Indicates that the item should be centred horizontally and placed at the bottom.
  121. This is equivalent to (horizontallyCentred | bottom)
  122. */
  123. centredBottom = 20,
  124. /** Indicates that the item should be placed in the top-left corner.
  125. This is equivalent to (left | top)
  126. */
  127. topLeft = 9,
  128. /** Indicates that the item should be placed in the top-right corner.
  129. This is equivalent to (right | top)
  130. */
  131. topRight = 10,
  132. /** Indicates that the item should be placed in the bottom-left corner.
  133. This is equivalent to (left | bottom)
  134. */
  135. bottomLeft = 17,
  136. /** Indicates that the item should be placed in the bottom-left corner.
  137. This is equivalent to (right | bottom)
  138. */
  139. bottomRight = 18
  140. };
  141. private:
  142. //==============================================================================
  143. int flags;
  144. };
  145. #endif // __JUCE_JUSTIFICATION_JUCEHEADER__