Browse Source

Added methods Viewport::canScrollVertically() and Viewport::canScrollHorizontally()

tags/2021-05-28
jules 8 years ago
parent
commit
320c8002da
2 changed files with 38 additions and 42 deletions
  1. +22
    -32
      modules/juce_gui_basics/layout/juce_Viewport.cpp
  2. +16
    -10
      modules/juce_gui_basics/layout/juce_Viewport.h

+ 22
- 32
modules/juce_gui_basics/layout/juce_Viewport.cpp View File

@@ -22,19 +22,7 @@
============================================================================== ==============================================================================
*/ */
Viewport::Viewport (const String& name)
: Component (name),
scrollBarThickness (0),
singleStepX (16),
singleStepY (16),
showHScrollbar (true),
showVScrollbar (true),
deleteContent (true),
customScrollBarThickness (false),
allowScrollingWithoutScrollbarV (false),
allowScrollingWithoutScrollbarH (false),
verticalScrollBar (true),
horizontalScrollBar (false)
Viewport::Viewport (const String& name) : Component (name)
{ {
// content holder is used to clip the contents so they don't overlap the scrollbars // content holder is used to clip the contents so they don't overlap the scrollbars
addAndMakeVisible (contentHolder); addAndMakeVisible (contentHolder);
@@ -104,14 +92,18 @@ void Viewport::setViewedComponent (Component* const newViewedComponent, const bo
} }
} }
int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
bool Viewport::canScrollVertically() const noexcept { return contentComp->getY() < 0 || contentComp->getBottom() > getHeight(); }
bool Viewport::canScrollHorizontally() const noexcept { return contentComp->getX() < 0 || contentComp->getRight() > getWidth(); }
Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
{ {
jassert (contentComp != nullptr); jassert (contentComp != nullptr);
Rectangle<int> contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
auto contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
Point<int> p (jmax (jmin (0, contentHolder.getWidth() - contentBounds.getWidth()), jmin (0, -(pos.x))), Point<int> p (jmax (jmin (0, contentHolder.getWidth() - contentBounds.getWidth()), jmin (0, -(pos.x))),
jmax (jmin (0, contentHolder.getHeight() - contentBounds.getHeight()), jmin (0, -(pos.y)))); jmax (jmin (0, contentHolder.getHeight() - contentBounds.getHeight()), jmin (0, -(pos.y))));
@@ -121,7 +113,7 @@ Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset) void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
{ {
setViewPosition (Point<int> (xPixelsOffset, yPixelsOffset));
setViewPosition ({ xPixelsOffset, yPixelsOffset });
} }
void Viewport::setViewPosition (Point<int> newPosition) void Viewport::setViewPosition (Point<int> newPosition)
@@ -143,7 +135,7 @@ bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeB
{ {
int dx = 0, dy = 0; int dx = 0, dy = 0;
if (horizontalScrollBar.isVisible() || contentComp->getX() < 0 || contentComp->getRight() > getWidth())
if (horizontalScrollBar.isVisible() || canScrollHorizontally())
{ {
if (mouseX < activeBorderThickness) if (mouseX < activeBorderThickness)
dx = activeBorderThickness - mouseX; dx = activeBorderThickness - mouseX;
@@ -156,7 +148,7 @@ bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeB
dx = jmin (dx, maximumSpeed, -contentComp->getX()); dx = jmin (dx, maximumSpeed, -contentComp->getX());
} }
if (verticalScrollBar.isVisible() || contentComp->getY() < 0 || contentComp->getBottom() > getHeight())
if (verticalScrollBar.isVisible() || canScrollVertically())
{ {
if (mouseY < activeBorderThickness) if (mouseY < activeBorderThickness)
dy = activeBorderThickness - mouseY; dy = activeBorderThickness - mouseY;
@@ -192,8 +184,7 @@ typedef AnimatedPosition<AnimatedPositionBehaviours::ContinuousWithMomentum> Vie
struct Viewport::DragToScrollListener : private MouseListener, struct Viewport::DragToScrollListener : private MouseListener,
private ViewportDragPosition::Listener private ViewportDragPosition::Listener
{ {
DragToScrollListener (Viewport& v)
: viewport (v), numTouches (0), isDragging (false), isViewportDragBlocked (false)
DragToScrollListener (Viewport& v) : viewport (v)
{ {
viewport.contentHolder.addMouseListener (this, true); viewport.contentHolder.addMouseListener (this, true);
offsetX.addListener (this); offsetX.addListener (this);
@@ -270,10 +261,9 @@ struct Viewport::DragToScrollListener : private MouseListener,
Viewport& viewport; Viewport& viewport;
ViewportDragPosition offsetX, offsetY; ViewportDragPosition offsetX, offsetY;
Point<int> originalViewPos; Point<int> originalViewPos;
int numTouches;
bool isDragging;
bool isViewportDragBlocked;
int numTouches = 0;
bool isDragging = false;
bool isViewportDragBlocked = false;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragToScrollListener) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragToScrollListener)
}; };
@@ -317,7 +307,7 @@ void Viewport::resized()
//============================================================================== //==============================================================================
void Viewport::updateVisibleArea() void Viewport::updateVisibleArea()
{ {
const int scrollbarWidth = getScrollBarThickness();
auto scrollbarWidth = getScrollBarThickness();
const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth; const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
const bool canShowHBar = showHScrollbar && canShowAnyBars; const bool canShowHBar = showHScrollbar && canShowAnyBars;
const bool canShowVBar = showVScrollbar && canShowAnyBars; const bool canShowVBar = showVScrollbar && canShowAnyBars;
@@ -358,7 +348,7 @@ void Viewport::updateVisibleArea()
break; break;
} }
const Rectangle<int> oldContentBounds (contentComp->getBounds());
auto oldContentBounds = contentComp->getBounds();
contentHolder.setBounds (contentArea); contentHolder.setBounds (contentArea);
// If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate.. // If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate..
@@ -370,7 +360,7 @@ void Viewport::updateVisibleArea()
if (contentComp != nullptr) if (contentComp != nullptr)
contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds()); contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());
Point<int> visibleOrigin (-contentBounds.getPosition());
auto visibleOrigin = -contentBounds.getPosition();
horizontalScrollBar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth); horizontalScrollBar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
horizontalScrollBar.setRangeLimits (0.0, contentBounds.getWidth()); horizontalScrollBar.setRangeLimits (0.0, contentBounds.getWidth());
@@ -396,7 +386,7 @@ void Viewport::updateVisibleArea()
if (contentComp != nullptr) if (contentComp != nullptr)
{ {
const Point<int> newContentCompPos (viewportPosToCompPos (visibleOrigin));
auto newContentCompPos = viewportPosToCompPos (visibleOrigin);
if (contentComp->getBounds().getPosition() != newContentCompPos) if (contentComp->getBounds().getPosition() != newContentCompPos)
{ {
@@ -517,10 +507,10 @@ bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelD
if (canScrollHorz || canScrollVert) if (canScrollHorz || canScrollVert)
{ {
const int deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
const int deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
auto deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
auto deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
Point<int> pos (getViewPosition());
auto pos = getViewPosition();
if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert) if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
{ {


+ 16
- 10
modules/juce_gui_basics/layout/juce_Viewport.h View File

@@ -211,15 +211,12 @@ public:
bool isHorizontalScrollBarShown() const noexcept { return showHScrollbar; } bool isHorizontalScrollBarShown() const noexcept { return showHScrollbar; }
/** Changes the width of the scrollbars. /** Changes the width of the scrollbars.
If this isn't specified, the default width from the LookAndFeel class will be used. If this isn't specified, the default width from the LookAndFeel class will be used.
@see LookAndFeel::getDefaultScrollbarWidth @see LookAndFeel::getDefaultScrollbarWidth
*/ */
void setScrollBarThickness (int thickness); void setScrollBarThickness (int thickness);
/** Returns the thickness of the scrollbars. /** Returns the thickness of the scrollbars.
@see setScrollBarThickness @see setScrollBarThickness
*/ */
int getScrollBarThickness() const; int getScrollBarThickness() const;
@@ -239,6 +236,15 @@ public:
*/ */
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.
*/
bool canScrollVertically() const noexcept;
/** True if there's any off-screen content that could be scrolled horizontally,
or false if everything is currently visible.
*/
bool canScrollHorizontally() const noexcept;
/** Enables or disables drag-to-scroll functionality in the viewport. */ /** Enables or disables drag-to-scroll functionality in the viewport. */
void setScrollOnDragEnabled (bool shouldScrollOnDrag); void setScrollOnDragEnabled (bool shouldScrollOnDrag);
@@ -271,15 +277,15 @@ public:
private: private:
//============================================================================== //==============================================================================
ScrollBar verticalScrollBar { true }, horizontalScrollBar { false };
Component contentHolder;
WeakReference<Component> contentComp; WeakReference<Component> contentComp;
Rectangle<int> lastVisibleArea; Rectangle<int> lastVisibleArea;
int scrollBarThickness;
int singleStepX, singleStepY;
bool showHScrollbar, showVScrollbar, deleteContent;
bool customScrollBarThickness;
bool allowScrollingWithoutScrollbarV, allowScrollingWithoutScrollbarH;
Component contentHolder;
ScrollBar verticalScrollBar, horizontalScrollBar;
int scrollBarThickness = 0;
int singleStepX = 16, singleStepY = 16;
bool showHScrollbar = true, showVScrollbar = true, deleteContent = true;
bool customScrollBarThickness = false;
bool allowScrollingWithoutScrollbarV = false, allowScrollingWithoutScrollbarH = false;
struct DragToScrollListener; struct DragToScrollListener;
friend struct DragToScrollListener; friend struct DragToScrollListener;


Loading…
Cancel
Save