2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_SCROLLBAR_JUCEHEADER__
27 #define __JUCE_SCROLLBAR_JUCEHEADER__
29 #include "../../../events/juce_AsyncUpdater.h"
30 #include "../../../maths/juce_Range.h"
31 #include "../../../events/juce_Timer.h"
32 #include "../buttons/juce_Button.h"
35 //==============================================================================
37 A scrollbar component.
39 To use a scrollbar, set up its total range using the setRangeLimits() method - this
40 sets the range of values it can represent. Then you can use setCurrentRange() to
41 change the position and size of the scrollbar's 'thumb'.
43 Registering a ScrollBar::Listener with the scrollbar will allow you to find out when
44 the user moves it, and you can use the getCurrentRangeStart() to find out where
47 The scrollbar will adjust its own visibility according to whether its thumb size
48 allows it to actually be scrolled.
50 For most purposes, it's probably easier to use a ViewportContainer or ListBox
51 instead of handling a scrollbar directly.
53 @see ScrollBar::Listener
55 class JUCE_API ScrollBar
: public Component
,
60 //==============================================================================
61 /** Creates a Scrollbar.
63 @param isVertical whether it should be a vertical or horizontal bar
64 @param buttonsAreVisible whether to show the up/down or left/right buttons
66 ScrollBar (bool isVertical
,
67 bool buttonsAreVisible
= true);
72 //==============================================================================
73 /** Returns true if the scrollbar is vertical, false if it's horizontal. */
74 bool isVertical() const noexcept
{ return vertical
; }
76 /** Changes the scrollbar's direction.
78 You'll also need to resize the bar appropriately - this just changes its internal
81 @param shouldBeVertical true makes it vertical; false makes it horizontal.
83 void setOrientation (bool shouldBeVertical
);
85 /** Shows or hides the scrollbar's buttons. */
86 void setButtonVisibility (bool buttonsAreVisible
);
88 /** Tells the scrollbar whether to make itself invisible when not needed.
90 The default behaviour is for a scrollbar to become invisible when the thumb
91 fills the whole of its range (i.e. when it can't be moved). Setting this
92 value to false forces the bar to always be visible.
95 void setAutoHide (bool shouldHideWhenFullRange
);
97 /** Returns true if this scrollbar is set to auto-hide when its thumb is as big
101 bool autoHides() const noexcept
;
103 //==============================================================================
104 /** Sets the minimum and maximum values that the bar will move between.
106 The bar's thumb will always be constrained so that the entire thumb lies
111 void setRangeLimits (const Range
<double>& newRangeLimit
);
113 /** Sets the minimum and maximum values that the bar will move between.
115 The bar's thumb will always be constrained so that the entire thumb lies
120 void setRangeLimits (double minimum
, double maximum
);
122 /** Returns the current limits on the thumb position.
125 const Range
<double> getRangeLimit() const noexcept
{ return totalRange
; }
127 /** Returns the lower value that the thumb can be set to.
129 This is the value set by setRangeLimits().
131 double getMinimumRangeLimit() const noexcept
{ return totalRange
.getStart(); }
133 /** Returns the upper value that the thumb can be set to.
135 This is the value set by setRangeLimits().
137 double getMaximumRangeLimit() const noexcept
{ return totalRange
.getEnd(); }
139 //==============================================================================
140 /** Changes the position of the scrollbar's 'thumb'.
142 If this method call actually changes the scrollbar's position, it will trigger an
143 asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
146 @see getCurrentRange. setCurrentRangeStart
148 void setCurrentRange (const Range
<double>& newRange
);
150 /** Changes the position of the scrollbar's 'thumb'.
152 This sets both the position and size of the thumb - to just set the position without
153 changing the size, you can use setCurrentRangeStart().
155 If this method call actually changes the scrollbar's position, it will trigger an
156 asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
159 @param newStart the top (or left) of the thumb, in the range
160 getMinimumRangeLimit() <= newStart <= getMaximumRangeLimit(). If the
161 value is beyond these limits, it will be clipped.
162 @param newSize the size of the thumb, such that
163 getMinimumRangeLimit() <= newStart + newSize <= getMaximumRangeLimit(). If the
164 size is beyond these limits, it will be clipped.
165 @see setCurrentRangeStart, getCurrentRangeStart, getCurrentRangeSize
167 void setCurrentRange (double newStart
, double newSize
);
169 /** Moves the bar's thumb position.
171 This will move the thumb position without changing the thumb size. Note
172 that the maximum thumb start position is (getMaximumRangeLimit() - getCurrentRangeSize()).
174 If this method call actually changes the scrollbar's position, it will trigger an
175 asynchronous call to ScrollBar::Listener::scrollBarMoved() for all the listeners that
180 void setCurrentRangeStart (double newStart
);
182 /** Returns the current thumb range.
183 @see getCurrentRange, setCurrentRange
185 const Range
<double> getCurrentRange() const noexcept
{ return visibleRange
; }
187 /** Returns the position of the top of the thumb.
188 @see getCurrentRange, setCurrentRangeStart
190 double getCurrentRangeStart() const noexcept
{ return visibleRange
.getStart(); }
192 /** Returns the current size of the thumb.
193 @see getCurrentRange, setCurrentRange
195 double getCurrentRangeSize() const noexcept
{ return visibleRange
.getLength(); }
197 //==============================================================================
198 /** Sets the amount by which the up and down buttons will move the bar.
200 The value here is in terms of the total range, and is added or subtracted
201 from the thumb position when the user clicks an up/down (or left/right) button.
203 void setSingleStepSize (double newSingleStepSize
);
205 /** Moves the scrollbar by a number of single-steps.
207 This will move the bar by a multiple of its single-step interval (as
208 specified using the setSingleStepSize() method).
210 A positive value here will move the bar down or to the right, a negative
211 value moves it up or to the left.
213 void moveScrollbarInSteps (int howManySteps
);
215 /** Moves the scroll bar up or down in pages.
217 This will move the bar by a multiple of its current thumb size, effectively
218 doing a page-up or down.
220 A positive value here will move the bar down or to the right, a negative
221 value moves it up or to the left.
223 void moveScrollbarInPages (int howManyPages
);
225 /** Scrolls to the top (or left).
227 This is the same as calling setCurrentRangeStart (getMinimumRangeLimit());
231 /** Scrolls to the bottom (or right).
233 This is the same as calling setCurrentRangeStart (getMaximumRangeLimit() - getCurrentRangeSize());
235 void scrollToBottom();
237 /** Changes the delay before the up and down buttons autorepeat when they are held
240 For an explanation of what the parameters are for, see Button::setRepeatSpeed().
242 @see Button::setRepeatSpeed
244 void setButtonRepeatSpeed (int initialDelayInMillisecs
,
245 int repeatDelayInMillisecs
,
246 int minimumDelayInMillisecs
= -1);
248 //==============================================================================
249 /** A set of colour IDs to use to change the colour of various aspects of the component.
251 These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
254 @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
258 backgroundColourId
= 0x1000300, /**< The background colour of the scrollbar. */
259 thumbColourId
= 0x1000400, /**< A base colour to use for the thumb. The look and feel will probably use variations on this colour. */
260 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. */
263 //==============================================================================
265 A class for receiving events from a ScrollBar.
267 You can register a ScrollBar::Listener with a ScrollBar using the ScrollBar::addListener()
268 method, and it will be called when the bar's position changes.
270 @see ScrollBar::addListener, ScrollBar::removeListener
272 class JUCE_API Listener
276 virtual ~Listener() {}
278 /** Called when a ScrollBar is moved.
280 @param scrollBarThatHasMoved the bar that has moved
281 @param newRangeStart the new range start of this bar
283 virtual void scrollBarMoved (ScrollBar
* scrollBarThatHasMoved
,
284 double newRangeStart
) = 0;
287 /** Registers a listener that will be called when the scrollbar is moved. */
288 void addListener (Listener
* listener
);
290 /** Deregisters a previously-registered listener. */
291 void removeListener (Listener
* listener
);
293 //==============================================================================
295 bool keyPressed (const KeyPress
& key
);
297 void mouseWheelMove (const MouseEvent
& e
, float wheelIncrementX
, float wheelIncrementY
);
299 void lookAndFeelChanged();
301 void handleAsyncUpdate();
303 void mouseDown (const MouseEvent
& e
);
305 void mouseDrag (const MouseEvent
& e
);
307 void mouseUp (const MouseEvent
& e
);
309 void paint (Graphics
& g
);
314 //==============================================================================
315 Range
<double> totalRange
, visibleRange
;
316 double singleStepSize
, dragStartRange
;
317 int thumbAreaStart
, thumbAreaSize
, thumbStart
, thumbSize
;
318 int dragStartMousePos
, lastMousePos
;
319 int initialDelayInMillisecs
, repeatDelayInMillisecs
, minimumDelayInMillisecs
;
320 bool vertical
, isDraggingThumb
, autohides
;
321 class ScrollbarButton
;
322 friend class ScopedPointer
<ScrollbarButton
>;
323 ScopedPointer
<ScrollbarButton
> upButton
, downButton
;
324 ListenerList
<Listener
> listeners
;
326 void updateThumbPosition();
327 void timerCallback();
329 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScrollBar
);
332 /** This typedef is just for compatibility with old code - newer code should use the ScrollBar::Listener class directly. */
333 typedef ScrollBar::Listener ScrollBarListener
;
336 #endif // __JUCE_SCROLLBAR_JUCEHEADER__