PageRenderTime 453ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/JuceLibraryCode/modules/juce_gui_basics/layout/juce_ScrollBar.cpp

https://bitbucket.org/tallen92/physical-modelling-synth
C++ | 416 lines | 317 code | 70 blank | 29 comment | 69 complexity | 030f2b149df1289560f74c25f4a36297 MD5 | raw file
  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. {
  81. const Range<double> constrainedRange (totalRange.constrainRange (newRange));
  82. if (visibleRange != constrainedRange)
  83. {
  84. visibleRange = constrainedRange;
  85. updateThumbPosition();
  86. triggerAsyncUpdate();
  87. return true;
  88. }
  89. return false;
  90. }
  91. void ScrollBar::setCurrentRange (const double newStart, const double newSize)
  92. {
  93. setCurrentRange (Range<double> (newStart, newStart + newSize));
  94. }
  95. void ScrollBar::setCurrentRangeStart (const double newStart)
  96. {
  97. setCurrentRange (visibleRange.movedToStartAt (newStart));
  98. }
  99. void ScrollBar::setSingleStepSize (const double newSingleStepSize) noexcept
  100. {
  101. singleStepSize = newSingleStepSize;
  102. }
  103. bool ScrollBar::moveScrollbarInSteps (const int howManySteps)
  104. {
  105. return setCurrentRange (visibleRange + howManySteps * singleStepSize);
  106. }
  107. bool ScrollBar::moveScrollbarInPages (const int howManyPages)
  108. {
  109. return setCurrentRange (visibleRange + howManyPages * visibleRange.getLength());
  110. }
  111. bool ScrollBar::scrollToTop()
  112. {
  113. return setCurrentRange (visibleRange.movedToStartAt (getMinimumRangeLimit()));
  114. }
  115. bool ScrollBar::scrollToBottom()
  116. {
  117. return setCurrentRange (visibleRange.movedToEndAt (getMaximumRangeLimit()));
  118. }
  119. void ScrollBar::setButtonRepeatSpeed (const int initialDelayInMillisecs_,
  120. const int repeatDelayInMillisecs_,
  121. const int minimumDelayInMillisecs_)
  122. {
  123. initialDelayInMillisecs = initialDelayInMillisecs_;
  124. repeatDelayInMillisecs = repeatDelayInMillisecs_;
  125. minimumDelayInMillisecs = minimumDelayInMillisecs_;
  126. if (upButton != nullptr)
  127. {
  128. upButton ->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  129. downButton->setRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  130. }
  131. }
  132. //==============================================================================
  133. void ScrollBar::addListener (Listener* const listener)
  134. {
  135. listeners.add (listener);
  136. }
  137. void ScrollBar::removeListener (Listener* const listener)
  138. {
  139. listeners.remove (listener);
  140. }
  141. void ScrollBar::handleAsyncUpdate()
  142. {
  143. double start = visibleRange.getStart(); // (need to use a temp variable for VC7 compatibility)
  144. listeners.call (&ScrollBar::Listener::scrollBarMoved, this, start);
  145. }
  146. //==============================================================================
  147. void ScrollBar::updateThumbPosition()
  148. {
  149. int newThumbSize = roundToInt (totalRange.getLength() > 0 ? (visibleRange.getLength() * thumbAreaSize) / totalRange.getLength()
  150. : thumbAreaSize);
  151. LookAndFeel& lf = getLookAndFeel();
  152. if (newThumbSize < lf.getMinimumScrollbarThumbSize (*this))
  153. newThumbSize = jmin (lf.getMinimumScrollbarThumbSize (*this), thumbAreaSize - 1);
  154. if (newThumbSize > thumbAreaSize)
  155. newThumbSize = thumbAreaSize;
  156. int newThumbStart = thumbAreaStart;
  157. if (totalRange.getLength() > visibleRange.getLength())
  158. newThumbStart += roundToInt (((visibleRange.getStart() - totalRange.getStart()) * (thumbAreaSize - newThumbSize))
  159. / (totalRange.getLength() - visibleRange.getLength()));
  160. setVisible ((! autohides) || (totalRange.getLength() > visibleRange.getLength() && visibleRange.getLength() > 0.0));
  161. if (thumbStart != newThumbStart || thumbSize != newThumbSize)
  162. {
  163. const int repaintStart = jmin (thumbStart, newThumbStart) - 4;
  164. const int repaintSize = jmax (thumbStart + thumbSize, newThumbStart + newThumbSize) + 8 - repaintStart;
  165. if (vertical)
  166. repaint (0, repaintStart, getWidth(), repaintSize);
  167. else
  168. repaint (repaintStart, 0, repaintSize, getHeight());
  169. thumbStart = newThumbStart;
  170. thumbSize = newThumbSize;
  171. }
  172. }
  173. void ScrollBar::setOrientation (const bool shouldBeVertical)
  174. {
  175. if (vertical != shouldBeVertical)
  176. {
  177. vertical = shouldBeVertical;
  178. if (upButton != nullptr)
  179. {
  180. upButton->direction = vertical ? 0 : 3;
  181. downButton->direction = vertical ? 2 : 1;
  182. }
  183. updateThumbPosition();
  184. }
  185. }
  186. void ScrollBar::setAutoHide (const bool shouldHideWhenFullRange)
  187. {
  188. autohides = shouldHideWhenFullRange;
  189. updateThumbPosition();
  190. }
  191. bool ScrollBar::autoHides() const noexcept
  192. {
  193. return autohides;
  194. }
  195. //==============================================================================
  196. void ScrollBar::paint (Graphics& g)
  197. {
  198. if (thumbAreaSize > 0)
  199. {
  200. LookAndFeel& lf = getLookAndFeel();
  201. const int thumb = (thumbAreaSize > lf.getMinimumScrollbarThumbSize (*this))
  202. ? thumbSize : 0;
  203. if (vertical)
  204. lf.drawScrollbar (g, *this, 0, thumbAreaStart, getWidth(), thumbAreaSize,
  205. vertical, thumbStart, thumb, isMouseOver(), isMouseButtonDown());
  206. else
  207. lf.drawScrollbar (g, *this, thumbAreaStart, 0, thumbAreaSize, getHeight(),
  208. vertical, thumbStart, thumb, isMouseOver(), isMouseButtonDown());
  209. }
  210. }
  211. void ScrollBar::lookAndFeelChanged()
  212. {
  213. setComponentEffect (getLookAndFeel().getScrollbarEffect());
  214. if (isVisible())
  215. resized();
  216. }
  217. void ScrollBar::resized()
  218. {
  219. const int length = vertical ? getHeight() : getWidth();
  220. LookAndFeel& lf = getLookAndFeel();
  221. const bool buttonsVisible = lf.areScrollbarButtonsVisible();
  222. int buttonSize = 0;
  223. if (buttonsVisible)
  224. {
  225. if (upButton == nullptr)
  226. {
  227. addAndMakeVisible (upButton = new ScrollbarButton (vertical ? 0 : 3, *this));
  228. addAndMakeVisible (downButton = new ScrollbarButton (vertical ? 2 : 1, *this));
  229. setButtonRepeatSpeed (initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs);
  230. }
  231. buttonSize = jmin (lf.getScrollbarButtonSize (*this), length / 2);
  232. }
  233. else
  234. {
  235. upButton = nullptr;
  236. downButton = nullptr;
  237. }
  238. if (length < 32 + lf.getMinimumScrollbarThumbSize (*this))
  239. {
  240. thumbAreaStart = length / 2;
  241. thumbAreaSize = 0;
  242. }
  243. else
  244. {
  245. thumbAreaStart = buttonSize;
  246. thumbAreaSize = length - (buttonSize << 1);
  247. }
  248. if (upButton != nullptr)
  249. {
  250. if (vertical)
  251. {
  252. upButton->setBounds (0, 0, getWidth(), buttonSize);
  253. downButton->setBounds (0, thumbAreaStart + thumbAreaSize, getWidth(), buttonSize);
  254. }
  255. else
  256. {
  257. upButton->setBounds (0, 0, buttonSize, getHeight());
  258. downButton->setBounds (thumbAreaStart + thumbAreaSize, 0, buttonSize, getHeight());
  259. }
  260. }
  261. updateThumbPosition();
  262. }
  263. void ScrollBar::mouseDown (const MouseEvent& e)
  264. {
  265. isDraggingThumb = false;
  266. lastMousePos = vertical ? e.y : e.x;
  267. dragStartMousePos = lastMousePos;
  268. dragStartRange = visibleRange.getStart();
  269. if (dragStartMousePos < thumbStart)
  270. {
  271. moveScrollbarInPages (-1);
  272. startTimer (400);
  273. }
  274. else if (dragStartMousePos >= thumbStart + thumbSize)
  275. {
  276. moveScrollbarInPages (1);
  277. startTimer (400);
  278. }
  279. else
  280. {
  281. isDraggingThumb = (thumbAreaSize > getLookAndFeel().getMinimumScrollbarThumbSize (*this))
  282. && (thumbAreaSize > thumbSize);
  283. }
  284. }
  285. void ScrollBar::mouseDrag (const MouseEvent& e)
  286. {
  287. const int mousePos = vertical ? e.y : e.x;
  288. if (isDraggingThumb && lastMousePos != mousePos)
  289. {
  290. const int deltaPixels = mousePos - dragStartMousePos;
  291. setCurrentRangeStart (dragStartRange
  292. + deltaPixels * (totalRange.getLength() - visibleRange.getLength())
  293. / (thumbAreaSize - thumbSize));
  294. }
  295. lastMousePos = mousePos;
  296. }
  297. void ScrollBar::mouseUp (const MouseEvent&)
  298. {
  299. isDraggingThumb = false;
  300. stopTimer();
  301. repaint();
  302. }
  303. void ScrollBar::mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel)
  304. {
  305. float increment = 10.0f * (vertical ? wheel.deltaY : wheel.deltaX);
  306. if (increment < 0)
  307. increment = jmin (increment, -1.0f);
  308. else if (increment > 0)
  309. increment = jmax (increment, 1.0f);
  310. setCurrentRange (visibleRange - singleStepSize * increment);
  311. }
  312. void ScrollBar::timerCallback()
  313. {
  314. if (isMouseButtonDown())
  315. {
  316. startTimer (40);
  317. if (lastMousePos < thumbStart)
  318. setCurrentRange (visibleRange - visibleRange.getLength());
  319. else if (lastMousePos > thumbStart + thumbSize)
  320. setCurrentRangeStart (visibleRange.getEnd());
  321. }
  322. else
  323. {
  324. stopTimer();
  325. }
  326. }
  327. bool ScrollBar::keyPressed (const KeyPress& key)
  328. {
  329. if (isVisible())
  330. {
  331. if (key == KeyPress::upKey || key == KeyPress::leftKey) return moveScrollbarInSteps (-1);
  332. else if (key == KeyPress::downKey || key == KeyPress::rightKey) return moveScrollbarInSteps (1);
  333. else if (key == KeyPress::pageUpKey) return moveScrollbarInPages (-1);
  334. else if (key == KeyPress::pageDownKey) return moveScrollbarInPages (1);
  335. else if (key == KeyPress::homeKey) return scrollToTop();
  336. else if (key == KeyPress::endKey) return scrollToBottom();
  337. }
  338. return false;
  339. }