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.

608 lines
20KB

  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. ScopedPointer<Component> oldCompDeleter (contentComp);
  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 = nullptr;
  82. horizontalScrollBar = nullptr;
  83. addChildComponent (verticalScrollBar = createScrollBarComponent (true));
  84. addChildComponent (horizontalScrollBar = createScrollBarComponent (false));
  85. getVerticalScrollBar().addListener (this);
  86. getHorizontalScrollBar().addListener (this);
  87. resized();
  88. }
  89. int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
  90. int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
  91. bool Viewport::canScrollVertically() const noexcept { return contentComp->getY() < 0 || contentComp->getBottom() > getHeight(); }
  92. bool Viewport::canScrollHorizontally() const noexcept { return contentComp->getX() < 0 || contentComp->getRight() > getWidth(); }
  93. Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
  94. {
  95. jassert (contentComp != nullptr);
  96. auto contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
  97. Point<int> p (jmax (jmin (0, contentHolder.getWidth() - contentBounds.getWidth()), jmin (0, -(pos.x))),
  98. jmax (jmin (0, contentHolder.getHeight() - contentBounds.getHeight()), jmin (0, -(pos.y))));
  99. return p.transformedBy (contentComp->getTransform().inverted());
  100. }
  101. void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
  102. {
  103. setViewPosition ({ xPixelsOffset, yPixelsOffset });
  104. }
  105. void Viewport::setViewPosition (Point<int> newPosition)
  106. {
  107. if (contentComp != nullptr)
  108. contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition));
  109. }
  110. void Viewport::setViewPositionProportionately (const double x, const double y)
  111. {
  112. if (contentComp != nullptr)
  113. setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
  114. jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
  115. }
  116. bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
  117. {
  118. if (contentComp != nullptr)
  119. {
  120. int dx = 0, dy = 0;
  121. if (getHorizontalScrollBar().isVisible() || canScrollHorizontally())
  122. {
  123. if (mouseX < activeBorderThickness)
  124. dx = activeBorderThickness - mouseX;
  125. else if (mouseX >= contentHolder.getWidth() - activeBorderThickness)
  126. dx = (contentHolder.getWidth() - activeBorderThickness) - mouseX;
  127. if (dx < 0)
  128. dx = jmax (dx, -maximumSpeed, contentHolder.getWidth() - contentComp->getRight());
  129. else
  130. dx = jmin (dx, maximumSpeed, -contentComp->getX());
  131. }
  132. if (getVerticalScrollBar().isVisible() || canScrollVertically())
  133. {
  134. if (mouseY < activeBorderThickness)
  135. dy = activeBorderThickness - mouseY;
  136. else if (mouseY >= contentHolder.getHeight() - activeBorderThickness)
  137. dy = (contentHolder.getHeight() - activeBorderThickness) - mouseY;
  138. if (dy < 0)
  139. dy = jmax (dy, -maximumSpeed, contentHolder.getHeight() - contentComp->getBottom());
  140. else
  141. dy = jmin (dy, maximumSpeed, -contentComp->getY());
  142. }
  143. if (dx != 0 || dy != 0)
  144. {
  145. contentComp->setTopLeftPosition (contentComp->getX() + dx,
  146. contentComp->getY() + dy);
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. void Viewport::componentMovedOrResized (Component&, bool, bool)
  153. {
  154. updateVisibleArea();
  155. }
  156. //==============================================================================
  157. typedef AnimatedPosition<AnimatedPositionBehaviours::ContinuousWithMomentum> ViewportDragPosition;
  158. struct Viewport::DragToScrollListener : private MouseListener,
  159. private ViewportDragPosition::Listener
  160. {
  161. DragToScrollListener (Viewport& v) : viewport (v)
  162. {
  163. viewport.contentHolder.addMouseListener (this, true);
  164. offsetX.addListener (this);
  165. offsetY.addListener (this);
  166. offsetX.behaviour.setMinimumVelocity (60);
  167. offsetY.behaviour.setMinimumVelocity (60);
  168. }
  169. ~DragToScrollListener()
  170. {
  171. viewport.contentHolder.removeMouseListener (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& e) override
  179. {
  180. if (doesMouseEventComponentBlockViewportDrag (e.eventComponent))
  181. isViewportDragBlocked = true;
  182. offsetX.setPosition (offsetX.getPosition());
  183. offsetY.setPosition (offsetY.getPosition());
  184. ++numTouches;
  185. }
  186. void mouseDrag (const MouseEvent& e) override
  187. {
  188. if (numTouches == 1 && ! isViewportDragBlocked)
  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& e) override
  208. {
  209. if (doesMouseEventComponentBlockViewportDrag (e.eventComponent))
  210. isViewportDragBlocked = false;
  211. if (--numTouches <= 0)
  212. {
  213. offsetX.endDrag();
  214. offsetY.endDrag();
  215. isDragging = false;
  216. numTouches = 0;
  217. }
  218. }
  219. bool doesMouseEventComponentBlockViewportDrag (const Component* eventComp)
  220. {
  221. for (auto c = eventComp; c != nullptr && c != &viewport; c = c->getParentComponent())
  222. if (c->getViewportIgnoreDragFlag())
  223. return true;
  224. return false;
  225. }
  226. Viewport& viewport;
  227. ViewportDragPosition offsetX, offsetY;
  228. Point<int> originalViewPos;
  229. int numTouches = 0;
  230. bool isDragging = false;
  231. bool isViewportDragBlocked = false;
  232. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragToScrollListener)
  233. };
  234. void Viewport::setScrollOnDragEnabled (bool shouldScrollOnDrag)
  235. {
  236. if (isScrollOnDragEnabled() != shouldScrollOnDrag)
  237. {
  238. if (shouldScrollOnDrag)
  239. dragToScrollListener = new DragToScrollListener (*this);
  240. else
  241. dragToScrollListener = nullptr;
  242. }
  243. }
  244. bool Viewport::isScrollOnDragEnabled() const noexcept
  245. {
  246. return dragToScrollListener != nullptr;
  247. }
  248. bool Viewport::isCurrentlyScrollingOnDrag() const noexcept
  249. {
  250. return dragToScrollListener != nullptr && dragToScrollListener->isDragging;
  251. }
  252. //==============================================================================
  253. void Viewport::lookAndFeelChanged()
  254. {
  255. if (! customScrollBarThickness)
  256. {
  257. scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
  258. resized();
  259. }
  260. }
  261. void Viewport::resized()
  262. {
  263. updateVisibleArea();
  264. }
  265. //==============================================================================
  266. void Viewport::updateVisibleArea()
  267. {
  268. auto scrollbarWidth = getScrollBarThickness();
  269. const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
  270. const bool canShowHBar = showHScrollbar && canShowAnyBars;
  271. const bool canShowVBar = showVScrollbar && canShowAnyBars;
  272. bool hBarVisible = false, vBarVisible = false;
  273. Rectangle<int> contentArea;
  274. for (int i = 3; --i >= 0;)
  275. {
  276. hBarVisible = canShowHBar && ! getHorizontalScrollBar().autoHides();
  277. vBarVisible = canShowVBar && ! getVerticalScrollBar().autoHides();
  278. contentArea = getLocalBounds();
  279. if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
  280. {
  281. hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
  282. vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());
  283. if (vBarVisible)
  284. contentArea.setWidth (getWidth() - scrollbarWidth);
  285. if (hBarVisible)
  286. contentArea.setHeight (getHeight() - scrollbarWidth);
  287. if (! contentArea.contains (contentComp->getBounds()))
  288. {
  289. hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
  290. vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
  291. }
  292. }
  293. if (vBarVisible) contentArea.setWidth (getWidth() - scrollbarWidth);
  294. if (hBarVisible) contentArea.setHeight (getHeight() - scrollbarWidth);
  295. if (contentComp == nullptr)
  296. {
  297. contentHolder.setBounds (contentArea);
  298. break;
  299. }
  300. auto oldContentBounds = contentComp->getBounds();
  301. contentHolder.setBounds (contentArea);
  302. // If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate..
  303. if (oldContentBounds == contentComp->getBounds())
  304. break;
  305. }
  306. Rectangle<int> contentBounds;
  307. if (contentComp != nullptr)
  308. contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
  309. auto visibleOrigin = -contentBounds.getPosition();
  310. auto& hbar = getHorizontalScrollBar();
  311. auto& vbar = getVerticalScrollBar();
  312. hbar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
  313. hbar.setRangeLimits (0.0, contentBounds.getWidth());
  314. hbar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
  315. hbar.setSingleStepSize (singleStepX);
  316. hbar.cancelPendingUpdate();
  317. if (canShowHBar && ! hBarVisible)
  318. visibleOrigin.setX (0);
  319. vbar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
  320. vbar.setRangeLimits (0.0, contentBounds.getHeight());
  321. vbar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
  322. vbar.setSingleStepSize (singleStepY);
  323. vbar.cancelPendingUpdate();
  324. if (canShowVBar && ! vBarVisible)
  325. visibleOrigin.setY (0);
  326. // Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
  327. hbar.setVisible (hBarVisible);
  328. vbar.setVisible (vBarVisible);
  329. if (contentComp != nullptr)
  330. {
  331. auto newContentCompPos = viewportPosToCompPos (visibleOrigin);
  332. if (contentComp->getBounds().getPosition() != newContentCompPos)
  333. {
  334. contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
  335. return;
  336. }
  337. }
  338. const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
  339. jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
  340. jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
  341. if (lastVisibleArea != visibleArea)
  342. {
  343. lastVisibleArea = visibleArea;
  344. visibleAreaChanged (visibleArea);
  345. }
  346. hbar.handleUpdateNowIfNeeded();
  347. vbar.handleUpdateNowIfNeeded();
  348. }
  349. //==============================================================================
  350. void Viewport::setSingleStepSizes (const int stepX, const int stepY)
  351. {
  352. if (singleStepX != stepX || singleStepY != stepY)
  353. {
  354. singleStepX = stepX;
  355. singleStepY = stepY;
  356. updateVisibleArea();
  357. }
  358. }
  359. void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
  360. const bool showHorizontalScrollbarIfNeeded,
  361. const bool allowVerticalScrollingWithoutScrollbar,
  362. const bool allowHorizontalScrollingWithoutScrollbar)
  363. {
  364. allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
  365. allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
  366. if (showVScrollbar != showVerticalScrollbarIfNeeded
  367. || showHScrollbar != showHorizontalScrollbarIfNeeded)
  368. {
  369. showVScrollbar = showVerticalScrollbarIfNeeded;
  370. showHScrollbar = showHorizontalScrollbarIfNeeded;
  371. updateVisibleArea();
  372. }
  373. }
  374. void Viewport::setScrollBarThickness (const int thickness)
  375. {
  376. int newThickness;
  377. // To stay compatible with the previous code: use the
  378. // default thickness if thickness parameter is zero
  379. // or negative
  380. if (thickness <= 0)
  381. {
  382. customScrollBarThickness = false;
  383. newThickness = getLookAndFeel().getDefaultScrollbarWidth();
  384. }
  385. else
  386. {
  387. customScrollBarThickness = true;
  388. newThickness = thickness;
  389. }
  390. if (scrollBarThickness != newThickness)
  391. {
  392. scrollBarThickness = newThickness;
  393. updateVisibleArea();
  394. }
  395. }
  396. int Viewport::getScrollBarThickness() const
  397. {
  398. return scrollBarThickness;
  399. }
  400. void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
  401. {
  402. const int newRangeStartInt = roundToInt (newRangeStart);
  403. if (scrollBarThatHasMoved == horizontalScrollBar)
  404. {
  405. setViewPosition (newRangeStartInt, getViewPositionY());
  406. }
  407. else if (scrollBarThatHasMoved == verticalScrollBar)
  408. {
  409. setViewPosition (getViewPositionX(), newRangeStartInt);
  410. }
  411. }
  412. void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  413. {
  414. if (! useMouseWheelMoveIfNeeded (e, wheel))
  415. Component::mouseWheelMove (e, wheel);
  416. }
  417. static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
  418. {
  419. if (distance == 0.0f)
  420. return 0;
  421. distance *= 14.0f * singleStepSize;
  422. return roundToInt (distance < 0 ? jmin (distance, -1.0f)
  423. : jmax (distance, 1.0f));
  424. }
  425. bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
  426. {
  427. if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
  428. {
  429. const bool canScrollVert = (allowScrollingWithoutScrollbarV || getVerticalScrollBar().isVisible());
  430. const bool canScrollHorz = (allowScrollingWithoutScrollbarH || getHorizontalScrollBar().isVisible());
  431. if (canScrollHorz || canScrollVert)
  432. {
  433. auto deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
  434. auto deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
  435. auto pos = getViewPosition();
  436. if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
  437. {
  438. pos.x -= deltaX;
  439. pos.y -= deltaY;
  440. }
  441. else if (canScrollHorz && (deltaX != 0 || e.mods.isShiftDown() || ! canScrollVert))
  442. {
  443. pos.x -= deltaX != 0 ? deltaX : deltaY;
  444. }
  445. else if (canScrollVert && deltaY != 0)
  446. {
  447. pos.y -= deltaY;
  448. }
  449. if (pos != getViewPosition())
  450. {
  451. setViewPosition (pos);
  452. return true;
  453. }
  454. }
  455. }
  456. return false;
  457. }
  458. static bool isUpDownKeyPress (const KeyPress& key)
  459. {
  460. return key == KeyPress::upKey
  461. || key == KeyPress::downKey
  462. || key == KeyPress::pageUpKey
  463. || key == KeyPress::pageDownKey
  464. || key == KeyPress::homeKey
  465. || key == KeyPress::endKey;
  466. }
  467. static bool isLeftRightKeyPress (const KeyPress& key)
  468. {
  469. return key == KeyPress::leftKey
  470. || key == KeyPress::rightKey;
  471. }
  472. bool Viewport::keyPressed (const KeyPress& key)
  473. {
  474. const bool isUpDownKey = isUpDownKeyPress (key);
  475. if (getVerticalScrollBar().isVisible() && isUpDownKey)
  476. return getVerticalScrollBar().keyPressed (key);
  477. const bool isLeftRightKey = isLeftRightKeyPress (key);
  478. if (getHorizontalScrollBar().isVisible() && (isUpDownKey || isLeftRightKey))
  479. return getHorizontalScrollBar().keyPressed (key);
  480. return false;
  481. }
  482. bool Viewport::respondsToKey (const KeyPress& key)
  483. {
  484. return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
  485. }
  486. ScrollBar* Viewport::createScrollBarComponent (bool isVertical)
  487. {
  488. return new ScrollBar (isVertical);
  489. }
  490. } // namespace juce