juce_Grid.cpp 50 KB

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