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.

435 lines
19KB

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