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.

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