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.

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