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.

407 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. /** This abstract base class is implemented by LookAndFeel classes to provide
  224. scrollbar-drawing functionality.
  225. */
  226. struct JUCE_API LookAndFeelMethods
  227. {
  228. virtual ~LookAndFeelMethods() {}
  229. virtual bool areScrollbarButtonsVisible() = 0;
  230. /** Draws one of the buttons on a scrollbar.
  231. @param g the context to draw into
  232. @param scrollbar the bar itself
  233. @param width the width of the button
  234. @param height the height of the button
  235. @param buttonDirection the direction of the button, where 0 = up, 1 = right, 2 = down, 3 = left
  236. @param isScrollbarVertical true if it's a vertical bar, false if horizontal
  237. @param isMouseOverButton whether the mouse is currently over the button (also true if it's held down)
  238. @param isButtonDown whether the mouse button's held down
  239. */
  240. virtual void drawScrollbarButton (Graphics& g,
  241. ScrollBar& scrollbar,
  242. int width, int height,
  243. int buttonDirection,
  244. bool isScrollbarVertical,
  245. bool isMouseOverButton,
  246. bool isButtonDown) = 0;
  247. /** Draws the thumb area of a scrollbar.
  248. @param g the context to draw into
  249. @param scrollbar the bar itself
  250. @param x the x position of the left edge of the thumb area to draw in
  251. @param y the y position of the top edge of the thumb area to draw in
  252. @param width the width of the thumb area to draw in
  253. @param height the height of the thumb area to draw in
  254. @param isScrollbarVertical true if it's a vertical bar, false if horizontal
  255. @param thumbStartPosition for vertical bars, the y coordinate of the top of the
  256. thumb, or its x position for horizontal bars
  257. @param thumbSize for vertical bars, the height of the thumb, or its width for
  258. horizontal bars. This may be 0 if the thumb shouldn't be drawn.
  259. @param isMouseOver whether the mouse is over the thumb area, also true if the mouse is
  260. currently dragging the thumb
  261. @param isMouseDown whether the mouse is currently dragging the scrollbar
  262. */
  263. virtual void drawScrollbar (Graphics& g, ScrollBar& scrollbar,
  264. int x, int y, int width, int height,
  265. bool isScrollbarVertical,
  266. int thumbStartPosition,
  267. int thumbSize,
  268. bool isMouseOver,
  269. bool isMouseDown) = 0;
  270. /** Returns the component effect to use for a scrollbar */
  271. virtual ImageEffectFilter* getScrollbarEffect() = 0;
  272. /** Returns the minimum length in pixels to use for a scrollbar thumb. */
  273. virtual int getMinimumScrollbarThumbSize (ScrollBar&) = 0;
  274. /** Returns the default thickness to use for a scrollbar. */
  275. virtual int getDefaultScrollbarWidth() = 0;
  276. /** Returns the length in pixels to use for a scrollbar button. */
  277. virtual int getScrollbarButtonSize (ScrollBar&) = 0;
  278. };
  279. //==============================================================================
  280. /** @internal */
  281. bool keyPressed (const KeyPress&) override;
  282. /** @internal */
  283. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  284. /** @internal */
  285. void lookAndFeelChanged() override;
  286. /** @internal */
  287. void mouseDown (const MouseEvent&) override;
  288. /** @internal */
  289. void mouseDrag (const MouseEvent&) override;
  290. /** @internal */
  291. void mouseUp (const MouseEvent&) override;
  292. /** @internal */
  293. void paint (Graphics&) override;
  294. /** @internal */
  295. void resized() override;
  296. /** @internal */
  297. void parentHierarchyChanged() override;
  298. private:
  299. //==============================================================================
  300. Range<double> totalRange, visibleRange;
  301. double singleStepSize, dragStartRange;
  302. int thumbAreaStart, thumbAreaSize, thumbStart, thumbSize;
  303. int dragStartMousePos, lastMousePos;
  304. int initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs;
  305. bool vertical, isDraggingThumb, autohides;
  306. class ScrollbarButton;
  307. friend struct ContainerDeletePolicy<ScrollbarButton>;
  308. ScopedPointer<ScrollbarButton> upButton, downButton;
  309. ListenerList<Listener> listeners;
  310. void handleAsyncUpdate() override;
  311. void updateThumbPosition();
  312. void timerCallback() override;
  313. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollBar)
  314. };
  315. /** This typedef is just for compatibility with old code - newer code should use the ScrollBar::Listener class directly. */
  316. typedef ScrollBar::Listener ScrollBarListener;
  317. #endif // JUCE_SCROLLBAR_H_INCLUDED