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.

431 lines
13KB

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