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.

603 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. 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, 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. }
  175. void positionChanged (ViewportDragPosition&, double) override
  176. {
  177. viewport.setViewPosition (originalViewPos - Point<int> ((int) offsetX.getPosition(),
  178. (int) offsetY.getPosition()));
  179. }
  180. void mouseDown (const MouseEvent&) override
  181. {
  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 && ! doesMouseEventComponentBlockViewportDrag (e.eventComponent))
  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&) override
  208. {
  209. if (--numTouches <= 0)
  210. {
  211. offsetX.endDrag();
  212. offsetY.endDrag();
  213. isDragging = false;
  214. numTouches = 0;
  215. }
  216. }
  217. bool doesMouseEventComponentBlockViewportDrag (const Component* eventComp)
  218. {
  219. for (auto c = eventComp; c != nullptr && c != &viewport; c = c->getParentComponent())
  220. if (c->getViewportIgnoreDragFlag())
  221. return true;
  222. return false;
  223. }
  224. Viewport& viewport;
  225. ViewportDragPosition offsetX, offsetY;
  226. Point<int> originalViewPos;
  227. int numTouches = 0;
  228. bool isDragging = 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.reset (new DragToScrollListener (*this));
  237. else
  238. dragToScrollListener.reset();
  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. auto newRangeStartInt = roundToInt (newRangeStart);
  400. if (scrollBarThatHasMoved == horizontalScrollBar.get())
  401. {
  402. setViewPosition (newRangeStartInt, getViewPositionY());
  403. }
  404. else if (scrollBarThatHasMoved == verticalScrollBar.get())
  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