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.

564 lines
19KB

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