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.

1348 lines
52KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. struct AllTracksIncludingImplicit
  16. {
  17. Array<Grid::TrackInfo> items;
  18. int numImplicitLeading; // The number of implicit items before the explicit items
  19. };
  20. struct Tracks
  21. {
  22. AllTracksIncludingImplicit columns, rows;
  23. };
  24. struct Grid::SizeCalculation
  25. {
  26. static float getTotalAbsoluteSize (const Array<TrackInfo>& tracks, Px gapSize) noexcept
  27. {
  28. float totalCellSize = 0.0f;
  29. for (const auto& trackInfo : tracks)
  30. if (! trackInfo.isFractional() || trackInfo.isAuto())
  31. totalCellSize += trackInfo.getSize();
  32. float totalGap = tracks.size() > 1 ? static_cast<float> ((tracks.size() - 1) * gapSize.pixels)
  33. : 0.0f;
  34. return totalCellSize + totalGap;
  35. }
  36. static float getRelativeUnitSize (float size, float totalAbsolute, const Array<TrackInfo>& tracks) noexcept
  37. {
  38. const float totalRelative = jlimit (0.0f, size, size - totalAbsolute);
  39. float factorsSum = 0.0f;
  40. for (const auto& trackInfo : tracks)
  41. if (trackInfo.isFractional())
  42. factorsSum += trackInfo.getSize();
  43. jassert (factorsSum != 0.0f);
  44. return totalRelative / factorsSum;
  45. }
  46. //==============================================================================
  47. static float getTotalAbsoluteHeight (const Array<TrackInfo>& rowTracks, Px rowGap)
  48. {
  49. return getTotalAbsoluteSize (rowTracks, rowGap);
  50. }
  51. static float getTotalAbsoluteWidth (const Array<TrackInfo>& columnTracks, Px columnGap)
  52. {
  53. return getTotalAbsoluteSize (columnTracks, columnGap);
  54. }
  55. static float getRelativeWidthUnit (float gridWidth, Px columnGap, const Array<TrackInfo>& columnTracks)
  56. {
  57. return getRelativeUnitSize (gridWidth, getTotalAbsoluteWidth (columnTracks, columnGap), columnTracks);
  58. }
  59. static float getRelativeHeightUnit (float gridHeight, Px rowGap, const Array<TrackInfo>& rowTracks)
  60. {
  61. return getRelativeUnitSize (gridHeight, getTotalAbsoluteHeight (rowTracks, rowGap), rowTracks);
  62. }
  63. //==============================================================================
  64. static bool hasAnyFractions (const Array<TrackInfo>& tracks)
  65. {
  66. return std::any_of (tracks.begin(),
  67. tracks.end(),
  68. [] (const auto& t) { return t.isFractional(); });
  69. }
  70. void computeSizes (float gridWidth, float gridHeight,
  71. Px columnGapToUse, Px rowGapToUse,
  72. const Tracks& tracks)
  73. {
  74. if (hasAnyFractions (tracks.columns.items))
  75. relativeWidthUnit = getRelativeWidthUnit (gridWidth, columnGapToUse, tracks.columns.items);
  76. else
  77. remainingWidth = gridWidth - getTotalAbsoluteSize (tracks.columns.items, columnGapToUse);
  78. if (hasAnyFractions (tracks.rows.items))
  79. relativeHeightUnit = getRelativeHeightUnit (gridHeight, rowGapToUse, tracks.rows.items);
  80. else
  81. remainingHeight = gridHeight - getTotalAbsoluteSize (tracks.rows.items, rowGapToUse);
  82. }
  83. float relativeWidthUnit = 0.0f;
  84. float relativeHeightUnit = 0.0f;
  85. float remainingWidth = 0.0f;
  86. float remainingHeight = 0.0f;
  87. };
  88. //==============================================================================
  89. struct Grid::PlacementHelpers
  90. {
  91. enum { invalid = -999999 };
  92. static constexpr auto emptyAreaCharacter = ".";
  93. //==============================================================================
  94. struct LineRange { int start, end; };
  95. struct LineArea { LineRange column, row; };
  96. struct LineInfo { StringArray lineNames; };
  97. struct NamedArea
  98. {
  99. String name;
  100. LineArea lines;
  101. };
  102. //==============================================================================
  103. static Array<LineInfo> getArrayOfLinesFromTracks (const Array<TrackInfo>& tracks)
  104. {
  105. // fill line info array
  106. Array<LineInfo> lines;
  107. for (int i = 1; i <= tracks.size(); ++i)
  108. {
  109. const auto& currentTrack = tracks.getReference (i - 1);
  110. if (i == 1) // start line
  111. {
  112. LineInfo li;
  113. li.lineNames.add (currentTrack.getStartLineName());
  114. lines.add (li);
  115. }
  116. if (i > 1 && i <= tracks.size()) // two lines in between tracks
  117. {
  118. const auto& prevTrack = tracks.getReference (i - 2);
  119. LineInfo li;
  120. li.lineNames.add (prevTrack.getEndLineName());
  121. li.lineNames.add (currentTrack.getStartLineName());
  122. lines.add (li);
  123. }
  124. if (i == tracks.size()) // end line
  125. {
  126. LineInfo li;
  127. li.lineNames.add (currentTrack.getEndLineName());
  128. lines.add (li);
  129. }
  130. }
  131. jassert (lines.size() == tracks.size() + 1);
  132. return lines;
  133. }
  134. //==============================================================================
  135. static int deduceAbsoluteLineNumberFromLineName (GridItem::Property prop,
  136. const Array<TrackInfo>& tracks)
  137. {
  138. jassert (prop.hasAbsolute());
  139. const auto lines = getArrayOfLinesFromTracks (tracks);
  140. int count = 0;
  141. for (int i = 0; i < lines.size(); i++)
  142. {
  143. for (const auto& name : lines.getReference (i).lineNames)
  144. {
  145. if (prop.getName() == name)
  146. {
  147. ++count;
  148. break;
  149. }
  150. }
  151. if (count == prop.getNumber())
  152. return i + 1;
  153. }
  154. jassertfalse;
  155. return count;
  156. }
  157. static int deduceAbsoluteLineNumber (GridItem::Property prop,
  158. const Array<TrackInfo>& tracks)
  159. {
  160. jassert (prop.hasAbsolute());
  161. if (prop.hasName())
  162. return deduceAbsoluteLineNumberFromLineName (prop, tracks);
  163. if (prop.getNumber() > 0)
  164. return prop.getNumber();
  165. if (prop.getNumber() < 0)
  166. return tracks.size() + 2 + prop.getNumber();
  167. // An integer value of 0 is invalid
  168. jassertfalse;
  169. return 1;
  170. }
  171. static int deduceAbsoluteLineNumberFromNamedSpan (int startLineNumber,
  172. GridItem::Property propertyWithSpan,
  173. const Array<TrackInfo>& tracks)
  174. {
  175. jassert (propertyWithSpan.hasSpan());
  176. const auto lines = getArrayOfLinesFromTracks (tracks);
  177. int count = 0;
  178. for (int i = startLineNumber; i < lines.size(); i++)
  179. {
  180. for (const auto& name : lines.getReference (i).lineNames)
  181. {
  182. if (propertyWithSpan.getName() == name)
  183. {
  184. ++count;
  185. break;
  186. }
  187. }
  188. if (count == propertyWithSpan.getNumber())
  189. return i + 1;
  190. }
  191. jassertfalse;
  192. return count;
  193. }
  194. static int deduceAbsoluteLineNumberBasedOnSpan (int startLineNumber,
  195. GridItem::Property propertyWithSpan,
  196. const Array<TrackInfo>& tracks)
  197. {
  198. jassert (propertyWithSpan.hasSpan());
  199. if (propertyWithSpan.hasName())
  200. return deduceAbsoluteLineNumberFromNamedSpan (startLineNumber, propertyWithSpan, tracks);
  201. return startLineNumber + propertyWithSpan.getNumber();
  202. }
  203. //==============================================================================
  204. static LineRange deduceLineRange (GridItem::StartAndEndProperty prop, const Array<TrackInfo>& tracks)
  205. {
  206. jassert (! (prop.start.hasAuto() && prop.end.hasAuto()));
  207. if (prop.start.hasAbsolute() && prop.end.hasAuto())
  208. {
  209. prop.end = GridItem::Span (1);
  210. }
  211. else if (prop.start.hasAuto() && prop.end.hasAbsolute())
  212. {
  213. prop.start = GridItem::Span (1);
  214. }
  215. auto s = [&]() -> LineRange
  216. {
  217. if (prop.start.hasAbsolute() && prop.end.hasAbsolute())
  218. {
  219. return { deduceAbsoluteLineNumber (prop.start, tracks),
  220. deduceAbsoluteLineNumber (prop.end, tracks) };
  221. }
  222. if (prop.start.hasAbsolute() && prop.end.hasSpan())
  223. {
  224. const auto start = deduceAbsoluteLineNumber (prop.start, tracks);
  225. return { start, deduceAbsoluteLineNumberBasedOnSpan (start, prop.end, tracks) };
  226. }
  227. if (prop.start.hasSpan() && prop.end.hasAbsolute())
  228. {
  229. const auto start = deduceAbsoluteLineNumber (prop.end, tracks);
  230. return { start, deduceAbsoluteLineNumberBasedOnSpan (start, prop.start, tracks) };
  231. }
  232. // Can't have an item with spans on both start and end.
  233. jassertfalse;
  234. return {};
  235. }();
  236. // swap if start overtakes end
  237. if (s.start > s.end)
  238. std::swap (s.start, s.end);
  239. else if (s.start == s.end)
  240. s.end = s.start + 1;
  241. return s;
  242. }
  243. static LineArea deduceLineArea (const GridItem& item,
  244. const Grid& grid,
  245. const std::map<String, LineArea>& namedAreas)
  246. {
  247. if (item.area.isNotEmpty() && ! grid.templateAreas.isEmpty())
  248. {
  249. // Must be a named area!
  250. jassert (namedAreas.count (item.area) != 0);
  251. return namedAreas.at (item.area);
  252. }
  253. return { deduceLineRange (item.column, grid.templateColumns),
  254. deduceLineRange (item.row, grid.templateRows) };
  255. }
  256. //==============================================================================
  257. static Array<StringArray> parseAreasProperty (const StringArray& areasStrings)
  258. {
  259. Array<StringArray> strings;
  260. for (const auto& areaString : areasStrings)
  261. strings.add (StringArray::fromTokens (areaString, false));
  262. if (strings.size() > 0)
  263. {
  264. for (auto s : strings)
  265. {
  266. jassert (s.size() == strings[0].size()); // all rows must have the same number of columns
  267. }
  268. }
  269. return strings;
  270. }
  271. static NamedArea findArea (Array<StringArray>& stringsArrays)
  272. {
  273. NamedArea area;
  274. for (auto& stringArray : stringsArrays)
  275. {
  276. for (auto& string : stringArray)
  277. {
  278. // find anchor
  279. if (area.name.isEmpty())
  280. {
  281. if (string != emptyAreaCharacter)
  282. {
  283. area.name = string;
  284. area.lines.row.start = stringsArrays.indexOf (stringArray) + 1; // non-zero indexed;
  285. area.lines.column.start = stringArray.indexOf (string) + 1; // non-zero indexed;
  286. area.lines.row.end = stringsArrays.indexOf (stringArray) + 2;
  287. area.lines.column.end = stringArray.indexOf (string) + 2;
  288. // mark as visited
  289. string = emptyAreaCharacter;
  290. }
  291. }
  292. else
  293. {
  294. if (string == area.name)
  295. {
  296. area.lines.row.end = stringsArrays.indexOf (stringArray) + 2;
  297. area.lines.column.end = stringArray.indexOf (string) + 2;
  298. // mark as visited
  299. string = emptyAreaCharacter;
  300. }
  301. }
  302. }
  303. }
  304. return area;
  305. }
  306. //==============================================================================
  307. static std::map<String, LineArea> deduceNamedAreas (const StringArray& areasStrings)
  308. {
  309. auto stringsArrays = parseAreasProperty (areasStrings);
  310. std::map<String, LineArea> areas;
  311. for (auto area = findArea (stringsArrays); area.name.isNotEmpty(); area = findArea (stringsArrays))
  312. {
  313. if (areas.count (area.name) == 0)
  314. areas[area.name] = area.lines;
  315. else
  316. // Make sure your template-areas property only has one area with the same name and is well-formed
  317. jassertfalse;
  318. }
  319. return areas;
  320. }
  321. //==============================================================================
  322. static float getCoord (int trackNumber, float relativeUnit, Px gap, const Array<TrackInfo>& tracks)
  323. {
  324. float c = 0;
  325. for (const auto* it = tracks.begin(); it != tracks.begin() + trackNumber; ++it)
  326. c += it->getAbsoluteSize (relativeUnit) + static_cast<float> (gap.pixels);
  327. return c;
  328. }
  329. static Rectangle<float> getCellBounds (int columnNumber, int rowNumber,
  330. const Tracks& tracks,
  331. SizeCalculation calculation,
  332. Px columnGap, Px rowGap)
  333. {
  334. const auto correctedColumn = columnNumber - 1 + tracks.columns.numImplicitLeading;
  335. const auto correctedRow = rowNumber - 1 + tracks.rows .numImplicitLeading;
  336. jassert (isPositiveAndBelow (correctedColumn, tracks.columns.items.size()));
  337. jassert (isPositiveAndBelow (correctedRow, tracks.rows .items.size()));
  338. return { getCoord (correctedColumn, calculation.relativeWidthUnit, columnGap, tracks.columns.items),
  339. getCoord (correctedRow, calculation.relativeHeightUnit, rowGap, tracks.rows .items),
  340. tracks.columns.items.getReference (correctedColumn).getAbsoluteSize (calculation.relativeWidthUnit),
  341. tracks.rows .items.getReference (correctedRow) .getAbsoluteSize (calculation.relativeHeightUnit) };
  342. }
  343. static Rectangle<float> alignCell (Rectangle<float> area,
  344. int columnNumber, int rowNumber,
  345. int numberOfColumns, int numberOfRows,
  346. SizeCalculation calculation,
  347. AlignContent alignContent,
  348. JustifyContent justifyContent)
  349. {
  350. if (alignContent == AlignContent::end)
  351. area.setY (area.getY() + calculation.remainingHeight);
  352. if (justifyContent == JustifyContent::end)
  353. area.setX (area.getX() + calculation.remainingWidth);
  354. if (alignContent == AlignContent::center)
  355. area.setY (area.getY() + calculation.remainingHeight / 2);
  356. if (justifyContent == JustifyContent::center)
  357. area.setX (area.getX() + calculation.remainingWidth / 2);
  358. if (alignContent == AlignContent::spaceBetween)
  359. {
  360. const auto shift = ((float) (rowNumber - 1) * (calculation.remainingHeight / float(numberOfRows - 1)));
  361. area.setY (area.getY() + shift);
  362. }
  363. if (justifyContent == JustifyContent::spaceBetween)
  364. {
  365. const auto shift = ((float) (columnNumber - 1) * (calculation.remainingWidth / float(numberOfColumns - 1)));
  366. area.setX (area.getX() + shift);
  367. }
  368. if (alignContent == AlignContent::spaceEvenly)
  369. {
  370. const auto shift = ((float) rowNumber * (calculation.remainingHeight / float(numberOfRows + 1)));
  371. area.setY (area.getY() + shift);
  372. }
  373. if (justifyContent == JustifyContent::spaceEvenly)
  374. {
  375. const auto shift = ((float) columnNumber * (calculation.remainingWidth / float(numberOfColumns + 1)));
  376. area.setX (area.getX() + shift);
  377. }
  378. if (alignContent == AlignContent::spaceAround)
  379. {
  380. const auto inbetweenShift = calculation.remainingHeight / float(numberOfRows);
  381. const auto sidesShift = inbetweenShift / 2;
  382. auto shift = (float) (rowNumber - 1) * inbetweenShift + sidesShift;
  383. area.setY (area.getY() + shift);
  384. }
  385. if (justifyContent == JustifyContent::spaceAround)
  386. {
  387. const auto inbetweenShift = calculation.remainingWidth / float(numberOfColumns);
  388. const auto sidesShift = inbetweenShift / 2;
  389. auto shift = (float) (columnNumber - 1) * inbetweenShift + sidesShift;
  390. area.setX (area.getX() + shift);
  391. }
  392. return area;
  393. }
  394. static Rectangle<float> getAreaBounds (PlacementHelpers::LineRange columnRange,
  395. PlacementHelpers::LineRange rowRange,
  396. const Tracks& tracks,
  397. SizeCalculation calculation,
  398. AlignContent alignContent,
  399. JustifyContent justifyContent,
  400. Px columnGap, Px rowGap)
  401. {
  402. const auto findAlignedCell = [&] (int column, int row)
  403. {
  404. const auto cell = getCellBounds (column, row, tracks, calculation, columnGap, rowGap);
  405. return alignCell (cell,
  406. column,
  407. row,
  408. tracks.columns.items.size(),
  409. tracks.rows.items.size(),
  410. calculation,
  411. alignContent,
  412. justifyContent);
  413. };
  414. const auto startCell = findAlignedCell (columnRange.start, rowRange.start);
  415. const auto endCell = findAlignedCell (columnRange.end - 1, rowRange.end - 1);
  416. const auto horizontalRange = startCell.getHorizontalRange().getUnionWith (endCell.getHorizontalRange());
  417. const auto verticalRange = startCell.getVerticalRange() .getUnionWith (endCell.getVerticalRange());
  418. return { horizontalRange.getStart(), verticalRange.getStart(),
  419. horizontalRange.getLength(), verticalRange.getLength() };
  420. }
  421. };
  422. template <typename Item>
  423. static Array<Item> operator+ (const Array<Item>& a, const Array<Item>& b)
  424. {
  425. auto copy = a;
  426. copy.addArray (b);
  427. return copy;
  428. }
  429. //==============================================================================
  430. struct Grid::AutoPlacement
  431. {
  432. using ItemPlacementArray = Array<std::pair<GridItem*, PlacementHelpers::LineArea>>;
  433. //==============================================================================
  434. struct OccupancyPlane
  435. {
  436. struct Cell { int column, row; };
  437. OccupancyPlane (int highestColumnToUse, int highestRowToUse, bool isColumnFirst)
  438. : highestCrossDimension (isColumnFirst ? highestRowToUse : highestColumnToUse),
  439. columnFirst (isColumnFirst)
  440. {}
  441. PlacementHelpers::LineArea setCell (Cell cell, int columnSpan, int rowSpan)
  442. {
  443. for (int i = 0; i < columnSpan; i++)
  444. for (int j = 0; j < rowSpan; j++)
  445. setCell (cell.column + i, cell.row + j);
  446. return { { cell.column, cell.column + columnSpan }, { cell.row, cell.row + rowSpan } };
  447. }
  448. PlacementHelpers::LineArea setCell (Cell start, Cell end)
  449. {
  450. return setCell (start, std::abs (end.column - start.column),
  451. std::abs (end.row - start.row));
  452. }
  453. Cell nextAvailable (Cell referenceCell, int columnSpan, int rowSpan)
  454. {
  455. while (isOccupied (referenceCell, columnSpan, rowSpan) || isOutOfBounds (referenceCell, columnSpan, rowSpan))
  456. referenceCell = advance (referenceCell);
  457. return referenceCell;
  458. }
  459. Cell nextAvailableOnRow (Cell referenceCell, int columnSpan, int rowSpan, int rowNumber)
  460. {
  461. if (columnFirst && (rowNumber + rowSpan) > highestCrossDimension)
  462. highestCrossDimension = rowNumber + rowSpan;
  463. while (isOccupied (referenceCell, columnSpan, rowSpan)
  464. || (referenceCell.row != rowNumber))
  465. referenceCell = advance (referenceCell);
  466. return referenceCell;
  467. }
  468. Cell nextAvailableOnColumn (Cell referenceCell, int columnSpan, int rowSpan, int columnNumber)
  469. {
  470. if (! columnFirst && (columnNumber + columnSpan) > highestCrossDimension)
  471. highestCrossDimension = columnNumber + columnSpan;
  472. while (isOccupied (referenceCell, columnSpan, rowSpan)
  473. || (referenceCell.column != columnNumber))
  474. referenceCell = advance (referenceCell);
  475. return referenceCell;
  476. }
  477. private:
  478. struct SortableCell
  479. {
  480. int column, row;
  481. bool columnFirst;
  482. bool operator< (const SortableCell& other) const
  483. {
  484. if (columnFirst)
  485. {
  486. if (row == other.row)
  487. return column < other.column;
  488. return row < other.row;
  489. }
  490. if (row == other.row)
  491. return column < other.column;
  492. return row < other.row;
  493. }
  494. };
  495. void setCell (int column, int row)
  496. {
  497. occupiedCells.insert ({ column, row, columnFirst });
  498. }
  499. bool isOccupied (Cell cell) const
  500. {
  501. return occupiedCells.count ({ cell.column, cell.row, columnFirst }) > 0;
  502. }
  503. bool isOccupied (Cell cell, int columnSpan, int rowSpan) const
  504. {
  505. for (int i = 0; i < columnSpan; i++)
  506. for (int j = 0; j < rowSpan; j++)
  507. if (isOccupied ({ cell.column + i, cell.row + j }))
  508. return true;
  509. return false;
  510. }
  511. bool isOutOfBounds (Cell cell, int columnSpan, int rowSpan) const
  512. {
  513. const auto crossSpan = columnFirst ? rowSpan : columnSpan;
  514. return (getCrossDimension (cell) + crossSpan) > getHighestCrossDimension();
  515. }
  516. int getHighestCrossDimension() const
  517. {
  518. Cell cell { 1, 1 };
  519. if (occupiedCells.size() > 0)
  520. cell = { occupiedCells.crbegin()->column, occupiedCells.crbegin()->row };
  521. return std::max (getCrossDimension (cell), highestCrossDimension);
  522. }
  523. Cell advance (Cell cell) const
  524. {
  525. if ((getCrossDimension (cell) + 1) >= getHighestCrossDimension())
  526. return fromDimensions (getMainDimension (cell) + 1, 1);
  527. return fromDimensions (getMainDimension (cell), getCrossDimension (cell) + 1);
  528. }
  529. int getMainDimension (Cell cell) const { return columnFirst ? cell.column : cell.row; }
  530. int getCrossDimension (Cell cell) const { return columnFirst ? cell.row : cell.column; }
  531. Cell fromDimensions (int mainDimension, int crossDimension) const
  532. {
  533. if (columnFirst)
  534. return { mainDimension, crossDimension };
  535. return { crossDimension, mainDimension };
  536. }
  537. int highestCrossDimension;
  538. bool columnFirst;
  539. std::set<SortableCell> occupiedCells;
  540. };
  541. //==============================================================================
  542. static bool isFixed (GridItem::StartAndEndProperty prop)
  543. {
  544. return prop.start.hasName() || prop.start.hasAbsolute() || prop.end.hasName() || prop.end.hasAbsolute();
  545. }
  546. static bool hasFullyFixedPlacement (const GridItem& item)
  547. {
  548. if (item.area.isNotEmpty())
  549. return true;
  550. if (isFixed (item.column) && isFixed (item.row))
  551. return true;
  552. return false;
  553. }
  554. static bool hasPartialFixedPlacement (const GridItem& item)
  555. {
  556. if (item.area.isNotEmpty())
  557. return false;
  558. if (isFixed (item.column) ^ isFixed (item.row))
  559. return true;
  560. return false;
  561. }
  562. static bool hasAutoPlacement (const GridItem& item)
  563. {
  564. return ! hasFullyFixedPlacement (item) && ! hasPartialFixedPlacement (item);
  565. }
  566. //==============================================================================
  567. static bool hasDenseAutoFlow (AutoFlow autoFlow)
  568. {
  569. return autoFlow == AutoFlow::columnDense
  570. || autoFlow == AutoFlow::rowDense;
  571. }
  572. static bool isColumnAutoFlow (AutoFlow autoFlow)
  573. {
  574. return autoFlow == AutoFlow::column
  575. || autoFlow == AutoFlow::columnDense;
  576. }
  577. //==============================================================================
  578. static int getSpanFromAuto (GridItem::StartAndEndProperty prop)
  579. {
  580. if (prop.end.hasSpan())
  581. return prop.end.getNumber();
  582. if (prop.start.hasSpan())
  583. return prop.start.getNumber();
  584. return 1;
  585. }
  586. //==============================================================================
  587. ItemPlacementArray deduceAllItems (Grid& grid) const
  588. {
  589. const auto namedAreas = PlacementHelpers::deduceNamedAreas (grid.templateAreas);
  590. OccupancyPlane plane (jmax (grid.templateColumns.size() + 1, 2),
  591. jmax (grid.templateRows.size() + 1, 2),
  592. isColumnAutoFlow (grid.autoFlow));
  593. ItemPlacementArray itemPlacementArray;
  594. Array<GridItem*> sortedItems;
  595. for (auto& item : grid.items)
  596. sortedItems.add (&item);
  597. std::stable_sort (sortedItems.begin(), sortedItems.end(),
  598. [] (const GridItem* i1, const GridItem* i2) { return i1->order < i2->order; });
  599. // place fixed items first
  600. for (auto* item : sortedItems)
  601. {
  602. if (hasFullyFixedPlacement (*item))
  603. {
  604. const auto a = PlacementHelpers::deduceLineArea (*item, grid, namedAreas);
  605. plane.setCell ({ a.column.start, a.row.start }, { a.column.end, a.row.end });
  606. itemPlacementArray.add ({ item, a });
  607. }
  608. }
  609. OccupancyPlane::Cell lastInsertionCell = { 1, 1 };
  610. for (auto* item : sortedItems)
  611. {
  612. if (hasPartialFixedPlacement (*item))
  613. {
  614. if (isFixed (item->column))
  615. {
  616. const auto p = PlacementHelpers::deduceLineRange (item->column, grid.templateColumns);
  617. const auto columnSpan = std::abs (p.start - p.end);
  618. const auto rowSpan = getSpanFromAuto (item->row);
  619. const auto insertionCell = hasDenseAutoFlow (grid.autoFlow) ? OccupancyPlane::Cell { p.start, 1 }
  620. : lastInsertionCell;
  621. const auto nextAvailableCell = plane.nextAvailableOnColumn (insertionCell, columnSpan, rowSpan, p.start);
  622. const auto lineArea = plane.setCell (nextAvailableCell, columnSpan, rowSpan);
  623. lastInsertionCell = nextAvailableCell;
  624. itemPlacementArray.add ({ item, lineArea });
  625. }
  626. else if (isFixed (item->row))
  627. {
  628. const auto p = PlacementHelpers::deduceLineRange (item->row, grid.templateRows);
  629. const auto columnSpan = getSpanFromAuto (item->column);
  630. const auto rowSpan = std::abs (p.start - p.end);
  631. const auto insertionCell = hasDenseAutoFlow (grid.autoFlow) ? OccupancyPlane::Cell { 1, p.start }
  632. : lastInsertionCell;
  633. const auto nextAvailableCell = plane.nextAvailableOnRow (insertionCell, columnSpan, rowSpan, p.start);
  634. const auto lineArea = plane.setCell (nextAvailableCell, columnSpan, rowSpan);
  635. lastInsertionCell = nextAvailableCell;
  636. itemPlacementArray.add ({ item, lineArea });
  637. }
  638. }
  639. }
  640. lastInsertionCell = { 1, 1 };
  641. for (auto* item : sortedItems)
  642. {
  643. if (hasAutoPlacement (*item))
  644. {
  645. const auto columnSpan = getSpanFromAuto (item->column);
  646. const auto rowSpan = getSpanFromAuto (item->row);
  647. const auto nextAvailableCell = plane.nextAvailable (lastInsertionCell, columnSpan, rowSpan);
  648. const auto lineArea = plane.setCell (nextAvailableCell, columnSpan, rowSpan);
  649. if (! hasDenseAutoFlow (grid.autoFlow))
  650. lastInsertionCell = nextAvailableCell;
  651. itemPlacementArray.add ({ item, lineArea });
  652. }
  653. }
  654. return itemPlacementArray;
  655. }
  656. //==============================================================================
  657. template <typename Accessor>
  658. static PlacementHelpers::LineRange findFullLineRange (const ItemPlacementArray& items, Accessor&& accessor)
  659. {
  660. if (items.isEmpty())
  661. return { 1, 1 };
  662. const auto combine = [&accessor] (const auto& acc, const auto& item)
  663. {
  664. const auto newRange = accessor (item);
  665. return PlacementHelpers::LineRange { std::min (acc.start, newRange.start),
  666. std::max (acc.end, newRange.end) };
  667. };
  668. return std::accumulate (std::next (items.begin()), items.end(), accessor (*items.begin()), combine);
  669. }
  670. static PlacementHelpers::LineArea findFullLineArea (const ItemPlacementArray& items)
  671. {
  672. return { findFullLineRange (items, [] (const auto& item) { return item.second.column; }),
  673. findFullLineRange (items, [] (const auto& item) { return item.second.row; }) };
  674. }
  675. template <typename Item>
  676. static Array<Item> repeated (int repeats, const Item& item)
  677. {
  678. Array<Item> result;
  679. result.insertMultiple (-1, item, repeats);
  680. return result;
  681. }
  682. static Tracks createImplicitTracks (const Grid& grid, const ItemPlacementArray& items)
  683. {
  684. const auto fullArea = findFullLineArea (items);
  685. const auto leadingColumns = std::max (0, 1 - fullArea.column.start);
  686. const auto leadingRows = std::max (0, 1 - fullArea.row.start);
  687. const auto trailingColumns = std::max (0, fullArea.column.end - grid.templateColumns.size() - 1);
  688. const auto trailingRows = std::max (0, fullArea.row .end - grid.templateRows .size() - 1);
  689. return { { repeated (leadingColumns, grid.autoColumns) + grid.templateColumns + repeated (trailingColumns, grid.autoColumns),
  690. leadingColumns },
  691. { repeated (leadingRows, grid.autoRows) + grid.templateRows + repeated (trailingRows, grid.autoRows),
  692. leadingRows } };
  693. }
  694. //==============================================================================
  695. static void applySizeForAutoTracks (Tracks& tracks, const ItemPlacementArray& placements)
  696. {
  697. const auto setSizes = [&placements] (auto& tracksInDirection, const auto& getItem, const auto& getItemSize)
  698. {
  699. auto& array = tracksInDirection.items;
  700. for (int index = 0; index < array.size(); ++index)
  701. {
  702. if (array.getReference (index).isAuto())
  703. {
  704. const auto combiner = [&] (const auto acc, const auto& element)
  705. {
  706. const auto item = getItem (element.second);
  707. const auto isNotSpan = std::abs (item.end - item.start) <= 1;
  708. return isNotSpan && item.start == index + 1 - tracksInDirection.numImplicitLeading
  709. ? std::max (acc, getItemSize (*element.first))
  710. : acc;
  711. };
  712. array.getReference (index).size = std::accumulate (placements.begin(), placements.end(), 0.0f, combiner);
  713. }
  714. }
  715. };
  716. setSizes (tracks.rows,
  717. [] (const auto& i) { return i.row; },
  718. [] (const auto& i) { return i.height + i.margin.top + i.margin.bottom; });
  719. setSizes (tracks.columns,
  720. [] (const auto& i) { return i.column; },
  721. [] (const auto& i) { return i.width + i.margin.left + i.margin.right; });
  722. }
  723. };
  724. //==============================================================================
  725. struct Grid::BoxAlignment
  726. {
  727. static Rectangle<float> alignItem (const GridItem& item,
  728. const Grid& grid,
  729. Rectangle<float> area)
  730. {
  731. // if item align is auto, inherit value from grid
  732. const auto alignType = item.alignSelf == GridItem::AlignSelf::autoValue
  733. ? grid.alignItems
  734. : static_cast<AlignItems> (item.alignSelf);
  735. const auto justifyType = item.justifySelf == GridItem::JustifySelf::autoValue
  736. ? grid.justifyItems
  737. : static_cast<JustifyItems> (item.justifySelf);
  738. // subtract margin from area
  739. area = BorderSize<float> (item.margin.top, item.margin.left, item.margin.bottom, item.margin.right)
  740. .subtractedFrom (area);
  741. // align and justify
  742. auto r = area;
  743. if (item.width != (float) GridItem::notAssigned) r.setWidth (item.width);
  744. if (item.height != (float) GridItem::notAssigned) r.setHeight (item.height);
  745. if (item.maxWidth != (float) GridItem::notAssigned) r.setWidth (jmin (item.maxWidth, r.getWidth()));
  746. if (item.minWidth > 0.0f) r.setWidth (jmax (item.minWidth, r.getWidth()));
  747. if (item.maxHeight != (float) GridItem::notAssigned) r.setHeight (jmin (item.maxHeight, r.getHeight()));
  748. if (item.minHeight > 0.0f) r.setHeight (jmax (item.minHeight, r.getHeight()));
  749. if (alignType == AlignItems::start && justifyType == JustifyItems::start)
  750. return r;
  751. if (alignType == AlignItems::end) r.setY (r.getY() + (area.getHeight() - r.getHeight()));
  752. if (justifyType == JustifyItems::end) r.setX (r.getX() + (area.getWidth() - r.getWidth()));
  753. if (alignType == AlignItems::center) r.setCentre (r.getCentreX(), area.getCentreY());
  754. if (justifyType == JustifyItems::center) r.setCentre (area.getCentreX(), r.getCentreY());
  755. return r;
  756. }
  757. };
  758. //==============================================================================
  759. Grid::TrackInfo::TrackInfo() noexcept : hasKeyword (true) {}
  760. Grid::TrackInfo::TrackInfo (Px sizeInPixels) noexcept
  761. : size (static_cast<float> (sizeInPixels.pixels)), isFraction (false) {}
  762. Grid::TrackInfo::TrackInfo (Fr fractionOfFreeSpace) noexcept
  763. : size ((float)fractionOfFreeSpace.fraction), isFraction (true) {}
  764. Grid::TrackInfo::TrackInfo (Px sizeInPixels, const String& endLineNameToUse) noexcept
  765. : TrackInfo (sizeInPixels)
  766. {
  767. endLineName = endLineNameToUse;
  768. }
  769. Grid::TrackInfo::TrackInfo (Fr fractionOfFreeSpace, const String& endLineNameToUse) noexcept
  770. : TrackInfo (fractionOfFreeSpace)
  771. {
  772. endLineName = endLineNameToUse;
  773. }
  774. Grid::TrackInfo::TrackInfo (const String& startLineNameToUse, Px sizeInPixels) noexcept
  775. : TrackInfo (sizeInPixels)
  776. {
  777. startLineName = startLineNameToUse;
  778. }
  779. Grid::TrackInfo::TrackInfo (const String& startLineNameToUse, Fr fractionOfFreeSpace) noexcept
  780. : TrackInfo (fractionOfFreeSpace)
  781. {
  782. startLineName = startLineNameToUse;
  783. }
  784. Grid::TrackInfo::TrackInfo (const String& startLineNameToUse, Px sizeInPixels, const String& endLineNameToUse) noexcept
  785. : TrackInfo (startLineNameToUse, sizeInPixels)
  786. {
  787. endLineName = endLineNameToUse;
  788. }
  789. Grid::TrackInfo::TrackInfo (const String& startLineNameToUse, Fr fractionOfFreeSpace, const String& endLineNameToUse) noexcept
  790. : TrackInfo (startLineNameToUse, fractionOfFreeSpace)
  791. {
  792. endLineName = endLineNameToUse;
  793. }
  794. float Grid::TrackInfo::getAbsoluteSize (float relativeFractionalUnit) const
  795. {
  796. return isFractional() ? size * relativeFractionalUnit : size;
  797. }
  798. //==============================================================================
  799. void Grid::performLayout (Rectangle<int> targetArea)
  800. {
  801. const auto itemsAndAreas = AutoPlacement().deduceAllItems (*this);
  802. auto implicitTracks = AutoPlacement::createImplicitTracks (*this, itemsAndAreas);
  803. AutoPlacement::applySizeForAutoTracks (implicitTracks, itemsAndAreas);
  804. SizeCalculation calculation;
  805. calculation.computeSizes (targetArea.toFloat().getWidth(),
  806. targetArea.toFloat().getHeight(),
  807. columnGap,
  808. rowGap,
  809. implicitTracks);
  810. for (auto& itemAndArea : itemsAndAreas)
  811. {
  812. const auto a = itemAndArea.second;
  813. const auto areaBounds = PlacementHelpers::getAreaBounds (a.column,
  814. a.row,
  815. implicitTracks,
  816. calculation,
  817. alignContent,
  818. justifyContent,
  819. columnGap,
  820. rowGap);
  821. auto* item = itemAndArea.first;
  822. item->currentBounds = BoxAlignment::alignItem (*item, *this, areaBounds)
  823. + targetArea.toFloat().getPosition();
  824. if (auto* c = item->associatedComponent)
  825. c->setBounds (item->currentBounds.toNearestIntEdges());
  826. }
  827. }
  828. //==============================================================================
  829. #if JUCE_UNIT_TESTS
  830. struct GridTests : public UnitTest
  831. {
  832. GridTests()
  833. : UnitTest ("Grid", UnitTestCategories::gui)
  834. {}
  835. void runTest() override
  836. {
  837. using Fr = Grid::Fr;
  838. using Tr = Grid::TrackInfo;
  839. using Rect = Rectangle<float>;
  840. beginTest ("Layout calculation of an empty grid is a no-op");
  841. {
  842. const Rectangle<int> bounds { 100, 200 };
  843. Grid grid;
  844. grid.performLayout (bounds);
  845. }
  846. {
  847. Grid grid;
  848. grid.templateColumns.add (Tr (1_fr));
  849. grid.templateRows.addArray ({ Tr (20_px), Tr (1_fr) });
  850. grid.items.addArray ({ GridItem().withArea (1, 1),
  851. GridItem().withArea (2, 1) });
  852. grid.performLayout (Rectangle<int> (200, 400));
  853. beginTest ("Layout calculation test: 1 column x 2 rows: no gap");
  854. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 200.f, 20.0f));
  855. expect (grid.items[1].currentBounds == Rect (0.0f, 20.0f, 200.f, 380.0f));
  856. grid.templateColumns.add (Tr (50_px));
  857. grid.templateRows.add (Tr (2_fr));
  858. grid.items.addArray ( { GridItem().withArea (1, 2),
  859. GridItem().withArea (2, 2),
  860. GridItem().withArea (3, 1),
  861. GridItem().withArea (3, 2) });
  862. grid.performLayout (Rectangle<int> (150, 170));
  863. beginTest ("Layout calculation test: 2 columns x 3 rows: no gap");
  864. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 100.0f, 20.0f));
  865. expect (grid.items[1].currentBounds == Rect (0.0f, 20.0f, 100.0f, 50.0f));
  866. expect (grid.items[2].currentBounds == Rect (100.0f, 0.0f, 50.0f, 20.0f));
  867. expect (grid.items[3].currentBounds == Rect (100.0f, 20.0f, 50.0f, 50.0f));
  868. expect (grid.items[4].currentBounds == Rect (0.0f, 70.0f, 100.0f, 100.0f));
  869. expect (grid.items[5].currentBounds == Rect (100.0f, 70.0f, 50.0f, 100.0f));
  870. grid.columnGap = 20_px;
  871. grid.rowGap = 10_px;
  872. grid.performLayout (Rectangle<int> (200, 310));
  873. beginTest ("Layout calculation test: 2 columns x 3 rows: rowGap of 10 and columnGap of 20");
  874. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 130.0f, 20.0f));
  875. expect (grid.items[1].currentBounds == Rect (0.0f, 30.0f, 130.0f, 90.0f));
  876. expect (grid.items[2].currentBounds == Rect (150.0f, 0.0f, 50.0f, 20.0f));
  877. expect (grid.items[3].currentBounds == Rect (150.0f, 30.0f, 50.0f, 90.0f));
  878. expect (grid.items[4].currentBounds == Rect (0.0f, 130.0f, 130.0f, 180.0f));
  879. expect (grid.items[5].currentBounds == Rect (150.0f, 130.0f, 50.0f, 180.0f));
  880. }
  881. {
  882. Grid grid;
  883. grid.templateColumns.addArray ({ Tr ("first", 20_px, "in"), Tr ("in", 1_fr, "in"), Tr (20_px, "last") });
  884. grid.templateRows.addArray ({ Tr (1_fr),
  885. Tr (20_px)});
  886. {
  887. beginTest ("Grid items placement tests: integer and custom ident, counting forward");
  888. GridItem i1, i2, i3, i4, i5;
  889. i1.column = { 1, 4 };
  890. i1.row = { 1, 2 };
  891. i2.column = { 1, 3 };
  892. i2.row = { 1, 3 };
  893. i3.column = { "first", "in" };
  894. i3.row = { 2, 3 };
  895. i4.column = { "first", { 2, "in" } };
  896. i4.row = { 1, 2 };
  897. i5.column = { "first", "last" };
  898. i5.row = { 1, 2 };
  899. grid.items.addArray ({ i1, i2, i3, i4, i5 });
  900. grid.performLayout ({ 140, 100 });
  901. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  902. expect (grid.items[1].currentBounds == Rect (0.0f, 0.0f, 120.0f, 100.0f));
  903. expect (grid.items[2].currentBounds == Rect (0.0f, 80.0f, 20.0f, 20.0f));
  904. expect (grid.items[3].currentBounds == Rect (0.0f, 0.0f, 120.0f, 80.0f));
  905. expect (grid.items[4].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  906. }
  907. }
  908. {
  909. Grid grid;
  910. grid.templateColumns.addArray ({ Tr ("first", 20_px, "in"), Tr ("in", 1_fr, "in"), Tr (20_px, "last") });
  911. grid.templateRows.addArray ({ Tr (1_fr),
  912. Tr (20_px)});
  913. beginTest ("Grid items placement tests: integer and custom ident, counting forward, reversed end and start");
  914. GridItem i1, i2, i3, i4, i5;
  915. i1.column = { 4, 1 };
  916. i1.row = { 2, 1 };
  917. i2.column = { 3, 1 };
  918. i2.row = { 3, 1 };
  919. i3.column = { "in", "first" };
  920. i3.row = { 3, 2 };
  921. i4.column = { "first", { 2, "in" } };
  922. i4.row = { 1, 2 };
  923. i5.column = { "last", "first" };
  924. i5.row = { 1, 2 };
  925. grid.items.addArray ({ i1, i2, i3, i4, i5 });
  926. grid.performLayout ({ 140, 100 });
  927. expect (grid.items[0].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  928. expect (grid.items[1].currentBounds == Rect (0.0f, 0.0f, 120.0f, 100.0f));
  929. expect (grid.items[2].currentBounds == Rect (0.0f, 80.0f, 20.0f, 20.0f));
  930. expect (grid.items[3].currentBounds == Rect (0.0f, 0.0f, 120.0f, 80.0f));
  931. expect (grid.items[4].currentBounds == Rect (0.0f, 0.0f, 140.0f, 80.0f));
  932. }
  933. {
  934. Grid grid;
  935. grid.templateColumns = { Tr ("first", 20_px, "in"), Tr ("in", 1_fr, "in"), Tr (20_px, "last") };
  936. grid.templateRows = { Tr (1_fr), Tr (20_px) };
  937. beginTest ("Grid items placement tests: integer, counting backward");
  938. grid.items = { GridItem{}.withColumn ({ -2, -1 }).withRow ({ 1, 3 }),
  939. GridItem{}.withColumn ({ -10, -1 }).withRow ({ 1, -1 }) };
  940. grid.performLayout ({ 140, 100 });
  941. expect (grid.items[0].currentBounds == Rect (120.0f, 0.0f, 20.0f, 100.0f));
  942. expect (grid.items[1].currentBounds == Rect (0.0f, 0.0f, 140.0f, 100.0f));
  943. }
  944. {
  945. beginTest ("Grid items placement tests: areas");
  946. Grid grid;
  947. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (Fr (1_fr)), Tr (50_px) };
  948. grid.templateRows = { Tr (50_px),
  949. Tr (1_fr),
  950. Tr (50_px) };
  951. grid.templateAreas = { "header header header header",
  952. "main main . sidebar",
  953. "footer footer footer footer" };
  954. grid.items.addArray ({ GridItem().withArea ("header"),
  955. GridItem().withArea ("main"),
  956. GridItem().withArea ("sidebar"),
  957. GridItem().withArea ("footer"),
  958. });
  959. grid.performLayout ({ 300, 150 });
  960. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 300.f, 50.f));
  961. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  962. expect (grid.items[2].currentBounds == Rect (250.f, 50.f, 50.f, 50.f));
  963. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 300.f, 50.f));
  964. }
  965. {
  966. beginTest ("Grid implicit rows and columns: triggered by areas");
  967. Grid grid;
  968. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (1_fr), Tr (50_px) };
  969. grid.templateRows = { Tr (50_px),
  970. Tr (1_fr),
  971. Tr (50_px) };
  972. grid.autoRows = Tr (30_px);
  973. grid.autoColumns = Tr (30_px);
  974. grid.templateAreas = { "header header header header header",
  975. "main main . sidebar sidebar",
  976. "footer footer footer footer footer",
  977. "sub sub sub sub sub"};
  978. grid.items.addArray ({ GridItem().withArea ("header"),
  979. GridItem().withArea ("main"),
  980. GridItem().withArea ("sidebar"),
  981. GridItem().withArea ("footer"),
  982. GridItem().withArea ("sub"),
  983. });
  984. grid.performLayout ({ 330, 180 });
  985. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 330.f, 50.f));
  986. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  987. expect (grid.items[2].currentBounds == Rect (250.f, 50.f, 80.f, 50.f));
  988. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 330.f, 50.f));
  989. expect (grid.items[4].currentBounds == Rect (0.f, 150.f, 330.f, 30.f));
  990. }
  991. {
  992. beginTest ("Grid implicit rows and columns: triggered by areas");
  993. Grid grid;
  994. grid.templateColumns = { Tr (50_px), Tr (100_px), Tr (1_fr), Tr (50_px) };
  995. grid.templateRows = { Tr (50_px),
  996. Tr (1_fr),
  997. Tr (50_px) };
  998. grid.autoRows = Tr (1_fr);
  999. grid.autoColumns = Tr (1_fr);
  1000. grid.templateAreas = { "header header header header",
  1001. "main main . sidebar",
  1002. "footer footer footer footer" };
  1003. grid.items.addArray ({ GridItem().withArea ("header"),
  1004. GridItem().withArea ("main"),
  1005. GridItem().withArea ("sidebar"),
  1006. GridItem().withArea ("footer"),
  1007. GridItem().withArea (4, 5, 6, 7)
  1008. });
  1009. grid.performLayout ({ 350, 250 });
  1010. expect (grid.items[0].currentBounds == Rect (0.f, 0.f, 250.f, 50.f));
  1011. expect (grid.items[1].currentBounds == Rect (0.f, 50.f, 150.f, 50.f));
  1012. expect (grid.items[2].currentBounds == Rect (200.f, 50.f, 50.f, 50.f));
  1013. expect (grid.items[3].currentBounds == Rect (0.f, 100.f, 250.f, 50.f));
  1014. expect (grid.items[4].currentBounds == Rect (250.f, 150.f, 100.f, 100.f));
  1015. }
  1016. {
  1017. beginTest ("Grid implicit rows and columns: triggered by out-of-bounds indices");
  1018. Grid grid;
  1019. grid.templateColumns = { Tr (1_fr), Tr (1_fr) };
  1020. grid.templateRows = { Tr (60_px), Tr (60_px) };
  1021. grid.autoColumns = Tr (20_px);
  1022. grid.autoRows = Tr (1_fr);
  1023. grid.items = { GridItem{}.withColumn ({ 5, 8 }).withRow ({ -5, -4 }),
  1024. GridItem{}.withColumn ({ 4, 7 }).withRow ({ -4, -3 }),
  1025. GridItem{}.withColumn ({ -2, -1 }).withRow ({ 4, 5 }) };
  1026. grid.performLayout ({ 500, 400 });
  1027. // -3 -2 -1
  1028. // 1 2 3 4 5 6 7 8
  1029. // -5 +---+---+---+---+---+---+---+ 0
  1030. // | | | | | 0 | 0 | 0 |
  1031. // -4 +---+---+---+---+---+---+---+ 70
  1032. // | | | | 1 | 1 | 1 | |
  1033. // -3 1 +---+---+---+---+---+---+---+ 140
  1034. // | x | x | | | | | |
  1035. // -2 2 +---+---+---+---+---+---+---+ 200 y positions
  1036. // | x | x | | | | | |
  1037. // -1 3 +---+---+---+---+---+---+---+ 260
  1038. // | | | | | | | |
  1039. // 4 +---+---+---+---+---+---+---+ 330
  1040. // | | 2 | | | | | |
  1041. // 5 +---+---+---+---+---+---+---+ 400
  1042. //
  1043. // 0 200 400 420 440 460 480 500
  1044. // x positions
  1045. //
  1046. // The cells marked "x" are the explicit cells specified by the template rows
  1047. // and columns.
  1048. //
  1049. // The cells marked 0/1/2 correspond to the GridItems at those indices in the
  1050. // items array.
  1051. //
  1052. // Note that negative indices count back from the last explicit line
  1053. // number in that direction, so "2" and "-2" both correspond to the same line.
  1054. expect (grid.items[0].currentBounds == Rect (440.0f, 0.0f, 60.0f, 70.0f));
  1055. expect (grid.items[1].currentBounds == Rect (420.0f, 70.0f, 60.0f, 70.0f));
  1056. expect (grid.items[2].currentBounds == Rect (200.0f, 330.0f, 200.0f, 70.0f));
  1057. }
  1058. }
  1059. };
  1060. static GridTests gridUnitTests;
  1061. #endif
  1062. } // namespace juce