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 16KB

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