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.

641 lines
21KB

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