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.

636 lines
21KB

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