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.

623 lines
21KB

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