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

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