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.cpp 50KB

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