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.

424 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 (const int direction_, ScrollBar& owner_)
  21. : Button (String::empty), direction (direction_), owner (owner_)
  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 vertical_)
  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. minimumScrollBarThumbSize (0),
  49. initialDelayInMillisecs (100),
  50. repeatDelayInMillisecs (50),
  51. minimumDelayInMillisecs (10),
  52. vertical (vertical_),
  53. isDraggingThumb (false),
  54. autohides (true)
  55. {
  56. minimumScrollBarThumbSize = getLookAndFeel().getMinimumScrollbarThumbSize (*this);
  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,
  81. const NotificationType notification)
  82. {
  83. const Range<double> constrainedRange (totalRange.constrainRange (newRange));
  84. if (visibleRange != constrainedRange)
  85. {
  86. visibleRange = constrainedRange;
  87. updateThumbPosition();
  88. if (notification != dontSendNotification)
  89. triggerAsyncUpdate();
  90. if (notification == sendNotificationSync)
  91. handleUpdateNowIfNeeded();
  92. return true;
  93. }
  94. return false;
  95. }
  96. void ScrollBar::setCurrentRange (const double newStart, const double newSize, NotificationType notification)
  97. {
  98. setCurrentRange (Range<double> (newStart, newStart + newSize), notification);
  99. }
  100. void ScrollBar::setCurrentRangeStart (const double newStart, NotificationType notification)
  101. {
  102. setCurrentRange (visibleRange.movedToStartAt (newStart), notification);
  103. }
  104. void ScrollBar::setSingleStepSize (const double newSingleStepSize) noexcept
  105. {
  106. singleStepSize = newSingleStepSize;
  107. }
  108. bool ScrollBar::moveScrollbarInSteps (const int howManySteps, NotificationType notification)
  109. {
  110. return setCurrentRange (visibleRange + howManySteps * singleStepSize, notification);
  111. }
  112. bool ScrollBar::moveScrollbarInPages (const int howManyPages, NotificationType notification)
  113. {
  114. return setCurrentRange (visibleRange + howManyPages * visibleRange.getLength(), notification);
  115. }
  116. bool ScrollBar::scrollToTop (NotificationType notification)
  117. {
  118. return setCurrentRange (visibleRange.movedToStartAt (getMinimumRangeLimit()), notification);
  119. }
  120. bool ScrollBar::scrollToBottom (NotificationType notification)
  121. {
  122. return setCurrentRange (visibleRange.movedToEndAt (getMaximumRangeLimit()), notification);
  123. }
  124. void ScrollBar::setButtonRepeatSpeed (const int initialDelayInMillisecs_,
  125. const int repeatDelayInMillisecs_,
  126. const int minimumDelayInMillisecs_)
  127. {
  128. initialDelayInMillisecs = initialDelayInMillisecs_;
  129. repeatDelayInMillisecs = repeatDelayInMillisecs_;
  130. minimumDelayInMillisecs = minimumDelayInMillisecs_;
  131. if (upButton != nullptr)
  132. {
  133. upButton ->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  134. downButton->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  135. }
  136. }
  137. //==============================================================================
  138. void ScrollBar::addListener (Listener* const listener)
  139. {
  140. listeners.add (listener);
  141. }
  142. void ScrollBar::removeListener (Listener* const listener)
  143. {
  144. listeners.remove (listener);
  145. }
  146. void ScrollBar::handleAsyncUpdate()
  147. {
  148. double start = visibleRange.getStart(); // (need to use a temp variable for VC7 compatibility)
  149. listeners.call (&ScrollBar::Listener::scrollBarMoved, this, start);
  150. }
  151. //==============================================================================
  152. void ScrollBar::updateThumbPosition()
  153. {
  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() && visibleRange.getLength() > 0.0));
  165. if (thumbStart != newThumbStart || thumbSize != newThumbSize)
  166. {
  167. const int repaintStart = jmin (thumbStart, newThumbStart) - 4;
  168. const int repaintSize = jmax (thumbStart + thumbSize, newThumbStart + newThumbSize) + 8 - repaintStart;
  169. if (vertical)
  170. repaint (0, repaintStart, getWidth(), repaintSize);
  171. else
  172. repaint (repaintStart, 0, repaintSize, getHeight());
  173. thumbStart = newThumbStart;
  174. thumbSize = newThumbSize;
  175. }
  176. }
  177. void ScrollBar::setOrientation (const bool shouldBeVertical)
  178. {
  179. if (vertical != shouldBeVertical)
  180. {
  181. vertical = shouldBeVertical;
  182. if (upButton != nullptr)
  183. {
  184. upButton->direction = vertical ? 0 : 3;
  185. downButton->direction = vertical ? 2 : 1;
  186. }
  187. updateThumbPosition();
  188. }
  189. }
  190. void ScrollBar::setAutoHide (const bool shouldHideWhenFullRange)
  191. {
  192. autohides = shouldHideWhenFullRange;
  193. updateThumbPosition();
  194. }
  195. bool ScrollBar::autoHides() const noexcept
  196. {
  197. return autohides;
  198. }
  199. //==============================================================================
  200. void ScrollBar::paint (Graphics& g)
  201. {
  202. if (thumbAreaSize > 0)
  203. {
  204. LookAndFeel& lf = getLookAndFeel();
  205. const int thumb = (thumbAreaSize > lf.getMinimumScrollbarThumbSize (*this))
  206. ? thumbSize : 0;
  207. if (vertical)
  208. lf.drawScrollbar (g, *this, 0, thumbAreaStart, getWidth(), thumbAreaSize,
  209. vertical, thumbStart, thumb, isMouseOver(), isMouseButtonDown());
  210. else
  211. lf.drawScrollbar (g, *this, thumbAreaStart, 0, thumbAreaSize, getHeight(),
  212. vertical, thumbStart, thumb, isMouseOver(), isMouseButtonDown());
  213. }
  214. }
  215. void ScrollBar::lookAndFeelChanged()
  216. {
  217. setComponentEffect (getLookAndFeel().getScrollbarEffect());
  218. if (isVisible())
  219. resized();
  220. }
  221. void ScrollBar::resized()
  222. {
  223. const int length = vertical ? getHeight() : getWidth();
  224. LookAndFeel& lf = getLookAndFeel();
  225. minimumScrollBarThumbSize = lf.getMinimumScrollbarThumbSize (*this);
  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 - (buttonSize << 1);
  252. }
  253. if (upButton != nullptr)
  254. {
  255. if (vertical)
  256. {
  257. upButton->setBounds (0, 0, getWidth(), buttonSize);
  258. downButton->setBounds (0, thumbAreaStart + thumbAreaSize, getWidth(), buttonSize);
  259. }
  260. else
  261. {
  262. upButton->setBounds (0, 0, buttonSize, getHeight());
  263. downButton->setBounds (thumbAreaStart + thumbAreaSize, 0, buttonSize, getHeight());
  264. }
  265. }
  266. updateThumbPosition();
  267. }
  268. void ScrollBar::mouseDown (const MouseEvent& e)
  269. {
  270. isDraggingThumb = false;
  271. lastMousePos = vertical ? e.y : e.x;
  272. dragStartMousePos = lastMousePos;
  273. dragStartRange = visibleRange.getStart();
  274. if (dragStartMousePos < thumbStart)
  275. {
  276. moveScrollbarInPages (-1);
  277. startTimer (400);
  278. }
  279. else if (dragStartMousePos >= thumbStart + thumbSize)
  280. {
  281. moveScrollbarInPages (1);
  282. startTimer (400);
  283. }
  284. else
  285. {
  286. isDraggingThumb = (thumbAreaSize > getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  287. && (thumbAreaSize > thumbSize);
  288. }
  289. }
  290. void ScrollBar::mouseDrag (const MouseEvent& e)
  291. {
  292. const int mousePos = vertical ? e.y : e.x;
  293. if (isDraggingThumb && lastMousePos != mousePos && thumbAreaSize > thumbSize)
  294. {
  295. const int deltaPixels = mousePos - dragStartMousePos;
  296. setCurrentRangeStart (dragStartRange
  297. + deltaPixels * (totalRange.getLength() - visibleRange.getLength())
  298. / (thumbAreaSize - thumbSize));
  299. }
  300. lastMousePos = mousePos;
  301. }
  302. void ScrollBar::mouseUp (const MouseEvent&)
  303. {
  304. isDraggingThumb = false;
  305. stopTimer();
  306. repaint();
  307. }
  308. void ScrollBar::mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel)
  309. {
  310. float increment = 10.0f * (vertical ? wheel.deltaY : wheel.deltaX);
  311. if (increment < 0)
  312. increment = jmin (increment, -1.0f);
  313. else if (increment > 0)
  314. increment = jmax (increment, 1.0f);
  315. setCurrentRange (visibleRange - singleStepSize * increment);
  316. }
  317. void ScrollBar::timerCallback()
  318. {
  319. if (isMouseButtonDown())
  320. {
  321. startTimer (40);
  322. if (lastMousePos < thumbStart)
  323. setCurrentRange (visibleRange - visibleRange.getLength());
  324. else if (lastMousePos > thumbStart + thumbSize)
  325. setCurrentRangeStart (visibleRange.getEnd());
  326. }
  327. else
  328. {
  329. stopTimer();
  330. }
  331. }
  332. bool ScrollBar::keyPressed (const KeyPress& key)
  333. {
  334. if (isVisible())
  335. {
  336. if (key == KeyPress::upKey || key == KeyPress::leftKey) return moveScrollbarInSteps (-1);
  337. if (key == KeyPress::downKey || key == KeyPress::rightKey) return moveScrollbarInSteps (1);
  338. if (key == KeyPress::pageUpKey) return moveScrollbarInPages (-1);
  339. if (key == KeyPress::pageDownKey) return moveScrollbarInPages (1);
  340. if (key == KeyPress::homeKey) return scrollToTop();
  341. if (key == KeyPress::endKey) return scrollToBottom();
  342. }
  343. return false;
  344. }