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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. */
  27. class JUCE_API Grid
  28. {
  29. public:
  30. //==============================================================================
  31. /** A size in pixels */
  32. struct Px
  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
  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. /** */
  49. struct TrackInfo
  50. {
  51. /** Creates a track with auto dimension. */
  52. TrackInfo() noexcept;
  53. /** */
  54. TrackInfo (Px sizeInPixels) noexcept;
  55. /** */
  56. TrackInfo (Fr fractionOfFreeSpace) noexcept;
  57. /** */
  58. TrackInfo (Px sizeInPixels, const juce::String& endLineNameToUse) noexcept;
  59. /** */
  60. TrackInfo (Fr fractionOfFreeSpace, const juce::String& endLineNameToUse) noexcept;
  61. /** */
  62. TrackInfo (const juce::String& startLineNameToUse, Px sizeInPixels) noexcept;
  63. /** */
  64. TrackInfo (const juce::String& startLineNameToUse, Fr fractionOfFreeSpace) noexcept;
  65. /** */
  66. TrackInfo (const juce::String& startLineNameToUse, Px sizeInPixels, const juce::String& endLineNameToUse) noexcept;
  67. /** */
  68. TrackInfo (const juce::String& startLineNameToUse, Fr fractionOfFreeSpace, const juce::String& endLineNameToUse) noexcept;
  69. private:
  70. friend class Grid;
  71. friend class GridItem;
  72. float size = 0; // Either a fraction or an absolute size in pixels
  73. bool isFraction = false;
  74. bool hasKeyword = false;
  75. juce::String startLineName, endLineName;
  76. };
  77. //==============================================================================
  78. /** */
  79. enum class JustifyItems : int { start = 0, end, center, stretch };
  80. /** */
  81. enum class AlignItems : int { start = 0, end, center, stretch };
  82. /** */
  83. enum class JustifyContent { start, end, center, stretch, spaceAround, spaceBetween, spaceEvenly };
  84. /** */
  85. enum class AlignContent { start, end, center, stretch, spaceAround, spaceBetween, spaceEvenly };
  86. /** */
  87. enum class AutoFlow { row, column, rowDense, columnDense };
  88. //==============================================================================
  89. /** */
  90. Grid() noexcept;
  91. /** Destructor */
  92. ~Grid() noexcept;
  93. //==============================================================================
  94. /** */
  95. JustifyItems justifyItems = JustifyItems::stretch;
  96. /** */
  97. AlignItems alignItems = AlignItems::stretch;
  98. /** */
  99. JustifyContent justifyContent = JustifyContent::stretch;
  100. /** */
  101. AlignContent alignContent = AlignContent::stretch;
  102. /** */
  103. AutoFlow autoFlow = AutoFlow::row;
  104. //==============================================================================
  105. /** */
  106. juce::Array<TrackInfo> templateColumns;
  107. /** */
  108. juce::Array<TrackInfo> templateRows;
  109. /** Template areas */
  110. juce::StringArray templateAreas;
  111. /** */
  112. TrackInfo autoRows;
  113. /** */
  114. TrackInfo autoColumns;
  115. /** */
  116. Px columnGap { 0 };
  117. /** */
  118. Px rowGap { 0 };
  119. /** */
  120. void setGap (Px sizeInPixels) noexcept { rowGap = columnGap = sizeInPixels; }
  121. //==============================================================================
  122. /** */
  123. juce::Array<GridItem> items;
  124. //==============================================================================
  125. /** */
  126. void performLayout (juce::Rectangle<int>);
  127. //==============================================================================
  128. /** */
  129. int getNumberOfColumns() const noexcept { return templateColumns.size(); }
  130. /** */
  131. int getNumberOfRows() const noexcept { return templateRows.size(); }
  132. private:
  133. //==============================================================================
  134. struct SizeCalculation;
  135. struct PlacementHelpers;
  136. struct AutoPlacement;
  137. struct BoxAlignment;
  138. };
  139. constexpr Grid::Px operator"" _px (long double px) { return Grid::Px { px }; }
  140. constexpr Grid::Px operator"" _px (unsigned long long px) { return Grid::Px { px }; }
  141. constexpr Grid::Fr operator"" _fr (unsigned long long fr) { return Grid::Fr { fr }; }
  142. } // namespace juce