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

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