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.

334 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_SCROLLBAR_JUCEHEADER__
  19. #define __JUCE_SCROLLBAR_JUCEHEADER__
  20. #include "../buttons/juce_Button.h"
  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 ViewportContainer 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 whether it should be a vertical or horizontal bar
  44. @param buttonsAreVisible whether to show the up/down or left/right buttons
  45. */
  46. ScrollBar (bool isVertical,
  47. bool buttonsAreVisible = true);
  48. /** Destructor. */
  49. ~ScrollBar();
  50. //==============================================================================
  51. /** Returns true if the scrollbar is vertical, false if it's horizontal. */
  52. bool isVertical() const noexcept { return vertical; }
  53. /** Changes the scrollbar's direction.
  54. You'll also need to resize the bar appropriately - this just changes its internal
  55. layout.
  56. @param shouldBeVertical true makes it vertical; false makes it horizontal.
  57. */
  58. void setOrientation (bool shouldBeVertical);
  59. /** Shows or hides the scrollbar's buttons. */
  60. void setButtonVisibility (bool buttonsAreVisible);
  61. /** Tells the scrollbar whether to make itself invisible when not needed.
  62. The default behaviour is for a scrollbar to become invisible when the thumb
  63. fills the whole of its range (i.e. when it can't be moved). Setting this
  64. value to false forces the bar to always be visible.
  65. @see autoHides()
  66. */
  67. void setAutoHide (bool shouldHideWhenFullRange);
  68. /** Returns true if this scrollbar is set to auto-hide when its thumb is as big
  69. as its maximum range.
  70. @see setAutoHide
  71. */
  72. bool autoHides() const noexcept;
  73. //==============================================================================
  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 (const Range<double>& newRangeLimit);
  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. @see setCurrentRange
  84. */
  85. void setRangeLimits (double minimum, double maximum);
  86. /** Returns the current limits on the thumb position.
  87. @see setRangeLimits
  88. */
  89. const Range<double> getRangeLimit() const noexcept { return totalRange; }
  90. /** Returns the lower value that the thumb can be set to.
  91. This is the value set by setRangeLimits().
  92. */
  93. double getMinimumRangeLimit() const noexcept { return totalRange.getStart(); }
  94. /** Returns the upper value that the thumb can be set to.
  95. This is the value set by setRangeLimits().
  96. */
  97. double getMaximumRangeLimit() const noexcept { return totalRange.getEnd(); }
  98. //==============================================================================
  99. /** Changes the position of the scrollbar's 'thumb'.
  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. @see getCurrentRange. setCurrentRangeStart
  104. */
  105. void setCurrentRange (const Range<double>& newRange);
  106. /** Changes the position of the scrollbar's 'thumb'.
  107. This sets both the position and size of the thumb - to just set the position without
  108. changing the size, you can use setCurrentRangeStart().
  109. If this method call actually changes the scrollbar's position, it will trigger an
  110. asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
  111. are registered.
  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. @see setCurrentRangeStart, getCurrentRangeStart, getCurrentRangeSize
  119. */
  120. void setCurrentRange (double newStart, double newSize);
  121. /** Moves the bar's thumb position.
  122. This will move the thumb position without changing the thumb size. Note
  123. that the maximum thumb start position is (getMaximumRangeLimit() - getCurrentRangeSize()).
  124. If this method call actually changes the scrollbar's position, it will trigger an
  125. asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
  126. are registered.
  127. @see setCurrentRange
  128. */
  129. void setCurrentRangeStart (double newStart);
  130. /** Returns the current thumb range.
  131. @see getCurrentRange, setCurrentRange
  132. */
  133. const Range<double> getCurrentRange() const noexcept { return visibleRange; }
  134. /** Returns the position of the top of the thumb.
  135. @see getCurrentRange, setCurrentRangeStart
  136. */
  137. double getCurrentRangeStart() const noexcept { return visibleRange.getStart(); }
  138. /** Returns the current size of the thumb.
  139. @see getCurrentRange, setCurrentRange
  140. */
  141. double getCurrentRangeSize() const noexcept { return visibleRange.getLength(); }
  142. //==============================================================================
  143. /** Sets the amount by which the up and down buttons will move the bar.
  144. The value here is in terms of the total range, and is added or subtracted
  145. from the thumb position when the user clicks an up/down (or left/right) button.
  146. */
  147. void setSingleStepSize (double newSingleStepSize);
  148. /** Moves the scrollbar by a number of single-steps.
  149. This will move the bar by a multiple of its single-step interval (as
  150. specified using the setSingleStepSize() method).
  151. A positive value here will move the bar down or to the right, a negative
  152. value moves it up or to the left.
  153. */
  154. void moveScrollbarInSteps (int howManySteps);
  155. /** Moves the scroll bar up or down in pages.
  156. This will move the bar by a multiple of its current thumb size, effectively
  157. doing a page-up or down.
  158. A positive value here will move the bar down or to the right, a negative
  159. value moves it up or to the left.
  160. */
  161. void moveScrollbarInPages (int howManyPages);
  162. /** Scrolls to the top (or left).
  163. This is the same as calling setCurrentRangeStart (getMinimumRangeLimit());
  164. */
  165. void scrollToTop();
  166. /** Scrolls to the bottom (or right).
  167. This is the same as calling setCurrentRangeStart (getMaximumRangeLimit() - getCurrentRangeSize());
  168. */
  169. void scrollToBottom();
  170. /** Changes the delay before the up and down buttons autorepeat when they are held
  171. down.
  172. For an explanation of what the parameters are for, see Button::setRepeatSpeed().
  173. @see Button::setRepeatSpeed
  174. */
  175. void setButtonRepeatSpeed (int initialDelayInMillisecs,
  176. int repeatDelayInMillisecs,
  177. int minimumDelayInMillisecs = -1);
  178. //==============================================================================
  179. /** A set of colour IDs to use to change the colour of various aspects of the component.
  180. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  181. methods.
  182. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  183. */
  184. enum ColourIds
  185. {
  186. backgroundColourId = 0x1000300, /**< The background colour of the scrollbar. */
  187. thumbColourId = 0x1000400, /**< A base colour to use for the thumb. The look and feel will probably use variations on this colour. */
  188. 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. */
  189. };
  190. //==============================================================================
  191. /**
  192. A class for receiving events from a ScrollBar.
  193. You can register a ScrollBar::Listener with a ScrollBar using the ScrollBar::addListener()
  194. method, and it will be called when the bar's position changes.
  195. @see ScrollBar::addListener, ScrollBar::removeListener
  196. */
  197. class JUCE_API Listener
  198. {
  199. public:
  200. /** Destructor. */
  201. virtual ~Listener() {}
  202. /** Called when a ScrollBar is moved.
  203. @param scrollBarThatHasMoved the bar that has moved
  204. @param newRangeStart the new range start of this bar
  205. */
  206. virtual void scrollBarMoved (ScrollBar* scrollBarThatHasMoved,
  207. double newRangeStart) = 0;
  208. };
  209. /** Registers a listener that will be called when the scrollbar is moved. */
  210. void addListener (Listener* listener);
  211. /** Deregisters a previously-registered listener. */
  212. void removeListener (Listener* listener);
  213. //==============================================================================
  214. /** @internal */
  215. bool keyPressed (const KeyPress& key);
  216. /** @internal */
  217. void mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY);
  218. /** @internal */
  219. void lookAndFeelChanged();
  220. /** @internal */
  221. void handleAsyncUpdate();
  222. /** @internal */
  223. void mouseDown (const MouseEvent& e);
  224. /** @internal */
  225. void mouseDrag (const MouseEvent& e);
  226. /** @internal */
  227. void mouseUp (const MouseEvent& e);
  228. /** @internal */
  229. void paint (Graphics& g);
  230. /** @internal */
  231. void resized();
  232. private:
  233. //==============================================================================
  234. Range <double> totalRange, visibleRange;
  235. double singleStepSize, dragStartRange;
  236. int thumbAreaStart, thumbAreaSize, thumbStart, thumbSize;
  237. int dragStartMousePos, lastMousePos;
  238. int initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs;
  239. bool vertical, isDraggingThumb, autohides;
  240. class ScrollbarButton;
  241. friend class ScopedPointer<ScrollbarButton>;
  242. ScopedPointer<ScrollbarButton> upButton, downButton;
  243. ListenerList <Listener> listeners;
  244. void updateThumbPosition();
  245. void timerCallback();
  246. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollBar);
  247. };
  248. /** This typedef is just for compatibility with old code - newer code should use the ScrollBar::Listener class directly. */
  249. typedef ScrollBar::Listener ScrollBarListener;
  250. #endif // __JUCE_SCROLLBAR_JUCEHEADER__