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.

359 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. int i;
  191. double totalIdealSize = 0.0;
  192. int totalMinimums = 0;
  193. for (i = startIndex; i < endIndex; ++i)
  194. {
  195. ItemLayoutProperties* const layout = items.getUnchecked (i);
  196. layout->currentSize = sizeToRealSize (layout->minSize, totalSize);
  197. totalMinimums += layout->currentSize;
  198. totalIdealSize += sizeToRealSize (layout->preferredSize, totalSize);
  199. }
  200. if (totalIdealSize <= 0)
  201. totalIdealSize = 1.0;
  202. // now calc the best sizes..
  203. int extraSpace = availableSpace - totalMinimums;
  204. while (extraSpace > 0)
  205. {
  206. int numWantingMoreSpace = 0;
  207. int numHavingTakenExtraSpace = 0;
  208. // first figure out how many comps want a slice of the extra space..
  209. for (i = startIndex; i < endIndex; ++i)
  210. {
  211. ItemLayoutProperties* const layout = items.getUnchecked (i);
  212. double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  213. const int bestSize = jlimit (layout->currentSize,
  214. jmax (layout->currentSize,
  215. sizeToRealSize (layout->maxSize, totalSize)),
  216. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  217. if (bestSize > layout->currentSize)
  218. ++numWantingMoreSpace;
  219. }
  220. // ..share out the extra space..
  221. for (i = startIndex; i < endIndex; ++i)
  222. {
  223. ItemLayoutProperties* const layout = items.getUnchecked (i);
  224. double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  225. int bestSize = jlimit (layout->currentSize,
  226. jmax (layout->currentSize, sizeToRealSize (layout->maxSize, totalSize)),
  227. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  228. const int extraWanted = bestSize - layout->currentSize;
  229. if (extraWanted > 0)
  230. {
  231. const int extraAllowed = jmin (extraWanted,
  232. extraSpace / jmax (1, numWantingMoreSpace));
  233. if (extraAllowed > 0)
  234. {
  235. ++numHavingTakenExtraSpace;
  236. --numWantingMoreSpace;
  237. layout->currentSize += extraAllowed;
  238. extraSpace -= extraAllowed;
  239. }
  240. }
  241. }
  242. if (numHavingTakenExtraSpace <= 0)
  243. break;
  244. }
  245. // ..and calculate the end position
  246. for (i = startIndex; i < endIndex; ++i)
  247. {
  248. ItemLayoutProperties* const layout = items.getUnchecked(i);
  249. startPos += layout->currentSize;
  250. }
  251. return startPos;
  252. }
  253. int StretchableLayoutManager::getMinimumSizeOfItems (const int startIndex,
  254. const int endIndex) const
  255. {
  256. int totalMinimums = 0;
  257. for (int i = startIndex; i < endIndex; ++i)
  258. totalMinimums += sizeToRealSize (items.getUnchecked (i)->minSize, totalSize);
  259. return totalMinimums;
  260. }
  261. int StretchableLayoutManager::getMaximumSizeOfItems (const int startIndex, const int endIndex) const
  262. {
  263. int totalMaximums = 0;
  264. for (int i = startIndex; i < endIndex; ++i)
  265. totalMaximums += sizeToRealSize (items.getUnchecked (i)->maxSize, totalSize);
  266. return totalMaximums;
  267. }
  268. void StretchableLayoutManager::updatePrefSizesToMatchCurrentPositions()
  269. {
  270. for (int i = 0; i < items.size(); ++i)
  271. {
  272. ItemLayoutProperties* const layout = items.getUnchecked (i);
  273. layout->preferredSize
  274. = (layout->preferredSize < 0) ? getItemCurrentRelativeSize (i)
  275. : getItemCurrentAbsoluteSize (i);
  276. }
  277. }
  278. int StretchableLayoutManager::sizeToRealSize (double size, int totalSpace)
  279. {
  280. if (size < 0)
  281. size *= -totalSpace;
  282. return roundToInt (size);
  283. }