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.

223 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. /**
  16. Container that handles geometry for grid layouts (fixed columns and rows) using a set of declarative rules.
  17. Implemented from the `CSS Grid Layout` specification as described at:
  18. https://css-tricks.com/snippets/css/complete-guide-grid/
  19. @see GridItem
  20. @tags{GUI}
  21. */
  22. class JUCE_API Grid final
  23. {
  24. public:
  25. //==============================================================================
  26. /** A size in pixels */
  27. struct Px final
  28. {
  29. explicit Px (float p) : pixels (static_cast<long double>(p)) { /*sta (p >= 0.0f);*/ }
  30. explicit Px (int p) : pixels (static_cast<long double>(p)) { /*sta (p >= 0.0f);*/ }
  31. explicit constexpr Px (long double p) : pixels (p) {}
  32. explicit constexpr Px (unsigned long long p) : pixels (static_cast<long double>(p)) {}
  33. long double pixels;
  34. };
  35. /** A fractional ratio integer */
  36. struct Fr final
  37. {
  38. explicit Fr (int f) : fraction (static_cast<unsigned long long> (f)) {}
  39. explicit constexpr Fr (unsigned long long p) : fraction (p) {}
  40. unsigned long long fraction;
  41. };
  42. //==============================================================================
  43. /** Represents a track. */
  44. struct TrackInfo final
  45. {
  46. /** Creates a track with auto dimension. */
  47. TrackInfo() noexcept;
  48. TrackInfo (Px sizeInPixels) noexcept;
  49. TrackInfo (Fr fractionOfFreeSpace) noexcept;
  50. TrackInfo (Px sizeInPixels, const String& endLineNameToUse) noexcept;
  51. TrackInfo (Fr fractionOfFreeSpace, const String& endLineNameToUse) noexcept;
  52. TrackInfo (const String& startLineNameToUse, Px sizeInPixels) noexcept;
  53. TrackInfo (const String& startLineNameToUse, Fr fractionOfFreeSpace) noexcept;
  54. TrackInfo (const String& startLineNameToUse, Px sizeInPixels, const String& endLineNameToUse) noexcept;
  55. TrackInfo (const String& startLineNameToUse, Fr fractionOfFreeSpace, const String& endLineNameToUse) noexcept;
  56. bool isAuto() const noexcept { return hasKeyword; }
  57. bool isFractional() const noexcept { return isFraction; }
  58. bool isPixels() const noexcept { return ! isFraction; }
  59. const String& getStartLineName() const noexcept { return startLineName; }
  60. const String& getEndLineName() const noexcept { return endLineName; }
  61. /** Get the track's size - which might mean an absolute pixels value or a fractional ratio. */
  62. float getSize() const noexcept { return size; }
  63. private:
  64. friend class Grid;
  65. float getAbsoluteSize (float relativeFractionalUnit) const;
  66. float size = 0; // Either a fraction or an absolute size in pixels
  67. bool isFraction = false;
  68. bool hasKeyword = false;
  69. String startLineName, endLineName;
  70. };
  71. //==============================================================================
  72. /** Possible values for the justifyItems property. */
  73. enum class JustifyItems : int
  74. {
  75. start = 0, /**< Content inside the item is justified towards the left. */
  76. end, /**< Content inside the item is justified towards the right. */
  77. center, /**< Content inside the item is justified towards the center. */
  78. stretch /**< Content inside the item is stretched from left to right. */
  79. };
  80. /** Possible values for the alignItems property. */
  81. enum class AlignItems : int
  82. {
  83. start = 0, /**< Content inside the item is aligned towards the top. */
  84. end, /**< Content inside the item is aligned towards the bottom. */
  85. center, /**< Content inside the item is aligned towards the center. */
  86. stretch /**< Content inside the item is stretched from top to bottom. */
  87. };
  88. /** Possible values for the justifyContent property. */
  89. enum class JustifyContent
  90. {
  91. start, /**< Items are justified towards the left of the container. */
  92. end, /**< Items are justified towards the right of the container. */
  93. center, /**< Items are justified towards the center of the container. */
  94. stretch, /**< Items are stretched from left to right of the container. */
  95. spaceAround, /**< Items are evenly spaced along the row with spaces between them. */
  96. spaceBetween, /**< Items are evenly spaced along the row with spaces around them. */
  97. spaceEvenly /**< Items are evenly spaced along the row with even amount of spaces between them. */
  98. };
  99. /** Possible values for the alignContent property. */
  100. enum class AlignContent
  101. {
  102. start, /**< Items are aligned towards the top of the container. */
  103. end, /**< Items are aligned towards the bottom of the container. */
  104. center, /**< Items are aligned towards the center of the container. */
  105. stretch, /**< Items are stretched from top to bottom of the container. */
  106. spaceAround, /**< Items are evenly spaced along the column with spaces between them. */
  107. spaceBetween, /**< Items are evenly spaced along the column with spaces around them. */
  108. spaceEvenly /**< Items are evenly spaced along the column with even amount of spaces between them. */
  109. };
  110. /** Possible values for the autoFlow property. */
  111. enum class AutoFlow
  112. {
  113. row, /**< Fills the grid by adding rows of items. */
  114. column, /**< Fills the grid by adding columns of items. */
  115. rowDense, /**< Fills the grid by adding rows of items and attempts to fill in gaps. */
  116. columnDense /**< Fills the grid by adding columns of items and attempts to fill in gaps. */
  117. };
  118. //==============================================================================
  119. /** Creates an empty Grid container with default parameters. */
  120. Grid() noexcept;
  121. /** Destructor */
  122. ~Grid() noexcept;
  123. //==============================================================================
  124. /** Specifies the alignment of content inside the items along the rows. */
  125. JustifyItems justifyItems = JustifyItems::stretch;
  126. /** Specifies the alignment of content inside the items along the columns. */
  127. AlignItems alignItems = AlignItems::stretch;
  128. /** Specifies the alignment of items along the rows. */
  129. JustifyContent justifyContent = JustifyContent::stretch;
  130. /** Specifies the alignment of items along the columns. */
  131. AlignContent alignContent = AlignContent::stretch;
  132. /** Specifies how the auto-placement algorithm places items. */
  133. AutoFlow autoFlow = AutoFlow::row;
  134. //==============================================================================
  135. /** The set of column tracks to lay out. */
  136. Array<TrackInfo> templateColumns;
  137. /** The set of row tracks to lay out. */
  138. Array<TrackInfo> templateRows;
  139. /** Template areas */
  140. StringArray templateAreas;
  141. /** The row track for auto dimension. */
  142. TrackInfo autoRows;
  143. /** The column track for auto dimension. */
  144. TrackInfo autoColumns;
  145. /** The gap in pixels between columns. */
  146. Px columnGap { 0 };
  147. /** The gap in pixels between rows. */
  148. Px rowGap { 0 };
  149. /** Sets the gap between rows and columns in pixels. */
  150. void setGap (Px sizeInPixels) noexcept { rowGap = columnGap = sizeInPixels; }
  151. //==============================================================================
  152. /** The set of items to lay-out. */
  153. Array<GridItem> items;
  154. //==============================================================================
  155. /** Lays-out the grid's items within the given rectangle. */
  156. void performLayout (Rectangle<int>);
  157. //==============================================================================
  158. /** Returns the number of columns. */
  159. int getNumberOfColumns() const noexcept { return templateColumns.size(); }
  160. /** Returns the number of rows. */
  161. int getNumberOfRows() const noexcept { return templateRows.size(); }
  162. private:
  163. //==============================================================================
  164. struct SizeCalculation;
  165. struct PlacementHelpers;
  166. struct AutoPlacement;
  167. struct BoxAlignment;
  168. };
  169. constexpr Grid::Px operator"" _px (long double px) { return Grid::Px { px }; }
  170. constexpr Grid::Px operator"" _px (unsigned long long px) { return Grid::Px { px }; }
  171. constexpr Grid::Fr operator"" _fr (unsigned long long fr) { return Grid::Fr { fr }; }
  172. } // namespace juce