The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

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