Audio plugin host https://kx.studio/carla
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.

297 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. ComponentBoundsConstrainer::ComponentBoundsConstrainer() noexcept {}
  21. ComponentBoundsConstrainer::~ComponentBoundsConstrainer() {}
  22. //==============================================================================
  23. void ComponentBoundsConstrainer::setMinimumWidth (int minimumWidth) noexcept { minW = minimumWidth; }
  24. void ComponentBoundsConstrainer::setMaximumWidth (int maximumWidth) noexcept { maxW = maximumWidth; }
  25. void ComponentBoundsConstrainer::setMinimumHeight (int minimumHeight) noexcept { minH = minimumHeight; }
  26. void ComponentBoundsConstrainer::setMaximumHeight (int maximumHeight) noexcept { maxH = maximumHeight; }
  27. void ComponentBoundsConstrainer::setMinimumSize (int minimumWidth, int minimumHeight) noexcept
  28. {
  29. jassert (maxW >= minimumWidth);
  30. jassert (maxH >= minimumHeight);
  31. jassert (minimumWidth > 0 && minimumHeight > 0);
  32. minW = minimumWidth;
  33. minH = minimumHeight;
  34. if (minW > maxW) maxW = minW;
  35. if (minH > maxH) maxH = minH;
  36. }
  37. void ComponentBoundsConstrainer::setMaximumSize (int maximumWidth, int maximumHeight) noexcept
  38. {
  39. jassert (maximumWidth >= minW);
  40. jassert (maximumHeight >= minH);
  41. jassert (maximumWidth > 0 && maximumHeight > 0);
  42. maxW = jmax (minW, maximumWidth);
  43. maxH = jmax (minH, maximumHeight);
  44. }
  45. void ComponentBoundsConstrainer::setSizeLimits (int minimumWidth,
  46. int minimumHeight,
  47. int maximumWidth,
  48. int maximumHeight) noexcept
  49. {
  50. jassert (maximumWidth >= minimumWidth);
  51. jassert (maximumHeight >= minimumHeight);
  52. jassert (maximumWidth > 0 && maximumHeight > 0);
  53. jassert (minimumWidth > 0 && minimumHeight > 0);
  54. minW = jmax (0, minimumWidth);
  55. minH = jmax (0, minimumHeight);
  56. maxW = jmax (minW, maximumWidth);
  57. maxH = jmax (minH, maximumHeight);
  58. }
  59. void ComponentBoundsConstrainer::setMinimumOnscreenAmounts (int minimumWhenOffTheTop,
  60. int minimumWhenOffTheLeft,
  61. int minimumWhenOffTheBottom,
  62. int minimumWhenOffTheRight) noexcept
  63. {
  64. minOffTop = minimumWhenOffTheTop;
  65. minOffLeft = minimumWhenOffTheLeft;
  66. minOffBottom = minimumWhenOffTheBottom;
  67. minOffRight = minimumWhenOffTheRight;
  68. }
  69. void ComponentBoundsConstrainer::setFixedAspectRatio (double widthOverHeight) noexcept
  70. {
  71. aspectRatio = jmax (0.0, widthOverHeight);
  72. }
  73. double ComponentBoundsConstrainer::getFixedAspectRatio() const noexcept
  74. {
  75. return aspectRatio;
  76. }
  77. void ComponentBoundsConstrainer::setBoundsForComponent (Component* component,
  78. Rectangle<int> targetBounds,
  79. bool isStretchingTop,
  80. bool isStretchingLeft,
  81. bool isStretchingBottom,
  82. bool isStretchingRight)
  83. {
  84. jassert (component != nullptr);
  85. Rectangle<int> limits, bounds (targetBounds);
  86. BorderSize<int> border;
  87. if (auto* parent = component->getParentComponent())
  88. {
  89. limits.setSize (parent->getWidth(), parent->getHeight());
  90. }
  91. else
  92. {
  93. if (auto* peer = component->getPeer())
  94. border = peer->getFrameSize();
  95. auto screenBounds = Desktop::getInstance().getDisplays().findDisplayForPoint (targetBounds.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. }
  242. } // namespace juce