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.

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