PageRenderTime 42ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/JuceLibraryCode/modules/juce_gui_basics/layout/juce_StretchableLayoutManager.cpp

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