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.

250 lines
8.8KB

  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. Defines an item in a Grid
  23. @see Grid
  24. */
  25. class JUCE_API GridItem
  26. {
  27. public:
  28. enum class Keyword { autoValue };
  29. //==============================================================================
  30. /** Represents a span. */
  31. struct Span
  32. {
  33. explicit Span (int numberToUse) noexcept : number (numberToUse)
  34. {
  35. /* Span must be at least one and positive */
  36. jassert (numberToUse > 0);
  37. }
  38. explicit Span (int numberToUse, const juce::String& nameToUse) : Span (numberToUse)
  39. {
  40. /* Name must not be empty */
  41. jassert (nameToUse.isNotEmpty());
  42. name = nameToUse;
  43. }
  44. explicit Span (const juce::String& nameToUse) : name (nameToUse)
  45. {
  46. /* Name must not be empty */
  47. jassert (nameToUse.isNotEmpty());
  48. }
  49. int number = 1;
  50. juce::String name;
  51. };
  52. //==============================================================================
  53. /** Represents a property. */
  54. struct Property
  55. {
  56. /** */
  57. Property() noexcept;
  58. /** */
  59. Property (Keyword keyword) noexcept;
  60. /** */
  61. Property (const char* lineNameToUse) noexcept;
  62. /** */
  63. Property (const juce::String& lineNameToUse) noexcept;
  64. /** */
  65. Property (int numberToUse) noexcept;
  66. /** */
  67. Property (int numberToUse, const juce::String& lineNameToUse) noexcept;
  68. /** */
  69. Property (Span spanToUse) noexcept;
  70. private:
  71. bool hasSpan() const noexcept { return isSpan && ! isAuto; }
  72. bool hasAbsolute() const noexcept { return ! (isSpan || isAuto); }
  73. bool hasAuto() const noexcept { return isAuto; }
  74. bool hasName() const noexcept { return name.isNotEmpty(); }
  75. friend class Grid;
  76. juce::String name;
  77. int number = 1; /** Either an absolute line number or number of lines to span across. */
  78. bool isSpan = false;
  79. bool isAuto = false;
  80. };
  81. //==============================================================================
  82. /** Represents start and end properties. */
  83. struct StartAndEndProperty { Property start, end; };
  84. //==============================================================================
  85. /** Possible values for the justifySelf property. */
  86. enum class JustifySelf : int
  87. {
  88. start = 0, /**< Content inside the item is justified towards the left. */
  89. end, /**< Content inside the item is justified towards the right. */
  90. center, /**< Content inside the item is justified towards the center. */
  91. stretch, /**< Content inside the item is stretched from left to right. */
  92. autoValue /**< Follows the Grid container's justifyItems property. */
  93. };
  94. /** Possible values for the alignSelf property. */
  95. enum class AlignSelf : int
  96. {
  97. start = 0, /**< Content inside the item is aligned towards the top. */
  98. end, /**< Content inside the item is aligned towards the bottom. */
  99. center, /**< Content inside the item is aligned towards the center. */
  100. stretch, /**< Content inside the item is stretched from top to bottom. */
  101. autoValue /**< Follows the Grid container's alignItems property. */
  102. };
  103. /** Creates an item with default parameters. */
  104. GridItem() noexcept;
  105. /** Creates an item with a given Component to use. */
  106. GridItem (juce::Component& componentToUse) noexcept;
  107. /** Creates an item with a given Component to use. */
  108. GridItem (juce::Component* componentToUse) noexcept;
  109. /** Destructor. */
  110. ~GridItem() noexcept;
  111. //==============================================================================
  112. /** If this is non-null, it represents a Component whose bounds are controlled by this item. */
  113. juce::Component* associatedComponent = nullptr;
  114. //==============================================================================
  115. /** Determines the order used to lay out items in their grid container. */
  116. int order = 0;
  117. /** This is the justify-self property of the item.
  118. This determines the alignment of the item along the row.
  119. */
  120. JustifySelf justifySelf = JustifySelf::autoValue;
  121. /** This is the align-self property of the item.
  122. This determines the alignment of the item along the column.
  123. */
  124. AlignSelf alignSelf = AlignSelf::autoValue;
  125. /** These are the start and end properties of the column. */
  126. StartAndEndProperty column = { Keyword::autoValue, Keyword::autoValue };
  127. /** These are the start and end properties of the row. */
  128. StartAndEndProperty row = { Keyword::autoValue, Keyword::autoValue };
  129. /** */
  130. juce::String area;
  131. //==============================================================================
  132. enum
  133. {
  134. useDefaultValue = -2, /* TODO: useDefaultValue should be named useAuto */
  135. notAssigned = -1
  136. };
  137. /* TODO: move all of this into a common class that is shared with the FlexItem */
  138. float width = notAssigned;
  139. float minWidth = 0;
  140. float maxWidth = notAssigned;
  141. float height = notAssigned;
  142. float minHeight = 0;
  143. float maxHeight = notAssigned;
  144. /** Represents a margin. */
  145. struct Margin
  146. {
  147. Margin() noexcept;
  148. Margin (int size) noexcept;
  149. Margin (float size) noexcept;
  150. Margin (float top, float right, float bottom, float left) noexcept; /**< Creates a margin with these sizes. */
  151. float left;
  152. float right;
  153. float top;
  154. float bottom;
  155. };
  156. /** The margin to leave around this item. */
  157. Margin margin;
  158. /** The item's current bounds. */
  159. juce::Rectangle<float> currentBounds;
  160. /** Short-hand */
  161. void setArea (Property rowStart, Property columnStart, Property rowEnd, Property columnEnd);
  162. /** Short-hand, span of 1 by default */
  163. void setArea (Property rowStart, Property columnStart);
  164. /** Short-hand */
  165. void setArea (const juce::String& areaName);
  166. /** Short-hand */
  167. GridItem withArea (Property rowStart, Property columnStart, Property rowEnd, Property columnEnd) const noexcept;
  168. /** Short-hand, span of 1 by default */
  169. GridItem withArea (Property rowStart, Property columnStart) const noexcept;
  170. /** Short-hand */
  171. GridItem withArea (const juce::String& areaName) const noexcept;
  172. /** Returns a copy of this object with a new row property. */
  173. GridItem withRow (StartAndEndProperty row) const noexcept;
  174. /** Returns a copy of this object with a new column property. */
  175. GridItem withColumn (StartAndEndProperty column) const noexcept;
  176. /** Returns a copy of this object with a new alignSelf property. */
  177. GridItem withAlignSelf (AlignSelf newAlignSelf) const noexcept;
  178. /** Returns a copy of this object with a new justifySelf property. */
  179. GridItem withJustifySelf (JustifySelf newJustifySelf) const noexcept;
  180. /** Returns a copy of this object with a new width. */
  181. GridItem withWidth (float newWidth) const noexcept;
  182. /** Returns a copy of this object with a new height. */
  183. GridItem withHeight (float newHeight) const noexcept;
  184. /** Returns a copy of this object with a new size. */
  185. GridItem withSize (float newWidth, float newHeight) const noexcept;
  186. /** Returns a copy of this object with a new margin. */
  187. GridItem withMargin (Margin newMargin) const noexcept;
  188. /** Returns a copy of this object with a new order. */
  189. GridItem withOrder (int newOrder) const noexcept;
  190. };
  191. } // namespace juce