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.

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