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.

451 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_ScrollBar.h"
  21. #include "../lookandfeel/juce_LookAndFeel.h"
  22. #include "../buttons/juce_Button.h"
  23. //==============================================================================
  24. class ScrollbarButton : public Button
  25. {
  26. public:
  27. int direction;
  28. ScrollbarButton (const int direction_,
  29. ScrollBar& owner_) throw()
  30. : Button (String::empty),
  31. direction (direction_),
  32. owner (owner_)
  33. {
  34. setWantsKeyboardFocus (false);
  35. }
  36. ~ScrollbarButton()
  37. {
  38. }
  39. void paintButton (Graphics& g,
  40. bool isMouseOver,
  41. bool isMouseDown)
  42. {
  43. getLookAndFeel()
  44. .drawScrollbarButton (g, owner,
  45. getWidth(), getHeight(),
  46. direction,
  47. owner.isVertical(),
  48. isMouseOver, isMouseDown);
  49. }
  50. void clicked()
  51. {
  52. owner.moveScrollbarInSteps ((direction == 1 || direction == 2) ? 1 : -1);
  53. }
  54. juce_UseDebuggingNewOperator
  55. private:
  56. ScrollBar& owner;
  57. ScrollbarButton (const ScrollbarButton&);
  58. ScrollbarButton& operator= (const ScrollbarButton&);
  59. };
  60. //==============================================================================
  61. ScrollBar::ScrollBar (const bool vertical_,
  62. const bool buttonsAreVisible)
  63. : minimum (0.0),
  64. maximum (1.0),
  65. rangeStart (0.0),
  66. rangeSize (0.1),
  67. singleStepSize (0.1),
  68. thumbAreaStart (0),
  69. thumbAreaSize (0),
  70. thumbStart (0),
  71. thumbSize (0),
  72. initialDelayInMillisecs (100),
  73. repeatDelayInMillisecs (50),
  74. minimumDelayInMillisecs (10),
  75. vertical (vertical_),
  76. isDraggingThumb (false),
  77. alwaysVisible (false),
  78. upButton (0),
  79. downButton (0)
  80. {
  81. setButtonVisibility (buttonsAreVisible);
  82. setRepaintsOnMouseActivity (true);
  83. setFocusContainer (true);
  84. }
  85. ScrollBar::~ScrollBar()
  86. {
  87. deleteAllChildren();
  88. }
  89. //==============================================================================
  90. void ScrollBar::setRangeLimits (const double newMinimum,
  91. const double newMaximum) throw()
  92. {
  93. minimum = newMinimum;
  94. maximum = newMaximum;
  95. jassert (maximum >= minimum); // these can't be the wrong way round!
  96. setCurrentRangeStart (rangeStart);
  97. updateThumbPosition();
  98. }
  99. void ScrollBar::setCurrentRange (double newStart,
  100. double newSize) throw()
  101. {
  102. newSize = jlimit (0.0, maximum - minimum, newSize);
  103. newStart = jlimit (minimum, maximum - newSize, newStart);
  104. if (rangeStart != newStart
  105. || rangeSize != newSize)
  106. {
  107. rangeStart = newStart;
  108. rangeSize = newSize;
  109. updateThumbPosition();
  110. triggerAsyncUpdate();
  111. }
  112. }
  113. void ScrollBar::setCurrentRangeStart (double newStart) throw()
  114. {
  115. setCurrentRange (newStart, rangeSize);
  116. }
  117. void ScrollBar::setSingleStepSize (const double newSingleStepSize) throw()
  118. {
  119. singleStepSize = newSingleStepSize;
  120. }
  121. void ScrollBar::moveScrollbarInSteps (const int howManySteps) throw()
  122. {
  123. setCurrentRangeStart (rangeStart + howManySteps * singleStepSize);
  124. }
  125. void ScrollBar::moveScrollbarInPages (const int howManyPages) throw()
  126. {
  127. setCurrentRangeStart (rangeStart + howManyPages * rangeSize);
  128. }
  129. void ScrollBar::scrollToTop() throw()
  130. {
  131. setCurrentRangeStart (minimum);
  132. }
  133. void ScrollBar::scrollToBottom() throw()
  134. {
  135. setCurrentRangeStart (maximum - rangeSize);
  136. }
  137. void ScrollBar::setButtonRepeatSpeed (const int initialDelayInMillisecs_,
  138. const int repeatDelayInMillisecs_,
  139. const int minimumDelayInMillisecs_) throw()
  140. {
  141. initialDelayInMillisecs = initialDelayInMillisecs_;
  142. repeatDelayInMillisecs = repeatDelayInMillisecs_;
  143. minimumDelayInMillisecs = minimumDelayInMillisecs_;
  144. if (upButton != 0)
  145. {
  146. upButton->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  147. downButton->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  148. }
  149. }
  150. //==============================================================================
  151. void ScrollBar::addListener (ScrollBarListener* const listener) throw()
  152. {
  153. listeners.add (listener);
  154. }
  155. void ScrollBar::removeListener (ScrollBarListener* const listener) throw()
  156. {
  157. listeners.remove (listener);
  158. }
  159. void ScrollBar::handleAsyncUpdate()
  160. {
  161. listeners.call (&ScrollBarListener::scrollBarMoved, this, rangeStart);
  162. }
  163. //==============================================================================
  164. void ScrollBar::updateThumbPosition() throw()
  165. {
  166. int newThumbSize = roundToInt ((maximum > minimum) ? (rangeSize * thumbAreaSize) / (maximum - minimum)
  167. : thumbAreaSize);
  168. if (newThumbSize < getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  169. newThumbSize = jmin (getLookAndFeel().getMinimumScrollbarThumbSize (*this), thumbAreaSize - 1);
  170. if (newThumbSize > thumbAreaSize)
  171. newThumbSize = thumbAreaSize;
  172. int newThumbStart = thumbAreaStart;
  173. if (maximum - minimum > rangeSize)
  174. newThumbStart += roundToInt (((rangeStart - minimum) * (thumbAreaSize - newThumbSize))
  175. / ((maximum - minimum) - rangeSize));
  176. setVisible (alwaysVisible || (maximum - minimum > rangeSize && rangeSize > 0.0));
  177. if (thumbStart != newThumbStart || thumbSize != newThumbSize)
  178. {
  179. const int repaintStart = jmin (thumbStart, newThumbStart) - 4;
  180. const int repaintSize = jmax (thumbStart + thumbSize, newThumbStart + newThumbSize) + 8 - repaintStart;
  181. if (vertical)
  182. repaint (0, repaintStart, getWidth(), repaintSize);
  183. else
  184. repaint (repaintStart, 0, repaintSize, getHeight());
  185. thumbStart = newThumbStart;
  186. thumbSize = newThumbSize;
  187. }
  188. }
  189. void ScrollBar::setOrientation (const bool shouldBeVertical) throw()
  190. {
  191. if (vertical != shouldBeVertical)
  192. {
  193. vertical = shouldBeVertical;
  194. if (upButton != 0)
  195. {
  196. ((ScrollbarButton*) upButton)->direction = (vertical) ? 0 : 3;
  197. ((ScrollbarButton*) downButton)->direction = (vertical) ? 2 : 1;
  198. }
  199. updateThumbPosition();
  200. }
  201. }
  202. void ScrollBar::setButtonVisibility (const bool buttonsAreVisible)
  203. {
  204. deleteAndZero (upButton);
  205. deleteAndZero (downButton);
  206. if (buttonsAreVisible)
  207. {
  208. addAndMakeVisible (upButton = new ScrollbarButton ((vertical) ? 0 : 3, *this));
  209. addAndMakeVisible (downButton = new ScrollbarButton ((vertical) ? 2 : 1, *this));
  210. setButtonRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  211. }
  212. updateThumbPosition();
  213. }
  214. void ScrollBar::setAutoHide (const bool shouldHideWhenFullRange)
  215. {
  216. alwaysVisible = ! shouldHideWhenFullRange;
  217. updateThumbPosition();
  218. }
  219. //==============================================================================
  220. void ScrollBar::paint (Graphics& g)
  221. {
  222. if (thumbAreaSize > 0)
  223. {
  224. LookAndFeel& lf = getLookAndFeel();
  225. const int thumb = (thumbAreaSize > lf.getMinimumScrollbarThumbSize (*this))
  226. ? thumbSize : 0;
  227. if (vertical)
  228. {
  229. lf.drawScrollbar (g, *this,
  230. 0, thumbAreaStart,
  231. getWidth(), thumbAreaSize,
  232. vertical,
  233. thumbStart, thumb,
  234. isMouseOver(), isMouseButtonDown());
  235. }
  236. else
  237. {
  238. lf.drawScrollbar (g, *this,
  239. thumbAreaStart, 0,
  240. thumbAreaSize, getHeight(),
  241. vertical,
  242. thumbStart, thumb,
  243. isMouseOver(), isMouseButtonDown());
  244. }
  245. }
  246. }
  247. void ScrollBar::lookAndFeelChanged()
  248. {
  249. setComponentEffect (getLookAndFeel().getScrollbarEffect());
  250. }
  251. void ScrollBar::resized()
  252. {
  253. const int length = ((vertical) ? getHeight() : getWidth());
  254. const int buttonSize = (upButton != 0) ? jmin (getLookAndFeel().getScrollbarButtonSize (*this), (length >> 1))
  255. : 0;
  256. if (length < 32 + getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  257. {
  258. thumbAreaStart = length >> 1;
  259. thumbAreaSize = 0;
  260. }
  261. else
  262. {
  263. thumbAreaStart = buttonSize;
  264. thumbAreaSize = length - (buttonSize << 1);
  265. }
  266. if (upButton != 0)
  267. {
  268. if (vertical)
  269. {
  270. upButton->setBounds (0, 0, getWidth(), buttonSize);
  271. downButton->setBounds (0, thumbAreaStart + thumbAreaSize, getWidth(), buttonSize);
  272. }
  273. else
  274. {
  275. upButton->setBounds (0, 0, buttonSize, getHeight());
  276. downButton->setBounds (thumbAreaStart + thumbAreaSize, 0, buttonSize, getHeight());
  277. }
  278. }
  279. updateThumbPosition();
  280. }
  281. void ScrollBar::mouseDown (const MouseEvent& e)
  282. {
  283. isDraggingThumb = false;
  284. lastMousePos = vertical ? e.y : e.x;
  285. dragStartMousePos = lastMousePos;
  286. dragStartRange = rangeStart;
  287. if (dragStartMousePos < thumbStart)
  288. {
  289. moveScrollbarInPages (-1);
  290. startTimer (400);
  291. }
  292. else if (dragStartMousePos >= thumbStart + thumbSize)
  293. {
  294. moveScrollbarInPages (1);
  295. startTimer (400);
  296. }
  297. else
  298. {
  299. isDraggingThumb = (thumbAreaSize > getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  300. && (thumbAreaSize > thumbSize);
  301. }
  302. }
  303. void ScrollBar::mouseDrag (const MouseEvent& e)
  304. {
  305. if (isDraggingThumb)
  306. {
  307. const int deltaPixels = ((vertical) ? e.y : e.x) - dragStartMousePos;
  308. setCurrentRangeStart (dragStartRange
  309. + deltaPixels * ((maximum - minimum) - rangeSize)
  310. / (thumbAreaSize - thumbSize));
  311. }
  312. else
  313. {
  314. lastMousePos = (vertical) ? e.y : e.x;
  315. }
  316. }
  317. void ScrollBar::mouseUp (const MouseEvent&)
  318. {
  319. isDraggingThumb = false;
  320. stopTimer();
  321. repaint();
  322. }
  323. void ScrollBar::mouseWheelMove (const MouseEvent&,
  324. float wheelIncrementX,
  325. float wheelIncrementY)
  326. {
  327. float increment = vertical ? wheelIncrementY : wheelIncrementX;
  328. if (increment < 0)
  329. increment = jmin (increment * 10.0f, -1.0f);
  330. else if (increment > 0)
  331. increment = jmax (increment * 10.0f, 1.0f);
  332. setCurrentRangeStart (rangeStart - singleStepSize * increment);
  333. }
  334. void ScrollBar::timerCallback()
  335. {
  336. if (isMouseButtonDown())
  337. {
  338. startTimer (40);
  339. if (lastMousePos < thumbStart)
  340. setCurrentRangeStart (rangeStart - rangeSize);
  341. else if (lastMousePos > thumbStart + thumbSize)
  342. setCurrentRangeStart (rangeStart + rangeSize);
  343. }
  344. else
  345. {
  346. stopTimer();
  347. }
  348. }
  349. bool ScrollBar::keyPressed (const KeyPress& key)
  350. {
  351. if (! isVisible())
  352. return false;
  353. if (key.isKeyCode (KeyPress::upKey) || key.isKeyCode (KeyPress::leftKey))
  354. moveScrollbarInSteps (-1);
  355. else if (key.isKeyCode (KeyPress::downKey) || key.isKeyCode (KeyPress::rightKey))
  356. moveScrollbarInSteps (1);
  357. else if (key.isKeyCode (KeyPress::pageUpKey))
  358. moveScrollbarInPages (-1);
  359. else if (key.isKeyCode (KeyPress::pageDownKey))
  360. moveScrollbarInPages (1);
  361. else if (key.isKeyCode (KeyPress::homeKey))
  362. scrollToTop();
  363. else if (key.isKeyCode (KeyPress::endKey))
  364. scrollToBottom();
  365. else
  366. return false;
  367. return true;
  368. }
  369. END_JUCE_NAMESPACE