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.

444 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. setFocusContainer (true);
  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 ([=] (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. } // namespace juce