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.

428 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. A scrollbar component.
  18. To use a scrollbar, set up its total range using the setRangeLimits() method - this
  19. sets the range of values it can represent. Then you can use setCurrentRange() to
  20. change the position and size of the scrollbar's 'thumb'.
  21. Registering a ScrollBar::Listener with the scrollbar will allow you to find out when
  22. the user moves it, and you can use the getCurrentRangeStart() to find out where
  23. they moved it to.
  24. The scrollbar will adjust its own visibility according to whether its thumb size
  25. allows it to actually be scrolled.
  26. For most purposes, it's probably easier to use a Viewport or ListBox
  27. instead of handling a scrollbar directly.
  28. @see ScrollBar::Listener
  29. @tags{GUI}
  30. */
  31. class JUCE_API ScrollBar : public Component,
  32. public AsyncUpdater,
  33. private Timer
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates a Scrollbar.
  38. @param isVertical specifies whether the bar should be a vertical or horizontal
  39. */
  40. ScrollBar (bool isVertical);
  41. /** Destructor. */
  42. ~ScrollBar() override;
  43. //==============================================================================
  44. /** Returns true if the scrollbar is vertical, false if it's horizontal. */
  45. bool isVertical() const noexcept { return vertical; }
  46. /** Changes the scrollbar's direction.
  47. You'll also need to resize the bar appropriately - this just changes its internal
  48. layout.
  49. @param shouldBeVertical true makes it vertical; false makes it horizontal.
  50. */
  51. void setOrientation (bool shouldBeVertical);
  52. /** Tells the scrollbar whether to make itself invisible when not needed.
  53. The default behaviour is for a scrollbar to become invisible when the thumb
  54. fills the whole of its range (i.e. when it can't be moved). Setting this
  55. value to false forces the bar to always be visible.
  56. @see autoHides()
  57. */
  58. void setAutoHide (bool shouldHideWhenFullRange);
  59. /** Returns true if this scrollbar is set to auto-hide when its thumb is as big
  60. as its maximum range.
  61. @see setAutoHide
  62. */
  63. bool autoHides() const noexcept;
  64. //==============================================================================
  65. /** Sets the minimum and maximum values that the bar will move between.
  66. The bar's thumb will always be constrained so that the entire thumb lies
  67. within this range.
  68. @param newRangeLimit the new range.
  69. @param notification whether to send a notification of the change to listeners.
  70. A notification will only be sent if the range has changed.
  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. @param minimum the new range minimum.
  79. @param maximum the new range maximum.
  80. @param notification whether to send a notification of the change to listeners.
  81. A notification will only be sent if the range has changed.
  82. @see setCurrentRange
  83. */
  84. void setRangeLimits (double minimum, double maximum,
  85. NotificationType notification = sendNotificationAsync);
  86. /** Returns the current limits on the thumb position.
  87. @see setRangeLimits
  88. */
  89. Range<double> getRangeLimit() const noexcept { return totalRange; }
  90. /** Returns the lower value that the thumb can be set to.
  91. This is the value set by setRangeLimits().
  92. */
  93. double getMinimumRangeLimit() const noexcept { return totalRange.getStart(); }
  94. /** Returns the upper value that the thumb can be set to.
  95. This is the value set by setRangeLimits().
  96. */
  97. double getMaximumRangeLimit() const noexcept { return totalRange.getEnd(); }
  98. //==============================================================================
  99. /** Changes the position of the scrollbar's 'thumb'.
  100. This sets both the position and size of the thumb - to just set the position without
  101. changing the size, you can use setCurrentRangeStart().
  102. If this method call actually changes the scrollbar's position, it will trigger an
  103. asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
  104. are registered.
  105. The notification parameter can be used to optionally send or inhibit a callback to
  106. any scrollbar listeners.
  107. @returns true if the range was changed, or false if nothing was changed.
  108. @see getCurrentRange. setCurrentRangeStart
  109. */
  110. bool setCurrentRange (Range<double> newRange,
  111. NotificationType notification = sendNotificationAsync);
  112. /** Changes the position of the scrollbar's 'thumb'.
  113. This sets both the position and size of the thumb - to just set the position without
  114. changing the size, you can use setCurrentRangeStart().
  115. @param newStart the top (or left) of the thumb, in the range
  116. getMinimumRangeLimit() <= newStart <= getMaximumRangeLimit(). If the
  117. value is beyond these limits, it will be clipped.
  118. @param newSize the size of the thumb, such that
  119. getMinimumRangeLimit() <= newStart + newSize <= getMaximumRangeLimit(). If the
  120. size is beyond these limits, it will be clipped.
  121. @param notification specifies if and how a callback should be made to any listeners
  122. if the range actually changes
  123. @see setCurrentRangeStart, getCurrentRangeStart, getCurrentRangeSize
  124. */
  125. void setCurrentRange (double newStart, double newSize,
  126. NotificationType notification = sendNotificationAsync);
  127. /** Moves the bar's thumb position.
  128. This will move the thumb position without changing the thumb size. Note
  129. that the maximum thumb start position is (getMaximumRangeLimit() - getCurrentRangeSize()).
  130. If this method call actually changes the scrollbar's position, it will trigger an
  131. asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
  132. are registered.
  133. @see setCurrentRange
  134. */
  135. void setCurrentRangeStart (double newStart,
  136. NotificationType notification = sendNotificationAsync);
  137. /** Returns the current thumb range.
  138. @see getCurrentRange, setCurrentRange
  139. */
  140. Range<double> getCurrentRange() const noexcept { return visibleRange; }
  141. /** Returns the position of the top of the thumb.
  142. @see getCurrentRange, setCurrentRangeStart
  143. */
  144. double getCurrentRangeStart() const noexcept { return visibleRange.getStart(); }
  145. /** Returns the current size of the thumb.
  146. @see getCurrentRange, setCurrentRange
  147. */
  148. double getCurrentRangeSize() const noexcept { return visibleRange.getLength(); }
  149. //==============================================================================
  150. /** Sets the amount by which the up and down buttons will move the bar.
  151. The value here is in terms of the total range, and is added or subtracted
  152. from the thumb position when the user clicks an up/down (or left/right) button.
  153. */
  154. void setSingleStepSize (double newSingleStepSize) noexcept;
  155. /** Moves the scrollbar by a number of single-steps.
  156. This will move the bar by a multiple of its single-step interval (as
  157. specified using the setSingleStepSize() method).
  158. A positive value here will move the bar down or to the right, a negative
  159. value moves it up or to the left.
  160. @param howManySteps the number of steps to move the scrollbar
  161. @param notification whether to send a notification of the change to listeners.
  162. A notification will only be sent if the position has changed.
  163. @returns true if the scrollbar's position actually changed.
  164. */
  165. bool moveScrollbarInSteps (int howManySteps,
  166. NotificationType notification = sendNotificationAsync);
  167. /** Moves the scroll bar up or down in pages.
  168. This will move the bar by a multiple of its current thumb size, effectively
  169. doing a page-up or down.
  170. A positive value here will move the bar down or to the right, a negative
  171. value moves it up or to the left.
  172. @param howManyPages the number of pages to move the scrollbar
  173. @param notification whether to send a notification of the change to listeners.
  174. A notification will only be sent if the position has changed.
  175. @returns true if the scrollbar's position actually changed.
  176. */
  177. bool moveScrollbarInPages (int howManyPages,
  178. NotificationType notification = sendNotificationAsync);
  179. /** Scrolls to the top (or left).
  180. This is the same as calling setCurrentRangeStart (getMinimumRangeLimit());
  181. @param notification whether to send a notification of the change to listeners.
  182. A notification will only be sent if the position has changed.
  183. @returns true if the scrollbar's position actually changed.
  184. */
  185. bool scrollToTop (NotificationType notification = sendNotificationAsync);
  186. /** Scrolls to the bottom (or right).
  187. This is the same as calling setCurrentRangeStart (getMaximumRangeLimit() - getCurrentRangeSize());
  188. @param notification whether to send a notification of the change to listeners.
  189. A notification will only be sent if the position has changed.
  190. @returns true if the scrollbar's position actually changed.
  191. */
  192. bool scrollToBottom (NotificationType notification = sendNotificationAsync);
  193. /** Changes the delay before the up and down buttons autorepeat when they are held
  194. down.
  195. For an explanation of what the parameters are for, see Button::setRepeatSpeed().
  196. @see Button::setRepeatSpeed
  197. */
  198. void setButtonRepeatSpeed (int initialDelayInMillisecs,
  199. int repeatDelayInMillisecs,
  200. int minimumDelayInMillisecs = -1);
  201. //==============================================================================
  202. /** A set of colour IDs to use to change the colour of various aspects of the component.
  203. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  204. methods.
  205. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  206. */
  207. enum ColourIds
  208. {
  209. backgroundColourId = 0x1000300, /**< The background colour of the scrollbar. */
  210. thumbColourId = 0x1000400, /**< A base colour to use for the thumb. The look and feel will probably use variations on this colour. */
  211. 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. */
  212. };
  213. //==============================================================================
  214. /**
  215. A class for receiving events from a ScrollBar.
  216. You can register a ScrollBar::Listener with a ScrollBar using the ScrollBar::addListener()
  217. method, and it will be called when the bar's position changes.
  218. @see ScrollBar::addListener, ScrollBar::removeListener
  219. */
  220. class JUCE_API Listener
  221. {
  222. public:
  223. /** Destructor. */
  224. virtual ~Listener() = default;
  225. /** Called when a ScrollBar is moved.
  226. @param scrollBarThatHasMoved the bar that has moved
  227. @param newRangeStart the new range start of this bar
  228. */
  229. virtual void scrollBarMoved (ScrollBar* scrollBarThatHasMoved,
  230. double newRangeStart) = 0;
  231. };
  232. /** Registers a listener that will be called when the scrollbar is moved. */
  233. void addListener (Listener* listener);
  234. /** Deregisters a previously-registered listener. */
  235. void removeListener (Listener* listener);
  236. //==============================================================================
  237. /** This abstract base class is implemented by LookAndFeel classes to provide
  238. scrollbar-drawing functionality.
  239. */
  240. struct JUCE_API LookAndFeelMethods
  241. {
  242. virtual ~LookAndFeelMethods() = default;
  243. virtual bool areScrollbarButtonsVisible() = 0;
  244. /** Draws one of the buttons on a scrollbar.
  245. @param g the context to draw into
  246. @param scrollbar the bar itself
  247. @param width the width of the button
  248. @param height the height of the button
  249. @param buttonDirection the direction of the button, where 0 = up, 1 = right, 2 = down, 3 = left
  250. @param isScrollbarVertical true if it's a vertical bar, false if horizontal
  251. @param isMouseOverButton whether the mouse is currently over the button (also true if it's held down)
  252. @param isButtonDown whether the mouse button's held down
  253. */
  254. virtual void drawScrollbarButton (Graphics& g,
  255. ScrollBar& scrollbar,
  256. int width, int height,
  257. int buttonDirection,
  258. bool isScrollbarVertical,
  259. bool isMouseOverButton,
  260. bool isButtonDown) = 0;
  261. /** Draws the thumb area of a scrollbar.
  262. @param g the context to draw into
  263. @param scrollbar the bar itself
  264. @param x the x position of the left edge of the thumb area to draw in
  265. @param y the y position of the top edge of the thumb area to draw in
  266. @param width the width of the thumb area to draw in
  267. @param height the height of the thumb area to draw in
  268. @param isScrollbarVertical true if it's a vertical bar, false if horizontal
  269. @param thumbStartPosition for vertical bars, the y coordinate of the top of the
  270. thumb, or its x position for horizontal bars
  271. @param thumbSize for vertical bars, the height of the thumb, or its width for
  272. horizontal bars. This may be 0 if the thumb shouldn't be drawn.
  273. @param isMouseOver whether the mouse is over the thumb area, also true if the mouse is
  274. currently dragging the thumb
  275. @param isMouseDown whether the mouse is currently dragging the scrollbar
  276. */
  277. virtual void drawScrollbar (Graphics& g, ScrollBar& scrollbar,
  278. int x, int y, int width, int height,
  279. bool isScrollbarVertical,
  280. int thumbStartPosition,
  281. int thumbSize,
  282. bool isMouseOver,
  283. bool isMouseDown) = 0;
  284. /** Returns the component effect to use for a scrollbar */
  285. virtual ImageEffectFilter* getScrollbarEffect() = 0;
  286. /** Returns the minimum length in pixels to use for a scrollbar thumb. */
  287. virtual int getMinimumScrollbarThumbSize (ScrollBar&) = 0;
  288. /** Returns the default thickness to use for a scrollbar. */
  289. virtual int getDefaultScrollbarWidth() = 0;
  290. /** Returns the length in pixels to use for a scrollbar button. */
  291. virtual int getScrollbarButtonSize (ScrollBar&) = 0;
  292. };
  293. //==============================================================================
  294. /** @internal */
  295. bool keyPressed (const KeyPress&) override;
  296. /** @internal */
  297. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  298. /** @internal */
  299. void lookAndFeelChanged() override;
  300. /** @internal */
  301. void mouseDown (const MouseEvent&) override;
  302. /** @internal */
  303. void mouseDrag (const MouseEvent&) override;
  304. /** @internal */
  305. void mouseUp (const MouseEvent&) override;
  306. /** @internal */
  307. void paint (Graphics&) override;
  308. /** @internal */
  309. void resized() override;
  310. /** @internal */
  311. void parentHierarchyChanged() override;
  312. /** @internal */
  313. void setVisible (bool) override;
  314. private:
  315. //==============================================================================
  316. Range<double> totalRange { 0.0, 1.0 }, visibleRange { 0.0, 1.0 };
  317. double singleStepSize = 0.1, dragStartRange = 0;
  318. int thumbAreaStart = 0, thumbAreaSize = 0, thumbStart = 0, thumbSize = 0;
  319. int dragStartMousePos = 0, lastMousePos = 0;
  320. int initialDelayInMillisecs = 100, repeatDelayInMillisecs = 50, minimumDelayInMillisecs = 10;
  321. bool vertical, isDraggingThumb = false, autohides = true, userVisibilityFlag = false;
  322. class ScrollbarButton;
  323. std::unique_ptr<ScrollbarButton> upButton, downButton;
  324. ListenerList<Listener> listeners;
  325. void handleAsyncUpdate() override;
  326. void updateThumbPosition();
  327. void timerCallback() override;
  328. bool getVisibility() const noexcept;
  329. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollBar)
  330. };
  331. } // namespace juce