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 18KB

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