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.

juce_Grid.h 9.8KB

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