Audio plugin host https://kx.studio/carla
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.

juce_Viewport.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 = createScrollBarComponent (true));
  28. addChildComponent (horizontalScrollBar = createScrollBarComponent (false));
  29. getVerticalScrollBar().addListener (this);
  30. getHorizontalScrollBar().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 (getHorizontalScrollBar().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 (getVerticalScrollBar().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. auto 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 && ! getHorizontalScrollBar().autoHides();
  267. vBarVisible = canShowVBar && ! getVerticalScrollBar().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. auto& hbar = getHorizontalScrollBar();
  301. auto& vbar = getVerticalScrollBar();
  302. hbar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
  303. hbar.setRangeLimits (0.0, contentBounds.getWidth());
  304. hbar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
  305. hbar.setSingleStepSize (singleStepX);
  306. hbar.cancelPendingUpdate();
  307. if (canShowHBar && ! hBarVisible)
  308. visibleOrigin.setX (0);
  309. vbar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
  310. vbar.setRangeLimits (0.0, contentBounds.getHeight());
  311. vbar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
  312. vbar.setSingleStepSize (singleStepY);
  313. vbar.cancelPendingUpdate();
  314. if (canShowVBar && ! vBarVisible)
  315. visibleOrigin.setY (0);
  316. // Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
  317. hbar.setVisible (hBarVisible);
  318. vbar.setVisible (vBarVisible);
  319. if (contentComp != nullptr)
  320. {
  321. auto newContentCompPos = viewportPosToCompPos (visibleOrigin);
  322. if (contentComp->getBounds().getPosition() != newContentCompPos)
  323. {
  324. contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
  325. return;
  326. }
  327. }
  328. const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
  329. jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
  330. jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
  331. if (lastVisibleArea != visibleArea)
  332. {
  333. lastVisibleArea = visibleArea;
  334. visibleAreaChanged (visibleArea);
  335. }
  336. hbar.handleUpdateNowIfNeeded();
  337. vbar.handleUpdateNowIfNeeded();
  338. }
  339. //==============================================================================
  340. void Viewport::setSingleStepSizes (const int stepX, const int stepY)
  341. {
  342. if (singleStepX != stepX || singleStepY != stepY)
  343. {
  344. singleStepX = stepX;
  345. singleStepY = stepY;
  346. updateVisibleArea();
  347. }
  348. }
  349. void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
  350. const bool showHorizontalScrollbarIfNeeded,
  351. const bool allowVerticalScrollingWithoutScrollbar,
  352. const bool allowHorizontalScrollingWithoutScrollbar)
  353. {
  354. allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
  355. allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
  356. if (showVScrollbar != showVerticalScrollbarIfNeeded
  357. || showHScrollbar != showHorizontalScrollbarIfNeeded)
  358. {
  359. showVScrollbar = showVerticalScrollbarIfNeeded;
  360. showHScrollbar = showHorizontalScrollbarIfNeeded;
  361. updateVisibleArea();
  362. }
  363. }
  364. void Viewport::setScrollBarThickness (const int thickness)
  365. {
  366. int newThickness;
  367. // To stay compatible with the previous code: use the
  368. // default thickness if thickness parameter is zero
  369. // or negative
  370. if (thickness <= 0)
  371. {
  372. customScrollBarThickness = false;
  373. newThickness = getLookAndFeel().getDefaultScrollbarWidth();
  374. }
  375. else
  376. {
  377. customScrollBarThickness = true;
  378. newThickness = thickness;
  379. }
  380. if (scrollBarThickness != newThickness)
  381. {
  382. scrollBarThickness = newThickness;
  383. updateVisibleArea();
  384. }
  385. }
  386. int Viewport::getScrollBarThickness() const
  387. {
  388. return scrollBarThickness;
  389. }
  390. void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
  391. {
  392. const int newRangeStartInt = roundToInt (newRangeStart);
  393. if (scrollBarThatHasMoved == horizontalScrollBar)
  394. {
  395. setViewPosition (newRangeStartInt, getViewPositionY());
  396. }
  397. else if (scrollBarThatHasMoved == verticalScrollBar)
  398. {
  399. setViewPosition (getViewPositionX(), newRangeStartInt);
  400. }
  401. }
  402. void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  403. {
  404. if (! useMouseWheelMoveIfNeeded (e, wheel))
  405. Component::mouseWheelMove (e, wheel);
  406. }
  407. static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
  408. {
  409. if (distance == 0.0f)
  410. return 0;
  411. distance *= 14.0f * singleStepSize;
  412. return roundToInt (distance < 0 ? jmin (distance, -1.0f)
  413. : jmax (distance, 1.0f));
  414. }
  415. bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
  416. {
  417. if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
  418. {
  419. const bool canScrollVert = (allowScrollingWithoutScrollbarV || getVerticalScrollBar().isVisible());
  420. const bool canScrollHorz = (allowScrollingWithoutScrollbarH || getHorizontalScrollBar().isVisible());
  421. if (canScrollHorz || canScrollVert)
  422. {
  423. auto deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
  424. auto deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
  425. auto pos = getViewPosition();
  426. if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
  427. {
  428. pos.x -= deltaX;
  429. pos.y -= deltaY;
  430. }
  431. else if (canScrollHorz && (deltaX != 0 || e.mods.isShiftDown() || ! canScrollVert))
  432. {
  433. pos.x -= deltaX != 0 ? deltaX : deltaY;
  434. }
  435. else if (canScrollVert && deltaY != 0)
  436. {
  437. pos.y -= deltaY;
  438. }
  439. if (pos != getViewPosition())
  440. {
  441. setViewPosition (pos);
  442. return true;
  443. }
  444. }
  445. }
  446. return false;
  447. }
  448. static bool isUpDownKeyPress (const KeyPress& key)
  449. {
  450. return key == KeyPress::upKey
  451. || key == KeyPress::downKey
  452. || key == KeyPress::pageUpKey
  453. || key == KeyPress::pageDownKey
  454. || key == KeyPress::homeKey
  455. || key == KeyPress::endKey;
  456. }
  457. static bool isLeftRightKeyPress (const KeyPress& key)
  458. {
  459. return key == KeyPress::leftKey
  460. || key == KeyPress::rightKey;
  461. }
  462. bool Viewport::keyPressed (const KeyPress& key)
  463. {
  464. const bool isUpDownKey = isUpDownKeyPress (key);
  465. if (getVerticalScrollBar().isVisible() && isUpDownKey)
  466. return getVerticalScrollBar().keyPressed (key);
  467. const bool isLeftRightKey = isLeftRightKeyPress (key);
  468. if (getHorizontalScrollBar().isVisible() && (isUpDownKey || isLeftRightKey))
  469. return getHorizontalScrollBar().keyPressed (key);
  470. return false;
  471. }
  472. bool Viewport::respondsToKey (const KeyPress& key)
  473. {
  474. return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
  475. }
  476. ScrollBar* Viewport::createScrollBarComponent (bool isVertical)
  477. {
  478. return new ScrollBar (isVertical);
  479. }
  480. } // namespace juce