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.

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