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.

601 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.reset();
  82. horizontalScrollBar.reset();
  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&) override
  179. {
  180. offsetX.setPosition (offsetX.getPosition());
  181. offsetY.setPosition (offsetY.getPosition());
  182. ++numTouches;
  183. }
  184. void mouseDrag (const MouseEvent& e) override
  185. {
  186. if (numTouches == 1 && ! doesMouseEventComponentBlockViewportDrag (e.eventComponent))
  187. {
  188. auto totalOffset = e.getOffsetFromDragStart().toFloat();
  189. if (! isDragging && totalOffset.getDistanceFromOrigin() > 8.0f)
  190. {
  191. isDragging = true;
  192. originalViewPos = viewport.getViewPosition();
  193. offsetX.setPosition (0.0);
  194. offsetX.beginDrag();
  195. offsetY.setPosition (0.0);
  196. offsetY.beginDrag();
  197. }
  198. if (isDragging)
  199. {
  200. offsetX.drag (totalOffset.x);
  201. offsetY.drag (totalOffset.y);
  202. }
  203. }
  204. }
  205. void mouseUp (const MouseEvent&) override
  206. {
  207. if (--numTouches <= 0)
  208. {
  209. offsetX.endDrag();
  210. offsetY.endDrag();
  211. isDragging = false;
  212. numTouches = 0;
  213. }
  214. }
  215. bool doesMouseEventComponentBlockViewportDrag (const Component* eventComp)
  216. {
  217. for (auto c = eventComp; c != nullptr && c != &viewport; c = c->getParentComponent())
  218. if (c->getViewportIgnoreDragFlag())
  219. return true;
  220. return false;
  221. }
  222. Viewport& viewport;
  223. ViewportDragPosition offsetX, offsetY;
  224. Point<int> originalViewPos;
  225. int numTouches = 0;
  226. bool isDragging = false;
  227. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragToScrollListener)
  228. };
  229. void Viewport::setScrollOnDragEnabled (bool shouldScrollOnDrag)
  230. {
  231. if (isScrollOnDragEnabled() != shouldScrollOnDrag)
  232. {
  233. if (shouldScrollOnDrag)
  234. dragToScrollListener = new DragToScrollListener (*this);
  235. else
  236. dragToScrollListener.reset();
  237. }
  238. }
  239. bool Viewport::isScrollOnDragEnabled() const noexcept
  240. {
  241. return dragToScrollListener != nullptr;
  242. }
  243. bool Viewport::isCurrentlyScrollingOnDrag() const noexcept
  244. {
  245. return dragToScrollListener != nullptr && dragToScrollListener->isDragging;
  246. }
  247. //==============================================================================
  248. void Viewport::lookAndFeelChanged()
  249. {
  250. if (! customScrollBarThickness)
  251. {
  252. scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
  253. resized();
  254. }
  255. }
  256. void Viewport::resized()
  257. {
  258. updateVisibleArea();
  259. }
  260. //==============================================================================
  261. void Viewport::updateVisibleArea()
  262. {
  263. auto scrollbarWidth = getScrollBarThickness();
  264. const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
  265. const bool canShowHBar = showHScrollbar && canShowAnyBars;
  266. const bool canShowVBar = showVScrollbar && canShowAnyBars;
  267. bool hBarVisible = false, vBarVisible = false;
  268. Rectangle<int> contentArea;
  269. for (int i = 3; --i >= 0;)
  270. {
  271. hBarVisible = canShowHBar && ! getHorizontalScrollBar().autoHides();
  272. vBarVisible = canShowVBar && ! getVerticalScrollBar().autoHides();
  273. contentArea = getLocalBounds();
  274. if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
  275. {
  276. hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
  277. vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());
  278. if (vBarVisible)
  279. contentArea.setWidth (getWidth() - scrollbarWidth);
  280. if (hBarVisible)
  281. contentArea.setHeight (getHeight() - scrollbarWidth);
  282. if (! contentArea.contains (contentComp->getBounds()))
  283. {
  284. hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
  285. vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
  286. }
  287. }
  288. if (vBarVisible) contentArea.setWidth (getWidth() - scrollbarWidth);
  289. if (hBarVisible) contentArea.setHeight (getHeight() - scrollbarWidth);
  290. if (contentComp == nullptr)
  291. {
  292. contentHolder.setBounds (contentArea);
  293. break;
  294. }
  295. auto oldContentBounds = contentComp->getBounds();
  296. contentHolder.setBounds (contentArea);
  297. // If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate..
  298. if (oldContentBounds == contentComp->getBounds())
  299. break;
  300. }
  301. Rectangle<int> contentBounds;
  302. if (contentComp != nullptr)
  303. contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
  304. auto visibleOrigin = -contentBounds.getPosition();
  305. auto& hbar = getHorizontalScrollBar();
  306. auto& vbar = getVerticalScrollBar();
  307. hbar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
  308. hbar.setRangeLimits (0.0, contentBounds.getWidth());
  309. hbar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
  310. hbar.setSingleStepSize (singleStepX);
  311. hbar.cancelPendingUpdate();
  312. if (canShowHBar && ! hBarVisible)
  313. visibleOrigin.setX (0);
  314. vbar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
  315. vbar.setRangeLimits (0.0, contentBounds.getHeight());
  316. vbar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
  317. vbar.setSingleStepSize (singleStepY);
  318. vbar.cancelPendingUpdate();
  319. if (canShowVBar && ! vBarVisible)
  320. visibleOrigin.setY (0);
  321. // Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
  322. hbar.setVisible (hBarVisible);
  323. vbar.setVisible (vBarVisible);
  324. if (contentComp != nullptr)
  325. {
  326. auto newContentCompPos = viewportPosToCompPos (visibleOrigin);
  327. if (contentComp->getBounds().getPosition() != newContentCompPos)
  328. {
  329. contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
  330. return;
  331. }
  332. }
  333. const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
  334. jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
  335. jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
  336. if (lastVisibleArea != visibleArea)
  337. {
  338. lastVisibleArea = visibleArea;
  339. visibleAreaChanged (visibleArea);
  340. }
  341. hbar.handleUpdateNowIfNeeded();
  342. vbar.handleUpdateNowIfNeeded();
  343. }
  344. //==============================================================================
  345. void Viewport::setSingleStepSizes (const int stepX, const int stepY)
  346. {
  347. if (singleStepX != stepX || singleStepY != stepY)
  348. {
  349. singleStepX = stepX;
  350. singleStepY = stepY;
  351. updateVisibleArea();
  352. }
  353. }
  354. void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
  355. const bool showHorizontalScrollbarIfNeeded,
  356. const bool allowVerticalScrollingWithoutScrollbar,
  357. const bool allowHorizontalScrollingWithoutScrollbar)
  358. {
  359. allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
  360. allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
  361. if (showVScrollbar != showVerticalScrollbarIfNeeded
  362. || showHScrollbar != showHorizontalScrollbarIfNeeded)
  363. {
  364. showVScrollbar = showVerticalScrollbarIfNeeded;
  365. showHScrollbar = showHorizontalScrollbarIfNeeded;
  366. updateVisibleArea();
  367. }
  368. }
  369. void Viewport::setScrollBarThickness (const int thickness)
  370. {
  371. int newThickness;
  372. // To stay compatible with the previous code: use the
  373. // default thickness if thickness parameter is zero
  374. // or negative
  375. if (thickness <= 0)
  376. {
  377. customScrollBarThickness = false;
  378. newThickness = getLookAndFeel().getDefaultScrollbarWidth();
  379. }
  380. else
  381. {
  382. customScrollBarThickness = true;
  383. newThickness = thickness;
  384. }
  385. if (scrollBarThickness != newThickness)
  386. {
  387. scrollBarThickness = newThickness;
  388. updateVisibleArea();
  389. }
  390. }
  391. int Viewport::getScrollBarThickness() const
  392. {
  393. return scrollBarThickness;
  394. }
  395. void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
  396. {
  397. const int newRangeStartInt = roundToInt (newRangeStart);
  398. if (scrollBarThatHasMoved == horizontalScrollBar)
  399. {
  400. setViewPosition (newRangeStartInt, getViewPositionY());
  401. }
  402. else if (scrollBarThatHasMoved == verticalScrollBar)
  403. {
  404. setViewPosition (getViewPositionX(), newRangeStartInt);
  405. }
  406. }
  407. void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  408. {
  409. if (! useMouseWheelMoveIfNeeded (e, wheel))
  410. Component::mouseWheelMove (e, wheel);
  411. }
  412. static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
  413. {
  414. if (distance == 0.0f)
  415. return 0;
  416. distance *= 14.0f * singleStepSize;
  417. return roundToInt (distance < 0 ? jmin (distance, -1.0f)
  418. : jmax (distance, 1.0f));
  419. }
  420. bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
  421. {
  422. if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
  423. {
  424. const bool canScrollVert = (allowScrollingWithoutScrollbarV || getVerticalScrollBar().isVisible());
  425. const bool canScrollHorz = (allowScrollingWithoutScrollbarH || getHorizontalScrollBar().isVisible());
  426. if (canScrollHorz || canScrollVert)
  427. {
  428. auto deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
  429. auto deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
  430. auto pos = getViewPosition();
  431. if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
  432. {
  433. pos.x -= deltaX;
  434. pos.y -= deltaY;
  435. }
  436. else if (canScrollHorz && (deltaX != 0 || e.mods.isShiftDown() || ! canScrollVert))
  437. {
  438. pos.x -= deltaX != 0 ? deltaX : deltaY;
  439. }
  440. else if (canScrollVert && deltaY != 0)
  441. {
  442. pos.y -= deltaY;
  443. }
  444. if (pos != getViewPosition())
  445. {
  446. setViewPosition (pos);
  447. return true;
  448. }
  449. }
  450. }
  451. return false;
  452. }
  453. static bool isUpDownKeyPress (const KeyPress& key)
  454. {
  455. return key == KeyPress::upKey
  456. || key == KeyPress::downKey
  457. || key == KeyPress::pageUpKey
  458. || key == KeyPress::pageDownKey
  459. || key == KeyPress::homeKey
  460. || key == KeyPress::endKey;
  461. }
  462. static bool isLeftRightKeyPress (const KeyPress& key)
  463. {
  464. return key == KeyPress::leftKey
  465. || key == KeyPress::rightKey;
  466. }
  467. bool Viewport::keyPressed (const KeyPress& key)
  468. {
  469. const bool isUpDownKey = isUpDownKeyPress (key);
  470. if (getVerticalScrollBar().isVisible() && isUpDownKey)
  471. return getVerticalScrollBar().keyPressed (key);
  472. const bool isLeftRightKey = isLeftRightKeyPress (key);
  473. if (getHorizontalScrollBar().isVisible() && (isUpDownKey || isLeftRightKey))
  474. return getHorizontalScrollBar().keyPressed (key);
  475. return false;
  476. }
  477. bool Viewport::respondsToKey (const KeyPress& key)
  478. {
  479. return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
  480. }
  481. ScrollBar* Viewport::createScrollBarComponent (bool isVertical)
  482. {
  483. return new ScrollBar (isVertical);
  484. }
  485. } // namespace juce