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.

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