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.

581 lines
19KB

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