1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-02-02 03:20:06 +00:00

Added an option for Viewport subclasses to create custom scrollbars. Also modified Viewport::getVerticalScrollBar() and ListBox::getVerticalScrollBar() to return references instead of pointers

This commit is contained in:
jules 2017-09-25 11:22:03 +01:00
parent 533fd23439
commit a4f5663fce
9 changed files with 138 additions and 121 deletions

View file

@ -61,7 +61,7 @@ void FileListComponent::deselectAllFiles()
void FileListComponent::scrollToTop()
{
getVerticalScrollBar()->setCurrentRangeStart (0);
getVerticalScrollBar().setCurrentRangeStart (0);
}
void FileListComponent::setSelectedFile (const File& f)

View file

@ -91,7 +91,7 @@ public:
{
jassert (parentContentsList != nullptr);
DirectoryContentsList* const l = new DirectoryContentsList (parentContentsList->getFilter(), thread);
auto l = new DirectoryContentsList (parentContentsList->getFilter(), thread);
l->setDirectory (file,
parentContentsList->isFindingDirectories(),
@ -299,7 +299,7 @@ void FileTreeComponent::deselectAllFiles()
void FileTreeComponent::scrollToTop()
{
getViewport()->getVerticalScrollBar()->setCurrentRangeStart (0);
getViewport()->getVerticalScrollBar().setCurrentRangeStart (0);
}
void FileTreeComponent::setDragAndDropDescription (const String& description)

View file

@ -35,11 +35,11 @@ Viewport::Viewport (const String& name) : Component (name)
scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
addChildComponent (verticalScrollBar);
addChildComponent (horizontalScrollBar);
addChildComponent (verticalScrollBar = createScrollBarComponent (true));
addChildComponent (horizontalScrollBar = createScrollBarComponent (false));
verticalScrollBar.addListener (this);
horizontalScrollBar.addListener (this);
getVerticalScrollBar().addListener (this);
getHorizontalScrollBar().addListener (this);
setInterceptsMouseClicks (false, true);
setWantsKeyboardFocus (true);
@ -140,7 +140,7 @@ bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeB
{
int dx = 0, dy = 0;
if (horizontalScrollBar.isVisible() || canScrollHorizontally())
if (getHorizontalScrollBar().isVisible() || canScrollHorizontally())
{
if (mouseX < activeBorderThickness)
dx = activeBorderThickness - mouseX;
@ -153,7 +153,7 @@ bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeB
dx = jmin (dx, maximumSpeed, -contentComp->getX());
}
if (verticalScrollBar.isVisible() || canScrollVertically())
if (getVerticalScrollBar().isVisible() || canScrollVertically())
{
if (mouseY < activeBorderThickness)
dy = activeBorderThickness - mouseY;
@ -223,7 +223,7 @@ struct Viewport::DragToScrollListener : private MouseListener,
{
if (numTouches == 1 && ! isViewportDragBlocked)
{
Point<float> totalOffset = e.getOffsetFromDragStart().toFloat();
auto totalOffset = e.getOffsetFromDragStart().toFloat();
if (! isDragging && totalOffset.getDistanceFromOrigin() > 8.0f)
{
@ -326,8 +326,8 @@ void Viewport::updateVisibleArea()
for (int i = 3; --i >= 0;)
{
hBarVisible = canShowHBar && ! horizontalScrollBar.autoHides();
vBarVisible = canShowVBar && ! verticalScrollBar.autoHides();
hBarVisible = canShowHBar && ! getHorizontalScrollBar().autoHides();
vBarVisible = canShowVBar && ! getVerticalScrollBar().autoHides();
contentArea = getLocalBounds();
if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
@ -371,27 +371,30 @@ void Viewport::updateVisibleArea()
auto visibleOrigin = -contentBounds.getPosition();
horizontalScrollBar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
horizontalScrollBar.setRangeLimits (0.0, contentBounds.getWidth());
horizontalScrollBar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
horizontalScrollBar.setSingleStepSize (singleStepX);
horizontalScrollBar.cancelPendingUpdate();
auto& hbar = getHorizontalScrollBar();
auto& vbar = getVerticalScrollBar();
hbar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
hbar.setRangeLimits (0.0, contentBounds.getWidth());
hbar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
hbar.setSingleStepSize (singleStepX);
hbar.cancelPendingUpdate();
if (canShowHBar && ! hBarVisible)
visibleOrigin.setX (0);
verticalScrollBar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
verticalScrollBar.setRangeLimits (0.0, contentBounds.getHeight());
verticalScrollBar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
verticalScrollBar.setSingleStepSize (singleStepY);
verticalScrollBar.cancelPendingUpdate();
vbar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
vbar.setRangeLimits (0.0, contentBounds.getHeight());
vbar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
vbar.setSingleStepSize (singleStepY);
vbar.cancelPendingUpdate();
if (canShowVBar && ! vBarVisible)
visibleOrigin.setY (0);
// Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
horizontalScrollBar.setVisible (hBarVisible);
verticalScrollBar.setVisible (vBarVisible);
hbar.setVisible (hBarVisible);
vbar.setVisible (vBarVisible);
if (contentComp != nullptr)
{
@ -414,8 +417,8 @@ void Viewport::updateVisibleArea()
visibleAreaChanged (visibleArea);
}
horizontalScrollBar.handleUpdateNowIfNeeded();
verticalScrollBar.handleUpdateNowIfNeeded();
hbar.handleUpdateNowIfNeeded();
vbar.handleUpdateNowIfNeeded();
}
//==============================================================================
@ -480,11 +483,11 @@ void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRange
{
const int newRangeStartInt = roundToInt (newRangeStart);
if (scrollBarThatHasMoved == &horizontalScrollBar)
if (scrollBarThatHasMoved == horizontalScrollBar)
{
setViewPosition (newRangeStartInt, getViewPositionY());
}
else if (scrollBarThatHasMoved == &verticalScrollBar)
else if (scrollBarThatHasMoved == verticalScrollBar)
{
setViewPosition (getViewPositionX(), newRangeStartInt);
}
@ -511,8 +514,8 @@ bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelD
{
if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
{
const bool canScrollVert = (allowScrollingWithoutScrollbarV || verticalScrollBar.isVisible());
const bool canScrollHorz = (allowScrollingWithoutScrollbarH || horizontalScrollBar.isVisible());
const bool canScrollVert = (allowScrollingWithoutScrollbarV || getVerticalScrollBar().isVisible());
const bool canScrollHorz = (allowScrollingWithoutScrollbarH || getHorizontalScrollBar().isVisible());
if (canScrollHorz || canScrollVert)
{
@ -566,13 +569,13 @@ bool Viewport::keyPressed (const KeyPress& key)
{
const bool isUpDownKey = isUpDownKeyPress (key);
if (verticalScrollBar.isVisible() && isUpDownKey)
return verticalScrollBar.keyPressed (key);
if (getVerticalScrollBar().isVisible() && isUpDownKey)
return getVerticalScrollBar().keyPressed (key);
const bool isLeftRightKey = isLeftRightKeyPress (key);
if (horizontalScrollBar.isVisible() && (isUpDownKey || isLeftRightKey))
return horizontalScrollBar.keyPressed (key);
if (getHorizontalScrollBar().isVisible() && (isUpDownKey || isLeftRightKey))
return getHorizontalScrollBar().keyPressed (key);
return false;
}
@ -582,4 +585,9 @@ bool Viewport::respondsToKey (const KeyPress& key)
return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
}
ScrollBar* Viewport::createScrollBarComponent (bool isVertical)
{
return new ScrollBar (isVertical);
}
} // namespace juce

View file

@ -231,12 +231,12 @@ public:
/** Returns a pointer to the scrollbar component being used.
Handy if you need to customise the bar somehow.
*/
ScrollBar* getVerticalScrollBar() noexcept { return &verticalScrollBar; }
ScrollBar& getVerticalScrollBar() noexcept { return *verticalScrollBar; }
/** Returns a pointer to the scrollbar component being used.
Handy if you need to customise the bar somehow.
*/
ScrollBar* getHorizontalScrollBar() noexcept { return &horizontalScrollBar; }
ScrollBar& getHorizontalScrollBar() noexcept { return *horizontalScrollBar; }
/** True if there's any off-screen content that could be scrolled vertically,
or false if everything is currently visible.
@ -277,9 +277,16 @@ public:
/** @internal */
static bool respondsToKey (const KeyPress&);
protected:
//==============================================================================
/** Creates the Scrollbar components that will be used for that Viewport.
Subclasses can override this if they need to customise the scroolbars in some way.
*/
virtual ScrollBar* createScrollBarComponent (bool isVertical);
private:
//==============================================================================
ScrollBar verticalScrollBar { true }, horizontalScrollBar { false };
ScopedPointer<ScrollBar> verticalScrollBar, horizontalScrollBar;
Component contentHolder;
WeakReference<Component> contentComp;
Rectangle<int> lastVisibleArea;

View file

@ -791,16 +791,16 @@ void ListBox::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& whee
{
bool eventWasUsed = false;
if (wheel.deltaX != 0.0f && viewport->getHorizontalScrollBar()->isVisible())
if (wheel.deltaX != 0.0f && getHorizontalScrollBar().isVisible())
{
eventWasUsed = true;
viewport->getHorizontalScrollBar()->mouseWheelMove (e, wheel);
getHorizontalScrollBar().mouseWheelMove (e, wheel);
}
if (wheel.deltaY != 0.0f && viewport->getVerticalScrollBar()->isVisible())
if (wheel.deltaY != 0.0f && getVerticalScrollBar().isVisible())
{
eventWasUsed = true;
viewport->getVerticalScrollBar()->mouseWheelMove (e, wheel);
getVerticalScrollBar().mouseWheelMove (e, wheel);
}
if (! eventWasUsed)
@ -834,8 +834,8 @@ void ListBox::setMinimumContentWidth (const int newMinimumWidth)
int ListBox::getVisibleContentWidth() const noexcept { return viewport->getMaximumVisibleWidth(); }
ScrollBar* ListBox::getVerticalScrollBar() const noexcept { return viewport->getVerticalScrollBar(); }
ScrollBar* ListBox::getHorizontalScrollBar() const noexcept { return viewport->getHorizontalScrollBar(); }
ScrollBar& ListBox::getVerticalScrollBar() const noexcept { return viewport->getVerticalScrollBar(); }
ScrollBar& ListBox::getHorizontalScrollBar() const noexcept { return viewport->getHorizontalScrollBar(); }
void ListBox::colourChanged()
{

View file

@ -379,11 +379,11 @@ public:
/** Scrolls if necessary to make sure that a particular row is visible. */
void scrollToEnsureRowIsOnscreen (int row);
/** Returns a pointer to the vertical scrollbar. */
ScrollBar* getVerticalScrollBar() const noexcept;
/** Returns a reference to the vertical scrollbar. */
ScrollBar& getVerticalScrollBar() const noexcept;
/** Returns a pointer to the horizontal scrollbar. */
ScrollBar* getHorizontalScrollBar() const noexcept;
/** Returns a reference to the horizontal scrollbar. */
ScrollBar& getHorizontalScrollBar() const noexcept;
/** Finds the row index that contains a given x,y position.
The position is relative to the ListBox's top-left.

View file

@ -357,20 +357,18 @@ Component* TableListBox::getCellComponent (int columnId, int rowNumber) const
void TableListBox::scrollToEnsureColumnIsOnscreen (int columnId)
{
if (auto* scrollbar = getHorizontalScrollBar())
{
auto pos = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
auto& scrollbar = getHorizontalScrollBar();
auto pos = header->getColumnPosition (header->getIndexOfColumnId (columnId, true));
auto x = scrollbar->getCurrentRangeStart();
auto w = scrollbar->getCurrentRangeSize();
auto x = scrollbar.getCurrentRangeStart();
auto w = scrollbar.getCurrentRangeSize();
if (pos.getX() < x)
x = pos.getX();
else if (pos.getRight() > x + w)
x += jmax (0.0, pos.getRight() - (x + w));
if (pos.getX() < x)
x = pos.getX();
else if (pos.getRight() > x + w)
x += jmax (0.0, pos.getRight() - (x + w));
scrollbar->setCurrentRangeStart (x);
}
scrollbar.setCurrentRangeStart (x);
}
int TableListBox::getNumRows()

View file

@ -1869,8 +1869,7 @@ bool TextEditor::pageDown (bool selecting)
void TextEditor::scrollByLines (int deltaLines)
{
if (auto* scrollbar = viewport->getVerticalScrollBar())
scrollbar->moveScrollbarInSteps (deltaLines);
viewport->getVerticalScrollBar().moveScrollbarInSteps (deltaLines);
}
bool TextEditor::scrollDown()