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.

859 lines
33KB

  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 FlexBoxLayoutCalculation
  22. {
  23. using Coord = double;
  24. FlexBoxLayoutCalculation (const FlexBox& fb, Coord w, Coord h)
  25. : owner (fb), parentWidth (w), parentHeight (h), numItems (owner.items.size()),
  26. isRowDirection (fb.flexDirection == FlexBox::Direction::row
  27. || fb.flexDirection == FlexBox::Direction::rowReverse),
  28. containerLineLength (isRowDirection ? parentWidth : parentHeight)
  29. {
  30. lineItems.calloc ((size_t) (numItems * numItems));
  31. lineInfo.calloc ((size_t) numItems);
  32. }
  33. struct ItemWithState
  34. {
  35. ItemWithState (FlexItem& source) noexcept : item (&source) {}
  36. FlexItem* item;
  37. Coord lockedWidth = 0, lockedHeight = 0;
  38. Coord lockedMarginLeft = 0, lockedMarginRight = 0, lockedMarginTop = 0, lockedMarginBottom = 0;
  39. Coord preferredWidth = 0, preferredHeight = 0;
  40. bool locked = false;
  41. void resetItemLockedSize() noexcept
  42. {
  43. lockedWidth = preferredWidth;
  44. lockedHeight = preferredHeight;
  45. lockedMarginLeft = getValueOrZeroIfAuto (item->margin.left);
  46. lockedMarginRight = getValueOrZeroIfAuto (item->margin.right);
  47. lockedMarginTop = getValueOrZeroIfAuto (item->margin.top);
  48. lockedMarginBottom = getValueOrZeroIfAuto (item->margin.bottom);
  49. }
  50. void setWidthChecked (Coord newWidth) noexcept
  51. {
  52. if (isAssigned (item->maxWidth)) newWidth = jmin (newWidth, static_cast<Coord> (item->maxWidth));
  53. if (isAssigned (item->minWidth)) newWidth = jmax (newWidth, static_cast<Coord> (item->minWidth));
  54. lockedWidth = newWidth;
  55. }
  56. void setHeightChecked (Coord newHeight) noexcept
  57. {
  58. if (isAssigned (item->maxHeight)) newHeight = jmin (newHeight, (Coord) item->maxHeight);
  59. if (isAssigned (item->minHeight)) newHeight = jmax (newHeight, (Coord) item->minHeight);
  60. lockedHeight = newHeight;
  61. }
  62. };
  63. struct RowInfo
  64. {
  65. int numItems;
  66. Coord crossSize, lineY, totalLength;
  67. };
  68. const FlexBox& owner;
  69. const Coord parentWidth, parentHeight;
  70. const int numItems;
  71. const bool isRowDirection;
  72. const Coord containerLineLength;
  73. int numberOfRows = 1;
  74. Coord containerCrossLength = 0;
  75. HeapBlock<ItemWithState*> lineItems;
  76. HeapBlock<RowInfo> lineInfo;
  77. Array<ItemWithState> itemStates;
  78. ItemWithState& getItem (int x, int y) const noexcept { return *lineItems[y * numItems + x]; }
  79. static bool isAuto (Coord value) noexcept { return value == FlexItem::autoValue; }
  80. static bool isAssigned (Coord value) noexcept { return value != FlexItem::notAssigned; }
  81. static Coord getValueOrZeroIfAuto (Coord value) noexcept { return isAuto (value) ? Coord() : value; }
  82. //==============================================================================
  83. void createStates()
  84. {
  85. itemStates.ensureStorageAllocated (numItems);
  86. for (auto& item : owner.items)
  87. itemStates.add (item);
  88. itemStates.sort (*this, true);
  89. for (auto& item : itemStates)
  90. {
  91. item.preferredWidth = getPreferredWidth (item);
  92. item.preferredHeight = getPreferredHeight (item);
  93. }
  94. }
  95. void initialiseItems() noexcept
  96. {
  97. if (owner.flexWrap == FlexBox::Wrap::noWrap) // for single-line, all items go in line 1
  98. {
  99. lineInfo[0].numItems = numItems;
  100. int i = 0;
  101. for (auto& item : itemStates)
  102. {
  103. item.resetItemLockedSize();
  104. lineItems[i++] = &item;
  105. }
  106. }
  107. else // if multi-line, group the flexbox items into multiple lines
  108. {
  109. auto currentLength = containerLineLength;
  110. int column = 0, row = 0;
  111. bool firstRow = true;
  112. for (auto& item : itemStates)
  113. {
  114. item.resetItemLockedSize();
  115. const auto flexitemLength = getItemLength (item);
  116. if (flexitemLength > currentLength)
  117. {
  118. if (! firstRow)
  119. row++;
  120. if (row >= numItems)
  121. break;
  122. column = 0;
  123. currentLength = containerLineLength;
  124. numberOfRows = jmax (numberOfRows, row + 1);
  125. }
  126. currentLength -= flexitemLength;
  127. lineItems[row * numItems + column] = &item;
  128. ++column;
  129. lineInfo[row].numItems = jmax (lineInfo[row].numItems, column);
  130. firstRow = false;
  131. }
  132. }
  133. }
  134. void resolveFlexibleLengths() noexcept
  135. {
  136. for (int row = 0; row < numberOfRows; ++row)
  137. {
  138. resetRowItems (row);
  139. for (int maxLoops = numItems; --maxLoops >= 0;)
  140. {
  141. resetUnlockedRowItems (row);
  142. if (layoutRowItems (row))
  143. break;
  144. }
  145. }
  146. }
  147. void resolveAutoMarginsOnMainAxis() noexcept
  148. {
  149. for (int row = 0; row < numberOfRows; ++row)
  150. {
  151. Coord allFlexGrow = 0;
  152. const auto numColumns = lineInfo[row].numItems;
  153. const auto remainingLength = containerLineLength - lineInfo[row].totalLength;
  154. for (int column = 0; column < numColumns; ++column)
  155. {
  156. auto& item = getItem (column, row);
  157. if (isRowDirection)
  158. {
  159. if (isAuto (item.item->margin.left)) ++allFlexGrow;
  160. if (isAuto (item.item->margin.right)) ++allFlexGrow;
  161. }
  162. else
  163. {
  164. if (isAuto (item.item->margin.top)) ++allFlexGrow;
  165. if (isAuto (item.item->margin.bottom)) ++allFlexGrow;
  166. }
  167. }
  168. auto changeUnitWidth = remainingLength / allFlexGrow;
  169. if (changeUnitWidth > 0)
  170. {
  171. for (int column = 0; column < numColumns; ++column)
  172. {
  173. auto& item = getItem (column, row);
  174. if (isRowDirection)
  175. {
  176. if (isAuto (item.item->margin.left)) item.lockedMarginLeft = changeUnitWidth;
  177. if (isAuto (item.item->margin.right)) item.lockedMarginRight = changeUnitWidth;
  178. }
  179. else
  180. {
  181. if (isAuto (item.item->margin.top)) item.lockedMarginTop = changeUnitWidth;
  182. if (isAuto (item.item->margin.bottom)) item.lockedMarginBottom = changeUnitWidth;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. void calculateCrossSizesByLine() noexcept
  189. {
  190. for (int row = 0; row < numberOfRows; ++row)
  191. {
  192. Coord maxSize = 0;
  193. const auto numColumns = lineInfo[row].numItems;
  194. for (int column = 0; column < numColumns; ++column)
  195. {
  196. auto& item = getItem (column, row);
  197. maxSize = jmax (maxSize, isRowDirection ? item.lockedHeight + item.lockedMarginTop + item.lockedMarginBottom
  198. : item.lockedWidth + item.lockedMarginLeft + item.lockedMarginRight);
  199. }
  200. lineInfo[row].crossSize = maxSize;
  201. }
  202. }
  203. void calculateCrossSizeOfAllItems() noexcept
  204. {
  205. for (int row = 0; row < numberOfRows; ++row)
  206. {
  207. const auto numColumns = lineInfo[row].numItems;
  208. for (int column = 0; column < numColumns; ++column)
  209. {
  210. auto& item = getItem (column, row);
  211. if (isAssigned (item.item->maxHeight) && item.lockedHeight > item.item->maxHeight)
  212. item.lockedHeight = item.item->maxHeight;
  213. if (isAssigned (item.item->maxWidth) && item.lockedWidth > item.item->maxWidth)
  214. item.lockedWidth = item.item->maxWidth;
  215. }
  216. }
  217. }
  218. void alignLinesPerAlignContent() noexcept
  219. {
  220. containerCrossLength = isRowDirection ? parentHeight : parentWidth;
  221. if (owner.alignContent == FlexBox::AlignContent::flexStart)
  222. {
  223. for (int row = 0; row < numberOfRows; ++row)
  224. for (int row2 = row; row2 < numberOfRows; ++row2)
  225. lineInfo[row].lineY = row == 0 ? 0 : lineInfo[row - 1].lineY + lineInfo[row - 1].crossSize;
  226. }
  227. else if (owner.alignContent == FlexBox::AlignContent::flexEnd)
  228. {
  229. for (int row = 0; row < numberOfRows; ++row)
  230. {
  231. Coord crossHeights = 0;
  232. for (int row2 = row; row2 < numberOfRows; ++row2)
  233. crossHeights += lineInfo[row2].crossSize;
  234. lineInfo[row].lineY = containerCrossLength - crossHeights;
  235. }
  236. }
  237. else
  238. {
  239. Coord totalHeight = 0;
  240. for (int row = 0; row < numberOfRows; ++row)
  241. totalHeight += lineInfo[row].crossSize;
  242. if (owner.alignContent == FlexBox::AlignContent::stretch)
  243. {
  244. const auto difference = jmax (Coord(), (containerCrossLength - totalHeight) / numberOfRows);
  245. for (int row = 0; row < numberOfRows; ++row)
  246. {
  247. lineInfo[row].crossSize += difference;
  248. lineInfo[row].lineY = row == 0 ? 0 : lineInfo[row - 1].lineY + lineInfo[row - 1].crossSize;
  249. }
  250. }
  251. else if (owner.alignContent == FlexBox::AlignContent::center)
  252. {
  253. const auto additionalength = (containerCrossLength - totalHeight) / 2;
  254. for (int row = 0; row < numberOfRows; ++row)
  255. lineInfo[row].lineY = row == 0 ? additionalength : lineInfo[row - 1].lineY + lineInfo[row - 1].crossSize;
  256. }
  257. else if (owner.alignContent == FlexBox::AlignContent::spaceBetween)
  258. {
  259. const auto additionalength = numberOfRows <= 1 ? Coord() : jmax (Coord(), (containerCrossLength - totalHeight)
  260. / static_cast<Coord> (numberOfRows - 1));
  261. lineInfo[0].lineY = 0;
  262. for (int row = 1; row < numberOfRows; ++row)
  263. lineInfo[row].lineY += additionalength + lineInfo[row - 1].lineY + lineInfo[row - 1].crossSize;
  264. }
  265. else if (owner.alignContent == FlexBox::AlignContent::spaceAround)
  266. {
  267. const auto additionalength = numberOfRows <= 1 ? Coord() : jmax (Coord(), (containerCrossLength - totalHeight)
  268. / static_cast<Coord> (2 + (2 * (numberOfRows - 1))));
  269. lineInfo[0].lineY = additionalength;
  270. for (int row = 1; row < numberOfRows; ++row)
  271. lineInfo[row].lineY += (2 * additionalength) + lineInfo[row - 1].lineY + lineInfo[row - 1].crossSize;
  272. }
  273. }
  274. }
  275. void resolveAutoMarginsOnCrossAxis() noexcept
  276. {
  277. for (int row = 0; row < numberOfRows; ++row)
  278. {
  279. const auto numColumns = lineInfo[row].numItems;
  280. const auto crossSizeForLine = lineInfo[row].crossSize;
  281. for (int column = 0; column < numColumns; ++column)
  282. {
  283. auto& item = getItem (column, row);
  284. if (isRowDirection)
  285. {
  286. if (isAuto (item.item->margin.top) && isAuto (item.item->margin.bottom))
  287. item.lockedMarginTop = (crossSizeForLine - item.lockedHeight) / 2;
  288. else if (isAuto (item.item->margin.top))
  289. item.lockedMarginTop = crossSizeForLine - item.lockedHeight - item.item->margin.bottom;
  290. }
  291. else if (isAuto (item.item->margin.left) && isAuto (item.item->margin.right))
  292. {
  293. item.lockedMarginLeft = jmax (Coord(), (crossSizeForLine - item.lockedWidth) / 2);
  294. }
  295. else if (isAuto (item.item->margin.top))
  296. {
  297. item.lockedMarginLeft = jmax (Coord(), crossSizeForLine - item.lockedHeight - item.item->margin.bottom);
  298. }
  299. }
  300. }
  301. }
  302. void alignItemsInCrossAxisInLinesPerAlignItems() noexcept
  303. {
  304. for (int row = 0; row < numberOfRows; ++row)
  305. {
  306. const auto numColumns = lineInfo[row].numItems;
  307. const auto lineSize = lineInfo[row].crossSize;
  308. for (int column = 0; column < numColumns; ++column)
  309. {
  310. auto& item = getItem (column, row);
  311. if (item.item->alignSelf == FlexItem::AlignSelf::autoAlign)
  312. {
  313. if (owner.alignItems == FlexBox::AlignItems::stretch)
  314. {
  315. item.lockedMarginTop = item.item->margin.top;
  316. if (isRowDirection)
  317. item.setHeightChecked (lineSize - item.item->margin.top - item.item->margin.bottom);
  318. }
  319. else if (owner.alignItems == FlexBox::AlignItems::flexStart)
  320. {
  321. item.lockedMarginTop = item.item->margin.top;
  322. }
  323. else if (owner.alignItems == FlexBox::AlignItems::flexEnd)
  324. {
  325. item.lockedMarginTop = lineSize - item.lockedHeight - item.item->margin.bottom;
  326. }
  327. else if (owner.alignItems == FlexBox::AlignItems::center)
  328. {
  329. item.lockedMarginTop = (lineSize - item.lockedHeight - item.item->margin.top - item.item->margin.bottom) / 2;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. void alignLinesPerAlignSelf() noexcept
  336. {
  337. for (int row = 0; row < numberOfRows; ++row)
  338. {
  339. const auto numColumns = lineInfo[row].numItems;
  340. const auto lineSize = lineInfo[row].crossSize;
  341. for (int column = 0; column < numColumns; ++column)
  342. {
  343. auto& item = getItem (column, row);
  344. if (! isAuto (item.item->margin.top))
  345. {
  346. if (item.item->alignSelf == FlexItem::AlignSelf::flexStart)
  347. {
  348. if (isRowDirection)
  349. item.lockedMarginTop = item.item->margin.top;
  350. else
  351. item.lockedMarginLeft = item.item->margin.left;
  352. }
  353. else if (item.item->alignSelf == FlexItem::AlignSelf::flexEnd)
  354. {
  355. if (isRowDirection)
  356. item.lockedMarginTop = lineSize - item.lockedHeight - item.item->margin.bottom;
  357. else
  358. item.lockedMarginLeft = lineSize - item.lockedWidth - item.item->margin.right;
  359. }
  360. else if (item.item->alignSelf == FlexItem::AlignSelf::center)
  361. {
  362. if (isRowDirection)
  363. item.lockedMarginTop = item.item->margin.top + (lineSize - item.lockedHeight - item.item->margin.top - item.item->margin.bottom) / 2;
  364. else
  365. item.lockedMarginLeft = item.item->margin.left + (lineSize - item.lockedWidth - item.item->margin.left - item.item->margin.right) / 2;
  366. }
  367. else if (item.item->alignSelf == FlexItem::AlignSelf::stretch)
  368. {
  369. item.lockedMarginTop = item.item->margin.top;
  370. item.lockedMarginLeft = item.item->margin.left;
  371. if (isRowDirection)
  372. item.setHeightChecked (isAssigned (item.item->height) ? getPreferredHeight (item)
  373. : lineSize - item.item->margin.top - item.item->margin.bottom);
  374. else
  375. item.setWidthChecked (isAssigned (item.item->width) ? getPreferredWidth (item)
  376. : lineSize - item.item->margin.left - item.item->margin.right);
  377. }
  378. }
  379. }
  380. }
  381. }
  382. void alignItemsByJustifyContent() noexcept
  383. {
  384. Coord additionalMarginRight = 0, additionalMarginLeft = 0;
  385. recalculateTotalItemLengthPerLineArray();
  386. for (int row = 0; row < numberOfRows; ++row)
  387. {
  388. const auto numColumns = lineInfo[row].numItems;
  389. Coord x = 0;
  390. if (owner.justifyContent == FlexBox::JustifyContent::flexEnd)
  391. {
  392. x = containerLineLength - lineInfo[row].totalLength;
  393. }
  394. else if (owner.justifyContent == FlexBox::JustifyContent::center)
  395. {
  396. x = (containerLineLength - lineInfo[row].totalLength) / 2;
  397. }
  398. else if (owner.justifyContent == FlexBox::JustifyContent::spaceBetween)
  399. {
  400. additionalMarginRight
  401. = jmax (Coord(), (containerLineLength - lineInfo[row].totalLength) / jmax (1, numColumns - 1));
  402. }
  403. else if (owner.justifyContent == FlexBox::JustifyContent::spaceAround)
  404. {
  405. additionalMarginLeft = additionalMarginRight
  406. = jmax (Coord(), (containerLineLength - lineInfo[row].totalLength) / jmax (1, 2 * numColumns));
  407. }
  408. for (int column = 0; column < numColumns; ++column)
  409. {
  410. auto& item = getItem (column, row);
  411. if (isRowDirection)
  412. {
  413. item.lockedMarginLeft += additionalMarginLeft;
  414. item.lockedMarginRight += additionalMarginRight;
  415. item.item->currentBounds.setPosition ((float) (x + item.lockedMarginLeft), (float) item.lockedMarginTop);
  416. x += item.lockedWidth + item.lockedMarginLeft + item.lockedMarginRight;
  417. }
  418. else
  419. {
  420. item.lockedMarginTop += additionalMarginLeft;
  421. item.lockedMarginBottom += additionalMarginRight;
  422. item.item->currentBounds.setPosition ((float) item.lockedMarginLeft, (float) (x + item.lockedMarginTop));
  423. x += item.lockedHeight + item.lockedMarginTop + item.lockedMarginBottom;
  424. }
  425. }
  426. }
  427. }
  428. void layoutAllItems() noexcept
  429. {
  430. for (int row = 0; row < numberOfRows; ++row)
  431. {
  432. const auto lineY = lineInfo[row].lineY;
  433. const auto numColumns = lineInfo[row].numItems;
  434. for (int column = 0; column < numColumns; ++column)
  435. {
  436. auto& item = getItem (column, row);
  437. if (isRowDirection)
  438. item.item->currentBounds.setY ((float) (lineY + item.lockedMarginTop));
  439. else
  440. item.item->currentBounds.setX ((float) (lineY + item.lockedMarginLeft));
  441. item.item->currentBounds.setSize ((float) item.lockedWidth,
  442. (float) item.lockedHeight);
  443. }
  444. }
  445. reverseLocations();
  446. reverseWrap();
  447. }
  448. static int compareElements (const ItemWithState& i1, const ItemWithState& i2) noexcept
  449. {
  450. return i1.item->order < i2.item->order ? -1 : (i2.item->order < i1.item->order ? 1 : 0);
  451. }
  452. private:
  453. void resetRowItems (const int row) noexcept
  454. {
  455. const auto numColumns = lineInfo[row].numItems;
  456. for (int column = 0; column < numColumns; ++column)
  457. resetItem (getItem (column, row));
  458. }
  459. void resetUnlockedRowItems (const int row) noexcept
  460. {
  461. const auto numColumns = lineInfo[row].numItems;
  462. for (int column = 0; column < numColumns; ++column)
  463. {
  464. auto& item = getItem (column, row);
  465. if (! item.locked)
  466. resetItem (item);
  467. }
  468. }
  469. void resetItem (ItemWithState& item) noexcept
  470. {
  471. item.locked = false;
  472. item.lockedWidth = getPreferredWidth (item);
  473. item.lockedHeight = getPreferredHeight (item);
  474. }
  475. bool layoutRowItems (const int row) noexcept
  476. {
  477. const auto numColumns = lineInfo[row].numItems;
  478. auto flexContainerLength = containerLineLength;
  479. Coord totalItemsLength = 0, totalFlexGrow = 0, totalFlexShrink = 0;
  480. for (int column = 0; column < numColumns; ++column)
  481. {
  482. const auto& item = getItem (column, row);
  483. if (item.locked)
  484. {
  485. flexContainerLength -= getItemLength (item);
  486. }
  487. else
  488. {
  489. totalItemsLength += getItemLength (item);
  490. totalFlexGrow += item.item->flexGrow;
  491. totalFlexShrink += item.item->flexShrink;
  492. }
  493. }
  494. Coord changeUnit = 0;
  495. const auto difference = flexContainerLength - totalItemsLength;
  496. const bool positiveFlexibility = difference > 0;
  497. if (positiveFlexibility)
  498. {
  499. if (totalFlexGrow != 0.0)
  500. changeUnit = difference / totalFlexGrow;
  501. }
  502. else
  503. {
  504. if (totalFlexShrink != 0.0)
  505. changeUnit = difference / totalFlexShrink;
  506. }
  507. bool ok = true;
  508. for (int column = 0; column < numColumns; ++column)
  509. {
  510. auto& item = getItem (column, row);
  511. if (! item.locked)
  512. if (! addToItemLength (item, (positiveFlexibility ? item.item->flexGrow
  513. : item.item->flexShrink) * changeUnit, row))
  514. ok = false;
  515. }
  516. return ok;
  517. }
  518. void recalculateTotalItemLengthPerLineArray() noexcept
  519. {
  520. for (int row = 0; row < numberOfRows; ++row)
  521. {
  522. lineInfo[row].totalLength = 0;
  523. const auto numColumns = lineInfo[row].numItems;
  524. for (int column = 0; column < numColumns; ++column)
  525. {
  526. const auto& item = getItem (column, row);
  527. lineInfo[row].totalLength += isRowDirection ? item.lockedWidth + item.lockedMarginLeft + item.lockedMarginRight
  528. : item.lockedHeight + item.lockedMarginTop + item.lockedMarginBottom;
  529. }
  530. }
  531. }
  532. void reverseLocations() noexcept
  533. {
  534. if (owner.flexDirection == FlexBox::Direction::rowReverse)
  535. {
  536. for (auto& item : owner.items)
  537. item.currentBounds.setX ((float) (containerLineLength - item.currentBounds.getRight()));
  538. }
  539. else if (owner.flexDirection == FlexBox::Direction::columnReverse)
  540. {
  541. for (auto& item : owner.items)
  542. item.currentBounds.setY ((float) (containerLineLength - item.currentBounds.getBottom()));
  543. }
  544. }
  545. void reverseWrap() noexcept
  546. {
  547. if (owner.flexWrap == FlexBox::Wrap::wrapReverse)
  548. {
  549. if (isRowDirection)
  550. {
  551. for (auto& item : owner.items)
  552. item.currentBounds.setY ((float) (containerCrossLength - item.currentBounds.getBottom()));
  553. }
  554. else
  555. {
  556. for (auto& item : owner.items)
  557. item.currentBounds.setX ((float) (containerCrossLength - item.currentBounds.getRight()));
  558. }
  559. }
  560. }
  561. Coord getItemLength (const ItemWithState& item) const noexcept
  562. {
  563. return isRowDirection ? item.lockedWidth + item.lockedMarginLeft + item.lockedMarginRight
  564. : item.lockedHeight + item.lockedMarginTop + item.lockedMarginBottom;
  565. }
  566. Coord getItemCrossSize (const ItemWithState& item) const noexcept
  567. {
  568. return isRowDirection ? item.lockedHeight + item.lockedMarginTop + item.lockedMarginBottom
  569. : item.lockedWidth + item.lockedMarginLeft + item.lockedMarginRight;
  570. }
  571. bool addToItemLength (ItemWithState& item, const Coord length, int row) const noexcept
  572. {
  573. bool ok = false;
  574. if (isRowDirection)
  575. {
  576. const auto prefWidth = getPreferredWidth (item);
  577. if (isAssigned (item.item->maxWidth) && item.item->maxWidth < prefWidth + length)
  578. {
  579. item.lockedWidth = item.item->maxWidth;
  580. item.locked = true;
  581. }
  582. else if (isAssigned (prefWidth) && item.item->minWidth > prefWidth + length)
  583. {
  584. item.lockedWidth = item.item->minWidth;
  585. item.locked = true;
  586. }
  587. else
  588. {
  589. ok = true;
  590. item.lockedWidth = prefWidth + length;
  591. }
  592. lineInfo[row].totalLength += item.lockedWidth + item.lockedMarginLeft + item.lockedMarginRight;
  593. }
  594. else
  595. {
  596. const auto prefHeight = getPreferredHeight (item);
  597. if (isAssigned (item.item->maxHeight) && item.item->maxHeight < prefHeight + length)
  598. {
  599. item.lockedHeight = item.item->maxHeight;
  600. item.locked = true;
  601. }
  602. else if (isAssigned (prefHeight) && item.item->minHeight > prefHeight + length)
  603. {
  604. item.lockedHeight = item.item->minHeight;
  605. item.locked = true;
  606. }
  607. else
  608. {
  609. ok = true;
  610. item.lockedHeight = prefHeight + length;
  611. }
  612. lineInfo[row].totalLength += item.lockedHeight + item.lockedMarginTop + item.lockedMarginBottom;
  613. }
  614. return ok;
  615. }
  616. Coord getPreferredWidth (const ItemWithState& itemWithState) const noexcept
  617. {
  618. const auto& item = *itemWithState.item;
  619. auto preferredWidth = (item.flexBasis > 0 && isRowDirection)
  620. ? item.flexBasis
  621. : (isAssigned (item.width) ? item.width : item.minWidth);
  622. if (isAssigned (item.minWidth) && preferredWidth < item.minWidth) return item.minWidth;
  623. if (isAssigned (item.maxWidth) && preferredWidth > item.maxWidth) return item.maxWidth;
  624. return preferredWidth;
  625. }
  626. Coord getPreferredHeight (const ItemWithState& itemWithState) const noexcept
  627. {
  628. const auto& item = *itemWithState.item;
  629. auto preferredHeight = (item.flexBasis > 0 && ! isRowDirection)
  630. ? item.flexBasis
  631. : (isAssigned (item.height) ? item.height : item.minHeight);
  632. if (isAssigned (item.minHeight) && preferredHeight < item.minHeight) return item.minHeight;
  633. if (isAssigned (item.maxHeight) && preferredHeight > item.maxHeight) return item.maxHeight;
  634. return preferredHeight;
  635. }
  636. };
  637. //==============================================================================
  638. FlexBox::FlexBox() noexcept {}
  639. FlexBox::~FlexBox() noexcept {}
  640. FlexBox::FlexBox (JustifyContent jc) noexcept : justifyContent (jc) {}
  641. FlexBox::FlexBox (Direction d, Wrap w, AlignContent ac, AlignItems ai, JustifyContent jc) noexcept
  642. : flexDirection (d), flexWrap (w), alignContent (ac), alignItems (ai), justifyContent (jc)
  643. {}
  644. void FlexBox::performLayout (Rectangle<float> targetArea)
  645. {
  646. if (! items.isEmpty())
  647. {
  648. FlexBoxLayoutCalculation layout (*this, targetArea.getWidth(), targetArea.getHeight());
  649. layout.createStates();
  650. layout.initialiseItems();
  651. layout.resolveFlexibleLengths();
  652. layout.resolveAutoMarginsOnMainAxis();
  653. layout.calculateCrossSizesByLine();
  654. layout.calculateCrossSizeOfAllItems();
  655. layout.alignLinesPerAlignContent();
  656. layout.resolveAutoMarginsOnCrossAxis();
  657. layout.alignItemsInCrossAxisInLinesPerAlignItems();
  658. layout.alignLinesPerAlignSelf();
  659. layout.alignItemsByJustifyContent();
  660. layout.layoutAllItems();
  661. for (auto& item : items)
  662. {
  663. item.currentBounds += targetArea.getPosition();
  664. if (auto comp = item.associatedComponent)
  665. {
  666. auto position = item.currentBounds.getPosition().roundToInt();
  667. comp->setBounds (position.getX(),
  668. position.getY(),
  669. roundToInt (item.currentBounds.getRight()) - position.getX(),
  670. roundToInt (item.currentBounds.getBottom()) - position.getY());
  671. }
  672. if (auto box = item.associatedFlexBox)
  673. box->performLayout (item.currentBounds);
  674. }
  675. }
  676. }
  677. void FlexBox::performLayout (Rectangle<int> targetArea)
  678. {
  679. performLayout (targetArea.toFloat());
  680. }
  681. //==============================================================================
  682. FlexItem::FlexItem() noexcept {}
  683. FlexItem::FlexItem (float w, float h) noexcept : currentBounds (w, h), minWidth (w), minHeight (h) {}
  684. FlexItem::FlexItem (float w, float h, Component& c) noexcept : FlexItem (w, h) { associatedComponent = &c; }
  685. FlexItem::FlexItem (float w, float h, FlexBox& fb) noexcept : FlexItem (w, h) { associatedFlexBox = &fb; }
  686. FlexItem::FlexItem (Component& c) noexcept : associatedComponent (&c) {}
  687. FlexItem::FlexItem (FlexBox& fb) noexcept : associatedFlexBox (&fb) {}
  688. FlexItem::Margin::Margin() noexcept : left(), right(), top(), bottom() {}
  689. FlexItem::Margin::Margin (float v) noexcept : left (v), right (v), top (v), bottom (v) {}
  690. FlexItem::Margin::Margin (float t, float r, float b, float l) noexcept : left (l), right (r), top (t), bottom (b) {}
  691. //==============================================================================
  692. FlexItem FlexItem::withFlex (float newFlexGrow) const noexcept
  693. {
  694. auto fi = *this;
  695. fi.flexGrow = newFlexGrow;
  696. return fi;
  697. }
  698. FlexItem FlexItem::withFlex (float newFlexGrow, float newFlexShrink) const noexcept
  699. {
  700. auto fi = withFlex (newFlexGrow);
  701. fi.flexShrink = newFlexShrink;
  702. return fi;
  703. }
  704. FlexItem FlexItem::withFlex (float newFlexGrow, float newFlexShrink, float newFlexBasis) const noexcept
  705. {
  706. auto fi = withFlex (newFlexGrow, newFlexShrink);
  707. fi.flexBasis = newFlexBasis;
  708. return fi;
  709. }
  710. FlexItem FlexItem::withWidth (float newWidth) const noexcept { auto fi = *this; fi.width = newWidth; return fi; }
  711. FlexItem FlexItem::withMinWidth (float newMinWidth) const noexcept { auto fi = *this; fi.minWidth = newMinWidth; return fi; }
  712. FlexItem FlexItem::withMaxWidth (float newMaxWidth) const noexcept { auto fi = *this; fi.maxWidth = newMaxWidth; return fi; }
  713. FlexItem FlexItem::withMinHeight (float newMinHeight) const noexcept { auto fi = *this; fi.minHeight = newMinHeight; return fi; }
  714. FlexItem FlexItem::withMaxHeight (float newMaxHeight) const noexcept { auto fi = *this; fi.maxHeight = newMaxHeight; return fi; }
  715. FlexItem FlexItem::withHeight (float newHeight) const noexcept { auto fi = *this; fi.height = newHeight; return fi; }
  716. FlexItem FlexItem::withMargin (Margin m) const noexcept { auto fi = *this; fi.margin = m; return fi; }
  717. FlexItem FlexItem::withOrder (int newOrder) const noexcept { auto fi = *this; fi.order = newOrder; return fi; }
  718. FlexItem FlexItem::withAlignSelf (AlignSelf a) const noexcept { auto fi = *this; fi.alignSelf = a; return fi; }
  719. } // namespace juce