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

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