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.

294 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. ComponentBoundsConstrainer::ComponentBoundsConstrainer() noexcept {}
  20. ComponentBoundsConstrainer::~ComponentBoundsConstrainer() {}
  21. //==============================================================================
  22. void ComponentBoundsConstrainer::setMinimumWidth (int minimumWidth) noexcept { minW = minimumWidth; }
  23. void ComponentBoundsConstrainer::setMaximumWidth (int maximumWidth) noexcept { maxW = maximumWidth; }
  24. void ComponentBoundsConstrainer::setMinimumHeight (int minimumHeight) noexcept { minH = minimumHeight; }
  25. void ComponentBoundsConstrainer::setMaximumHeight (int maximumHeight) noexcept { maxH = maximumHeight; }
  26. void ComponentBoundsConstrainer::setMinimumSize (int minimumWidth, int minimumHeight) noexcept
  27. {
  28. jassert (maxW >= minimumWidth);
  29. jassert (maxH >= minimumHeight);
  30. jassert (minimumWidth > 0 && minimumHeight > 0);
  31. minW = minimumWidth;
  32. minH = minimumHeight;
  33. if (minW > maxW) maxW = minW;
  34. if (minH > maxH) maxH = minH;
  35. }
  36. void ComponentBoundsConstrainer::setMaximumSize (int maximumWidth, int maximumHeight) noexcept
  37. {
  38. jassert (maximumWidth >= minW);
  39. jassert (maximumHeight >= minH);
  40. jassert (maximumWidth > 0 && maximumHeight > 0);
  41. maxW = jmax (minW, maximumWidth);
  42. maxH = jmax (minH, maximumHeight);
  43. }
  44. void ComponentBoundsConstrainer::setSizeLimits (int minimumWidth,
  45. int minimumHeight,
  46. int maximumWidth,
  47. int maximumHeight) noexcept
  48. {
  49. jassert (maximumWidth >= minimumWidth);
  50. jassert (maximumHeight >= minimumHeight);
  51. jassert (maximumWidth > 0 && maximumHeight > 0);
  52. jassert (minimumWidth > 0 && minimumHeight > 0);
  53. minW = jmax (0, minimumWidth);
  54. minH = jmax (0, minimumHeight);
  55. maxW = jmax (minW, maximumWidth);
  56. maxH = jmax (minH, maximumHeight);
  57. }
  58. void ComponentBoundsConstrainer::setMinimumOnscreenAmounts (int minimumWhenOffTheTop,
  59. int minimumWhenOffTheLeft,
  60. int minimumWhenOffTheBottom,
  61. int minimumWhenOffTheRight) noexcept
  62. {
  63. minOffTop = minimumWhenOffTheTop;
  64. minOffLeft = minimumWhenOffTheLeft;
  65. minOffBottom = minimumWhenOffTheBottom;
  66. minOffRight = minimumWhenOffTheRight;
  67. }
  68. void ComponentBoundsConstrainer::setFixedAspectRatio (double widthOverHeight) noexcept
  69. {
  70. aspectRatio = jmax (0.0, widthOverHeight);
  71. }
  72. double ComponentBoundsConstrainer::getFixedAspectRatio() const noexcept
  73. {
  74. return aspectRatio;
  75. }
  76. void ComponentBoundsConstrainer::setBoundsForComponent (Component* component,
  77. Rectangle<int> targetBounds,
  78. bool isStretchingTop,
  79. bool isStretchingLeft,
  80. bool isStretchingBottom,
  81. bool isStretchingRight)
  82. {
  83. jassert (component != nullptr);
  84. Rectangle<int> limits, bounds (targetBounds);
  85. BorderSize<int> border;
  86. if (auto* parent = component->getParentComponent())
  87. {
  88. limits.setSize (parent->getWidth(), parent->getHeight());
  89. }
  90. else
  91. {
  92. if (auto* peer = component->getPeer())
  93. border = peer->getFrameSize();
  94. auto targetScreenArea = component->localAreaToGlobal (targetBounds);
  95. auto screenBounds = Desktop::getInstance().getDisplays().getDisplayContaining (targetScreenArea.getCentre()).userArea;
  96. limits = component->getLocalArea (nullptr, screenBounds) + component->getPosition();
  97. }
  98. border.addTo (bounds);
  99. checkBounds (bounds,
  100. border.addedTo (component->getBounds()), limits,
  101. isStretchingTop, isStretchingLeft,
  102. isStretchingBottom, isStretchingRight);
  103. border.subtractFrom (bounds);
  104. applyBoundsToComponent (*component, bounds);
  105. }
  106. void ComponentBoundsConstrainer::checkComponentBounds (Component* component)
  107. {
  108. setBoundsForComponent (component, component->getBounds(),
  109. false, false, false, false);
  110. }
  111. void ComponentBoundsConstrainer::applyBoundsToComponent (Component& component, Rectangle<int> bounds)
  112. {
  113. if (auto* positioner = component.getPositioner())
  114. positioner->applyNewBounds (bounds);
  115. else
  116. component.setBounds (bounds);
  117. }
  118. //==============================================================================
  119. void ComponentBoundsConstrainer::resizeStart()
  120. {
  121. }
  122. void ComponentBoundsConstrainer::resizeEnd()
  123. {
  124. }
  125. //==============================================================================
  126. void ComponentBoundsConstrainer::checkBounds (Rectangle<int>& bounds,
  127. const Rectangle<int>& old,
  128. const Rectangle<int>& limits,
  129. bool isStretchingTop,
  130. bool isStretchingLeft,
  131. bool isStretchingBottom,
  132. bool isStretchingRight)
  133. {
  134. if (isStretchingLeft)
  135. bounds.setLeft (jlimit (old.getRight() - maxW, old.getRight() - minW, bounds.getX()));
  136. else
  137. bounds.setWidth (jlimit (minW, maxW, bounds.getWidth()));
  138. if (isStretchingTop)
  139. bounds.setTop (jlimit (old.getBottom() - maxH, old.getBottom() - minH, bounds.getY()));
  140. else
  141. bounds.setHeight (jlimit (minH, maxH, bounds.getHeight()));
  142. if (bounds.isEmpty())
  143. return;
  144. if (minOffTop > 0)
  145. {
  146. const int limit = limits.getY() + jmin (minOffTop - bounds.getHeight(), 0);
  147. if (bounds.getY() < limit)
  148. {
  149. if (isStretchingTop)
  150. bounds.setTop (limits.getY());
  151. else
  152. bounds.setY (limit);
  153. }
  154. }
  155. if (minOffLeft > 0)
  156. {
  157. const int limit = limits.getX() + jmin (minOffLeft - bounds.getWidth(), 0);
  158. if (bounds.getX() < limit)
  159. {
  160. if (isStretchingLeft)
  161. bounds.setLeft (limits.getX());
  162. else
  163. bounds.setX (limit);
  164. }
  165. }
  166. if (minOffBottom > 0)
  167. {
  168. const int limit = limits.getBottom() - jmin (minOffBottom, bounds.getHeight());
  169. if (bounds.getY() > limit)
  170. {
  171. if (isStretchingBottom)
  172. bounds.setBottom (limits.getBottom());
  173. else
  174. bounds.setY (limit);
  175. }
  176. }
  177. if (minOffRight > 0)
  178. {
  179. const int limit = limits.getRight() - jmin (minOffRight, bounds.getWidth());
  180. if (bounds.getX() > limit)
  181. {
  182. if (isStretchingRight)
  183. bounds.setRight (limits.getRight());
  184. else
  185. bounds.setX (limit);
  186. }
  187. }
  188. // constrain the aspect ratio if one has been specified..
  189. if (aspectRatio > 0.0)
  190. {
  191. bool adjustWidth;
  192. if ((isStretchingTop || isStretchingBottom) && ! (isStretchingLeft || isStretchingRight))
  193. {
  194. adjustWidth = true;
  195. }
  196. else if ((isStretchingLeft || isStretchingRight) && ! (isStretchingTop || isStretchingBottom))
  197. {
  198. adjustWidth = false;
  199. }
  200. else
  201. {
  202. const double oldRatio = (old.getHeight() > 0) ? std::abs (old.getWidth() / (double) old.getHeight()) : 0.0;
  203. const double newRatio = std::abs (bounds.getWidth() / (double) bounds.getHeight());
  204. adjustWidth = (oldRatio > newRatio);
  205. }
  206. if (adjustWidth)
  207. {
  208. bounds.setWidth (roundToInt (bounds.getHeight() * aspectRatio));
  209. if (bounds.getWidth() > maxW || bounds.getWidth() < minW)
  210. {
  211. bounds.setWidth (jlimit (minW, maxW, bounds.getWidth()));
  212. bounds.setHeight (roundToInt (bounds.getWidth() / aspectRatio));
  213. }
  214. }
  215. else
  216. {
  217. bounds.setHeight (roundToInt (bounds.getWidth() / aspectRatio));
  218. if (bounds.getHeight() > maxH || bounds.getHeight() < minH)
  219. {
  220. bounds.setHeight (jlimit (minH, maxH, bounds.getHeight()));
  221. bounds.setWidth (roundToInt (bounds.getHeight() * aspectRatio));
  222. }
  223. }
  224. if ((isStretchingTop || isStretchingBottom) && ! (isStretchingLeft || isStretchingRight))
  225. {
  226. bounds.setX (old.getX() + (old.getWidth() - bounds.getWidth()) / 2);
  227. }
  228. else if ((isStretchingLeft || isStretchingRight) && ! (isStretchingTop || isStretchingBottom))
  229. {
  230. bounds.setY (old.getY() + (old.getHeight() - bounds.getHeight()) / 2);
  231. }
  232. else
  233. {
  234. if (isStretchingLeft)
  235. bounds.setX (old.getRight() - bounds.getWidth());
  236. if (isStretchingTop)
  237. bounds.setY (old.getBottom() - bounds.getHeight());
  238. }
  239. }
  240. jassert (! bounds.isEmpty());
  241. }