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.

436 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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. class ScrollBar::ScrollbarButton : public Button
  21. {
  22. public:
  23. ScrollbarButton (const int direction_, ScrollBar& owner_)
  24. : Button (String::empty),
  25. direction (direction_),
  26. owner (owner_)
  27. {
  28. setWantsKeyboardFocus (false);
  29. }
  30. void paintButton (Graphics& g, bool over, bool down)
  31. {
  32. getLookAndFeel()
  33. .drawScrollbarButton (g, owner,
  34. getWidth(), getHeight(),
  35. direction,
  36. owner.isVertical(),
  37. over, down);
  38. }
  39. void clicked()
  40. {
  41. owner.moveScrollbarInSteps ((direction == 1 || direction == 2) ? 1 : -1);
  42. }
  43. int direction;
  44. private:
  45. ScrollBar& owner;
  46. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollbarButton);
  47. };
  48. //==============================================================================
  49. ScrollBar::ScrollBar (const bool vertical_,
  50. const bool buttonsAreVisible)
  51. : totalRange (0.0, 1.0),
  52. visibleRange (0.0, 0.1),
  53. singleStepSize (0.1),
  54. thumbAreaStart (0),
  55. thumbAreaSize (0),
  56. thumbStart (0),
  57. thumbSize (0),
  58. initialDelayInMillisecs (100),
  59. repeatDelayInMillisecs (50),
  60. minimumDelayInMillisecs (10),
  61. vertical (vertical_),
  62. isDraggingThumb (false),
  63. autohides (true)
  64. {
  65. setButtonVisibility (buttonsAreVisible);
  66. setRepaintsOnMouseActivity (true);
  67. setFocusContainer (true);
  68. }
  69. ScrollBar::~ScrollBar()
  70. {
  71. upButton = nullptr;
  72. downButton = nullptr;
  73. }
  74. //==============================================================================
  75. void ScrollBar::setRangeLimits (const Range<double>& newRangeLimit)
  76. {
  77. if (totalRange != newRangeLimit)
  78. {
  79. totalRange = newRangeLimit;
  80. setCurrentRange (visibleRange);
  81. updateThumbPosition();
  82. }
  83. }
  84. void ScrollBar::setRangeLimits (const double newMinimum, const double newMaximum)
  85. {
  86. jassert (newMaximum >= newMinimum); // these can't be the wrong way round!
  87. setRangeLimits (Range<double> (newMinimum, newMaximum));
  88. }
  89. void ScrollBar::setCurrentRange (const Range<double>& newRange)
  90. {
  91. const Range<double> constrainedRange (totalRange.constrainRange (newRange));
  92. if (visibleRange != constrainedRange)
  93. {
  94. visibleRange = constrainedRange;
  95. updateThumbPosition();
  96. triggerAsyncUpdate();
  97. }
  98. }
  99. void ScrollBar::setCurrentRange (const double newStart, const double newSize)
  100. {
  101. setCurrentRange (Range<double> (newStart, newStart + newSize));
  102. }
  103. void ScrollBar::setCurrentRangeStart (const double newStart)
  104. {
  105. setCurrentRange (visibleRange.movedToStartAt (newStart));
  106. }
  107. void ScrollBar::setSingleStepSize (const double newSingleStepSize)
  108. {
  109. singleStepSize = newSingleStepSize;
  110. }
  111. void ScrollBar::moveScrollbarInSteps (const int howManySteps)
  112. {
  113. setCurrentRange (visibleRange + howManySteps * singleStepSize);
  114. }
  115. void ScrollBar::moveScrollbarInPages (const int howManyPages)
  116. {
  117. setCurrentRange (visibleRange + howManyPages * visibleRange.getLength());
  118. }
  119. void ScrollBar::scrollToTop()
  120. {
  121. setCurrentRange (visibleRange.movedToStartAt (getMinimumRangeLimit()));
  122. }
  123. void ScrollBar::scrollToBottom()
  124. {
  125. setCurrentRange (visibleRange.movedToEndAt (getMaximumRangeLimit()));
  126. }
  127. void ScrollBar::setButtonRepeatSpeed (const int initialDelayInMillisecs_,
  128. const int repeatDelayInMillisecs_,
  129. const int minimumDelayInMillisecs_)
  130. {
  131. initialDelayInMillisecs = initialDelayInMillisecs_;
  132. repeatDelayInMillisecs = repeatDelayInMillisecs_;
  133. minimumDelayInMillisecs = minimumDelayInMillisecs_;
  134. if (upButton != nullptr)
  135. {
  136. upButton->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  137. downButton->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  138. }
  139. }
  140. //==============================================================================
  141. void ScrollBar::addListener (Listener* const listener)
  142. {
  143. listeners.add (listener);
  144. }
  145. void ScrollBar::removeListener (Listener* const listener)
  146. {
  147. listeners.remove (listener);
  148. }
  149. void ScrollBar::handleAsyncUpdate()
  150. {
  151. double start = visibleRange.getStart(); // (need to use a temp variable for VC7 compatibility)
  152. listeners.call (&ScrollBar::Listener::scrollBarMoved, this, start);
  153. }
  154. //==============================================================================
  155. void ScrollBar::updateThumbPosition()
  156. {
  157. int newThumbSize = roundToInt (totalRange.getLength() > 0 ? (visibleRange.getLength() * thumbAreaSize) / totalRange.getLength()
  158. : thumbAreaSize);
  159. if (newThumbSize < getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  160. newThumbSize = jmin (getLookAndFeel().getMinimumScrollbarThumbSize (*this), thumbAreaSize - 1);
  161. if (newThumbSize > thumbAreaSize)
  162. newThumbSize = thumbAreaSize;
  163. int newThumbStart = thumbAreaStart;
  164. if (totalRange.getLength() > visibleRange.getLength())
  165. newThumbStart += roundToInt (((visibleRange.getStart() - totalRange.getStart()) * (thumbAreaSize - newThumbSize))
  166. / (totalRange.getLength() - visibleRange.getLength()));
  167. setVisible ((! autohides) || (totalRange.getLength() > visibleRange.getLength() && 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::setButtonVisibility (const bool buttonsAreVisible)
  194. {
  195. upButton = nullptr;
  196. downButton = nullptr;
  197. if (buttonsAreVisible)
  198. {
  199. addAndMakeVisible (upButton = new ScrollbarButton (vertical ? 0 : 3, *this));
  200. addAndMakeVisible (downButton = new ScrollbarButton (vertical ? 2 : 1, *this));
  201. setButtonRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  202. }
  203. updateThumbPosition();
  204. }
  205. void ScrollBar::setAutoHide (const bool shouldHideWhenFullRange)
  206. {
  207. autohides = shouldHideWhenFullRange;
  208. updateThumbPosition();
  209. }
  210. bool ScrollBar::autoHides() const noexcept
  211. {
  212. return autohides;
  213. }
  214. //==============================================================================
  215. void ScrollBar::paint (Graphics& g)
  216. {
  217. if (thumbAreaSize > 0)
  218. {
  219. LookAndFeel& lf = getLookAndFeel();
  220. const int thumb = (thumbAreaSize > lf.getMinimumScrollbarThumbSize (*this))
  221. ? thumbSize : 0;
  222. if (vertical)
  223. {
  224. lf.drawScrollbar (g, *this,
  225. 0, thumbAreaStart,
  226. getWidth(), thumbAreaSize,
  227. vertical,
  228. thumbStart, thumb,
  229. isMouseOver(), isMouseButtonDown());
  230. }
  231. else
  232. {
  233. lf.drawScrollbar (g, *this,
  234. thumbAreaStart, 0,
  235. thumbAreaSize, getHeight(),
  236. vertical,
  237. thumbStart, thumb,
  238. isMouseOver(), isMouseButtonDown());
  239. }
  240. }
  241. }
  242. void ScrollBar::lookAndFeelChanged()
  243. {
  244. setComponentEffect (getLookAndFeel().getScrollbarEffect());
  245. }
  246. void ScrollBar::resized()
  247. {
  248. const int length = vertical ? getHeight() : getWidth();
  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&,
  317. float wheelIncrementX,
  318. float wheelIncrementY)
  319. {
  320. float increment = 10.0f * (vertical ? wheelIncrementY : wheelIncrementX);
  321. if (increment < 0)
  322. increment = jmin (increment, -1.0f);
  323. else if (increment > 0)
  324. increment = jmax (increment, 1.0f);
  325. setCurrentRange (visibleRange - singleStepSize * increment);
  326. }
  327. void ScrollBar::timerCallback()
  328. {
  329. if (isMouseButtonDown())
  330. {
  331. startTimer (40);
  332. if (lastMousePos < thumbStart)
  333. setCurrentRange (visibleRange - visibleRange.getLength());
  334. else if (lastMousePos > thumbStart + thumbSize)
  335. setCurrentRangeStart (visibleRange.getEnd());
  336. }
  337. else
  338. {
  339. stopTimer();
  340. }
  341. }
  342. bool ScrollBar::keyPressed (const KeyPress& key)
  343. {
  344. if (! isVisible())
  345. return false;
  346. if (key.isKeyCode (KeyPress::upKey)
  347. || key.isKeyCode (KeyPress::leftKey)) moveScrollbarInSteps (-1);
  348. else if (key.isKeyCode (KeyPress::downKey)
  349. || key.isKeyCode (KeyPress::rightKey)) moveScrollbarInSteps (1);
  350. else if (key.isKeyCode (KeyPress::pageUpKey)) moveScrollbarInPages (-1);
  351. else if (key.isKeyCode (KeyPress::pageDownKey)) moveScrollbarInPages (1);
  352. else if (key.isKeyCode (KeyPress::homeKey)) scrollToTop();
  353. else if (key.isKeyCode (KeyPress::endKey)) scrollToBottom();
  354. else return false;
  355. return true;
  356. }
  357. END_JUCE_NAMESPACE