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.

229 lines
9.5KB

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