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.

304 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. auto bounds = targetBounds;
  86. auto limits = [&]() -> Rectangle<int>
  87. {
  88. if (auto* parent = component->getParentComponent())
  89. return { parent->getWidth(), parent->getHeight() };
  90. if (auto* display = Desktop::getInstance().getDisplays().getDisplayForPoint (targetBounds.getCentre()))
  91. return component->getLocalArea (nullptr, display->userArea) + component->getPosition();
  92. const auto max = std::numeric_limits<int>::max();
  93. return { max, max };
  94. }();
  95. auto border = [&]() -> BorderSize<int>
  96. {
  97. if (component->getParentComponent() == nullptr)
  98. if (auto* peer = component->getPeer())
  99. if (const auto frameSize = peer->getFrameSizeIfPresent())
  100. return *frameSize;
  101. return {};
  102. }();
  103. border.addTo (bounds);
  104. checkBounds (bounds,
  105. border.addedTo (component->getBounds()), limits,
  106. isStretchingTop, isStretchingLeft,
  107. isStretchingBottom, isStretchingRight);
  108. border.subtractFrom (bounds);
  109. applyBoundsToComponent (*component, bounds);
  110. }
  111. void ComponentBoundsConstrainer::checkComponentBounds (Component* component)
  112. {
  113. setBoundsForComponent (component, component->getBounds(),
  114. false, false, false, false);
  115. }
  116. void ComponentBoundsConstrainer::applyBoundsToComponent (Component& component, Rectangle<int> bounds)
  117. {
  118. if (auto* positioner = component.getPositioner())
  119. positioner->applyNewBounds (bounds);
  120. else
  121. component.setBounds (bounds);
  122. }
  123. //==============================================================================
  124. void ComponentBoundsConstrainer::resizeStart()
  125. {
  126. }
  127. void ComponentBoundsConstrainer::resizeEnd()
  128. {
  129. }
  130. //==============================================================================
  131. void ComponentBoundsConstrainer::checkBounds (Rectangle<int>& bounds,
  132. const Rectangle<int>& old,
  133. const Rectangle<int>& limits,
  134. bool isStretchingTop,
  135. bool isStretchingLeft,
  136. bool isStretchingBottom,
  137. bool isStretchingRight)
  138. {
  139. if (isStretchingLeft)
  140. bounds.setLeft (jlimit (old.getRight() - maxW, old.getRight() - minW, bounds.getX()));
  141. else
  142. bounds.setWidth (jlimit (minW, maxW, bounds.getWidth()));
  143. if (isStretchingTop)
  144. bounds.setTop (jlimit (old.getBottom() - maxH, old.getBottom() - minH, bounds.getY()));
  145. else
  146. bounds.setHeight (jlimit (minH, maxH, bounds.getHeight()));
  147. if (bounds.isEmpty())
  148. return;
  149. if (minOffTop > 0)
  150. {
  151. const int limit = limits.getY() + jmin (minOffTop - bounds.getHeight(), 0);
  152. if (bounds.getY() < limit)
  153. {
  154. if (isStretchingTop)
  155. bounds.setTop (limits.getY());
  156. else
  157. bounds.setY (limit);
  158. }
  159. }
  160. if (minOffLeft > 0)
  161. {
  162. const int limit = limits.getX() + jmin (minOffLeft - bounds.getWidth(), 0);
  163. if (bounds.getX() < limit)
  164. {
  165. if (isStretchingLeft)
  166. bounds.setLeft (limits.getX());
  167. else
  168. bounds.setX (limit);
  169. }
  170. }
  171. if (minOffBottom > 0)
  172. {
  173. const int limit = limits.getBottom() - jmin (minOffBottom, bounds.getHeight());
  174. if (bounds.getY() > limit)
  175. {
  176. if (isStretchingBottom)
  177. bounds.setBottom (limits.getBottom());
  178. else
  179. bounds.setY (limit);
  180. }
  181. }
  182. if (minOffRight > 0)
  183. {
  184. const int limit = limits.getRight() - jmin (minOffRight, bounds.getWidth());
  185. if (bounds.getX() > limit)
  186. {
  187. if (isStretchingRight)
  188. bounds.setRight (limits.getRight());
  189. else
  190. bounds.setX (limit);
  191. }
  192. }
  193. // constrain the aspect ratio if one has been specified..
  194. if (aspectRatio > 0.0)
  195. {
  196. bool adjustWidth;
  197. if ((isStretchingTop || isStretchingBottom) && ! (isStretchingLeft || isStretchingRight))
  198. {
  199. adjustWidth = true;
  200. }
  201. else if ((isStretchingLeft || isStretchingRight) && ! (isStretchingTop || isStretchingBottom))
  202. {
  203. adjustWidth = false;
  204. }
  205. else
  206. {
  207. const double oldRatio = (old.getHeight() > 0) ? std::abs (old.getWidth() / (double) old.getHeight()) : 0.0;
  208. const double newRatio = std::abs (bounds.getWidth() / (double) bounds.getHeight());
  209. adjustWidth = (oldRatio > newRatio);
  210. }
  211. if (adjustWidth)
  212. {
  213. bounds.setWidth (roundToInt (bounds.getHeight() * aspectRatio));
  214. if (bounds.getWidth() > maxW || bounds.getWidth() < minW)
  215. {
  216. bounds.setWidth (jlimit (minW, maxW, bounds.getWidth()));
  217. bounds.setHeight (roundToInt (bounds.getWidth() / aspectRatio));
  218. }
  219. }
  220. else
  221. {
  222. bounds.setHeight (roundToInt (bounds.getWidth() / aspectRatio));
  223. if (bounds.getHeight() > maxH || bounds.getHeight() < minH)
  224. {
  225. bounds.setHeight (jlimit (minH, maxH, bounds.getHeight()));
  226. bounds.setWidth (roundToInt (bounds.getHeight() * aspectRatio));
  227. }
  228. }
  229. if ((isStretchingTop || isStretchingBottom) && ! (isStretchingLeft || isStretchingRight))
  230. {
  231. bounds.setX (old.getX() + (old.getWidth() - bounds.getWidth()) / 2);
  232. }
  233. else if ((isStretchingLeft || isStretchingRight) && ! (isStretchingTop || isStretchingBottom))
  234. {
  235. bounds.setY (old.getY() + (old.getHeight() - bounds.getHeight()) / 2);
  236. }
  237. else
  238. {
  239. if (isStretchingLeft)
  240. bounds.setX (old.getRight() - bounds.getWidth());
  241. if (isStretchingTop)
  242. bounds.setY (old.getBottom() - bounds.getHeight());
  243. }
  244. }
  245. jassert (! bounds.isEmpty());
  246. }
  247. } // namespace juce