The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

423 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class ScrollBar::ScrollbarButton : public Button
  19. {
  20. public:
  21. ScrollbarButton (const int direction_, ScrollBar& owner_)
  22. : Button (String::empty), direction (direction_), owner (owner_)
  23. {
  24. setWantsKeyboardFocus (false);
  25. }
  26. void paintButton (Graphics& g, bool over, bool down)
  27. {
  28. getLookAndFeel().drawScrollbarButton (g, owner, getWidth(), getHeight(),
  29. direction, owner.isVertical(), over, down);
  30. }
  31. void clicked()
  32. {
  33. owner.moveScrollbarInSteps ((direction == 1 || direction == 2) ? 1 : -1);
  34. }
  35. int direction;
  36. private:
  37. ScrollBar& owner;
  38. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollbarButton);
  39. };
  40. //==============================================================================
  41. ScrollBar::ScrollBar (const bool vertical_)
  42. : totalRange (0.0, 1.0),
  43. visibleRange (0.0, 0.1),
  44. singleStepSize (0.1),
  45. thumbAreaStart (0),
  46. thumbAreaSize (0),
  47. thumbStart (0),
  48. thumbSize (0),
  49. initialDelayInMillisecs (100),
  50. repeatDelayInMillisecs (50),
  51. minimumDelayInMillisecs (10),
  52. vertical (vertical_),
  53. isDraggingThumb (false),
  54. autohides (true)
  55. {
  56. setRepaintsOnMouseActivity (true);
  57. setFocusContainer (true);
  58. }
  59. ScrollBar::~ScrollBar()
  60. {
  61. upButton = nullptr;
  62. downButton = nullptr;
  63. }
  64. //==============================================================================
  65. void ScrollBar::setRangeLimits (const Range<double>& newRangeLimit)
  66. {
  67. if (totalRange != newRangeLimit)
  68. {
  69. totalRange = newRangeLimit;
  70. setCurrentRange (visibleRange);
  71. updateThumbPosition();
  72. }
  73. }
  74. void ScrollBar::setRangeLimits (const double newMinimum, const double newMaximum)
  75. {
  76. jassert (newMaximum >= newMinimum); // these can't be the wrong way round!
  77. setRangeLimits (Range<double> (newMinimum, newMaximum));
  78. }
  79. bool ScrollBar::setCurrentRange (const Range<double>& newRange,
  80. 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)
  96. {
  97. setCurrentRange (Range<double> (newStart, newStart + newSize));
  98. }
  99. void ScrollBar::setCurrentRangeStart (const double newStart)
  100. {
  101. setCurrentRange (visibleRange.movedToStartAt (newStart));
  102. }
  103. void ScrollBar::setSingleStepSize (const double newSingleStepSize) noexcept
  104. {
  105. singleStepSize = newSingleStepSize;
  106. }
  107. bool ScrollBar::moveScrollbarInSteps (const int howManySteps)
  108. {
  109. return setCurrentRange (visibleRange + howManySteps * singleStepSize);
  110. }
  111. bool ScrollBar::moveScrollbarInPages (const int howManyPages)
  112. {
  113. return setCurrentRange (visibleRange + howManyPages * visibleRange.getLength());
  114. }
  115. bool ScrollBar::scrollToTop()
  116. {
  117. return setCurrentRange (visibleRange.movedToStartAt (getMinimumRangeLimit()));
  118. }
  119. bool ScrollBar::scrollToBottom()
  120. {
  121. return setCurrentRange (visibleRange.movedToEndAt (getMaximumRangeLimit()));
  122. }
  123. void ScrollBar::setButtonRepeatSpeed (const int initialDelayInMillisecs_,
  124. const int repeatDelayInMillisecs_,
  125. const int minimumDelayInMillisecs_)
  126. {
  127. initialDelayInMillisecs = initialDelayInMillisecs_;
  128. repeatDelayInMillisecs = repeatDelayInMillisecs_;
  129. minimumDelayInMillisecs = minimumDelayInMillisecs_;
  130. if (upButton != nullptr)
  131. {
  132. upButton ->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  133. downButton->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  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. int newThumbSize = roundToInt (totalRange.getLength() > 0 ? (visibleRange.getLength() * thumbAreaSize) / totalRange.getLength()
  154. : thumbAreaSize);
  155. LookAndFeel& lf = getLookAndFeel();
  156. if (newThumbSize < lf.getMinimumScrollbarThumbSize (*this))
  157. newThumbSize = jmin (lf.getMinimumScrollbarThumbSize (*this), 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. const bool buttonsVisible = lf.areScrollbarButtonsVisible();
  226. int buttonSize = 0;
  227. if (buttonsVisible)
  228. {
  229. if (upButton == nullptr)
  230. {
  231. addAndMakeVisible (upButton = new ScrollbarButton (vertical ? 0 : 3, *this));
  232. addAndMakeVisible (downButton = new ScrollbarButton (vertical ? 2 : 1, *this));
  233. setButtonRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  234. }
  235. buttonSize = jmin (lf.getScrollbarButtonSize (*this), length / 2);
  236. }
  237. else
  238. {
  239. upButton = nullptr;
  240. downButton = nullptr;
  241. }
  242. if (length < 32 + lf.getMinimumScrollbarThumbSize (*this))
  243. {
  244. thumbAreaStart = length / 2;
  245. thumbAreaSize = 0;
  246. }
  247. else
  248. {
  249. thumbAreaStart = buttonSize;
  250. thumbAreaSize = length - (buttonSize << 1);
  251. }
  252. if (upButton != nullptr)
  253. {
  254. if (vertical)
  255. {
  256. upButton->setBounds (0, 0, getWidth(), buttonSize);
  257. downButton->setBounds (0, thumbAreaStart + thumbAreaSize, getWidth(), buttonSize);
  258. }
  259. else
  260. {
  261. upButton->setBounds (0, 0, buttonSize, getHeight());
  262. downButton->setBounds (thumbAreaStart + thumbAreaSize, 0, buttonSize, getHeight());
  263. }
  264. }
  265. updateThumbPosition();
  266. }
  267. void ScrollBar::mouseDown (const MouseEvent& e)
  268. {
  269. isDraggingThumb = false;
  270. lastMousePos = vertical ? e.y : e.x;
  271. dragStartMousePos = lastMousePos;
  272. dragStartRange = visibleRange.getStart();
  273. if (dragStartMousePos < thumbStart)
  274. {
  275. moveScrollbarInPages (-1);
  276. startTimer (400);
  277. }
  278. else if (dragStartMousePos >= thumbStart + thumbSize)
  279. {
  280. moveScrollbarInPages (1);
  281. startTimer (400);
  282. }
  283. else
  284. {
  285. isDraggingThumb = (thumbAreaSize > getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  286. && (thumbAreaSize > thumbSize);
  287. }
  288. }
  289. void ScrollBar::mouseDrag (const MouseEvent& e)
  290. {
  291. const int mousePos = vertical ? e.y : e.x;
  292. if (isDraggingThumb && lastMousePos != mousePos)
  293. {
  294. const int deltaPixels = mousePos - dragStartMousePos;
  295. setCurrentRangeStart (dragStartRange
  296. + deltaPixels * (totalRange.getLength() - visibleRange.getLength())
  297. / (thumbAreaSize - thumbSize));
  298. }
  299. lastMousePos = mousePos;
  300. }
  301. void ScrollBar::mouseUp (const MouseEvent&)
  302. {
  303. isDraggingThumb = false;
  304. stopTimer();
  305. repaint();
  306. }
  307. void ScrollBar::mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel)
  308. {
  309. float increment = 10.0f * (vertical ? wheel.deltaY : wheel.deltaX);
  310. if (increment < 0)
  311. increment = jmin (increment, -1.0f);
  312. else if (increment > 0)
  313. increment = jmax (increment, 1.0f);
  314. setCurrentRange (visibleRange - singleStepSize * increment);
  315. }
  316. void ScrollBar::timerCallback()
  317. {
  318. if (isMouseButtonDown())
  319. {
  320. startTimer (40);
  321. if (lastMousePos < thumbStart)
  322. setCurrentRange (visibleRange - visibleRange.getLength());
  323. else if (lastMousePos > thumbStart + thumbSize)
  324. setCurrentRangeStart (visibleRange.getEnd());
  325. }
  326. else
  327. {
  328. stopTimer();
  329. }
  330. }
  331. bool ScrollBar::keyPressed (const KeyPress& key)
  332. {
  333. if (isVisible())
  334. {
  335. if (key == KeyPress::upKey || key == KeyPress::leftKey) return moveScrollbarInSteps (-1);
  336. else if (key == KeyPress::downKey || key == KeyPress::rightKey) return moveScrollbarInSteps (1);
  337. else if (key == KeyPress::pageUpKey) return moveScrollbarInPages (-1);
  338. else if (key == KeyPress::pageDownKey) return moveScrollbarInPages (1);
  339. else if (key == KeyPress::homeKey) return scrollToTop();
  340. else if (key == KeyPress::endKey) return scrollToBottom();
  341. }
  342. return false;
  343. }