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.

429 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),
  23. direction (direction_),
  24. owner (owner_)
  25. {
  26. setWantsKeyboardFocus (false);
  27. }
  28. void paintButton (Graphics& g, bool over, bool down)
  29. {
  30. getLookAndFeel()
  31. .drawScrollbarButton (g, owner,
  32. getWidth(), getHeight(),
  33. direction,
  34. owner.isVertical(),
  35. over, down);
  36. }
  37. void clicked()
  38. {
  39. owner.moveScrollbarInSteps ((direction == 1 || direction == 2) ? 1 : -1);
  40. }
  41. int direction;
  42. private:
  43. ScrollBar& owner;
  44. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollbarButton);
  45. };
  46. //==============================================================================
  47. ScrollBar::ScrollBar (const bool vertical_)
  48. : totalRange (0.0, 1.0),
  49. visibleRange (0.0, 0.1),
  50. singleStepSize (0.1),
  51. thumbAreaStart (0),
  52. thumbAreaSize (0),
  53. thumbStart (0),
  54. thumbSize (0),
  55. initialDelayInMillisecs (100),
  56. repeatDelayInMillisecs (50),
  57. minimumDelayInMillisecs (10),
  58. vertical (vertical_),
  59. isDraggingThumb (false),
  60. autohides (true)
  61. {
  62. setRepaintsOnMouseActivity (true);
  63. setFocusContainer (true);
  64. }
  65. ScrollBar::~ScrollBar()
  66. {
  67. upButton = nullptr;
  68. downButton = nullptr;
  69. }
  70. //==============================================================================
  71. void ScrollBar::setRangeLimits (const Range<double>& newRangeLimit)
  72. {
  73. if (totalRange != newRangeLimit)
  74. {
  75. totalRange = newRangeLimit;
  76. setCurrentRange (visibleRange);
  77. updateThumbPosition();
  78. }
  79. }
  80. void ScrollBar::setRangeLimits (const double newMinimum, const double newMaximum)
  81. {
  82. jassert (newMaximum >= newMinimum); // these can't be the wrong way round!
  83. setRangeLimits (Range<double> (newMinimum, newMaximum));
  84. }
  85. void ScrollBar::setCurrentRange (const Range<double>& newRange)
  86. {
  87. const Range<double> constrainedRange (totalRange.constrainRange (newRange));
  88. if (visibleRange != constrainedRange)
  89. {
  90. visibleRange = constrainedRange;
  91. updateThumbPosition();
  92. triggerAsyncUpdate();
  93. }
  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)
  104. {
  105. singleStepSize = newSingleStepSize;
  106. }
  107. void ScrollBar::moveScrollbarInSteps (const int howManySteps)
  108. {
  109. setCurrentRange (visibleRange + howManySteps * singleStepSize);
  110. }
  111. void ScrollBar::moveScrollbarInPages (const int howManyPages)
  112. {
  113. setCurrentRange (visibleRange + howManyPages * visibleRange.getLength());
  114. }
  115. void ScrollBar::scrollToTop()
  116. {
  117. setCurrentRange (visibleRange.movedToStartAt (getMinimumRangeLimit()));
  118. }
  119. void ScrollBar::scrollToBottom()
  120. {
  121. 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. if (newThumbSize < getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  156. newThumbSize = jmin (getLookAndFeel().getMinimumScrollbarThumbSize (*this), thumbAreaSize - 1);
  157. if (newThumbSize > thumbAreaSize)
  158. newThumbSize = thumbAreaSize;
  159. int newThumbStart = thumbAreaStart;
  160. if (totalRange.getLength() > visibleRange.getLength())
  161. newThumbStart += roundToInt (((visibleRange.getStart() - totalRange.getStart()) * (thumbAreaSize - newThumbSize))
  162. / (totalRange.getLength() - visibleRange.getLength()));
  163. setVisible ((! autohides) || (totalRange.getLength() > visibleRange.getLength() && 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. {
  208. lf.drawScrollbar (g, *this,
  209. 0, thumbAreaStart,
  210. getWidth(), thumbAreaSize,
  211. vertical,
  212. thumbStart, thumb,
  213. isMouseOver(), isMouseButtonDown());
  214. }
  215. else
  216. {
  217. lf.drawScrollbar (g, *this,
  218. thumbAreaStart, 0,
  219. thumbAreaSize, getHeight(),
  220. vertical,
  221. thumbStart, thumb,
  222. isMouseOver(), isMouseButtonDown());
  223. }
  224. }
  225. }
  226. void ScrollBar::lookAndFeelChanged()
  227. {
  228. setComponentEffect (getLookAndFeel().getScrollbarEffect());
  229. resized();
  230. }
  231. void ScrollBar::resized()
  232. {
  233. const int length = vertical ? getHeight() : getWidth();
  234. const bool buttonsVisible = getLookAndFeel().areScrollbarButtonsVisible();
  235. if (buttonsVisible)
  236. {
  237. if (upButton == nullptr)
  238. {
  239. addAndMakeVisible (upButton = new ScrollbarButton (vertical ? 0 : 3, *this));
  240. addAndMakeVisible (downButton = new ScrollbarButton (vertical ? 2 : 1, *this));
  241. setButtonRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  242. }
  243. }
  244. else
  245. {
  246. upButton = nullptr;
  247. downButton = nullptr;
  248. }
  249. const int buttonSize = upButton != nullptr ? jmin (getLookAndFeel().getScrollbarButtonSize (*this), length / 2)
  250. : 0;
  251. if (length < 32 + getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  252. {
  253. thumbAreaStart = length / 2;
  254. thumbAreaSize = 0;
  255. }
  256. else
  257. {
  258. thumbAreaStart = buttonSize;
  259. thumbAreaSize = length - (buttonSize << 1);
  260. }
  261. if (upButton != nullptr)
  262. {
  263. if (vertical)
  264. {
  265. upButton->setBounds (0, 0, getWidth(), buttonSize);
  266. downButton->setBounds (0, thumbAreaStart + thumbAreaSize, getWidth(), buttonSize);
  267. }
  268. else
  269. {
  270. upButton->setBounds (0, 0, buttonSize, getHeight());
  271. downButton->setBounds (thumbAreaStart + thumbAreaSize, 0, buttonSize, getHeight());
  272. }
  273. }
  274. updateThumbPosition();
  275. }
  276. void ScrollBar::mouseDown (const MouseEvent& e)
  277. {
  278. isDraggingThumb = false;
  279. lastMousePos = vertical ? e.y : e.x;
  280. dragStartMousePos = lastMousePos;
  281. dragStartRange = visibleRange.getStart();
  282. if (dragStartMousePos < thumbStart)
  283. {
  284. moveScrollbarInPages (-1);
  285. startTimer (400);
  286. }
  287. else if (dragStartMousePos >= thumbStart + thumbSize)
  288. {
  289. moveScrollbarInPages (1);
  290. startTimer (400);
  291. }
  292. else
  293. {
  294. isDraggingThumb = (thumbAreaSize > getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  295. && (thumbAreaSize > thumbSize);
  296. }
  297. }
  298. void ScrollBar::mouseDrag (const MouseEvent& e)
  299. {
  300. const int mousePos = vertical ? e.y : e.x;
  301. if (isDraggingThumb && lastMousePos != mousePos)
  302. {
  303. const int deltaPixels = mousePos - dragStartMousePos;
  304. setCurrentRangeStart (dragStartRange
  305. + deltaPixels * (totalRange.getLength() - visibleRange.getLength())
  306. / (thumbAreaSize - thumbSize));
  307. }
  308. lastMousePos = mousePos;
  309. }
  310. void ScrollBar::mouseUp (const MouseEvent&)
  311. {
  312. isDraggingThumb = false;
  313. stopTimer();
  314. repaint();
  315. }
  316. void ScrollBar::mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel)
  317. {
  318. float increment = 10.0f * (vertical ? wheel.deltaY : wheel.deltaX);
  319. if (increment < 0)
  320. increment = jmin (increment, -1.0f);
  321. else if (increment > 0)
  322. increment = jmax (increment, 1.0f);
  323. setCurrentRange (visibleRange - singleStepSize * increment);
  324. }
  325. void ScrollBar::timerCallback()
  326. {
  327. if (isMouseButtonDown())
  328. {
  329. startTimer (40);
  330. if (lastMousePos < thumbStart)
  331. setCurrentRange (visibleRange - visibleRange.getLength());
  332. else if (lastMousePos > thumbStart + thumbSize)
  333. setCurrentRangeStart (visibleRange.getEnd());
  334. }
  335. else
  336. {
  337. stopTimer();
  338. }
  339. }
  340. bool ScrollBar::keyPressed (const KeyPress& key)
  341. {
  342. if (! isVisible())
  343. return false;
  344. if (key.isKeyCode (KeyPress::upKey)
  345. || key.isKeyCode (KeyPress::leftKey)) moveScrollbarInSteps (-1);
  346. else if (key.isKeyCode (KeyPress::downKey)
  347. || key.isKeyCode (KeyPress::rightKey)) moveScrollbarInSteps (1);
  348. else if (key.isKeyCode (KeyPress::pageUpKey)) moveScrollbarInPages (-1);
  349. else if (key.isKeyCode (KeyPress::pageDownKey)) moveScrollbarInPages (1);
  350. else if (key.isKeyCode (KeyPress::homeKey)) scrollToTop();
  351. else if (key.isKeyCode (KeyPress::endKey)) scrollToBottom();
  352. else return false;
  353. return true;
  354. }