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.

464 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. allowScrollingWithoutScrollbarV (false),
  26. allowScrollingWithoutScrollbarH (false),
  27. verticalScrollBar (true),
  28. horizontalScrollBar (false)
  29. {
  30. // content holder is used to clip the contents so they don't overlap the scrollbars
  31. addAndMakeVisible (contentHolder);
  32. contentHolder.setInterceptsMouseClicks (false, true);
  33. addChildComponent (verticalScrollBar);
  34. addChildComponent (horizontalScrollBar);
  35. verticalScrollBar.addListener (this);
  36. horizontalScrollBar.addListener (this);
  37. setInterceptsMouseClicks (false, true);
  38. setWantsKeyboardFocus (true);
  39. }
  40. Viewport::~Viewport()
  41. {
  42. deleteContentComp();
  43. mouseWheelTimer = nullptr;
  44. }
  45. //==============================================================================
  46. void Viewport::visibleAreaChanged (const Rectangle<int>&) {}
  47. void Viewport::viewedComponentChanged (Component*) {}
  48. //==============================================================================
  49. void Viewport::deleteContentComp()
  50. {
  51. if (contentComp != nullptr)
  52. contentComp->removeComponentListener (this);
  53. if (deleteContent)
  54. {
  55. // This sets the content comp to a null pointer before deleting the old one, in case
  56. // anything tries to use the old one while it's in mid-deletion..
  57. ScopedPointer<Component> oldCompDeleter (contentComp);
  58. }
  59. else
  60. {
  61. contentComp = nullptr;
  62. }
  63. }
  64. void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
  65. {
  66. if (contentComp.get() != newViewedComponent)
  67. {
  68. deleteContentComp();
  69. contentComp = newViewedComponent;
  70. deleteContent = deleteComponentWhenNoLongerNeeded;
  71. if (contentComp != nullptr)
  72. {
  73. contentHolder.addAndMakeVisible (contentComp);
  74. setViewPosition (Point<int>());
  75. contentComp->addComponentListener (this);
  76. }
  77. viewedComponentChanged (contentComp);
  78. updateVisibleArea();
  79. }
  80. }
  81. int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
  82. int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
  83. Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
  84. {
  85. jassert (contentComp != nullptr);
  86. return Point<int> (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -(pos.x))),
  87. jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -(pos.y))));
  88. }
  89. void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
  90. {
  91. setViewPosition (Point<int> (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() || contentComp->getX() < 0 || contentComp->getRight() > getWidth())
  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() || contentComp->getY() < 0 || contentComp->getBottom() > getHeight())
  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. void Viewport::resized()
  145. {
  146. updateVisibleArea();
  147. }
  148. //==============================================================================
  149. void Viewport::updateVisibleArea()
  150. {
  151. const int scrollbarWidth = getScrollBarThickness();
  152. const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
  153. const bool canShowHBar = showHScrollbar && canShowAnyBars;
  154. const bool canShowVBar = showVScrollbar && canShowAnyBars;
  155. bool hBarVisible = false, vBarVisible = false;
  156. Rectangle<int> contentArea;
  157. for (int i = 3; --i >= 0;)
  158. {
  159. hBarVisible = canShowHBar && ! horizontalScrollBar.autoHides();
  160. vBarVisible = canShowVBar && ! verticalScrollBar.autoHides();
  161. contentArea = getLocalBounds();
  162. if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
  163. {
  164. hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
  165. vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());
  166. if (vBarVisible)
  167. contentArea.setWidth (getWidth() - scrollbarWidth);
  168. if (hBarVisible)
  169. contentArea.setHeight (getHeight() - scrollbarWidth);
  170. if (! contentArea.contains (contentComp->getBounds()))
  171. {
  172. hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
  173. vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
  174. }
  175. }
  176. if (vBarVisible) contentArea.setWidth (getWidth() - scrollbarWidth);
  177. if (hBarVisible) contentArea.setHeight (getHeight() - scrollbarWidth);
  178. if (contentComp == nullptr)
  179. {
  180. contentHolder.setBounds (contentArea);
  181. break;
  182. }
  183. const Rectangle<int> oldContentBounds (contentComp->getBounds());
  184. contentHolder.setBounds (contentArea);
  185. // If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate..
  186. if (oldContentBounds == contentComp->getBounds())
  187. break;
  188. }
  189. Rectangle<int> contentBounds;
  190. if (contentComp != nullptr)
  191. contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
  192. Point<int> visibleOrigin (-contentBounds.getPosition());
  193. horizontalScrollBar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
  194. horizontalScrollBar.setRangeLimits (0.0, contentBounds.getWidth());
  195. horizontalScrollBar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
  196. horizontalScrollBar.setSingleStepSize (singleStepX);
  197. horizontalScrollBar.cancelPendingUpdate();
  198. if (canShowHBar && ! hBarVisible)
  199. visibleOrigin.setX (0);
  200. verticalScrollBar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
  201. verticalScrollBar.setRangeLimits (0.0, contentBounds.getHeight());
  202. verticalScrollBar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
  203. verticalScrollBar.setSingleStepSize (singleStepY);
  204. verticalScrollBar.cancelPendingUpdate();
  205. if (canShowVBar && ! vBarVisible)
  206. visibleOrigin.setY (0);
  207. // Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
  208. horizontalScrollBar.setVisible (hBarVisible);
  209. verticalScrollBar.setVisible (vBarVisible);
  210. if (contentComp != nullptr)
  211. {
  212. const Point<int> newContentCompPos (viewportPosToCompPos (visibleOrigin));
  213. if (contentComp->getBounds().getPosition() != newContentCompPos)
  214. {
  215. contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
  216. return;
  217. }
  218. }
  219. const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
  220. jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
  221. jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
  222. if (lastVisibleArea != visibleArea)
  223. {
  224. lastVisibleArea = visibleArea;
  225. visibleAreaChanged (visibleArea);
  226. }
  227. horizontalScrollBar.handleUpdateNowIfNeeded();
  228. verticalScrollBar.handleUpdateNowIfNeeded();
  229. }
  230. //==============================================================================
  231. void Viewport::setSingleStepSizes (const int stepX, const int stepY)
  232. {
  233. if (singleStepX != stepX || singleStepY != stepY)
  234. {
  235. singleStepX = stepX;
  236. singleStepY = stepY;
  237. updateVisibleArea();
  238. }
  239. }
  240. void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
  241. const bool showHorizontalScrollbarIfNeeded,
  242. const bool allowVerticalScrollingWithoutScrollbar,
  243. const bool allowHorizontalScrollingWithoutScrollbar)
  244. {
  245. allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
  246. allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
  247. if (showVScrollbar != showVerticalScrollbarIfNeeded
  248. || showHScrollbar != showHorizontalScrollbarIfNeeded)
  249. {
  250. showVScrollbar = showVerticalScrollbarIfNeeded;
  251. showHScrollbar = showHorizontalScrollbarIfNeeded;
  252. updateVisibleArea();
  253. }
  254. }
  255. void Viewport::setScrollBarThickness (const int thickness)
  256. {
  257. if (scrollBarThickness != thickness)
  258. {
  259. scrollBarThickness = thickness;
  260. updateVisibleArea();
  261. }
  262. }
  263. int Viewport::getScrollBarThickness() const
  264. {
  265. return scrollBarThickness > 0 ? scrollBarThickness
  266. : getLookAndFeel().getDefaultScrollbarWidth();
  267. }
  268. void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
  269. {
  270. const int newRangeStartInt = roundToInt (newRangeStart);
  271. if (scrollBarThatHasMoved == &horizontalScrollBar)
  272. {
  273. setViewPosition (newRangeStartInt, getViewPositionY());
  274. }
  275. else if (scrollBarThatHasMoved == &verticalScrollBar)
  276. {
  277. setViewPosition (getViewPositionX(), newRangeStartInt);
  278. }
  279. }
  280. void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  281. {
  282. if (! useMouseWheelMoveIfNeeded (e, wheel))
  283. Component::mouseWheelMove (e, wheel);
  284. }
  285. static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
  286. {
  287. if (distance == 0)
  288. return 0;
  289. distance *= 14.0f * singleStepSize;
  290. return roundToInt (distance < 0 ? jmin (distance, -1.0f)
  291. : jmax (distance, 1.0f));
  292. }
  293. // This puts a temporary component overlay over the content component, to prevent
  294. // wheel events from reaching components inside it, so that while spinning a wheel
  295. // with momentum, it won't accidentally scroll any subcomponents of the viewport.
  296. struct Viewport::MouseWheelTimer : public Timer
  297. {
  298. MouseWheelTimer (Viewport& v) : viewport (v)
  299. {
  300. viewport.contentHolder.addAndMakeVisible (dummyOverlay);
  301. dummyOverlay.setAlwaysOnTop (true);
  302. dummyOverlay.setPaintingIsUnclipped (true);
  303. dummyOverlay.setBounds (viewport.contentHolder.getLocalBounds());
  304. }
  305. void timerCallback() override
  306. {
  307. viewport.mouseWheelTimer = nullptr;
  308. }
  309. Component dummyOverlay;
  310. Viewport& viewport;
  311. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseWheelTimer)
  312. };
  313. bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
  314. {
  315. if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
  316. {
  317. const bool canScrollVert = (allowScrollingWithoutScrollbarV || verticalScrollBar.isVisible());
  318. const bool canScrollHorz = (allowScrollingWithoutScrollbarH || horizontalScrollBar.isVisible());
  319. if (canScrollHorz || canScrollVert)
  320. {
  321. const int deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
  322. const int deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
  323. Point<int> pos (getViewPosition());
  324. if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
  325. {
  326. pos.x -= deltaX;
  327. pos.y -= deltaY;
  328. }
  329. else if (canScrollHorz && (deltaX != 0 || e.mods.isShiftDown() || ! canScrollVert))
  330. {
  331. pos.x -= deltaX != 0 ? deltaX : deltaY;
  332. }
  333. else if (canScrollVert && deltaY != 0)
  334. {
  335. pos.y -= deltaY;
  336. }
  337. if (pos != getViewPosition())
  338. {
  339. if (mouseWheelTimer == nullptr)
  340. mouseWheelTimer = new MouseWheelTimer (*this);
  341. mouseWheelTimer->startTimer (300);
  342. setViewPosition (pos);
  343. return true;
  344. }
  345. }
  346. }
  347. return false;
  348. }
  349. static bool isUpDownKeyPress (const KeyPress& key)
  350. {
  351. return key == KeyPress::upKey
  352. || key == KeyPress::downKey
  353. || key == KeyPress::pageUpKey
  354. || key == KeyPress::pageDownKey
  355. || key == KeyPress::homeKey
  356. || key == KeyPress::endKey;
  357. }
  358. static bool isLeftRightKeyPress (const KeyPress& key)
  359. {
  360. return key == KeyPress::leftKey
  361. || key == KeyPress::rightKey;
  362. }
  363. bool Viewport::keyPressed (const KeyPress& key)
  364. {
  365. const bool isUpDownKey = isUpDownKeyPress (key);
  366. if (verticalScrollBar.isVisible() && isUpDownKey)
  367. return verticalScrollBar.keyPressed (key);
  368. const bool isLeftRightKey = isLeftRightKeyPress (key);
  369. if (horizontalScrollBar.isVisible() && (isUpDownKey || isLeftRightKey))
  370. return horizontalScrollBar.keyPressed (key);
  371. return false;
  372. }
  373. bool Viewport::respondsToKey (const KeyPress& key)
  374. {
  375. return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
  376. }