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

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