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.

juce_ScrollBar.h 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. /** Returns the current step size.
  161. @see setSingleStepSize
  162. */
  163. double getSingleStepSize() const noexcept { return singleStepSize; }
  164. /** Moves the scrollbar by a number of single-steps.
  165. This will move the bar by a multiple of its single-step interval (as
  166. specified using the setSingleStepSize() method).
  167. A positive value here will move the bar down or to the right, a negative
  168. value moves it up or to the left.
  169. @param howManySteps the number of steps to move the scrollbar
  170. @param notification whether to send a notification of the change to listeners.
  171. A notification will only be sent if the position has changed.
  172. @returns true if the scrollbar's position actually changed.
  173. */
  174. bool moveScrollbarInSteps (int howManySteps,
  175. NotificationType notification = sendNotificationAsync);
  176. /** Moves the scroll bar up or down in pages.
  177. This will move the bar by a multiple of its current thumb size, effectively
  178. doing a page-up or down.
  179. A positive value here will move the bar down or to the right, a negative
  180. value moves it up or to the left.
  181. @param howManyPages the number of pages to move the scrollbar
  182. @param notification whether to send a notification of the change to listeners.
  183. A notification will only be sent if the position has changed.
  184. @returns true if the scrollbar's position actually changed.
  185. */
  186. bool moveScrollbarInPages (int howManyPages,
  187. NotificationType notification = sendNotificationAsync);
  188. /** Scrolls to the top (or left).
  189. This is the same as calling setCurrentRangeStart (getMinimumRangeLimit());
  190. @param notification whether to send a notification of the change to listeners.
  191. A notification will only be sent if the position has changed.
  192. @returns true if the scrollbar's position actually changed.
  193. */
  194. bool scrollToTop (NotificationType notification = sendNotificationAsync);
  195. /** Scrolls to the bottom (or right).
  196. This is the same as calling setCurrentRangeStart (getMaximumRangeLimit() - getCurrentRangeSize());
  197. @param notification whether to send a notification of the change to listeners.
  198. A notification will only be sent if the position has changed.
  199. @returns true if the scrollbar's position actually changed.
  200. */
  201. bool scrollToBottom (NotificationType notification = sendNotificationAsync);
  202. /** Changes the delay before the up and down buttons autorepeat when they are held
  203. down.
  204. For an explanation of what the parameters are for, see Button::setRepeatSpeed().
  205. @see Button::setRepeatSpeed
  206. */
  207. void setButtonRepeatSpeed (int initialDelayInMillisecs,
  208. int repeatDelayInMillisecs,
  209. int minimumDelayInMillisecs = -1);
  210. //==============================================================================
  211. /** A set of colour IDs to use to change the colour of various aspects of the component.
  212. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  213. methods.
  214. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  215. */
  216. enum ColourIds
  217. {
  218. backgroundColourId = 0x1000300, /**< The background colour of the scrollbar. */
  219. thumbColourId = 0x1000400, /**< A base colour to use for the thumb. The look and feel will probably use variations on this colour. */
  220. 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. */
  221. };
  222. //==============================================================================
  223. /**
  224. A class for receiving events from a ScrollBar.
  225. You can register a ScrollBar::Listener with a ScrollBar using the ScrollBar::addListener()
  226. method, and it will be called when the bar's position changes.
  227. @see ScrollBar::addListener, ScrollBar::removeListener
  228. */
  229. class JUCE_API Listener
  230. {
  231. public:
  232. /** Destructor. */
  233. virtual ~Listener() = default;
  234. /** Called when a ScrollBar is moved.
  235. @param scrollBarThatHasMoved the bar that has moved
  236. @param newRangeStart the new range start of this bar
  237. */
  238. virtual void scrollBarMoved (ScrollBar* scrollBarThatHasMoved,
  239. double newRangeStart) = 0;
  240. };
  241. /** Registers a listener that will be called when the scrollbar is moved. */
  242. void addListener (Listener* listener);
  243. /** Deregisters a previously-registered listener. */
  244. void removeListener (Listener* listener);
  245. //==============================================================================
  246. /** This abstract base class is implemented by LookAndFeel classes to provide
  247. scrollbar-drawing functionality.
  248. */
  249. struct JUCE_API LookAndFeelMethods
  250. {
  251. virtual ~LookAndFeelMethods() = default;
  252. virtual bool areScrollbarButtonsVisible() = 0;
  253. /** Draws one of the buttons on a scrollbar.
  254. @param g the context to draw into
  255. @param scrollbar the bar itself
  256. @param width the width of the button
  257. @param height the height of the button
  258. @param buttonDirection the direction of the button, where 0 = up, 1 = right, 2 = down, 3 = left
  259. @param isScrollbarVertical true if it's a vertical bar, false if horizontal
  260. @param isMouseOverButton whether the mouse is currently over the button (also true if it's held down)
  261. @param isButtonDown whether the mouse button's held down
  262. */
  263. virtual void drawScrollbarButton (Graphics& g,
  264. ScrollBar& scrollbar,
  265. int width, int height,
  266. int buttonDirection,
  267. bool isScrollbarVertical,
  268. bool isMouseOverButton,
  269. bool isButtonDown) = 0;
  270. /** Draws the thumb area of a scrollbar.
  271. @param g the context to draw into
  272. @param scrollbar the bar itself
  273. @param x the x position of the left edge of the thumb area to draw in
  274. @param y the y position of the top edge of the thumb area to draw in
  275. @param width the width of the thumb area to draw in
  276. @param height the height of the thumb area to draw in
  277. @param isScrollbarVertical true if it's a vertical bar, false if horizontal
  278. @param thumbStartPosition for vertical bars, the y coordinate of the top of the
  279. thumb, or its x position for horizontal bars
  280. @param thumbSize for vertical bars, the height of the thumb, or its width for
  281. horizontal bars. This may be 0 if the thumb shouldn't be drawn.
  282. @param isMouseOver whether the mouse is over the thumb area, also true if the mouse is
  283. currently dragging the thumb
  284. @param isMouseDown whether the mouse is currently dragging the scrollbar
  285. */
  286. virtual void drawScrollbar (Graphics& g, ScrollBar& scrollbar,
  287. int x, int y, int width, int height,
  288. bool isScrollbarVertical,
  289. int thumbStartPosition,
  290. int thumbSize,
  291. bool isMouseOver,
  292. bool isMouseDown) = 0;
  293. /** Returns the component effect to use for a scrollbar */
  294. virtual ImageEffectFilter* getScrollbarEffect() = 0;
  295. /** Returns the minimum length in pixels to use for a scrollbar thumb. */
  296. virtual int getMinimumScrollbarThumbSize (ScrollBar&) = 0;
  297. /** Returns the default thickness to use for a scrollbar. */
  298. virtual int getDefaultScrollbarWidth() = 0;
  299. /** Returns the length in pixels to use for a scrollbar button. */
  300. virtual int getScrollbarButtonSize (ScrollBar&) = 0;
  301. };
  302. //==============================================================================
  303. /** @internal */
  304. bool keyPressed (const KeyPress&) override;
  305. /** @internal */
  306. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  307. /** @internal */
  308. void lookAndFeelChanged() override;
  309. /** @internal */
  310. void mouseDown (const MouseEvent&) override;
  311. /** @internal */
  312. void mouseDrag (const MouseEvent&) override;
  313. /** @internal */
  314. void mouseUp (const MouseEvent&) override;
  315. /** @internal */
  316. void paint (Graphics&) override;
  317. /** @internal */
  318. void resized() override;
  319. /** @internal */
  320. void parentHierarchyChanged() override;
  321. /** @internal */
  322. void setVisible (bool) override;
  323. private:
  324. //==============================================================================
  325. Range<double> totalRange { 0.0, 1.0 }, visibleRange { 0.0, 1.0 };
  326. double singleStepSize = 0.1, dragStartRange = 0;
  327. int thumbAreaStart = 0, thumbAreaSize = 0, thumbStart = 0, thumbSize = 0;
  328. int dragStartMousePos = 0, lastMousePos = 0;
  329. int initialDelayInMillisecs = 100, repeatDelayInMillisecs = 50, minimumDelayInMillisecs = 10;
  330. bool vertical, isDraggingThumb = false, autohides = true, userVisibilityFlag = false;
  331. class ScrollbarButton;
  332. std::unique_ptr<ScrollbarButton> upButton, downButton;
  333. ListenerList<Listener> listeners;
  334. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  335. void handleAsyncUpdate() override;
  336. void updateThumbPosition();
  337. void timerCallback() override;
  338. bool getVisibility() const noexcept;
  339. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollBar)
  340. };
  341. } // namespace juce