juce_Grid.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. TrackInfo (Px sizeInPixels) noexcept;
  55. TrackInfo (Fr fractionOfFreeSpace) noexcept;
  56. TrackInfo (Px sizeInPixels, const String& endLineNameToUse) noexcept;
  57. TrackInfo (Fr fractionOfFreeSpace, const String& endLineNameToUse) noexcept;
  58. TrackInfo (const String& startLineNameToUse, Px sizeInPixels) noexcept;
  59. TrackInfo (const String& startLineNameToUse, Fr fractionOfFreeSpace) noexcept;
  60. TrackInfo (const String& startLineNameToUse, Px sizeInPixels, const String& endLineNameToUse) noexcept;
  61. TrackInfo (const String& startLineNameToUse, Fr fractionOfFreeSpace, const String& endLineNameToUse) noexcept;
  62. bool isAuto() const noexcept { return hasKeyword; }
  63. bool isFractional() const noexcept { return isFraction; }
  64. bool isPixels() const noexcept { return ! isFraction; }
  65. const String& getStartLineName() const noexcept { return startLineName; }
  66. const String& getEndLineName() const noexcept { return endLineName; }
  67. /** Get the track's size - which might mean an absolute pixels value or a fractional ratio. */
  68. float getSize() const noexcept { return size; }
  69. private:
  70. friend class Grid;
  71. float getAbsoluteSize (float relativeFractionalUnit) const;
  72. float size = 0; // Either a fraction or an absolute size in pixels
  73. bool isFraction = false;
  74. bool hasKeyword = false;
  75. String startLineName, endLineName;
  76. };
  77. //==============================================================================
  78. /** Possible values for the justifyItems property. */
  79. enum class JustifyItems : int
  80. {
  81. start = 0, /**< Content inside the item is justified towards the left. */
  82. end, /**< Content inside the item is justified towards the right. */
  83. center, /**< Content inside the item is justified towards the center. */
  84. stretch /**< Content inside the item is stretched from left to right. */
  85. };
  86. /** Possible values for the alignItems property. */
  87. enum class AlignItems : int
  88. {
  89. start = 0, /**< Content inside the item is aligned towards the top. */
  90. end, /**< Content inside the item is aligned towards the bottom. */
  91. center, /**< Content inside the item is aligned towards the center. */
  92. stretch /**< Content inside the item is stretched from top to bottom. */
  93. };
  94. /** Possible values for the justifyContent property. */
  95. enum class JustifyContent
  96. {
  97. start, /**< Items are justified towards the left of the container. */
  98. end, /**< Items are justified towards the right of the container. */
  99. center, /**< Items are justified towards the center of the container. */
  100. stretch, /**< Items are stretched from left to right of the container. */
  101. spaceAround, /**< Items are evenly spaced along the row with spaces between them. */
  102. spaceBetween, /**< Items are evenly spaced along the row with spaces around them. */
  103. spaceEvenly /**< Items are evenly spaced along the row with even amount of spaces between them. */
  104. };
  105. /** Possible values for the alignContent property. */
  106. enum class AlignContent
  107. {
  108. start, /**< Items are aligned towards the top of the container. */
  109. end, /**< Items are aligned towards the bottom of the container. */
  110. center, /**< Items are aligned towards the center of the container. */
  111. stretch, /**< Items are stretched from top to bottom of the container. */
  112. spaceAround, /**< Items are evenly spaced along the column with spaces between them. */
  113. spaceBetween, /**< Items are evenly spaced along the column with spaces around them. */
  114. spaceEvenly /**< Items are evenly spaced along the column with even amount of spaces between them. */
  115. };
  116. /** Possible values for the autoFlow property. */
  117. enum class AutoFlow
  118. {
  119. row, /**< Fills the grid by adding rows of items. */
  120. column, /**< Fills the grid by adding columns of items. */
  121. rowDense, /**< Fills the grid by adding rows of items and attempts to fill in gaps. */
  122. columnDense /**< Fills the grid by adding columns of items and attempts to fill in gaps. */
  123. };
  124. //==============================================================================
  125. /** Creates an empty Grid container with default parameters. */
  126. Grid() noexcept;
  127. /** Destructor */
  128. ~Grid() noexcept;
  129. //==============================================================================
  130. /** Specifies the alignment of content inside the items along the rows. */
  131. JustifyItems justifyItems = JustifyItems::stretch;
  132. /** Specifies the alignment of content inside the items along the columns. */
  133. AlignItems alignItems = AlignItems::stretch;
  134. /** Specifies the alignment of items along the rows. */
  135. JustifyContent justifyContent = JustifyContent::stretch;
  136. /** Specifies the alignment of items along the columns. */
  137. AlignContent alignContent = AlignContent::stretch;
  138. /** Specifies how the auto-placement algorithm places items. */
  139. AutoFlow autoFlow = AutoFlow::row;
  140. //==============================================================================
  141. /** The set of column tracks to lay out. */
  142. Array<TrackInfo> templateColumns;
  143. /** The set of row tracks to lay out. */
  144. Array<TrackInfo> templateRows;
  145. /** Template areas */
  146. StringArray templateAreas;
  147. /** The row track for auto dimension. */
  148. TrackInfo autoRows;
  149. /** The column track for auto dimension. */
  150. TrackInfo autoColumns;
  151. /** The gap in pixels between columns. */
  152. Px columnGap { 0 };
  153. /** The gap in pixels between rows. */
  154. Px rowGap { 0 };
  155. /** Sets the gap between rows and columns in pixels. */
  156. void setGap (Px sizeInPixels) noexcept { rowGap = columnGap = sizeInPixels; }
  157. //==============================================================================
  158. /** The set of items to lay-out. */
  159. Array<GridItem> items;
  160. //==============================================================================
  161. /** Lays-out the grid's items within the given rectangle. */
  162. void performLayout (Rectangle<int>);
  163. //==============================================================================
  164. /** Returns the number of columns. */
  165. int getNumberOfColumns() const noexcept { return templateColumns.size(); }
  166. /** Returns the number of rows. */
  167. int getNumberOfRows() const noexcept { return templateRows.size(); }
  168. private:
  169. //==============================================================================
  170. struct SizeCalculation;
  171. struct PlacementHelpers;
  172. struct AutoPlacement;
  173. struct BoxAlignment;
  174. };
  175. constexpr Grid::Px operator"" _px (long double px) { return Grid::Px { px }; }
  176. constexpr Grid::Px operator"" _px (unsigned long long px) { return Grid::Px { px }; }
  177. constexpr Grid::Fr operator"" _fr (unsigned long long fr) { return Grid::Fr { fr }; }
  178. } // namespace juce