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.

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