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_ScrollBar.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class ScrollBar::ScrollbarButton : public Button
  21. {
  22. public:
  23. ScrollbarButton (int direc, ScrollBar& s)
  24. : Button (String()), direction (direc), owner (s)
  25. {
  26. setWantsKeyboardFocus (false);
  27. }
  28. void paintButton (Graphics& g, bool over, bool down) override
  29. {
  30. getLookAndFeel().drawScrollbarButton (g, owner, getWidth(), getHeight(),
  31. direction, owner.isVertical(), over, down);
  32. }
  33. void clicked() override
  34. {
  35. owner.moveScrollbarInSteps ((direction == 1 || direction == 2) ? 1 : -1);
  36. }
  37. using Button::clicked;
  38. int direction;
  39. private:
  40. ScrollBar& owner;
  41. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollbarButton)
  42. };
  43. //==============================================================================
  44. ScrollBar::ScrollBar (bool shouldBeVertical) : vertical (shouldBeVertical)
  45. {
  46. setRepaintsOnMouseActivity (true);
  47. setFocusContainerType (FocusContainerType::keyboardFocusContainer);
  48. }
  49. ScrollBar::~ScrollBar()
  50. {
  51. upButton.reset();
  52. downButton.reset();
  53. }
  54. //==============================================================================
  55. void ScrollBar::setRangeLimits (Range<double> newRangeLimit, NotificationType notification)
  56. {
  57. if (totalRange != newRangeLimit)
  58. {
  59. totalRange = newRangeLimit;
  60. setCurrentRange (visibleRange, notification);
  61. updateThumbPosition();
  62. }
  63. }
  64. void ScrollBar::setRangeLimits (double newMinimum, double newMaximum, NotificationType notification)
  65. {
  66. jassert (newMaximum >= newMinimum); // these can't be the wrong way round!
  67. setRangeLimits (Range<double> (newMinimum, newMaximum), notification);
  68. }
  69. bool ScrollBar::setCurrentRange (Range<double> newRange, NotificationType notification)
  70. {
  71. auto constrainedRange = totalRange.constrainRange (newRange);
  72. if (visibleRange != constrainedRange)
  73. {
  74. visibleRange = constrainedRange;
  75. updateThumbPosition();
  76. if (notification != dontSendNotification)
  77. triggerAsyncUpdate();
  78. if (notification == sendNotificationSync)
  79. handleUpdateNowIfNeeded();
  80. return true;
  81. }
  82. return false;
  83. }
  84. void ScrollBar::setCurrentRange (double newStart, double newSize, NotificationType notification)
  85. {
  86. setCurrentRange (Range<double> (newStart, newStart + newSize), notification);
  87. }
  88. void ScrollBar::setCurrentRangeStart (double newStart, NotificationType notification)
  89. {
  90. setCurrentRange (visibleRange.movedToStartAt (newStart), notification);
  91. }
  92. void ScrollBar::setSingleStepSize (double newSingleStepSize) noexcept
  93. {
  94. singleStepSize = newSingleStepSize;
  95. }
  96. bool ScrollBar::moveScrollbarInSteps (int howManySteps, NotificationType notification)
  97. {
  98. return setCurrentRange (visibleRange + howManySteps * singleStepSize, notification);
  99. }
  100. bool ScrollBar::moveScrollbarInPages (int howManyPages, NotificationType notification)
  101. {
  102. return setCurrentRange (visibleRange + howManyPages * visibleRange.getLength(), notification);
  103. }
  104. bool ScrollBar::scrollToTop (NotificationType notification)
  105. {
  106. return setCurrentRange (visibleRange.movedToStartAt (getMinimumRangeLimit()), notification);
  107. }
  108. bool ScrollBar::scrollToBottom (NotificationType notification)
  109. {
  110. return setCurrentRange (visibleRange.movedToEndAt (getMaximumRangeLimit()), notification);
  111. }
  112. void ScrollBar::setButtonRepeatSpeed (int newInitialDelay,
  113. int newRepeatDelay,
  114. int newMinimumDelay)
  115. {
  116. initialDelayInMillisecs = newInitialDelay;
  117. repeatDelayInMillisecs = newRepeatDelay;
  118. minimumDelayInMillisecs = newMinimumDelay;
  119. if (upButton != nullptr)
  120. {
  121. upButton ->setRepeatSpeed (newInitialDelay, newRepeatDelay, newMinimumDelay);
  122. downButton->setRepeatSpeed (newInitialDelay, newRepeatDelay, newMinimumDelay);
  123. }
  124. }
  125. //==============================================================================
  126. void ScrollBar::addListener (Listener* listener)
  127. {
  128. listeners.add (listener);
  129. }
  130. void ScrollBar::removeListener (Listener* listener)
  131. {
  132. listeners.remove (listener);
  133. }
  134. void ScrollBar::handleAsyncUpdate()
  135. {
  136. auto start = visibleRange.getStart(); // (need to use a temp variable for VC7 compatibility)
  137. listeners.call ([this, start] (Listener& l) { l.scrollBarMoved (this, start); });
  138. }
  139. //==============================================================================
  140. void ScrollBar::updateThumbPosition()
  141. {
  142. auto minimumScrollBarThumbSize = getLookAndFeel().getMinimumScrollbarThumbSize (*this);
  143. int newThumbSize = roundToInt (totalRange.getLength() > 0 ? (visibleRange.getLength() * thumbAreaSize) / totalRange.getLength()
  144. : thumbAreaSize);
  145. if (newThumbSize < minimumScrollBarThumbSize)
  146. newThumbSize = jmin (minimumScrollBarThumbSize, thumbAreaSize - 1);
  147. if (newThumbSize > thumbAreaSize)
  148. newThumbSize = thumbAreaSize;
  149. int newThumbStart = thumbAreaStart;
  150. if (totalRange.getLength() > visibleRange.getLength())
  151. newThumbStart += roundToInt (((visibleRange.getStart() - totalRange.getStart()) * (thumbAreaSize - newThumbSize))
  152. / (totalRange.getLength() - visibleRange.getLength()));
  153. Component::setVisible (getVisibility());
  154. if (thumbStart != newThumbStart || thumbSize != newThumbSize)
  155. {
  156. auto repaintStart = jmin (thumbStart, newThumbStart) - 4;
  157. auto repaintSize = jmax (thumbStart + thumbSize, newThumbStart + newThumbSize) + 8 - repaintStart;
  158. if (vertical)
  159. repaint (0, repaintStart, getWidth(), repaintSize);
  160. else
  161. repaint (repaintStart, 0, repaintSize, getHeight());
  162. thumbStart = newThumbStart;
  163. thumbSize = newThumbSize;
  164. }
  165. }
  166. void ScrollBar::setOrientation (bool shouldBeVertical)
  167. {
  168. if (vertical != shouldBeVertical)
  169. {
  170. vertical = shouldBeVertical;
  171. if (upButton != nullptr)
  172. {
  173. upButton->direction = vertical ? 0 : 3;
  174. downButton->direction = vertical ? 2 : 1;
  175. }
  176. updateThumbPosition();
  177. }
  178. }
  179. void ScrollBar::setAutoHide (bool shouldHideWhenFullRange)
  180. {
  181. autohides = shouldHideWhenFullRange;
  182. updateThumbPosition();
  183. }
  184. bool ScrollBar::autoHides() const noexcept
  185. {
  186. return autohides;
  187. }
  188. //==============================================================================
  189. void ScrollBar::paint (Graphics& g)
  190. {
  191. if (thumbAreaSize > 0)
  192. {
  193. auto& lf = getLookAndFeel();
  194. auto thumb = (thumbAreaSize > lf.getMinimumScrollbarThumbSize (*this))
  195. ? thumbSize : 0;
  196. if (vertical)
  197. lf.drawScrollbar (g, *this, 0, thumbAreaStart, getWidth(), thumbAreaSize,
  198. vertical, thumbStart, thumb, isMouseOver(), isMouseButtonDown());
  199. else
  200. lf.drawScrollbar (g, *this, thumbAreaStart, 0, thumbAreaSize, getHeight(),
  201. vertical, thumbStart, thumb, isMouseOver(), isMouseButtonDown());
  202. }
  203. }
  204. void ScrollBar::lookAndFeelChanged()
  205. {
  206. setComponentEffect (getLookAndFeel().getScrollbarEffect());
  207. if (isVisible())
  208. resized();
  209. }
  210. void ScrollBar::resized()
  211. {
  212. auto length = vertical ? getHeight() : getWidth();
  213. auto& lf = getLookAndFeel();
  214. bool buttonsVisible = lf.areScrollbarButtonsVisible();
  215. int buttonSize = 0;
  216. if (buttonsVisible)
  217. {
  218. if (upButton == nullptr)
  219. {
  220. upButton .reset (new ScrollbarButton (vertical ? 0 : 3, *this));
  221. downButton.reset (new ScrollbarButton (vertical ? 2 : 1, *this));
  222. addAndMakeVisible (upButton.get());
  223. addAndMakeVisible (downButton.get());
  224. setButtonRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  225. }
  226. buttonSize = jmin (lf.getScrollbarButtonSize (*this), length / 2);
  227. }
  228. else
  229. {
  230. upButton.reset();
  231. downButton.reset();
  232. }
  233. if (length < 32 + lf.getMinimumScrollbarThumbSize (*this))
  234. {
  235. thumbAreaStart = length / 2;
  236. thumbAreaSize = 0;
  237. }
  238. else
  239. {
  240. thumbAreaStart = buttonSize;
  241. thumbAreaSize = length - 2 * buttonSize;
  242. }
  243. if (upButton != nullptr)
  244. {
  245. auto r = getLocalBounds();
  246. if (vertical)
  247. {
  248. upButton->setBounds (r.removeFromTop (buttonSize));
  249. downButton->setBounds (r.removeFromBottom (buttonSize));
  250. }
  251. else
  252. {
  253. upButton->setBounds (r.removeFromLeft (buttonSize));
  254. downButton->setBounds (r.removeFromRight (buttonSize));
  255. }
  256. }
  257. updateThumbPosition();
  258. }
  259. void ScrollBar::parentHierarchyChanged()
  260. {
  261. lookAndFeelChanged();
  262. }
  263. void ScrollBar::mouseDown (const MouseEvent& e)
  264. {
  265. isDraggingThumb = false;
  266. lastMousePos = vertical ? e.y : e.x;
  267. dragStartMousePos = lastMousePos;
  268. dragStartRange = visibleRange.getStart();
  269. if (dragStartMousePos < thumbStart)
  270. {
  271. moveScrollbarInPages (-1);
  272. startTimer (400);
  273. }
  274. else if (dragStartMousePos >= thumbStart + thumbSize)
  275. {
  276. moveScrollbarInPages (1);
  277. startTimer (400);
  278. }
  279. else
  280. {
  281. isDraggingThumb = (thumbAreaSize > getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  282. && (thumbAreaSize > thumbSize);
  283. }
  284. }
  285. void ScrollBar::mouseDrag (const MouseEvent& e)
  286. {
  287. auto mousePos = vertical ? e.y : e.x;
  288. if (isDraggingThumb && lastMousePos != mousePos && thumbAreaSize > thumbSize)
  289. {
  290. auto deltaPixels = mousePos - dragStartMousePos;
  291. setCurrentRangeStart (dragStartRange
  292. + deltaPixels * (totalRange.getLength() - visibleRange.getLength())
  293. / (thumbAreaSize - thumbSize));
  294. }
  295. lastMousePos = mousePos;
  296. }
  297. void ScrollBar::mouseUp (const MouseEvent&)
  298. {
  299. isDraggingThumb = false;
  300. stopTimer();
  301. repaint();
  302. }
  303. void ScrollBar::mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel)
  304. {
  305. float increment = 10.0f * (vertical ? wheel.deltaY : wheel.deltaX);
  306. if (increment < 0)
  307. increment = jmin (increment, -1.0f);
  308. else if (increment > 0)
  309. increment = jmax (increment, 1.0f);
  310. setCurrentRange (visibleRange - singleStepSize * increment);
  311. }
  312. void ScrollBar::timerCallback()
  313. {
  314. if (isMouseButtonDown())
  315. {
  316. startTimer (40);
  317. if (lastMousePos < thumbStart)
  318. setCurrentRange (visibleRange - visibleRange.getLength());
  319. else if (lastMousePos > thumbStart + thumbSize)
  320. setCurrentRangeStart (visibleRange.getEnd());
  321. }
  322. else
  323. {
  324. stopTimer();
  325. }
  326. }
  327. bool ScrollBar::keyPressed (const KeyPress& key)
  328. {
  329. if (isVisible())
  330. {
  331. if (key == KeyPress::upKey || key == KeyPress::leftKey) return moveScrollbarInSteps (-1);
  332. if (key == KeyPress::downKey || key == KeyPress::rightKey) return moveScrollbarInSteps (1);
  333. if (key == KeyPress::pageUpKey) return moveScrollbarInPages (-1);
  334. if (key == KeyPress::pageDownKey) return moveScrollbarInPages (1);
  335. if (key == KeyPress::homeKey) return scrollToTop();
  336. if (key == KeyPress::endKey) return scrollToBottom();
  337. }
  338. return false;
  339. }
  340. void ScrollBar::setVisible (bool shouldBeVisible)
  341. {
  342. if (userVisibilityFlag != shouldBeVisible)
  343. {
  344. userVisibilityFlag = shouldBeVisible;
  345. Component::setVisible (getVisibility());
  346. }
  347. }
  348. bool ScrollBar::getVisibility() const noexcept
  349. {
  350. if (! userVisibilityFlag)
  351. return false;
  352. return (! autohides) || (totalRange.getLength() > visibleRange.getLength()
  353. && visibleRange.getLength() > 0.0);
  354. }
  355. //==============================================================================
  356. std::unique_ptr<AccessibilityHandler> ScrollBar::createAccessibilityHandler()
  357. {
  358. class ValueInterface : public AccessibilityRangedNumericValueInterface
  359. {
  360. public:
  361. explicit ValueInterface (ScrollBar& scrollBarToWrap) : scrollBar (scrollBarToWrap) {}
  362. bool isReadOnly() const override { return false; }
  363. double getCurrentValue() const override { return scrollBar.getCurrentRangeStart(); }
  364. void setValue (double newValue) override { scrollBar.setCurrentRangeStart (newValue); }
  365. AccessibleValueRange getRange() const override
  366. {
  367. if (scrollBar.getRangeLimit().isEmpty())
  368. return {};
  369. return { { scrollBar.getMinimumRangeLimit(), scrollBar.getMaximumRangeLimit() },
  370. scrollBar.getSingleStepSize() };
  371. }
  372. private:
  373. ScrollBar& scrollBar;
  374. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ValueInterface)
  375. };
  376. return std::make_unique<AccessibilityHandler> (*this,
  377. AccessibilityRole::scrollBar,
  378. AccessibilityActions{},
  379. AccessibilityHandler::Interfaces { std::make_unique<ValueInterface> (*this) });
  380. }
  381. } // namespace juce