juce_Viewport.cpp 21 KB

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