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.

340 lines
14KB

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