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.

304 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #ifndef __JUCE_SCROLLBAR_JUCEHEADER__
  24. #define __JUCE_SCROLLBAR_JUCEHEADER__
  25. #include "../../../events/juce_AsyncUpdater.h"
  26. #include "../../../events/juce_Timer.h"
  27. #include "../buttons/juce_Button.h"
  28. class ScrollBar;
  29. //==============================================================================
  30. /**
  31. A class for receiving events from a ScrollBar.
  32. You can register a ScrollBarListener with a ScrollBar using the ScrollBar::addListener()
  33. method, and it will be called when the bar's position changes.
  34. @see ScrollBar::addListener, ScrollBar::removeListener
  35. */
  36. class JUCE_API ScrollBarListener
  37. {
  38. public:
  39. /** Destructor. */
  40. virtual ~ScrollBarListener() {}
  41. /** Called when a ScrollBar is moved.
  42. @param scrollBarThatHasMoved the bar that has moved
  43. @param newRangeStart the new range start of this bar
  44. */
  45. virtual void scrollBarMoved (ScrollBar* scrollBarThatHasMoved,
  46. const double newRangeStart) = 0;
  47. };
  48. //==============================================================================
  49. /**
  50. A scrollbar component.
  51. To use a scrollbar, set up its total range using the setRangeLimits() method - this
  52. sets the range of values it can represent. Then you can use setCurrentRange() to
  53. change the position and size of the scrollbar's 'thumb'.
  54. Registering a ScrollBarListener with the scrollbar will allow you to find out when
  55. the user moves it, and you can use the getCurrentRangeStart() to find out where
  56. they moved it to.
  57. The scrollbar will adjust its own visibility according to whether its thumb size
  58. allows it to actually be scrolled.
  59. For most purposes, it's probably easier to use a ViewportContainer or ListBox
  60. instead of handling a scrollbar directly.
  61. @see ScrollBarListener
  62. */
  63. class JUCE_API ScrollBar : public Component,
  64. public AsyncUpdater,
  65. private Timer
  66. {
  67. public:
  68. //==============================================================================
  69. /** Creates a Scrollbar.
  70. @param isVertical whether it should be a vertical or horizontal bar
  71. @param buttonsAreVisible whether to show the up/down or left/right buttons
  72. */
  73. ScrollBar (const bool isVertical,
  74. const bool buttonsAreVisible = true);
  75. /** Destructor. */
  76. ~ScrollBar();
  77. //==============================================================================
  78. /** Returns true if the scrollbar is vertical, false if it's horizontal. */
  79. bool isVertical() const throw() { return vertical; }
  80. /** Changes the scrollbar's direction.
  81. You'll also need to resize the bar appropriately - this just changes its internal
  82. layout.
  83. @param shouldBeVertical true makes it vertical; false makes it horizontal.
  84. */
  85. void setOrientation (const bool shouldBeVertical) throw();
  86. /** Shows or hides the scrollbar's buttons. */
  87. void setButtonVisibility (const bool buttonsAreVisible);
  88. //==============================================================================
  89. /** Sets the minimum and maximum values that the bar will move between.
  90. The bar's thumb will always be constrained so that the top of the thumb
  91. will be >= minimum, and the bottom of the thumb <= maximum.
  92. @see setCurrentRange
  93. */
  94. void setRangeLimits (const double minimum,
  95. const double maximum) throw();
  96. /** Returns the lower value that the thumb can be set to.
  97. This is the value set by setRangeLimits().
  98. */
  99. double getMinimumRangeLimit() const throw() { return minimum; }
  100. /** Returns the upper value that the thumb can be set to.
  101. This is the value set by setRangeLimits().
  102. */
  103. double getMaximumRangeLimit() const throw() { return maximum; }
  104. //==============================================================================
  105. /** Changes the position of the scrollbar's 'thumb'.
  106. This sets both the position and size of the thumb - to just set the position without
  107. changing the size, you can use setCurrentRangeStart().
  108. If this method call actually changes the scrollbar's position, it will trigger an
  109. asynchronous call to ScrollBarListener::scrollBarMoved() for all the listeners that
  110. are registered.
  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. @see setCurrentRangeStart, getCurrentRangeStart, getCurrentRangeSize
  118. */
  119. void setCurrentRange (double newStart,
  120. double newSize) throw();
  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 ScrollBarListener::scrollBarMoved() for all the listeners that
  126. are registered.
  127. @see setCurrentRange
  128. */
  129. void setCurrentRangeStart (double newStart) throw();
  130. /** Returns the position of the top of the thumb.
  131. @see setCurrentRangeStart
  132. */
  133. double getCurrentRangeStart() const throw() { return rangeStart; }
  134. /** Returns the current size of the thumb.
  135. @see setCurrentRange
  136. */
  137. double getCurrentRangeSize() const throw() { return rangeSize; }
  138. //==============================================================================
  139. /** Sets the amount by which the up and down buttons will move the bar.
  140. The value here is in terms of the total range, and is added or subtracted
  141. from the thumb position when the user clicks an up/down (or left/right) button.
  142. */
  143. void setSingleStepSize (const double newSingleStepSize) throw();
  144. /** Moves the scrollbar by a number of single-steps.
  145. This will move the bar by a multiple of its single-step interval (as
  146. specified using the setSingleStepSize() method).
  147. A positive value here will move the bar down or to the right, a negative
  148. value moves it up or to the left.
  149. */
  150. void moveScrollbarInSteps (const int howManySteps) throw();
  151. /** Moves the scroll bar up or down in pages.
  152. This will move the bar by a multiple of its current thumb size, effectively
  153. doing a page-up or down.
  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. */
  157. void moveScrollbarInPages (const int howManyPages) throw();
  158. /** Scrolls to the top (or left).
  159. This is the same as calling setCurrentRangeStart (getMinimumRangeLimit());
  160. */
  161. void scrollToTop() throw();
  162. /** Scrolls to the bottom (or right).
  163. This is the same as calling setCurrentRangeStart (getMaximumRangeLimit() - getCurrentRangeSize());
  164. */
  165. void scrollToBottom() throw();
  166. /** Changes the delay before the up and down buttons autorepeat when they are held
  167. down.
  168. For an explanation of what the parameters are for, see Button::setRepeatSpeed().
  169. @see Button::setRepeatSpeed
  170. */
  171. void setButtonRepeatSpeed (const int initialDelayInMillisecs,
  172. const int repeatDelayInMillisecs,
  173. const int minimumDelayInMillisecs = -1) throw();
  174. //==============================================================================
  175. /** A set of colour IDs to use to change the colour of various aspects of the component.
  176. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  177. methods.
  178. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  179. */
  180. enum ColourIds
  181. {
  182. backgroundColourId = 0x1000300, /**< The background colour of the scrollbar. */
  183. thumbColourId = 0x1000400 /**< A base colour to use for the thumb. The look and feel will probably use variations on this colour. */
  184. };
  185. //==============================================================================
  186. /** Registers a listener that will be called when the scrollbar is moved. */
  187. void addListener (ScrollBarListener* const listener) throw();
  188. /** Deregisters a previously-registered listener. */
  189. void removeListener (ScrollBarListener* const listener) throw();
  190. //==============================================================================
  191. /** @internal */
  192. bool keyPressed (const KeyPress& key);
  193. /** @internal */
  194. void mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY);
  195. /** @internal */
  196. void lookAndFeelChanged();
  197. /** @internal */
  198. void handleAsyncUpdate();
  199. /** @internal */
  200. void mouseDown (const MouseEvent& e);
  201. /** @internal */
  202. void mouseDrag (const MouseEvent& e);
  203. /** @internal */
  204. void mouseUp (const MouseEvent& e);
  205. /** @internal */
  206. void paint (Graphics& g);
  207. /** @internal */
  208. void resized();
  209. juce_UseDebuggingNewOperator
  210. private:
  211. //==============================================================================
  212. double minimum, maximum;
  213. double rangeStart, rangeSize;
  214. double singleStepSize, dragStartRange;
  215. int thumbAreaStart, thumbAreaSize, thumbStart, thumbSize;
  216. int dragStartMousePos, lastMousePos;
  217. int initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs;
  218. bool vertical, isDraggingThumb;
  219. Button* upButton;
  220. Button* downButton;
  221. SortedSet <void*> listeners;
  222. void updateThumbPosition() throw();
  223. void timerCallback();
  224. ScrollBar (const ScrollBar&);
  225. const ScrollBar& operator= (const ScrollBar&);
  226. };
  227. #endif // __JUCE_SCROLLBAR_JUCEHEADER__