juce_StretchableLayoutManager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. StretchableLayoutManager::StretchableLayoutManager() {}
  22. StretchableLayoutManager::~StretchableLayoutManager() {}
  23. //==============================================================================
  24. void StretchableLayoutManager::clearAllItems()
  25. {
  26. items.clear();
  27. totalSize = 0;
  28. }
  29. void StretchableLayoutManager::setItemLayout (const int itemIndex,
  30. const double minimumSize,
  31. const double maximumSize,
  32. const double preferredSize)
  33. {
  34. auto* layout = getInfoFor (itemIndex);
  35. if (layout == nullptr)
  36. {
  37. layout = new ItemLayoutProperties();
  38. layout->itemIndex = itemIndex;
  39. int i;
  40. for (i = 0; i < items.size(); ++i)
  41. if (items.getUnchecked (i)->itemIndex > itemIndex)
  42. break;
  43. items.insert (i, layout);
  44. }
  45. layout->minSize = minimumSize;
  46. layout->maxSize = maximumSize;
  47. layout->preferredSize = preferredSize;
  48. layout->currentSize = 0;
  49. }
  50. bool StretchableLayoutManager::getItemLayout (const int itemIndex,
  51. double& minimumSize,
  52. double& maximumSize,
  53. double& preferredSize) const
  54. {
  55. if (auto* layout = getInfoFor (itemIndex))
  56. {
  57. minimumSize = layout->minSize;
  58. maximumSize = layout->maxSize;
  59. preferredSize = layout->preferredSize;
  60. return true;
  61. }
  62. return false;
  63. }
  64. //==============================================================================
  65. void StretchableLayoutManager::setTotalSize (const int newTotalSize)
  66. {
  67. totalSize = newTotalSize;
  68. fitComponentsIntoSpace (0, items.size(), totalSize, 0);
  69. }
  70. int StretchableLayoutManager::getItemCurrentPosition (const int itemIndex) const
  71. {
  72. int pos = 0;
  73. for (int i = 0; i < itemIndex; ++i)
  74. if (auto* layout = getInfoFor (i))
  75. pos += layout->currentSize;
  76. return pos;
  77. }
  78. int StretchableLayoutManager::getItemCurrentAbsoluteSize (const int itemIndex) const
  79. {
  80. if (auto* layout = getInfoFor (itemIndex))
  81. return layout->currentSize;
  82. return 0;
  83. }
  84. double StretchableLayoutManager::getItemCurrentRelativeSize (const int itemIndex) const
  85. {
  86. if (auto* layout = getInfoFor (itemIndex))
  87. return -layout->currentSize / (double) totalSize;
  88. return 0;
  89. }
  90. void StretchableLayoutManager::setItemPosition (const int itemIndex,
  91. int newPosition)
  92. {
  93. for (int i = items.size(); --i >= 0;)
  94. {
  95. auto* layout = items.getUnchecked(i);
  96. if (layout->itemIndex == itemIndex)
  97. {
  98. auto realTotalSize = jmax (totalSize, getMinimumSizeOfItems (0, items.size()));
  99. auto minSizeAfterThisComp = getMinimumSizeOfItems (i, items.size());
  100. auto maxSizeAfterThisComp = getMaximumSizeOfItems (i + 1, items.size());
  101. newPosition = jmax (newPosition, totalSize - maxSizeAfterThisComp - layout->currentSize);
  102. newPosition = jmin (newPosition, realTotalSize - minSizeAfterThisComp);
  103. auto endPos = fitComponentsIntoSpace (0, i, newPosition, 0);
  104. endPos += layout->currentSize;
  105. fitComponentsIntoSpace (i + 1, items.size(), totalSize - endPos, endPos);
  106. updatePrefSizesToMatchCurrentPositions();
  107. break;
  108. }
  109. }
  110. }
  111. //==============================================================================
  112. void StretchableLayoutManager::layOutComponents (Component** const components,
  113. int numComponents,
  114. int x, int y, int w, int h,
  115. const bool vertically,
  116. const bool resizeOtherDimension)
  117. {
  118. setTotalSize (vertically ? h : w);
  119. int pos = vertically ? y : x;
  120. for (int i = 0; i < numComponents; ++i)
  121. {
  122. if (auto* layout = getInfoFor (i))
  123. {
  124. if (auto* c = components[i])
  125. {
  126. if (i == numComponents - 1)
  127. {
  128. // if it's the last item, crop it to exactly fit the available space..
  129. if (resizeOtherDimension)
  130. {
  131. if (vertically)
  132. c->setBounds (x, pos, w, jmax (layout->currentSize, h - pos));
  133. else
  134. c->setBounds (pos, y, jmax (layout->currentSize, w - pos), h);
  135. }
  136. else
  137. {
  138. if (vertically)
  139. c->setBounds (c->getX(), pos, c->getWidth(), jmax (layout->currentSize, h - pos));
  140. else
  141. c->setBounds (pos, c->getY(), jmax (layout->currentSize, w - pos), c->getHeight());
  142. }
  143. }
  144. else
  145. {
  146. if (resizeOtherDimension)
  147. {
  148. if (vertically)
  149. c->setBounds (x, pos, w, layout->currentSize);
  150. else
  151. c->setBounds (pos, y, layout->currentSize, h);
  152. }
  153. else
  154. {
  155. if (vertically)
  156. c->setBounds (c->getX(), pos, c->getWidth(), layout->currentSize);
  157. else
  158. c->setBounds (pos, c->getY(), layout->currentSize, c->getHeight());
  159. }
  160. }
  161. }
  162. pos += layout->currentSize;
  163. }
  164. }
  165. }
  166. //==============================================================================
  167. StretchableLayoutManager::ItemLayoutProperties* StretchableLayoutManager::getInfoFor (const int itemIndex) const
  168. {
  169. for (auto* i : items)
  170. if (i->itemIndex == itemIndex)
  171. return i;
  172. return nullptr;
  173. }
  174. int StretchableLayoutManager::fitComponentsIntoSpace (const int startIndex,
  175. const int endIndex,
  176. const int availableSpace,
  177. int startPos)
  178. {
  179. // calculate the total sizes
  180. double totalIdealSize = 0.0;
  181. int totalMinimums = 0;
  182. for (int i = startIndex; i < endIndex; ++i)
  183. {
  184. auto* layout = items.getUnchecked (i);
  185. layout->currentSize = sizeToRealSize (layout->minSize, totalSize);
  186. totalMinimums += layout->currentSize;
  187. totalIdealSize += sizeToRealSize (layout->preferredSize, totalSize);
  188. }
  189. if (totalIdealSize <= 0)
  190. totalIdealSize = 1.0;
  191. // now calc the best sizes..
  192. int extraSpace = availableSpace - totalMinimums;
  193. while (extraSpace > 0)
  194. {
  195. int numWantingMoreSpace = 0;
  196. int numHavingTakenExtraSpace = 0;
  197. // first figure out how many comps want a slice of the extra space..
  198. for (int i = startIndex; i < endIndex; ++i)
  199. {
  200. auto* layout = items.getUnchecked (i);
  201. auto sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  202. auto bestSize = jlimit (layout->currentSize,
  203. jmax (layout->currentSize,
  204. sizeToRealSize (layout->maxSize, totalSize)),
  205. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  206. if (bestSize > layout->currentSize)
  207. ++numWantingMoreSpace;
  208. }
  209. // ..share out the extra space..
  210. for (int i = startIndex; i < endIndex; ++i)
  211. {
  212. auto* layout = items.getUnchecked (i);
  213. auto sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  214. auto bestSize = jlimit (layout->currentSize,
  215. jmax (layout->currentSize, sizeToRealSize (layout->maxSize, totalSize)),
  216. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  217. auto extraWanted = bestSize - layout->currentSize;
  218. if (extraWanted > 0)
  219. {
  220. auto extraAllowed = jmin (extraWanted,
  221. extraSpace / jmax (1, numWantingMoreSpace));
  222. if (extraAllowed > 0)
  223. {
  224. ++numHavingTakenExtraSpace;
  225. --numWantingMoreSpace;
  226. layout->currentSize += extraAllowed;
  227. extraSpace -= extraAllowed;
  228. }
  229. }
  230. }
  231. if (numHavingTakenExtraSpace <= 0)
  232. break;
  233. }
  234. // ..and calculate the end position
  235. for (int i = startIndex; i < endIndex; ++i)
  236. {
  237. auto* layout = items.getUnchecked(i);
  238. startPos += layout->currentSize;
  239. }
  240. return startPos;
  241. }
  242. int StretchableLayoutManager::getMinimumSizeOfItems (const int startIndex,
  243. const int endIndex) const
  244. {
  245. int totalMinimums = 0;
  246. for (int i = startIndex; i < endIndex; ++i)
  247. totalMinimums += sizeToRealSize (items.getUnchecked (i)->minSize, totalSize);
  248. return totalMinimums;
  249. }
  250. int StretchableLayoutManager::getMaximumSizeOfItems (const int startIndex, const int endIndex) const
  251. {
  252. int totalMaximums = 0;
  253. for (int i = startIndex; i < endIndex; ++i)
  254. totalMaximums += sizeToRealSize (items.getUnchecked (i)->maxSize, totalSize);
  255. return totalMaximums;
  256. }
  257. void StretchableLayoutManager::updatePrefSizesToMatchCurrentPositions()
  258. {
  259. for (int i = 0; i < items.size(); ++i)
  260. {
  261. auto* layout = items.getUnchecked (i);
  262. layout->preferredSize
  263. = (layout->preferredSize < 0) ? getItemCurrentRelativeSize (i)
  264. : getItemCurrentAbsoluteSize (i);
  265. }
  266. }
  267. int StretchableLayoutManager::sizeToRealSize (double size, int totalSpace)
  268. {
  269. if (size < 0)
  270. size *= -totalSpace;
  271. return roundToInt (size);
  272. }
  273. } // namespace juce