The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

344 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. StretchableLayoutManager::StretchableLayoutManager()
  19. : totalSize (0)
  20. {
  21. }
  22. StretchableLayoutManager::~StretchableLayoutManager()
  23. {
  24. }
  25. //==============================================================================
  26. void StretchableLayoutManager::clearAllItems()
  27. {
  28. items.clear();
  29. totalSize = 0;
  30. }
  31. void StretchableLayoutManager::setItemLayout (const int itemIndex,
  32. const double minimumSize,
  33. const double maximumSize,
  34. const double preferredSize)
  35. {
  36. ItemLayoutProperties* layout = getInfoFor (itemIndex);
  37. if (layout == nullptr)
  38. {
  39. layout = new ItemLayoutProperties();
  40. layout->itemIndex = itemIndex;
  41. int i;
  42. for (i = 0; i < items.size(); ++i)
  43. if (items.getUnchecked (i)->itemIndex > itemIndex)
  44. break;
  45. items.insert (i, layout);
  46. }
  47. layout->minSize = minimumSize;
  48. layout->maxSize = maximumSize;
  49. layout->preferredSize = preferredSize;
  50. layout->currentSize = 0;
  51. }
  52. bool StretchableLayoutManager::getItemLayout (const int itemIndex,
  53. double& minimumSize,
  54. double& maximumSize,
  55. double& preferredSize) const
  56. {
  57. if (const ItemLayoutProperties* const layout = getInfoFor (itemIndex))
  58. {
  59. minimumSize = layout->minSize;
  60. maximumSize = layout->maxSize;
  61. preferredSize = layout->preferredSize;
  62. return true;
  63. }
  64. return false;
  65. }
  66. //==============================================================================
  67. void StretchableLayoutManager::setTotalSize (const int newTotalSize)
  68. {
  69. totalSize = newTotalSize;
  70. fitComponentsIntoSpace (0, items.size(), totalSize, 0);
  71. }
  72. int StretchableLayoutManager::getItemCurrentPosition (const int itemIndex) const
  73. {
  74. int pos = 0;
  75. for (int i = 0; i < itemIndex; ++i)
  76. if (const ItemLayoutProperties* const layout = getInfoFor (i))
  77. pos += layout->currentSize;
  78. return pos;
  79. }
  80. int StretchableLayoutManager::getItemCurrentAbsoluteSize (const int itemIndex) const
  81. {
  82. if (const ItemLayoutProperties* const layout = getInfoFor (itemIndex))
  83. return layout->currentSize;
  84. return 0;
  85. }
  86. double StretchableLayoutManager::getItemCurrentRelativeSize (const int itemIndex) const
  87. {
  88. if (const ItemLayoutProperties* const layout = getInfoFor (itemIndex))
  89. return -layout->currentSize / (double) totalSize;
  90. return 0;
  91. }
  92. void StretchableLayoutManager::setItemPosition (const int itemIndex,
  93. int newPosition)
  94. {
  95. for (int i = items.size(); --i >= 0;)
  96. {
  97. const ItemLayoutProperties* const layout = items.getUnchecked(i);
  98. if (layout->itemIndex == itemIndex)
  99. {
  100. int realTotalSize = jmax (totalSize, getMinimumSizeOfItems (0, items.size()));
  101. const int minSizeAfterThisComp = getMinimumSizeOfItems (i, items.size());
  102. const int maxSizeAfterThisComp = getMaximumSizeOfItems (i + 1, items.size());
  103. newPosition = jmax (newPosition, totalSize - maxSizeAfterThisComp - layout->currentSize);
  104. newPosition = jmin (newPosition, realTotalSize - minSizeAfterThisComp);
  105. int endPos = fitComponentsIntoSpace (0, i, newPosition, 0);
  106. endPos += layout->currentSize;
  107. fitComponentsIntoSpace (i + 1, items.size(), totalSize - endPos, endPos);
  108. updatePrefSizesToMatchCurrentPositions();
  109. break;
  110. }
  111. }
  112. }
  113. //==============================================================================
  114. void StretchableLayoutManager::layOutComponents (Component** const components,
  115. int numComponents,
  116. int x, int y, int w, int h,
  117. const bool vertically,
  118. const bool resizeOtherDimension)
  119. {
  120. setTotalSize (vertically ? h : w);
  121. int pos = vertically ? y : x;
  122. for (int i = 0; i < numComponents; ++i)
  123. {
  124. if (const ItemLayoutProperties* const layout = getInfoFor (i))
  125. {
  126. if (Component* const c = components[i])
  127. {
  128. if (i == numComponents - 1)
  129. {
  130. // if it's the last item, crop it to exactly fit the available space..
  131. if (resizeOtherDimension)
  132. {
  133. if (vertically)
  134. c->setBounds (x, pos, w, jmax (layout->currentSize, h - pos));
  135. else
  136. c->setBounds (pos, y, jmax (layout->currentSize, w - pos), h);
  137. }
  138. else
  139. {
  140. if (vertically)
  141. c->setBounds (c->getX(), pos, c->getWidth(), jmax (layout->currentSize, h - pos));
  142. else
  143. c->setBounds (pos, c->getY(), jmax (layout->currentSize, w - pos), c->getHeight());
  144. }
  145. }
  146. else
  147. {
  148. if (resizeOtherDimension)
  149. {
  150. if (vertically)
  151. c->setBounds (x, pos, w, layout->currentSize);
  152. else
  153. c->setBounds (pos, y, layout->currentSize, h);
  154. }
  155. else
  156. {
  157. if (vertically)
  158. c->setBounds (c->getX(), pos, c->getWidth(), layout->currentSize);
  159. else
  160. c->setBounds (pos, c->getY(), layout->currentSize, c->getHeight());
  161. }
  162. }
  163. }
  164. pos += layout->currentSize;
  165. }
  166. }
  167. }
  168. //==============================================================================
  169. StretchableLayoutManager::ItemLayoutProperties* StretchableLayoutManager::getInfoFor (const int itemIndex) const
  170. {
  171. for (int i = items.size(); --i >= 0;)
  172. if (items.getUnchecked(i)->itemIndex == itemIndex)
  173. return items.getUnchecked(i);
  174. return nullptr;
  175. }
  176. int StretchableLayoutManager::fitComponentsIntoSpace (const int startIndex,
  177. const int endIndex,
  178. const int availableSpace,
  179. int startPos)
  180. {
  181. // calculate the total sizes
  182. double totalIdealSize = 0.0;
  183. int totalMinimums = 0;
  184. for (int i = startIndex; i < endIndex; ++i)
  185. {
  186. ItemLayoutProperties* const layout = items.getUnchecked (i);
  187. layout->currentSize = sizeToRealSize (layout->minSize, totalSize);
  188. totalMinimums += layout->currentSize;
  189. totalIdealSize += sizeToRealSize (layout->preferredSize, totalSize);
  190. }
  191. if (totalIdealSize <= 0)
  192. totalIdealSize = 1.0;
  193. // now calc the best sizes..
  194. int extraSpace = availableSpace - totalMinimums;
  195. while (extraSpace > 0)
  196. {
  197. int numWantingMoreSpace = 0;
  198. int numHavingTakenExtraSpace = 0;
  199. // first figure out how many comps want a slice of the extra space..
  200. for (int i = startIndex; i < endIndex; ++i)
  201. {
  202. ItemLayoutProperties* const layout = items.getUnchecked (i);
  203. double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  204. const int bestSize = jlimit (layout->currentSize,
  205. jmax (layout->currentSize,
  206. sizeToRealSize (layout->maxSize, totalSize)),
  207. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  208. if (bestSize > layout->currentSize)
  209. ++numWantingMoreSpace;
  210. }
  211. // ..share out the extra space..
  212. for (int i = startIndex; i < endIndex; ++i)
  213. {
  214. ItemLayoutProperties* const layout = items.getUnchecked (i);
  215. double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  216. int bestSize = jlimit (layout->currentSize,
  217. jmax (layout->currentSize, sizeToRealSize (layout->maxSize, totalSize)),
  218. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  219. const int extraWanted = bestSize - layout->currentSize;
  220. if (extraWanted > 0)
  221. {
  222. const int extraAllowed = jmin (extraWanted,
  223. extraSpace / jmax (1, numWantingMoreSpace));
  224. if (extraAllowed > 0)
  225. {
  226. ++numHavingTakenExtraSpace;
  227. --numWantingMoreSpace;
  228. layout->currentSize += extraAllowed;
  229. extraSpace -= extraAllowed;
  230. }
  231. }
  232. }
  233. if (numHavingTakenExtraSpace <= 0)
  234. break;
  235. }
  236. // ..and calculate the end position
  237. for (int i = startIndex; i < endIndex; ++i)
  238. {
  239. ItemLayoutProperties* const layout = items.getUnchecked(i);
  240. startPos += layout->currentSize;
  241. }
  242. return startPos;
  243. }
  244. int StretchableLayoutManager::getMinimumSizeOfItems (const int startIndex,
  245. const int endIndex) const
  246. {
  247. int totalMinimums = 0;
  248. for (int i = startIndex; i < endIndex; ++i)
  249. totalMinimums += sizeToRealSize (items.getUnchecked (i)->minSize, totalSize);
  250. return totalMinimums;
  251. }
  252. int StretchableLayoutManager::getMaximumSizeOfItems (const int startIndex, const int endIndex) const
  253. {
  254. int totalMaximums = 0;
  255. for (int i = startIndex; i < endIndex; ++i)
  256. totalMaximums += sizeToRealSize (items.getUnchecked (i)->maxSize, totalSize);
  257. return totalMaximums;
  258. }
  259. void StretchableLayoutManager::updatePrefSizesToMatchCurrentPositions()
  260. {
  261. for (int i = 0; i < items.size(); ++i)
  262. {
  263. ItemLayoutProperties* const layout = items.getUnchecked (i);
  264. layout->preferredSize
  265. = (layout->preferredSize < 0) ? getItemCurrentRelativeSize (i)
  266. : getItemCurrentAbsoluteSize (i);
  267. }
  268. }
  269. int StretchableLayoutManager::sizeToRealSize (double size, int totalSpace)
  270. {
  271. if (size < 0)
  272. size *= -totalSpace;
  273. return roundToInt (size);
  274. }