Audio plugin host https://kx.studio/carla
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.

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