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.

630 lines
21KB

  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. Viewport::Viewport (const String& name) : Component (name)
  21. {
  22. // content holder is used to clip the contents so they don't overlap the scrollbars
  23. addAndMakeVisible (contentHolder);
  24. contentHolder.setInterceptsMouseClicks (false, true);
  25. scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
  26. setInterceptsMouseClicks (false, true);
  27. setWantsKeyboardFocus (true);
  28. setScrollOnDragEnabled (Desktop::getInstance().getMainMouseSource().isTouch());
  29. recreateScrollbars();
  30. }
  31. Viewport::~Viewport()
  32. {
  33. setScrollOnDragEnabled (false);
  34. deleteOrRemoveContentComp();
  35. }
  36. //==============================================================================
  37. void Viewport::visibleAreaChanged (const Rectangle<int>&) {}
  38. void Viewport::viewedComponentChanged (Component*) {}
  39. //==============================================================================
  40. void Viewport::deleteOrRemoveContentComp()
  41. {
  42. if (contentComp != nullptr)
  43. {
  44. contentComp->removeComponentListener (this);
  45. if (deleteContent)
  46. {
  47. // This sets the content comp to a null pointer before deleting the old one, in case
  48. // anything tries to use the old one while it's in mid-deletion..
  49. std::unique_ptr<Component> oldCompDeleter (contentComp.get());
  50. contentComp = nullptr;
  51. }
  52. else
  53. {
  54. contentHolder.removeChildComponent (contentComp);
  55. contentComp = nullptr;
  56. }
  57. }
  58. }
  59. void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
  60. {
  61. if (contentComp.get() != newViewedComponent)
  62. {
  63. deleteOrRemoveContentComp();
  64. contentComp = newViewedComponent;
  65. deleteContent = deleteComponentWhenNoLongerNeeded;
  66. if (contentComp != nullptr)
  67. {
  68. contentHolder.addAndMakeVisible (contentComp);
  69. setViewPosition (Point<int>());
  70. contentComp->addComponentListener (this);
  71. }
  72. viewedComponentChanged (contentComp);
  73. updateVisibleArea();
  74. }
  75. }
  76. void Viewport::recreateScrollbars()
  77. {
  78. verticalScrollBar.reset();
  79. horizontalScrollBar.reset();
  80. verticalScrollBar .reset (createScrollBarComponent (true));
  81. horizontalScrollBar.reset (createScrollBarComponent (false));
  82. addChildComponent (verticalScrollBar.get());
  83. addChildComponent (horizontalScrollBar.get());
  84. getVerticalScrollBar().addListener (this);
  85. getHorizontalScrollBar().addListener (this);
  86. resized();
  87. }
  88. int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
  89. int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
  90. bool Viewport::canScrollVertically() const noexcept { return contentComp->getY() < 0 || contentComp->getBottom() > getHeight(); }
  91. bool Viewport::canScrollHorizontally() const noexcept { return contentComp->getX() < 0 || contentComp->getRight() > getWidth(); }
  92. Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
  93. {
  94. jassert (contentComp != nullptr);
  95. auto contentBounds = contentHolder.getLocalArea (contentComp.get(), contentComp->getLocalBounds());
  96. Point<int> p (jmax (jmin (0, contentHolder.getWidth() - contentBounds.getWidth()), jmin (0, -(pos.x))),
  97. jmax (jmin (0, contentHolder.getHeight() - contentBounds.getHeight()), jmin (0, -(pos.y))));
  98. return p.transformedBy (contentComp->getTransform().inverted());
  99. }
  100. void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
  101. {
  102. setViewPosition ({ xPixelsOffset, yPixelsOffset });
  103. }
  104. void Viewport::setViewPosition (Point<int> newPosition)
  105. {
  106. if (contentComp != nullptr)
  107. contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition));
  108. }
  109. void Viewport::setViewPositionProportionately (const double x, const double y)
  110. {
  111. if (contentComp != nullptr)
  112. setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
  113. jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
  114. }
  115. bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
  116. {
  117. if (contentComp != nullptr)
  118. {
  119. int dx = 0, dy = 0;
  120. if (getHorizontalScrollBar().isVisible() || canScrollHorizontally())
  121. {
  122. if (mouseX < activeBorderThickness)
  123. dx = activeBorderThickness - mouseX;
  124. else if (mouseX >= contentHolder.getWidth() - activeBorderThickness)
  125. dx = (contentHolder.getWidth() - activeBorderThickness) - mouseX;
  126. if (dx < 0)
  127. dx = jmax (dx, -maximumSpeed, contentHolder.getWidth() - contentComp->getRight());
  128. else
  129. dx = jmin (dx, maximumSpeed, -contentComp->getX());
  130. }
  131. if (getVerticalScrollBar().isVisible() || canScrollVertically())
  132. {
  133. if (mouseY < activeBorderThickness)
  134. dy = activeBorderThickness - mouseY;
  135. else if (mouseY >= contentHolder.getHeight() - activeBorderThickness)
  136. dy = (contentHolder.getHeight() - activeBorderThickness) - mouseY;
  137. if (dy < 0)
  138. dy = jmax (dy, -maximumSpeed, contentHolder.getHeight() - contentComp->getBottom());
  139. else
  140. dy = jmin (dy, maximumSpeed, -contentComp->getY());
  141. }
  142. if (dx != 0 || dy != 0)
  143. {
  144. contentComp->setTopLeftPosition (contentComp->getX() + dx,
  145. contentComp->getY() + dy);
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. void Viewport::componentMovedOrResized (Component&, bool, bool)
  152. {
  153. updateVisibleArea();
  154. }
  155. //==============================================================================
  156. typedef AnimatedPosition<AnimatedPositionBehaviours::ContinuousWithMomentum> ViewportDragPosition;
  157. struct Viewport::DragToScrollListener : private MouseListener,
  158. private ViewportDragPosition::Listener
  159. {
  160. DragToScrollListener (Viewport& v) : viewport (v)
  161. {
  162. viewport.contentHolder.addMouseListener (this, true);
  163. offsetX.addListener (this);
  164. offsetY.addListener (this);
  165. offsetX.behaviour.setMinimumVelocity (60);
  166. offsetY.behaviour.setMinimumVelocity (60);
  167. }
  168. ~DragToScrollListener() override
  169. {
  170. viewport.contentHolder.removeMouseListener (this);
  171. Desktop::getInstance().removeGlobalMouseListener (this);
  172. }
  173. void positionChanged (ViewportDragPosition&, double) override
  174. {
  175. viewport.setViewPosition (originalViewPos - Point<int> ((int) offsetX.getPosition(),
  176. (int) offsetY.getPosition()));
  177. }
  178. void mouseDown (const MouseEvent&) override
  179. {
  180. if (! isGlobalMouseListener)
  181. {
  182. offsetX.setPosition (offsetX.getPosition());
  183. offsetY.setPosition (offsetY.getPosition());
  184. // switch to a global mouse listener so we still receive mouseUp events
  185. // if the original event component is deleted
  186. viewport.contentHolder.removeMouseListener (this);
  187. Desktop::getInstance().addGlobalMouseListener (this);
  188. isGlobalMouseListener = true;
  189. }
  190. }
  191. void mouseDrag (const MouseEvent& e) override
  192. {
  193. if (Desktop::getInstance().getNumDraggingMouseSources() == 1 && ! doesMouseEventComponentBlockViewportDrag (e.eventComponent))
  194. {
  195. auto totalOffset = e.getOffsetFromDragStart().toFloat();
  196. if (! isDragging && totalOffset.getDistanceFromOrigin() > 8.0f)
  197. {
  198. isDragging = true;
  199. originalViewPos = viewport.getViewPosition();
  200. offsetX.setPosition (0.0);
  201. offsetX.beginDrag();
  202. offsetY.setPosition (0.0);
  203. offsetY.beginDrag();
  204. }
  205. if (isDragging)
  206. {
  207. offsetX.drag (totalOffset.x);
  208. offsetY.drag (totalOffset.y);
  209. }
  210. }
  211. }
  212. void mouseUp (const MouseEvent&) override
  213. {
  214. if (isGlobalMouseListener && Desktop::getInstance().getNumDraggingMouseSources() == 0)
  215. endDragAndClearGlobalMouseListener();
  216. }
  217. void endDragAndClearGlobalMouseListener()
  218. {
  219. offsetX.endDrag();
  220. offsetY.endDrag();
  221. isDragging = false;
  222. viewport.contentHolder.addMouseListener (this, true);
  223. Desktop::getInstance().removeGlobalMouseListener (this);
  224. isGlobalMouseListener = false;
  225. }
  226. bool doesMouseEventComponentBlockViewportDrag (const Component* eventComp)
  227. {
  228. for (auto c = eventComp; c != nullptr && c != &viewport; c = c->getParentComponent())
  229. if (c->getViewportIgnoreDragFlag())
  230. return true;
  231. return false;
  232. }
  233. Viewport& viewport;
  234. ViewportDragPosition offsetX, offsetY;
  235. Point<int> originalViewPos;
  236. bool isDragging = false;
  237. bool isGlobalMouseListener = false;
  238. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragToScrollListener)
  239. };
  240. void Viewport::setScrollOnDragEnabled (bool shouldScrollOnDrag)
  241. {
  242. if (isScrollOnDragEnabled() != shouldScrollOnDrag)
  243. {
  244. if (shouldScrollOnDrag)
  245. dragToScrollListener.reset (new DragToScrollListener (*this));
  246. else
  247. dragToScrollListener.reset();
  248. }
  249. }
  250. bool Viewport::isScrollOnDragEnabled() const noexcept
  251. {
  252. return dragToScrollListener != nullptr;
  253. }
  254. bool Viewport::isCurrentlyScrollingOnDrag() const noexcept
  255. {
  256. return dragToScrollListener != nullptr && dragToScrollListener->isDragging;
  257. }
  258. //==============================================================================
  259. void Viewport::lookAndFeelChanged()
  260. {
  261. if (! customScrollBarThickness)
  262. {
  263. scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
  264. resized();
  265. }
  266. }
  267. void Viewport::resized()
  268. {
  269. updateVisibleArea();
  270. }
  271. //==============================================================================
  272. void Viewport::updateVisibleArea()
  273. {
  274. auto scrollbarWidth = getScrollBarThickness();
  275. const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
  276. const bool canShowHBar = showHScrollbar && canShowAnyBars;
  277. const bool canShowVBar = showVScrollbar && canShowAnyBars;
  278. bool hBarVisible = false, vBarVisible = false;
  279. Rectangle<int> contentArea;
  280. for (int i = 3; --i >= 0;)
  281. {
  282. hBarVisible = canShowHBar && ! getHorizontalScrollBar().autoHides();
  283. vBarVisible = canShowVBar && ! getVerticalScrollBar().autoHides();
  284. contentArea = getLocalBounds();
  285. if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
  286. {
  287. hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
  288. vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());
  289. if (vBarVisible)
  290. contentArea.setWidth (getWidth() - scrollbarWidth);
  291. if (hBarVisible)
  292. contentArea.setHeight (getHeight() - scrollbarWidth);
  293. if (! contentArea.contains (contentComp->getBounds()))
  294. {
  295. hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
  296. vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
  297. }
  298. }
  299. if (vBarVisible) contentArea.setWidth (getWidth() - scrollbarWidth);
  300. if (hBarVisible) contentArea.setHeight (getHeight() - scrollbarWidth);
  301. if (! vScrollbarRight && vBarVisible)
  302. contentArea.setX (scrollbarWidth);
  303. if (! hScrollbarBottom && hBarVisible)
  304. contentArea.setY (scrollbarWidth);
  305. if (contentComp == nullptr)
  306. {
  307. contentHolder.setBounds (contentArea);
  308. break;
  309. }
  310. auto oldContentBounds = contentComp->getBounds();
  311. contentHolder.setBounds (contentArea);
  312. // If the content has changed its size, that might affect our scrollbars, so go round again and re-calculate..
  313. if (oldContentBounds == contentComp->getBounds())
  314. break;
  315. }
  316. Rectangle<int> contentBounds;
  317. if (auto cc = contentComp.get())
  318. contentBounds = contentHolder.getLocalArea (cc, cc->getLocalBounds());
  319. auto visibleOrigin = -contentBounds.getPosition();
  320. auto& hbar = getHorizontalScrollBar();
  321. auto& vbar = getVerticalScrollBar();
  322. hbar.setBounds (contentArea.getX(), hScrollbarBottom ? contentArea.getHeight() : 0, contentArea.getWidth(), scrollbarWidth);
  323. hbar.setRangeLimits (0.0, contentBounds.getWidth());
  324. hbar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
  325. hbar.setSingleStepSize (singleStepX);
  326. if (canShowHBar && ! hBarVisible)
  327. visibleOrigin.setX (0);
  328. vbar.setBounds (vScrollbarRight ? contentArea.getWidth() : 0, contentArea.getY(), scrollbarWidth, contentArea.getHeight());
  329. vbar.setRangeLimits (0.0, contentBounds.getHeight());
  330. vbar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
  331. vbar.setSingleStepSize (singleStepY);
  332. if (canShowVBar && ! vBarVisible)
  333. visibleOrigin.setY (0);
  334. // Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
  335. hbar.setVisible (hBarVisible);
  336. vbar.setVisible (vBarVisible);
  337. if (contentComp != nullptr)
  338. {
  339. auto newContentCompPos = viewportPosToCompPos (visibleOrigin);
  340. if (contentComp->getBounds().getPosition() != newContentCompPos)
  341. {
  342. contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
  343. return;
  344. }
  345. }
  346. const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
  347. jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
  348. jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
  349. if (lastVisibleArea != visibleArea)
  350. {
  351. lastVisibleArea = visibleArea;
  352. visibleAreaChanged (visibleArea);
  353. }
  354. hbar.handleUpdateNowIfNeeded();
  355. vbar.handleUpdateNowIfNeeded();
  356. }
  357. //==============================================================================
  358. void Viewport::setSingleStepSizes (const int stepX, const int stepY)
  359. {
  360. if (singleStepX != stepX || singleStepY != stepY)
  361. {
  362. singleStepX = stepX;
  363. singleStepY = stepY;
  364. updateVisibleArea();
  365. }
  366. }
  367. void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
  368. const bool showHorizontalScrollbarIfNeeded,
  369. const bool allowVerticalScrollingWithoutScrollbar,
  370. const bool allowHorizontalScrollingWithoutScrollbar)
  371. {
  372. allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
  373. allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
  374. if (showVScrollbar != showVerticalScrollbarIfNeeded
  375. || showHScrollbar != showHorizontalScrollbarIfNeeded)
  376. {
  377. showVScrollbar = showVerticalScrollbarIfNeeded;
  378. showHScrollbar = showHorizontalScrollbarIfNeeded;
  379. updateVisibleArea();
  380. }
  381. }
  382. void Viewport::setScrollBarThickness (const int thickness)
  383. {
  384. int newThickness;
  385. // To stay compatible with the previous code: use the
  386. // default thickness if thickness parameter is zero
  387. // or negative
  388. if (thickness <= 0)
  389. {
  390. customScrollBarThickness = false;
  391. newThickness = getLookAndFeel().getDefaultScrollbarWidth();
  392. }
  393. else
  394. {
  395. customScrollBarThickness = true;
  396. newThickness = thickness;
  397. }
  398. if (scrollBarThickness != newThickness)
  399. {
  400. scrollBarThickness = newThickness;
  401. updateVisibleArea();
  402. }
  403. }
  404. int Viewport::getScrollBarThickness() const
  405. {
  406. return scrollBarThickness;
  407. }
  408. void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
  409. {
  410. auto newRangeStartInt = roundToInt (newRangeStart);
  411. if (scrollBarThatHasMoved == horizontalScrollBar.get())
  412. {
  413. setViewPosition (newRangeStartInt, getViewPositionY());
  414. }
  415. else if (scrollBarThatHasMoved == verticalScrollBar.get())
  416. {
  417. setViewPosition (getViewPositionX(), newRangeStartInt);
  418. }
  419. }
  420. void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  421. {
  422. if (! useMouseWheelMoveIfNeeded (e, wheel))
  423. Component::mouseWheelMove (e, wheel);
  424. }
  425. static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
  426. {
  427. if (distance == 0.0f)
  428. return 0;
  429. distance *= 14.0f * (float) singleStepSize;
  430. return roundToInt (distance < 0 ? jmin (distance, -1.0f)
  431. : jmax (distance, 1.0f));
  432. }
  433. bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
  434. {
  435. if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
  436. {
  437. const bool canScrollVert = (allowScrollingWithoutScrollbarV || getVerticalScrollBar().isVisible());
  438. const bool canScrollHorz = (allowScrollingWithoutScrollbarH || getHorizontalScrollBar().isVisible());
  439. if (canScrollHorz || canScrollVert)
  440. {
  441. auto deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
  442. auto deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
  443. auto pos = getViewPosition();
  444. if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
  445. {
  446. pos.x -= deltaX;
  447. pos.y -= deltaY;
  448. }
  449. else if (canScrollHorz && (deltaX != 0 || e.mods.isShiftDown() || ! canScrollVert))
  450. {
  451. pos.x -= deltaX != 0 ? deltaX : deltaY;
  452. }
  453. else if (canScrollVert && deltaY != 0)
  454. {
  455. pos.y -= deltaY;
  456. }
  457. if (pos != getViewPosition())
  458. {
  459. setViewPosition (pos);
  460. return true;
  461. }
  462. }
  463. }
  464. return false;
  465. }
  466. static bool isUpDownKeyPress (const KeyPress& key)
  467. {
  468. return key == KeyPress::upKey
  469. || key == KeyPress::downKey
  470. || key == KeyPress::pageUpKey
  471. || key == KeyPress::pageDownKey
  472. || key == KeyPress::homeKey
  473. || key == KeyPress::endKey;
  474. }
  475. static bool isLeftRightKeyPress (const KeyPress& key)
  476. {
  477. return key == KeyPress::leftKey
  478. || key == KeyPress::rightKey;
  479. }
  480. bool Viewport::keyPressed (const KeyPress& key)
  481. {
  482. const bool isUpDownKey = isUpDownKeyPress (key);
  483. if (getVerticalScrollBar().isVisible() && isUpDownKey)
  484. return getVerticalScrollBar().keyPressed (key);
  485. const bool isLeftRightKey = isLeftRightKeyPress (key);
  486. if (getHorizontalScrollBar().isVisible() && (isUpDownKey || isLeftRightKey))
  487. return getHorizontalScrollBar().keyPressed (key);
  488. return false;
  489. }
  490. bool Viewport::respondsToKey (const KeyPress& key)
  491. {
  492. return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
  493. }
  494. ScrollBar* Viewport::createScrollBarComponent (bool isVertical)
  495. {
  496. return new ScrollBar (isVertical);
  497. }
  498. void Viewport::setScrollBarPosition (bool verticalScrollbarOnRight,
  499. bool horizontalScrollbarAtBottom)
  500. {
  501. vScrollbarRight = verticalScrollbarOnRight;
  502. hScrollbarBottom = horizontalScrollbarAtBottom;
  503. resized();
  504. }
  505. } // namespace juce