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.

331 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. #ifndef __JUCE_SCROLLBAR_JUCEHEADER__
  19. #define __JUCE_SCROLLBAR_JUCEHEADER__
  20. #include "../buttons/juce_Button.h"
  21. class Viewport;
  22. //==============================================================================
  23. /**
  24. A scrollbar component.
  25. To use a scrollbar, set up its total range using the setRangeLimits() method - this
  26. sets the range of values it can represent. Then you can use setCurrentRange() to
  27. change the position and size of the scrollbar's 'thumb'.
  28. Registering a ScrollBar::Listener with the scrollbar will allow you to find out when
  29. the user moves it, and you can use the getCurrentRangeStart() to find out where
  30. they moved it to.
  31. The scrollbar will adjust its own visibility according to whether its thumb size
  32. allows it to actually be scrolled.
  33. For most purposes, it's probably easier to use a Viewport or ListBox
  34. instead of handling a scrollbar directly.
  35. @see ScrollBar::Listener
  36. */
  37. class JUCE_API ScrollBar : public Component,
  38. private AsyncUpdater,
  39. private Timer
  40. {
  41. public:
  42. //==============================================================================
  43. /** Creates a Scrollbar.
  44. @param isVertical whether it should be a vertical or horizontal bar
  45. @param buttonsAreVisible whether to show the up/down or left/right buttons
  46. */
  47. ScrollBar (bool isVertical);
  48. /** Destructor. */
  49. ~ScrollBar();
  50. //==============================================================================
  51. /** Returns true if the scrollbar is vertical, false if it's horizontal. */
  52. bool isVertical() const noexcept { return vertical; }
  53. /** Changes the scrollbar's direction.
  54. You'll also need to resize the bar appropriately - this just changes its internal
  55. layout.
  56. @param shouldBeVertical true makes it vertical; false makes it horizontal.
  57. */
  58. void setOrientation (bool shouldBeVertical);
  59. /** Tells the scrollbar whether to make itself invisible when not needed.
  60. The default behaviour is for a scrollbar to become invisible when the thumb
  61. fills the whole of its range (i.e. when it can't be moved). Setting this
  62. value to false forces the bar to always be visible.
  63. @see autoHides()
  64. */
  65. void setAutoHide (bool shouldHideWhenFullRange);
  66. /** Returns true if this scrollbar is set to auto-hide when its thumb is as big
  67. as its maximum range.
  68. @see setAutoHide
  69. */
  70. bool autoHides() const noexcept;
  71. //==============================================================================
  72. /** Sets the minimum and maximum values that the bar will move between.
  73. The bar's thumb will always be constrained so that the entire thumb lies
  74. within this range.
  75. @see setCurrentRange
  76. */
  77. void setRangeLimits (const Range<double>& newRangeLimit);
  78. /** Sets the minimum and maximum values that the bar will move between.
  79. The bar's thumb will always be constrained so that the entire thumb lies
  80. within this range.
  81. @see setCurrentRange
  82. */
  83. void setRangeLimits (double minimum, double maximum);
  84. /** Returns the current limits on the thumb position.
  85. @see setRangeLimits
  86. */
  87. const Range<double> getRangeLimit() const noexcept { return totalRange; }
  88. /** Returns the lower value that the thumb can be set to.
  89. This is the value set by setRangeLimits().
  90. */
  91. double getMinimumRangeLimit() const noexcept { return totalRange.getStart(); }
  92. /** Returns the upper value that the thumb can be set to.
  93. This is the value set by setRangeLimits().
  94. */
  95. double getMaximumRangeLimit() const noexcept { return totalRange.getEnd(); }
  96. //==============================================================================
  97. /** Changes the position of the scrollbar's 'thumb'.
  98. If this method call actually changes the scrollbar's position, it will trigger an
  99. asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
  100. are registered.
  101. @see getCurrentRange. setCurrentRangeStart
  102. */
  103. void setCurrentRange (const Range<double>& newRange);
  104. /** Changes the position of the scrollbar's 'thumb'.
  105. This sets both the position and size of the thumb - to just set the position without
  106. changing the size, you can use setCurrentRangeStart().
  107. If this method call actually changes the scrollbar's position, it will trigger an
  108. asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
  109. are registered.
  110. @param newStart the top (or left) of the thumb, in the range
  111. getMinimumRangeLimit() <= newStart <= getMaximumRangeLimit(). If the
  112. value is beyond these limits, it will be clipped.
  113. @param newSize the size of the thumb, such that
  114. getMinimumRangeLimit() <= newStart + newSize <= getMaximumRangeLimit(). If the
  115. size is beyond these limits, it will be clipped.
  116. @see setCurrentRangeStart, getCurrentRangeStart, getCurrentRangeSize
  117. */
  118. void setCurrentRange (double newStart, double newSize);
  119. /** Moves the bar's thumb position.
  120. This will move the thumb position without changing the thumb size. Note
  121. that the maximum thumb start position is (getMaximumRangeLimit() - getCurrentRangeSize()).
  122. If this method call actually changes the scrollbar's position, it will trigger an
  123. asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
  124. are registered.
  125. @see setCurrentRange
  126. */
  127. void setCurrentRangeStart (double newStart);
  128. /** Returns the current thumb range.
  129. @see getCurrentRange, setCurrentRange
  130. */
  131. const Range<double> getCurrentRange() const noexcept { return visibleRange; }
  132. /** Returns the position of the top of the thumb.
  133. @see getCurrentRange, setCurrentRangeStart
  134. */
  135. double getCurrentRangeStart() const noexcept { return visibleRange.getStart(); }
  136. /** Returns the current size of the thumb.
  137. @see getCurrentRange, setCurrentRange
  138. */
  139. double getCurrentRangeSize() const noexcept { return visibleRange.getLength(); }
  140. //==============================================================================
  141. /** Sets the amount by which the up and down buttons will move the bar.
  142. The value here is in terms of the total range, and is added or subtracted
  143. from the thumb position when the user clicks an up/down (or left/right) button.
  144. */
  145. void setSingleStepSize (double newSingleStepSize);
  146. /** Moves the scrollbar by a number of single-steps.
  147. This will move the bar by a multiple of its single-step interval (as
  148. specified using the setSingleStepSize() method).
  149. A positive value here will move the bar down or to the right, a negative
  150. value moves it up or to the left.
  151. */
  152. void moveScrollbarInSteps (int howManySteps);
  153. /** Moves the scroll bar up or down in pages.
  154. This will move the bar by a multiple of its current thumb size, effectively
  155. doing a page-up or down.
  156. A positive value here will move the bar down or to the right, a negative
  157. value moves it up or to the left.
  158. */
  159. void moveScrollbarInPages (int howManyPages);
  160. /** Scrolls to the top (or left).
  161. This is the same as calling setCurrentRangeStart (getMinimumRangeLimit());
  162. */
  163. void scrollToTop();
  164. /** Scrolls to the bottom (or right).
  165. This is the same as calling setCurrentRangeStart (getMaximumRangeLimit() - getCurrentRangeSize());
  166. */
  167. void scrollToBottom();
  168. /** Changes the delay before the up and down buttons autorepeat when they are held
  169. down.
  170. For an explanation of what the parameters are for, see Button::setRepeatSpeed().
  171. @see Button::setRepeatSpeed
  172. */
  173. void setButtonRepeatSpeed (int initialDelayInMillisecs,
  174. int repeatDelayInMillisecs,
  175. int minimumDelayInMillisecs = -1);
  176. //==============================================================================
  177. /** A set of colour IDs to use to change the colour of various aspects of the component.
  178. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  179. methods.
  180. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  181. */
  182. enum ColourIds
  183. {
  184. backgroundColourId = 0x1000300, /**< The background colour of the scrollbar. */
  185. thumbColourId = 0x1000400, /**< A base colour to use for the thumb. The look and feel will probably use variations on this colour. */
  186. trackColourId = 0x1000401 /**< A base colour to use for the slot area of the bar. The look and feel will probably use variations on this colour. */
  187. };
  188. //==============================================================================
  189. /**
  190. A class for receiving events from a ScrollBar.
  191. You can register a ScrollBar::Listener with a ScrollBar using the ScrollBar::addListener()
  192. method, and it will be called when the bar's position changes.
  193. @see ScrollBar::addListener, ScrollBar::removeListener
  194. */
  195. class JUCE_API Listener
  196. {
  197. public:
  198. /** Destructor. */
  199. virtual ~Listener() {}
  200. /** Called when a ScrollBar is moved.
  201. @param scrollBarThatHasMoved the bar that has moved
  202. @param newRangeStart the new range start of this bar
  203. */
  204. virtual void scrollBarMoved (ScrollBar* scrollBarThatHasMoved,
  205. double newRangeStart) = 0;
  206. };
  207. /** Registers a listener that will be called when the scrollbar is moved. */
  208. void addListener (Listener* listener);
  209. /** Deregisters a previously-registered listener. */
  210. void removeListener (Listener* listener);
  211. //==============================================================================
  212. /** @internal */
  213. bool keyPressed (const KeyPress&);
  214. /** @internal */
  215. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&);
  216. /** @internal */
  217. void lookAndFeelChanged();
  218. /** @internal */
  219. void mouseDown (const MouseEvent&);
  220. /** @internal */
  221. void mouseDrag (const MouseEvent&);
  222. /** @internal */
  223. void mouseUp (const MouseEvent&);
  224. /** @internal */
  225. void paint (Graphics&);
  226. /** @internal */
  227. void resized();
  228. private:
  229. //==============================================================================
  230. Range <double> totalRange, visibleRange;
  231. double singleStepSize, dragStartRange;
  232. int thumbAreaStart, thumbAreaSize, thumbStart, thumbSize;
  233. int dragStartMousePos, lastMousePos;
  234. int initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs;
  235. bool vertical, isDraggingThumb, autohides;
  236. class ScrollbarButton;
  237. friend class ScopedPointer<ScrollbarButton>;
  238. ScopedPointer<ScrollbarButton> upButton, downButton;
  239. ListenerList <Listener> listeners;
  240. void handleAsyncUpdate();
  241. void updateThumbPosition();
  242. void timerCallback();
  243. friend class Viewport;
  244. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollBar);
  245. };
  246. /** This typedef is just for compatibility with old code - newer code should use the ScrollBar::Listener class directly. */
  247. typedef ScrollBar::Listener ScrollBarListener;
  248. #endif // __JUCE_SCROLLBAR_JUCEHEADER__