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.

437 lines
15KB

  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. }
  44. //==============================================================================
  45. void Viewport::visibleAreaChanged (const Rectangle<int>&) {}
  46. void Viewport::viewedComponentChanged (Component*) {}
  47. //==============================================================================
  48. void Viewport::deleteContentComp()
  49. {
  50. if (contentComp != nullptr)
  51. contentComp->removeComponentListener (this);
  52. if (deleteContent)
  53. {
  54. // This sets the content comp to a null pointer before deleting the old one, in case
  55. // anything tries to use the old one while it's in mid-deletion..
  56. ScopedPointer<Component> oldCompDeleter (contentComp);
  57. }
  58. else
  59. {
  60. contentComp = nullptr;
  61. }
  62. }
  63. void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
  64. {
  65. if (contentComp.get() != newViewedComponent)
  66. {
  67. deleteContentComp();
  68. contentComp = newViewedComponent;
  69. deleteContent = deleteComponentWhenNoLongerNeeded;
  70. if (contentComp != nullptr)
  71. {
  72. contentHolder.addAndMakeVisible (contentComp);
  73. setViewPosition (Point<int>());
  74. contentComp->addComponentListener (this);
  75. }
  76. viewedComponentChanged (contentComp);
  77. updateVisibleArea();
  78. }
  79. }
  80. int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
  81. int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
  82. Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
  83. {
  84. jassert (contentComp != nullptr);
  85. return Point<int> (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -(pos.x))),
  86. jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -(pos.y))));
  87. }
  88. void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
  89. {
  90. setViewPosition (Point<int> (xPixelsOffset, yPixelsOffset));
  91. }
  92. void Viewport::setViewPosition (Point<int> newPosition)
  93. {
  94. if (contentComp != nullptr)
  95. contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition));
  96. }
  97. void Viewport::setViewPositionProportionately (const double x, const double y)
  98. {
  99. if (contentComp != nullptr)
  100. setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
  101. jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
  102. }
  103. bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
  104. {
  105. if (contentComp != nullptr)
  106. {
  107. int dx = 0, dy = 0;
  108. if (horizontalScrollBar.isVisible() || contentComp->getX() < 0 || contentComp->getRight() > getWidth())
  109. {
  110. if (mouseX < activeBorderThickness)
  111. dx = activeBorderThickness - mouseX;
  112. else if (mouseX >= contentHolder.getWidth() - activeBorderThickness)
  113. dx = (contentHolder.getWidth() - activeBorderThickness) - mouseX;
  114. if (dx < 0)
  115. dx = jmax (dx, -maximumSpeed, contentHolder.getWidth() - contentComp->getRight());
  116. else
  117. dx = jmin (dx, maximumSpeed, -contentComp->getX());
  118. }
  119. if (verticalScrollBar.isVisible() || contentComp->getY() < 0 || contentComp->getBottom() > getHeight())
  120. {
  121. if (mouseY < activeBorderThickness)
  122. dy = activeBorderThickness - mouseY;
  123. else if (mouseY >= contentHolder.getHeight() - activeBorderThickness)
  124. dy = (contentHolder.getHeight() - activeBorderThickness) - mouseY;
  125. if (dy < 0)
  126. dy = jmax (dy, -maximumSpeed, contentHolder.getHeight() - contentComp->getBottom());
  127. else
  128. dy = jmin (dy, maximumSpeed, -contentComp->getY());
  129. }
  130. if (dx != 0 || dy != 0)
  131. {
  132. contentComp->setTopLeftPosition (contentComp->getX() + dx,
  133. contentComp->getY() + dy);
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. void Viewport::componentMovedOrResized (Component&, bool, bool)
  140. {
  141. updateVisibleArea();
  142. }
  143. void Viewport::resized()
  144. {
  145. updateVisibleArea();
  146. }
  147. //==============================================================================
  148. void Viewport::updateVisibleArea()
  149. {
  150. const int scrollbarWidth = getScrollBarThickness();
  151. const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
  152. const bool canShowHBar = showHScrollbar && canShowAnyBars;
  153. const bool canShowVBar = showVScrollbar && canShowAnyBars;
  154. bool hBarVisible = false, vBarVisible = false;
  155. Rectangle<int> contentArea;
  156. for (int i = 3; --i >= 0;)
  157. {
  158. hBarVisible = canShowHBar && ! horizontalScrollBar.autoHides();
  159. vBarVisible = canShowVBar && ! verticalScrollBar.autoHides();
  160. contentArea = getLocalBounds();
  161. if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
  162. {
  163. hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
  164. vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());
  165. if (vBarVisible)
  166. contentArea.setWidth (getWidth() - scrollbarWidth);
  167. if (hBarVisible)
  168. contentArea.setHeight (getHeight() - scrollbarWidth);
  169. if (! contentArea.contains (contentComp->getBounds()))
  170. {
  171. hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
  172. vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
  173. }
  174. }
  175. if (vBarVisible) contentArea.setWidth (getWidth() - scrollbarWidth);
  176. if (hBarVisible) contentArea.setHeight (getHeight() - scrollbarWidth);
  177. if (contentComp == nullptr)
  178. {
  179. contentHolder.setBounds (contentArea);
  180. break;
  181. }
  182. const Rectangle<int> oldContentBounds (contentComp->getBounds());
  183. contentHolder.setBounds (contentArea);
  184. // If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate..
  185. if (oldContentBounds == contentComp->getBounds())
  186. break;
  187. }
  188. Rectangle<int> contentBounds;
  189. if (contentComp != nullptr)
  190. contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
  191. Point<int> visibleOrigin (-contentBounds.getPosition());
  192. horizontalScrollBar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
  193. horizontalScrollBar.setRangeLimits (0.0, contentBounds.getWidth());
  194. horizontalScrollBar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
  195. horizontalScrollBar.setSingleStepSize (singleStepX);
  196. horizontalScrollBar.cancelPendingUpdate();
  197. if (canShowHBar && ! hBarVisible)
  198. visibleOrigin.setX (0);
  199. verticalScrollBar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
  200. verticalScrollBar.setRangeLimits (0.0, contentBounds.getHeight());
  201. verticalScrollBar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
  202. verticalScrollBar.setSingleStepSize (singleStepY);
  203. verticalScrollBar.cancelPendingUpdate();
  204. if (canShowVBar && ! vBarVisible)
  205. visibleOrigin.setY (0);
  206. // Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
  207. horizontalScrollBar.setVisible (hBarVisible);
  208. verticalScrollBar.setVisible (vBarVisible);
  209. if (contentComp != nullptr)
  210. {
  211. const Point<int> newContentCompPos (viewportPosToCompPos (visibleOrigin));
  212. if (contentComp->getBounds().getPosition() != newContentCompPos)
  213. {
  214. contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
  215. return;
  216. }
  217. }
  218. const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
  219. jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
  220. jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
  221. if (lastVisibleArea != visibleArea)
  222. {
  223. lastVisibleArea = visibleArea;
  224. visibleAreaChanged (visibleArea);
  225. }
  226. horizontalScrollBar.handleUpdateNowIfNeeded();
  227. verticalScrollBar.handleUpdateNowIfNeeded();
  228. }
  229. //==============================================================================
  230. void Viewport::setSingleStepSizes (const int stepX, const int stepY)
  231. {
  232. if (singleStepX != stepX || singleStepY != stepY)
  233. {
  234. singleStepX = stepX;
  235. singleStepY = stepY;
  236. updateVisibleArea();
  237. }
  238. }
  239. void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
  240. const bool showHorizontalScrollbarIfNeeded,
  241. const bool allowVerticalScrollingWithoutScrollbar,
  242. const bool allowHorizontalScrollingWithoutScrollbar)
  243. {
  244. allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
  245. allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
  246. if (showVScrollbar != showVerticalScrollbarIfNeeded
  247. || showHScrollbar != showHorizontalScrollbarIfNeeded)
  248. {
  249. showVScrollbar = showVerticalScrollbarIfNeeded;
  250. showHScrollbar = showHorizontalScrollbarIfNeeded;
  251. updateVisibleArea();
  252. }
  253. }
  254. void Viewport::setScrollBarThickness (const int thickness)
  255. {
  256. if (scrollBarThickness != thickness)
  257. {
  258. scrollBarThickness = thickness;
  259. updateVisibleArea();
  260. }
  261. }
  262. int Viewport::getScrollBarThickness() const
  263. {
  264. return scrollBarThickness > 0 ? scrollBarThickness
  265. : getLookAndFeel().getDefaultScrollbarWidth();
  266. }
  267. void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
  268. {
  269. const int newRangeStartInt = roundToInt (newRangeStart);
  270. if (scrollBarThatHasMoved == &horizontalScrollBar)
  271. {
  272. setViewPosition (newRangeStartInt, getViewPositionY());
  273. }
  274. else if (scrollBarThatHasMoved == &verticalScrollBar)
  275. {
  276. setViewPosition (getViewPositionX(), newRangeStartInt);
  277. }
  278. }
  279. void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  280. {
  281. if (! useMouseWheelMoveIfNeeded (e, wheel))
  282. Component::mouseWheelMove (e, wheel);
  283. }
  284. static float rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
  285. {
  286. if (distance == 0)
  287. return 0;
  288. distance *= 14.0f * singleStepSize;
  289. return distance < 0 ? jmin (distance, -1.0f)
  290. : jmax (distance, 1.0f);
  291. }
  292. bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
  293. {
  294. if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
  295. {
  296. const bool canScrollVert = (allowScrollingWithoutScrollbarV || verticalScrollBar.isVisible());
  297. const bool canScrollHorz = (allowScrollingWithoutScrollbarH || horizontalScrollBar.isVisible());
  298. if (canScrollHorz || canScrollVert)
  299. {
  300. float wheelIncrementX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
  301. float wheelIncrementY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
  302. Point<int> pos (getViewPosition());
  303. if (wheelIncrementX != 0 && wheelIncrementY != 0 && canScrollHorz && canScrollVert)
  304. {
  305. pos.setX (pos.x - roundToInt (wheelIncrementX));
  306. pos.setY (pos.y - roundToInt (wheelIncrementY));
  307. }
  308. else if (canScrollHorz && (wheelIncrementX != 0 || e.mods.isShiftDown() || ! canScrollVert))
  309. {
  310. if (wheelIncrementX == 0 && ! canScrollVert)
  311. wheelIncrementX = wheelIncrementY;
  312. pos.setX (pos.x - roundToInt (wheelIncrementX));
  313. }
  314. else if (canScrollVert && wheelIncrementY != 0)
  315. {
  316. pos.setY (pos.y - roundToInt (wheelIncrementY));
  317. }
  318. if (pos != getViewPosition())
  319. {
  320. setViewPosition (pos);
  321. return true;
  322. }
  323. }
  324. }
  325. return false;
  326. }
  327. static bool isUpDownKeyPress (const KeyPress& key)
  328. {
  329. return key == KeyPress::upKey
  330. || key == KeyPress::downKey
  331. || key == KeyPress::pageUpKey
  332. || key == KeyPress::pageDownKey
  333. || key == KeyPress::homeKey
  334. || key == KeyPress::endKey;
  335. }
  336. static bool isLeftRightKeyPress (const KeyPress& key)
  337. {
  338. return key == KeyPress::leftKey
  339. || key == KeyPress::rightKey;
  340. }
  341. bool Viewport::keyPressed (const KeyPress& key)
  342. {
  343. const bool isUpDownKey = isUpDownKeyPress (key);
  344. if (verticalScrollBar.isVisible() && isUpDownKey)
  345. return verticalScrollBar.keyPressed (key);
  346. const bool isLeftRightKey = isLeftRightKeyPress (key);
  347. if (horizontalScrollBar.isVisible() && (isUpDownKey || isLeftRightKey))
  348. return horizontalScrollBar.keyPressed (key);
  349. return false;
  350. }
  351. bool Viewport::respondsToKey (const KeyPress& key)
  352. {
  353. return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
  354. }