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.

405 lines
18KB

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