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.

458 lines
16KB

  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. Viewport::Viewport (const String& name)
  18. : Component (name),
  19. scrollBarThickness (0),
  20. singleStepX (16),
  21. singleStepY (16),
  22. showHScrollbar (true),
  23. showVScrollbar (true),
  24. deleteContent (true),
  25. customScrollBarThickness (false),
  26. allowScrollingWithoutScrollbarV (false),
  27. allowScrollingWithoutScrollbarH (false),
  28. verticalScrollBar (true),
  29. horizontalScrollBar (false)
  30. {
  31. // content holder is used to clip the contents so they don't overlap the scrollbars
  32. addAndMakeVisible (contentHolder);
  33. contentHolder.setInterceptsMouseClicks (false, true);
  34. scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
  35. addChildComponent (verticalScrollBar);
  36. addChildComponent (horizontalScrollBar);
  37. verticalScrollBar.addListener (this);
  38. horizontalScrollBar.addListener (this);
  39. setInterceptsMouseClicks (false, true);
  40. setWantsKeyboardFocus (true);
  41. }
  42. Viewport::~Viewport()
  43. {
  44. deleteContentComp();
  45. }
  46. //==============================================================================
  47. void Viewport::visibleAreaChanged (const Rectangle<int>&) {}
  48. void Viewport::viewedComponentChanged (Component*) {}
  49. //==============================================================================
  50. void Viewport::deleteContentComp()
  51. {
  52. if (contentComp != nullptr)
  53. contentComp->removeComponentListener (this);
  54. if (deleteContent)
  55. {
  56. // This sets the content comp to a null pointer before deleting the old one, in case
  57. // anything tries to use the old one while it's in mid-deletion..
  58. ScopedPointer<Component> oldCompDeleter (contentComp);
  59. }
  60. else
  61. {
  62. contentComp = nullptr;
  63. }
  64. }
  65. void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
  66. {
  67. if (contentComp.get() != newViewedComponent)
  68. {
  69. deleteContentComp();
  70. contentComp = newViewedComponent;
  71. deleteContent = deleteComponentWhenNoLongerNeeded;
  72. if (contentComp != nullptr)
  73. {
  74. contentHolder.addAndMakeVisible (contentComp);
  75. setViewPosition (Point<int>());
  76. contentComp->addComponentListener (this);
  77. }
  78. viewedComponentChanged (contentComp);
  79. updateVisibleArea();
  80. }
  81. }
  82. int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
  83. int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
  84. Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
  85. {
  86. jassert (contentComp != nullptr);
  87. return Point<int> (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -(pos.x))),
  88. jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -(pos.y))));
  89. }
  90. void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
  91. {
  92. setViewPosition (Point<int> (xPixelsOffset, yPixelsOffset));
  93. }
  94. void Viewport::setViewPosition (Point<int> newPosition)
  95. {
  96. if (contentComp != nullptr)
  97. contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition));
  98. }
  99. void Viewport::setViewPositionProportionately (const double x, const double y)
  100. {
  101. if (contentComp != nullptr)
  102. setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
  103. jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
  104. }
  105. bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
  106. {
  107. if (contentComp != nullptr)
  108. {
  109. int dx = 0, dy = 0;
  110. if (horizontalScrollBar.isVisible() || contentComp->getX() < 0 || contentComp->getRight() > getWidth())
  111. {
  112. if (mouseX < activeBorderThickness)
  113. dx = activeBorderThickness - mouseX;
  114. else if (mouseX >= contentHolder.getWidth() - activeBorderThickness)
  115. dx = (contentHolder.getWidth() - activeBorderThickness) - mouseX;
  116. if (dx < 0)
  117. dx = jmax (dx, -maximumSpeed, contentHolder.getWidth() - contentComp->getRight());
  118. else
  119. dx = jmin (dx, maximumSpeed, -contentComp->getX());
  120. }
  121. if (verticalScrollBar.isVisible() || contentComp->getY() < 0 || contentComp->getBottom() > getHeight())
  122. {
  123. if (mouseY < activeBorderThickness)
  124. dy = activeBorderThickness - mouseY;
  125. else if (mouseY >= contentHolder.getHeight() - activeBorderThickness)
  126. dy = (contentHolder.getHeight() - activeBorderThickness) - mouseY;
  127. if (dy < 0)
  128. dy = jmax (dy, -maximumSpeed, contentHolder.getHeight() - contentComp->getBottom());
  129. else
  130. dy = jmin (dy, maximumSpeed, -contentComp->getY());
  131. }
  132. if (dx != 0 || dy != 0)
  133. {
  134. contentComp->setTopLeftPosition (contentComp->getX() + dx,
  135. contentComp->getY() + dy);
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. void Viewport::componentMovedOrResized (Component&, bool, bool)
  142. {
  143. updateVisibleArea();
  144. }
  145. void Viewport::lookAndFeelChanged()
  146. {
  147. if (! customScrollBarThickness)
  148. scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
  149. }
  150. void Viewport::resized()
  151. {
  152. updateVisibleArea();
  153. }
  154. //==============================================================================
  155. void Viewport::updateVisibleArea()
  156. {
  157. const int scrollbarWidth = getScrollBarThickness();
  158. const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
  159. const bool canShowHBar = showHScrollbar && canShowAnyBars;
  160. const bool canShowVBar = showVScrollbar && canShowAnyBars;
  161. bool hBarVisible = false, vBarVisible = false;
  162. Rectangle<int> contentArea;
  163. for (int i = 3; --i >= 0;)
  164. {
  165. hBarVisible = canShowHBar && ! horizontalScrollBar.autoHides();
  166. vBarVisible = canShowVBar && ! verticalScrollBar.autoHides();
  167. contentArea = getLocalBounds();
  168. if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
  169. {
  170. hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
  171. vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());
  172. if (vBarVisible)
  173. contentArea.setWidth (getWidth() - scrollbarWidth);
  174. if (hBarVisible)
  175. contentArea.setHeight (getHeight() - scrollbarWidth);
  176. if (! contentArea.contains (contentComp->getBounds()))
  177. {
  178. hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
  179. vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
  180. }
  181. }
  182. if (vBarVisible) contentArea.setWidth (getWidth() - scrollbarWidth);
  183. if (hBarVisible) contentArea.setHeight (getHeight() - scrollbarWidth);
  184. if (contentComp == nullptr)
  185. {
  186. contentHolder.setBounds (contentArea);
  187. break;
  188. }
  189. const Rectangle<int> oldContentBounds (contentComp->getBounds());
  190. contentHolder.setBounds (contentArea);
  191. // If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate..
  192. if (oldContentBounds == contentComp->getBounds())
  193. break;
  194. }
  195. Rectangle<int> contentBounds;
  196. if (contentComp != nullptr)
  197. contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
  198. Point<int> visibleOrigin (-contentBounds.getPosition());
  199. horizontalScrollBar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
  200. horizontalScrollBar.setRangeLimits (0.0, contentBounds.getWidth());
  201. horizontalScrollBar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
  202. horizontalScrollBar.setSingleStepSize (singleStepX);
  203. horizontalScrollBar.cancelPendingUpdate();
  204. if (canShowHBar && ! hBarVisible)
  205. visibleOrigin.setX (0);
  206. verticalScrollBar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
  207. verticalScrollBar.setRangeLimits (0.0, contentBounds.getHeight());
  208. verticalScrollBar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
  209. verticalScrollBar.setSingleStepSize (singleStepY);
  210. verticalScrollBar.cancelPendingUpdate();
  211. if (canShowVBar && ! vBarVisible)
  212. visibleOrigin.setY (0);
  213. // Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
  214. horizontalScrollBar.setVisible (hBarVisible);
  215. verticalScrollBar.setVisible (vBarVisible);
  216. if (contentComp != nullptr)
  217. {
  218. const Point<int> newContentCompPos (viewportPosToCompPos (visibleOrigin));
  219. if (contentComp->getBounds().getPosition() != newContentCompPos)
  220. {
  221. contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
  222. return;
  223. }
  224. }
  225. const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
  226. jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
  227. jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
  228. if (lastVisibleArea != visibleArea)
  229. {
  230. lastVisibleArea = visibleArea;
  231. visibleAreaChanged (visibleArea);
  232. }
  233. horizontalScrollBar.handleUpdateNowIfNeeded();
  234. verticalScrollBar.handleUpdateNowIfNeeded();
  235. }
  236. //==============================================================================
  237. void Viewport::setSingleStepSizes (const int stepX, const int stepY)
  238. {
  239. if (singleStepX != stepX || singleStepY != stepY)
  240. {
  241. singleStepX = stepX;
  242. singleStepY = stepY;
  243. updateVisibleArea();
  244. }
  245. }
  246. void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
  247. const bool showHorizontalScrollbarIfNeeded,
  248. const bool allowVerticalScrollingWithoutScrollbar,
  249. const bool allowHorizontalScrollingWithoutScrollbar)
  250. {
  251. allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
  252. allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
  253. if (showVScrollbar != showVerticalScrollbarIfNeeded
  254. || showHScrollbar != showHorizontalScrollbarIfNeeded)
  255. {
  256. showVScrollbar = showVerticalScrollbarIfNeeded;
  257. showHScrollbar = showHorizontalScrollbarIfNeeded;
  258. updateVisibleArea();
  259. }
  260. }
  261. void Viewport::setScrollBarThickness (const int thickness)
  262. {
  263. int newThickness;
  264. // To stay compatible with the previous code: use the
  265. // default thickness if thickness parameter is zero
  266. // or negative
  267. if (thickness <= 0)
  268. {
  269. customScrollBarThickness = false;
  270. newThickness = getLookAndFeel().getDefaultScrollbarWidth();
  271. }
  272. else
  273. {
  274. customScrollBarThickness = true;
  275. newThickness = thickness;
  276. }
  277. if (scrollBarThickness != newThickness)
  278. {
  279. scrollBarThickness = newThickness;
  280. updateVisibleArea();
  281. }
  282. }
  283. int Viewport::getScrollBarThickness() const
  284. {
  285. return scrollBarThickness;
  286. }
  287. void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
  288. {
  289. const int newRangeStartInt = roundToInt (newRangeStart);
  290. if (scrollBarThatHasMoved == &horizontalScrollBar)
  291. {
  292. setViewPosition (newRangeStartInt, getViewPositionY());
  293. }
  294. else if (scrollBarThatHasMoved == &verticalScrollBar)
  295. {
  296. setViewPosition (getViewPositionX(), newRangeStartInt);
  297. }
  298. }
  299. void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  300. {
  301. if (! useMouseWheelMoveIfNeeded (e, wheel))
  302. Component::mouseWheelMove (e, wheel);
  303. }
  304. static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
  305. {
  306. if (distance == 0)
  307. return 0;
  308. distance *= 14.0f * singleStepSize;
  309. return roundToInt (distance < 0 ? jmin (distance, -1.0f)
  310. : jmax (distance, 1.0f));
  311. }
  312. bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
  313. {
  314. if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
  315. {
  316. const bool canScrollVert = (allowScrollingWithoutScrollbarV || verticalScrollBar.isVisible());
  317. const bool canScrollHorz = (allowScrollingWithoutScrollbarH || horizontalScrollBar.isVisible());
  318. if (canScrollHorz || canScrollVert)
  319. {
  320. const int deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
  321. const int deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
  322. Point<int> pos (getViewPosition());
  323. if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
  324. {
  325. pos.x -= deltaX;
  326. pos.y -= deltaY;
  327. }
  328. else if (canScrollHorz && (deltaX != 0 || e.mods.isShiftDown() || ! canScrollVert))
  329. {
  330. pos.x -= deltaX != 0 ? deltaX : deltaY;
  331. }
  332. else if (canScrollVert && deltaY != 0)
  333. {
  334. pos.y -= deltaY;
  335. }
  336. if (pos != getViewPosition())
  337. {
  338. setViewPosition (pos);
  339. return true;
  340. }
  341. }
  342. }
  343. return false;
  344. }
  345. static bool isUpDownKeyPress (const KeyPress& key)
  346. {
  347. return key == KeyPress::upKey
  348. || key == KeyPress::downKey
  349. || key == KeyPress::pageUpKey
  350. || key == KeyPress::pageDownKey
  351. || key == KeyPress::homeKey
  352. || key == KeyPress::endKey;
  353. }
  354. static bool isLeftRightKeyPress (const KeyPress& key)
  355. {
  356. return key == KeyPress::leftKey
  357. || key == KeyPress::rightKey;
  358. }
  359. bool Viewport::keyPressed (const KeyPress& key)
  360. {
  361. const bool isUpDownKey = isUpDownKeyPress (key);
  362. if (verticalScrollBar.isVisible() && isUpDownKey)
  363. return verticalScrollBar.keyPressed (key);
  364. const bool isLeftRightKey = isLeftRightKeyPress (key);
  365. if (horizontalScrollBar.isVisible() && (isUpDownKey || isLeftRightKey))
  366. return horizontalScrollBar.keyPressed (key);
  367. return false;
  368. }
  369. bool Viewport::respondsToKey (const KeyPress& key)
  370. {
  371. return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
  372. }