juce_ios_Viewport

This is a very rough preliminary attempt at a JUCE Viewport that uses a UIScrollView when on iOS.

Attached is a zipped git patch that you can apply to JUCE modules project to see how it works. Again this is very rough, and has only been tested against the Juce Demo iOS project. ListBoxs work really well, there’s a minor issue with Drag and Drop. TableListBoxs work well too, but when the TableListBox is in portrait mode things stop working, haven’t had a chance to look at this yet.

-j

Here’s the code if you don’t want to use the patch.

juce_ios_Viewport.mm

[code]/*

This file is part of the JUCE library - "Jules’ Utility Class Extensions"
Copyright 2004-11 by Raw Material Software Ltd.


JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.

JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.


To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.

==============================================================================
*/

#if JUCE_IOS

//==============================================================================
@interface JuceUIScrollView : UIScrollView
{
@private
juce::Viewport* owner;
}

@end

@implementation JuceUIScrollView

  • (id) initWithOwner: (juce::Viewport*) owner_
    {
    if ((self = [super init]) != nil)
    {
    owner = owner_;
    self.delegate = self;
    }

    return self;
    }

  • (void) dealloc
    {
    [super dealloc];
    }

  • (void) scrollViewDidScroll: (UIScrollView*) scrollView
    {
    const juce::Rectangle visibleArea (scrollView.contentOffset.x, scrollView.contentOffset.y,
    MIN (scrollView.contentSize.width - scrollView.contentOffset.x, owner->getWidth()),
    MIN (scrollView.contentSize.height - scrollView.contentOffset.y, owner->getHeight()));
    owner->visibleAreaChanged (visibleArea);
    }

@end

BEGIN_JUCE_NAMESPACE

//==============================================================================
Viewport::Viewport (const String& name)
: Component (name),
scrollBarThickness (0),
singleStepX (16),
singleStepY (16),
showHScrollbar (true),
showVScrollbar (true),
deleteContent (true),
verticalScrollBar (true),
horizontalScrollBar (false)
{
// content holder is used to clip the contents so they don’t overlap the scrollbars
addAndMakeVisible (&contentHolder);
contentHolder.setInterceptsMouseClicks (false, true);

JuceUIScrollView* scroll = [[JuceUIScrollView alloc] initWithOwner: this];
scroll.clipsToBounds = YES;
contentHolder.setView (scroll);
[scroll release];

addChildComponent (&verticalScrollBar);
addChildComponent (&horizontalScrollBar);

verticalScrollBar.addListener (this);
horizontalScrollBar.addListener (this);

setInterceptsMouseClicks (false, true);
setWantsKeyboardFocus (true);

}

Viewport::~Viewport()
{
deleteContentComp();
}

//==============================================================================
void Viewport::visibleAreaChanged (const Rectangle&) {}
void Viewport::viewedComponentChanged (Component*) {}

//==============================================================================
void Viewport::deleteContentComp()
{
if (contentComp != nullptr)
contentComp->removeFromDesktop();

if (deleteContent)
{
    // This sets the content comp to a null pointer before deleting the old one, in case
    // anything tries to use the old one while it's in mid-deletion..
    ScopedPointer<Component> oldCompDeleter (contentComp);
    contentComp = nullptr;
}
else
{
    contentComp = nullptr;
}

}

void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
{
if (contentComp.get() != newViewedComponent)
{
deleteContentComp();
contentComp = newViewedComponent;
deleteContent = deleteComponentWhenNoLongerNeeded;

    if (contentComp != nullptr)
    {
		contentComp->addToDesktop (0, (UIView*) contentHolder.getView());
		contentComp->setVisible (true);
		((UIView*)contentComp->getWindowHandle()).backgroundColor = [UIColor clearColor];
        setViewPosition (Point<int>());
        contentComp->addComponentListener (this);
    }
	
    viewedComponentChanged (contentComp);
    updateVisibleArea();
}

}

Component* Viewport::getViewedComponent() const noexcept
{
return contentComp;
}

int Viewport::getMaximumVisibleWidth() const
{
return contentHolder.getWidth();
}

int Viewport::getMaximumVisibleHeight() const
{
return contentHolder.getHeight();
}

Point Viewport::viewportPosToCompPos (const Point& pos) const
{
jassert (contentComp != nullptr);
return Point (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -pos.getX())),
jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -pos.getY())));
}

void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
{
setViewPosition (Point (xPixelsOffset, yPixelsOffset));
}

void Viewport::setViewPosition (const Point& newPosition)
{
if (contentComp != nullptr)
[(UIScrollView*) contentHolder.getView() setContentOffset: CGPointMake (newPosition.getX(), newPosition.getY())];
}

void Viewport::setViewPositionProportionately (const double x, const double y)
{
if (contentComp != nullptr)
setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
}

bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
{
return false;
}

const Point Viewport::getViewPosition() const noexcept
{
CGPoint contentOffset = ((UIScrollView*) contentHolder.getView()).contentOffset;
return Point (contentOffset.x, contentOffset.y);
}

int Viewport::getViewPositionX() const noexcept
{
return getViewPosition().getX();
}

int Viewport::getViewPositionY() const noexcept
{
return getViewPosition().getY();
}

int Viewport::getViewWidth() const noexcept
{
return ((UIScrollView*) contentHolder.getView()).bounds.size.width;
}

int Viewport::getViewHeight() const noexcept
{
return ((UIScrollView*) contentHolder.getView()).bounds.size.height;
}

void Viewport::componentMovedOrResized (Component&, bool, bool)
{
UIScrollView* scrollView = (UIScrollView*) contentHolder.getView();

[scrollView setContentSize: CGSizeMake (contentComp->getWidth(), contentComp->getHeight())];

const juce::Rectangle<int> visibleArea (scrollView.contentOffset.x, scrollView.contentOffset.y,
										MIN (scrollView.contentSize.width  - scrollView.contentOffset.x, getWidth()),
										MIN (scrollView.contentSize.height - scrollView.contentOffset.y, getHeight()));
visibleAreaChanged (visibleArea);

}

void Viewport::resized()
{
contentHolder.setBounds (0, 0, getWidth(), getHeight());

UIScrollView* scrollView = (UIScrollView*) contentHolder.getView();

const juce::Rectangle<int> visibleArea (scrollView.contentOffset.x, scrollView.contentOffset.y,
										MIN (scrollView.contentSize.width  - scrollView.contentOffset.x, getWidth()),
										MIN (scrollView.contentSize.height - scrollView.contentOffset.y, getHeight()));
visibleAreaChanged (visibleArea);

}

//==============================================================================
void Viewport::updateVisibleArea()
{
//xxx
}

//==============================================================================
void Viewport::setSingleStepSizes (const int stepX, const int stepY)
{

}

void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
const bool showHorizontalScrollbarIfNeeded)
{

}

bool Viewport::isVerticalScrollBarShown() const noexcept
{
return false;
}

bool Viewport::isHorizontalScrollBarShown() const noexcept
{
return false;
}

void Viewport::setScrollBarThickness (const int thickness)
{

}

int Viewport::getScrollBarThickness() const
{
return 0;
}

void Viewport::setScrollBarButtonVisibility (const bool buttonsVisible)
{

}

ScrollBar* Viewport::getVerticalScrollBar() noexcept
{
return &verticalScrollBar;
}

ScrollBar* Viewport::getHorizontalScrollBar() noexcept
{
return &horizontalScrollBar;
}

void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{

}

void Viewport::mouseWheelMove (const MouseEvent& e, const float wheelIncrementX, const float wheelIncrementY)
{
if (! useMouseWheelMoveIfNeeded (e, wheelIncrementX, wheelIncrementY))
Component::mouseWheelMove (e, wheelIncrementX, wheelIncrementY);
}

bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
if (! (e.mods.isAltDown() || e.mods.isCtrlDown()))
{
const bool hasVertBar = verticalScrollBar.isVisible();
const bool hasHorzBar = horizontalScrollBar.isVisible();

    if (hasHorzBar || hasVertBar)
    {
        if (wheelIncrementX != 0)
        {
            wheelIncrementX *= 14.0f * singleStepX;
            wheelIncrementX = (wheelIncrementX < 0) ? jmin (wheelIncrementX, -1.0f)
			: jmax (wheelIncrementX, 1.0f);
        }
		
        if (wheelIncrementY != 0)
        {
            wheelIncrementY *= 14.0f * singleStepY;
            wheelIncrementY = (wheelIncrementY < 0) ? jmin (wheelIncrementY, -1.0f)
			: jmax (wheelIncrementY, 1.0f);
        }
		
        Point<int> pos (getViewPosition());
		
        if (wheelIncrementX != 0 && wheelIncrementY != 0 && hasHorzBar && hasVertBar)
        {
            pos.setX (pos.getX() - roundToInt (wheelIncrementX));
            pos.setY (pos.getY() - roundToInt (wheelIncrementY));
        }
        else if (hasHorzBar && (wheelIncrementX != 0 || e.mods.isShiftDown() || ! hasVertBar))
        {
            if (wheelIncrementX == 0 && ! hasVertBar)
                wheelIncrementX = wheelIncrementY;
			
            pos.setX (pos.getX() - roundToInt (wheelIncrementX));
        }
        else if (hasVertBar && wheelIncrementY != 0)
        {
            pos.setY (pos.getY() - roundToInt (wheelIncrementY));
        }
		
        if (pos != getViewPosition())
        {
            setViewPosition (pos);
            return true;
        }
    }
}

return false;

}

bool Viewport::keyPressed (const KeyPress& key)
{
const bool isUpDownKey = key.isKeyCode (KeyPress::upKey)
|| key.isKeyCode (KeyPress::downKey)
|| key.isKeyCode (KeyPress::pageUpKey)
|| key.isKeyCode (KeyPress::pageDownKey)
|| key.isKeyCode (KeyPress::homeKey)
|| key.isKeyCode (KeyPress::endKey);

if (verticalScrollBar.isVisible() && isUpDownKey)
    return verticalScrollBar.keyPressed (key);

const bool isLeftRightKey = key.isKeyCode (KeyPress::leftKey)
|| key.isKeyCode (KeyPress::rightKey);

if (horizontalScrollBar.isVisible() && (isUpDownKey || isLeftRightKey))
    return horizontalScrollBar.keyPressed (key);

return false;

}

END_JUCE_NAMESPACE

#endif[/code]

juce_Viewport.h

[code]/*

This file is part of the JUCE library - "Jules’ Utility Class Extensions"
Copyright 2004-11 by Raw Material Software Ltd.


JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.

JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.


To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.

==============================================================================
*/

#ifndef JUCE_VIEWPORT_JUCEHEADER
#define JUCE_VIEWPORT_JUCEHEADER

#include “juce_ScrollBar.h”

#if JUCE_IOS
#include “…/…/juce_gui_extra/embedding/juce_UIViewComponent.h”
#endif

//==============================================================================
/**
A Viewport is used to contain a larger child component, and allows the child
to be automatically scrolled around.

To use a Viewport, just create one and set the component that goes inside it
using the setViewedComponent() method. When the child component changes size,
the Viewport will adjust its scrollbars accordingly.

A subclass of the viewport can be created which will receive calls to its
visibleAreaChanged() method when the subcomponent changes position or size.

/
class JUCE_API Viewport : public Component,
private ComponentListener,
private ScrollBar::Listener
{
public:
//==============================================================================
/
* Creates a Viewport.

    The viewport is initially empty - use the setViewedComponent() method to
    add a child component for it to manage.
*/
explicit Viewport (const String& componentName = String::empty);

/** Destructor. */
~Viewport();

//==============================================================================
/** Sets the component that this viewport will contain and scroll around.

    This will add the given component to this Viewport and position it at (0, 0).

    (Don't add or remove any child components directly using the normal
    Component::addChildComponent() methods).

    @param newViewedComponent   the component to add to this viewport, or null to remove
                                the current component.
    @param deleteComponentWhenNoLongerNeeded    if true, the component will be deleted
                                automatically when the viewport is deleted or when a different
                                component is added. If false, the caller must manage the lifetime
                                of the component
    @see getViewedComponent
*/
void setViewedComponent (Component* newViewedComponent,
                         bool deleteComponentWhenNoLongerNeeded = true);

/** Returns the component that's currently being used inside the Viewport.

    @see setViewedComponent
*/
Component* getViewedComponent() const noexcept;

//==============================================================================
/** Changes the position of the viewed component.

    The inner component will be moved so that the pixel at the top left of
    the viewport will be the pixel at position (xPixelsOffset, yPixelsOffset)
    within the inner component.

    This will update the scrollbars and might cause a call to visibleAreaChanged().

    @see getViewPositionX, getViewPositionY, setViewPositionProportionately
*/
void setViewPosition (int xPixelsOffset, int yPixelsOffset);

/** Changes the position of the viewed component.

    The inner component will be moved so that the pixel at the top left of
    the viewport will be the pixel at the specified coordinates within the
    inner component.

    This will update the scrollbars and might cause a call to visibleAreaChanged().

    @see getViewPositionX, getViewPositionY, setViewPositionProportionately
*/
void setViewPosition (const Point<int>& newPosition);

/** Changes the view position as a proportion of the distance it can move.

    The values here are from 0.0 to 1.0 - where (0, 0) would put the
    visible area in the top-left, and (1, 1) would put it as far down and
    to the right as it's possible to go whilst keeping the child component
    on-screen.
*/
void setViewPositionProportionately (double proportionX, double proportionY);

/** If the specified position is at the edges of the viewport, this method scrolls
    the viewport to bring that position nearer to the centre.

    Call this if you're dragging an object inside a viewport and want to make it scroll
    when the user approaches an edge. You might also find Component::beginDragAutoRepeat()
    useful when auto-scrolling.

    @param mouseX       the x position, relative to the Viewport's top-left
    @param mouseY       the y position, relative to the Viewport's top-left
    @param distanceFromEdge     specifies how close to an edge the position needs to be
                        before the viewport should scroll in that direction
    @param maximumSpeed the maximum number of pixels that the viewport is allowed
                        to scroll by.
    @returns            true if the viewport was scrolled
*/
bool autoScroll (int mouseX, int mouseY, int distanceFromEdge, int maximumSpeed);

/** Returns the position within the child component of the top-left of its visible area.
*/
const Point<int> getViewPosition() const noexcept;

/** Returns the position within the child component of the top-left of its visible area.
    @see getViewWidth, setViewPosition
*/
int getViewPositionX() const noexcept;

/** Returns the position within the child component of the top-left of its visible area.
    @see getViewHeight, setViewPosition
*/
int getViewPositionY() const noexcept;

/** Returns the width of the visible area of the child component.

    This may be less than the width of this Viewport if there's a vertical scrollbar
    or if the child component is itself smaller.
*/
int getViewWidth() const noexcept;

/** Returns the height of the visible area of the child component.

    This may be less than the height of this Viewport if there's a horizontal scrollbar
    or if the child component is itself smaller.
*/
int getViewHeight() const noexcept;

/** Returns the width available within this component for the contents.

    This will be the width of the viewport component minus the width of a
    vertical scrollbar (if visible).
*/
int getMaximumVisibleWidth() const;

/** Returns the height available within this component for the contents.

    This will be the height of the viewport component minus the space taken up
    by a horizontal scrollbar (if visible).
*/
int getMaximumVisibleHeight() const;

//==============================================================================
/** Callback method that is called when the visible area changes.

    This will be called when the visible area is moved either be scrolling or
    by calls to setViewPosition(), etc.
*/
virtual void visibleAreaChanged (const Rectangle<int>& newVisibleArea);

/** Callback method that is called when the viewed component is added, removed or swapped. */
virtual void viewedComponentChanged (Component* newComponent);

//==============================================================================
/** Turns scrollbars on or off.

    If set to false, the scrollbars won't ever appear. When true (the default)
    they will appear only when needed.
*/
void setScrollBarsShown (bool showVerticalScrollbarIfNeeded,
                         bool showHorizontalScrollbarIfNeeded);

/** True if the vertical scrollbar is enabled.
    @see setScrollBarsShown
*/
bool isVerticalScrollBarShown() const noexcept;

/** True if the horizontal scrollbar is enabled.
    @see setScrollBarsShown
*/
bool isHorizontalScrollBarShown() const noexcept;

/** Changes the width of the scrollbars.

    If this isn't specified, the default width from the LookAndFeel class will be used.

    @see LookAndFeel::getDefaultScrollbarWidth
*/
void setScrollBarThickness (int thickness);

/** Returns the thickness of the scrollbars.

    @see setScrollBarThickness
*/
int getScrollBarThickness() const;

/** Changes the distance that a single-step click on a scrollbar button
    will move the viewport.
*/
void setSingleStepSizes (int stepX, int stepY);

/** Shows or hides the buttons on any scrollbars that are used.

    @see ScrollBar::setButtonVisibility
*/
void setScrollBarButtonVisibility (bool buttonsVisible);

/** Returns a pointer to the scrollbar component being used.
    Handy if you need to customise the bar somehow.
*/
ScrollBar* getVerticalScrollBar() noexcept;

/** Returns a pointer to the scrollbar component being used.
    Handy if you need to customise the bar somehow.
*/
ScrollBar* getHorizontalScrollBar() noexcept;


//==============================================================================
/** @internal */
void resized();
/** @internal */
void scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart);
/** @internal */
void mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY);
/** @internal */
bool keyPressed (const KeyPress& key);
/** @internal */
void componentMovedOrResized (Component& component, bool wasMoved, bool wasResized);
/** @internal */
bool useMouseWheelMoveIfNeeded (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY);

private:
//==============================================================================
WeakReference contentComp;
Rectangle lastVisibleArea;
int scrollBarThickness;
int singleStepX, singleStepY;
bool showHScrollbar, showVScrollbar, deleteContent;
#if JUCE_IOS
UIViewComponent contentHolder;
#else
Component contentHolder;
#endif
ScrollBar verticalScrollBar;
ScrollBar horizontalScrollBar;
Point viewportPosToCompPos (const Point&) const;

void updateVisibleArea();
void deleteContentComp();

#if JUCE_CATCH_DEPRECATED_CODE_MISUSE
// If you get an error here, it’s because this method’s parameters have changed! See the new definition above…
virtual int visibleAreaChanged (int, int, int, int) { return 0; }
#endif

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Viewport);

};

#endif // JUCE_VIEWPORT_JUCEHEADER
[/code]

juce_Viewport.cpp

[code]/*

This file is part of the JUCE library - "Jules’ Utility Class Extensions"
Copyright 2004-11 by Raw Material Software Ltd.


JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.

JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.


To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.

==============================================================================
*/

#if (!JUCE_IOS)

BEGIN_JUCE_NAMESPACE

//==============================================================================
Viewport::Viewport (const String& name)
: Component (name),
scrollBarThickness (0),
singleStepX (16),
singleStepY (16),
showHScrollbar (true),
showVScrollbar (true),
deleteContent (true),
verticalScrollBar (true),
horizontalScrollBar (false)
{
// content holder is used to clip the contents so they don’t overlap the scrollbars
addAndMakeVisible (&contentHolder);
contentHolder.setInterceptsMouseClicks (false, true);

addChildComponent (&verticalScrollBar);
addChildComponent (&horizontalScrollBar);

verticalScrollBar.addListener (this);
horizontalScrollBar.addListener (this);

setInterceptsMouseClicks (false, true);
setWantsKeyboardFocus (true);

}

Viewport::~Viewport()
{
deleteContentComp();
}

//==============================================================================
void Viewport::visibleAreaChanged (const Rectangle&) {}
void Viewport::viewedComponentChanged (Component*) {}

//==============================================================================
void Viewport::deleteContentComp()
{
if (deleteContent)
{
// This sets the content comp to a null pointer before deleting the old one, in case
// anything tries to use the old one while it’s in mid-deletion…
ScopedPointer oldCompDeleter (contentComp);
contentComp = nullptr;
}
else
{
contentComp = nullptr;
}
}

void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
{
if (contentComp.get() != newViewedComponent)
{
deleteContentComp();
contentComp = newViewedComponent;
deleteContent = deleteComponentWhenNoLongerNeeded;

    if (contentComp != nullptr)
    {
        contentHolder.addAndMakeVisible (contentComp);
        setViewPosition (Point<int>());
        contentComp->addComponentListener (this);
    }

    viewedComponentChanged (contentComp);
    updateVisibleArea();
}

}

Component* Viewport::getViewedComponent() const noexcept
{
return contentComp;
}

int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }

Point Viewport::viewportPosToCompPos (const Point& pos) const
{
jassert (contentComp != nullptr);
return Point (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -pos.getX())),
jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -pos.getY())));
}

void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
{
setViewPosition (Point (xPixelsOffset, yPixelsOffset));
}

void Viewport::setViewPosition (const Point& newPosition)
{
if (contentComp != nullptr)
contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition));
}

void Viewport::setViewPositionProportionately (const double x, const double y)
{
if (contentComp != nullptr)
setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
}

bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
{
if (contentComp != nullptr)
{
int dx = 0, dy = 0;

    if (horizontalScrollBar.isVisible() || contentComp->getX() < 0 || contentComp->getRight() > getWidth())
    {
        if (mouseX < activeBorderThickness)
            dx = activeBorderThickness - mouseX;
        else if (mouseX >= contentHolder.getWidth() - activeBorderThickness)
            dx = (contentHolder.getWidth() - activeBorderThickness) - mouseX;

        if (dx < 0)
            dx = jmax (dx, -maximumSpeed, contentHolder.getWidth() - contentComp->getRight());
        else
            dx = jmin (dx, maximumSpeed, -contentComp->getX());
    }

    if (verticalScrollBar.isVisible() || contentComp->getY() < 0 || contentComp->getBottom() > getHeight())
    {
        if (mouseY < activeBorderThickness)
            dy = activeBorderThickness - mouseY;
        else if (mouseY >= contentHolder.getHeight() - activeBorderThickness)
            dy = (contentHolder.getHeight() - activeBorderThickness) - mouseY;

        if (dy < 0)
            dy = jmax (dy, -maximumSpeed, contentHolder.getHeight() - contentComp->getBottom());
        else
            dy = jmin (dy, maximumSpeed, -contentComp->getY());
    }

    if (dx != 0 || dy != 0)
    {
        contentComp->setTopLeftPosition (contentComp->getX() + dx,
                                         contentComp->getY() + dy);

        return true;
    }
}

return false;

}

const Point Viewport::getViewPosition() const noexcept
{
return lastVisibleArea.getPosition();
}

int Viewport::getViewPositionX() const noexcept
{
return lastVisibleArea.getX();
}

int Viewport::getViewPositionY() const noexcept
{
return lastVisibleArea.getY();
}

int Viewport::getViewWidth() const noexcept
{
return lastVisibleArea.getWidth();
}

int Viewport::getViewHeight() const noexcept
{
return lastVisibleArea.getHeight();
}

void Viewport::componentMovedOrResized (Component&, bool, bool)
{
updateVisibleArea();
}

void Viewport::resized()
{
updateVisibleArea();
}

//==============================================================================
void Viewport::updateVisibleArea()
{
const int scrollbarWidth = getScrollBarThickness();
const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
const bool canShowHBar = showHScrollbar && canShowAnyBars;
const bool canShowVBar = showVScrollbar && canShowAnyBars;

bool hBarVisible = canShowHBar && ! horizontalScrollBar.autoHides();
bool vBarVisible = canShowVBar && ! verticalScrollBar.autoHides();

Rectangle<int> contentArea (getLocalBounds());

if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
{
    hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
    vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());

    if (vBarVisible)
        contentArea.setWidth (getWidth() - scrollbarWidth);

    if (hBarVisible)
        contentArea.setHeight (getHeight() - scrollbarWidth);

    if (! contentArea.contains (contentComp->getBounds()))
    {
        hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
        vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
    }
}

if (vBarVisible)
    contentArea.setWidth (getWidth() - scrollbarWidth);

if (hBarVisible)
    contentArea.setHeight (getHeight() - scrollbarWidth);

contentHolder.setBounds (contentArea);

Rectangle<int> contentBounds;
if (contentComp != nullptr)
    contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());

Point<int> visibleOrigin (-contentBounds.getPosition());

if (hBarVisible)
{
    horizontalScrollBar.setBounds (0, contentArea.getHeight(), contentArea.getWidth(), scrollbarWidth);
    horizontalScrollBar.setRangeLimits (0.0, contentBounds.getWidth());
    horizontalScrollBar.setCurrentRange (visibleOrigin.getX(), contentArea.getWidth());
    horizontalScrollBar.setSingleStepSize (singleStepX);
    horizontalScrollBar.cancelPendingUpdate();
}
else if (canShowHBar)
{
    visibleOrigin.setX (0);
}

if (vBarVisible)
{
    verticalScrollBar.setBounds (contentArea.getWidth(), 0, scrollbarWidth, contentArea.getHeight());
    verticalScrollBar.setRangeLimits (0.0, contentBounds.getHeight());
    verticalScrollBar.setCurrentRange (visibleOrigin.getY(), contentArea.getHeight());
    verticalScrollBar.setSingleStepSize (singleStepY);
    verticalScrollBar.cancelPendingUpdate();
}
else if (canShowVBar)
{
    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);

if (contentComp != nullptr)
{
    const Point<int> newContentCompPos (viewportPosToCompPos (visibleOrigin));

    if (contentComp->getBounds().getPosition() != newContentCompPos)
    {
        contentComp->setTopLeftPosition (newContentCompPos);  // (this will re-entrantly call updateVisibleArea again)
        return;
    }
}

const Rectangle<int> visibleArea (visibleOrigin.getX(), visibleOrigin.getY(),
                                  jmin (contentBounds.getWidth()  - visibleOrigin.getX(), contentArea.getWidth()),
                                  jmin (contentBounds.getHeight() - visibleOrigin.getY(), contentArea.getHeight()));

if (lastVisibleArea != visibleArea)
{
    lastVisibleArea = visibleArea;
    visibleAreaChanged (visibleArea);
}

horizontalScrollBar.handleUpdateNowIfNeeded();
verticalScrollBar.handleUpdateNowIfNeeded();

}

//==============================================================================
void Viewport::setSingleStepSizes (const int stepX, const int stepY)
{
if (singleStepX != stepX || singleStepY != stepY)
{
singleStepX = stepX;
singleStepY = stepY;
updateVisibleArea();
}
}

void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
const bool showHorizontalScrollbarIfNeeded)
{
if (showVScrollbar != showVerticalScrollbarIfNeeded
|| showHScrollbar != showHorizontalScrollbarIfNeeded)
{
showVScrollbar = showVerticalScrollbarIfNeeded;
showHScrollbar = showHorizontalScrollbarIfNeeded;
updateVisibleArea();
}
}

bool Viewport::isVerticalScrollBarShown() const noexcept
{
return showVScrollbar;
}

bool Viewport::isHorizontalScrollBarShown() const noexcept
{
return showHScrollbar;
}

void Viewport::setScrollBarThickness (const int thickness)
{
if (scrollBarThickness != thickness)
{
scrollBarThickness = thickness;
updateVisibleArea();
}
}

int Viewport::getScrollBarThickness() const
{
return scrollBarThickness > 0 ? scrollBarThickness
: getLookAndFeel().getDefaultScrollbarWidth();
}

void Viewport::setScrollBarButtonVisibility (const bool buttonsVisible)
{
verticalScrollBar.setButtonVisibility (buttonsVisible);
horizontalScrollBar.setButtonVisibility (buttonsVisible);
}

ScrollBar* Viewport::getVerticalScrollBar() noexcept
{
return &verticalScrollBar;
}

ScrollBar* Viewport::getHorizontalScrollBar() noexcept
{
return &horizontalScrollBar;
}

void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{
const int newRangeStartInt = roundToInt (newRangeStart);

if (scrollBarThatHasMoved == &horizontalScrollBar)
{
    setViewPosition (newRangeStartInt, getViewPositionY());
}
else if (scrollBarThatHasMoved == &verticalScrollBar)
{
    setViewPosition (getViewPositionX(), newRangeStartInt);
}

}

void Viewport::mouseWheelMove (const MouseEvent& e, const float wheelIncrementX, const float wheelIncrementY)
{
if (! useMouseWheelMoveIfNeeded (e, wheelIncrementX, wheelIncrementY))
Component::mouseWheelMove (e, wheelIncrementX, wheelIncrementY);
}

bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
if (! (e.mods.isAltDown() || e.mods.isCtrlDown()))
{
const bool hasVertBar = verticalScrollBar.isVisible();
const bool hasHorzBar = horizontalScrollBar.isVisible();

    if (hasHorzBar || hasVertBar)
    {
        if (wheelIncrementX != 0)
        {
            wheelIncrementX *= 14.0f * singleStepX;
            wheelIncrementX = (wheelIncrementX < 0) ? jmin (wheelIncrementX, -1.0f)
                                                    : jmax (wheelIncrementX, 1.0f);
        }

        if (wheelIncrementY != 0)
        {
            wheelIncrementY *= 14.0f * singleStepY;
            wheelIncrementY = (wheelIncrementY < 0) ? jmin (wheelIncrementY, -1.0f)
                                                    : jmax (wheelIncrementY, 1.0f);
        }

        Point<int> pos (getViewPosition());

        if (wheelIncrementX != 0 && wheelIncrementY != 0 && hasHorzBar && hasVertBar)
        {
            pos.setX (pos.getX() - roundToInt (wheelIncrementX));
            pos.setY (pos.getY() - roundToInt (wheelIncrementY));
        }
        else if (hasHorzBar && (wheelIncrementX != 0 || e.mods.isShiftDown() || ! hasVertBar))
        {
            if (wheelIncrementX == 0 && ! hasVertBar)
                wheelIncrementX = wheelIncrementY;

            pos.setX (pos.getX() - roundToInt (wheelIncrementX));
        }
        else if (hasVertBar && wheelIncrementY != 0)
        {
            pos.setY (pos.getY() - roundToInt (wheelIncrementY));
        }

        if (pos != getViewPosition())
        {
            setViewPosition (pos);
            return true;
        }
    }
}

return false;

}

bool Viewport::keyPressed (const KeyPress& key)
{
const bool isUpDownKey = key.isKeyCode (KeyPress::upKey)
|| key.isKeyCode (KeyPress::downKey)
|| key.isKeyCode (KeyPress::pageUpKey)
|| key.isKeyCode (KeyPress::pageDownKey)
|| key.isKeyCode (KeyPress::homeKey)
|| key.isKeyCode (KeyPress::endKey);

if (verticalScrollBar.isVisible() && isUpDownKey)
    return verticalScrollBar.keyPressed (key);

const bool isLeftRightKey = key.isKeyCode (KeyPress::leftKey)
                             || key.isKeyCode (KeyPress::rightKey);

if (horizontalScrollBar.isVisible() && (isUpDownKey || isLeftRightKey))
    return horizontalScrollBar.keyPressed (key);

return false;

}

END_JUCE_NAMESPACE

#else
#include “…/native/juce_ios_Viewport.mm”
#endif
[/code]

Interesting stuff. I also need to look at the changes in 10.7 that deal with mouse wheel inversion.

I’ve been thinking about writing a juce module with ios gui wrappers.
Basically it could contain wrappers for sliders, buttons, listboxes and so on, treated as Components.
Maybe we could do something similar also for android, but that’s not my field.

right… I totally forgot about that. Isn’t that an NSScrollView? Same thing basically, just swap it for Mac OS X.7. I think this should be configured with some sort of #define, can’t really force this on anyone using the library.

[quote]I’ve been thinking about writing a juce module with ios gui wrappers.
Basically it could contain wrappers for sliders, buttons, listboxes and so on, treated as Components.
Maybe we could do something similar also for android, but that’s not my field.[/quote]

Yes, Android would be worth trying to do the Viewport thing as-well. As far as other controls I only see a need to do it for TextBox. TextBox->UITextField on iOS. All the other controls work very well.

This looks cool - what does it add? Gesture scrolling on iOS? No pinch to zoom implemented yet? Could a magnifier do that, or just setting a matrix on the top component these days?

Bruce

a slightly tighter implementation… basically making the Viewport class an UIViewComponent when in iOS;

juce_ios_Viewport.mm

[code]//==============================================================================
@interface JuceUIScrollView : UIScrollView
{
@private
juce::Viewport* owner;
}

@end

@implementation JuceUIScrollView

  • (id) initWithOwner: (juce::Viewport*) owner_
    {
    if ((self = [super init]) != nil)
    {
    owner = owner_;
    self.delegate = self;
    }

    return self;
    }

  • (void) dealloc
    {
    [super dealloc];
    }

  • (void) scrollViewDidScroll: (UIScrollView*) scrollView
    {
    const juce::Rectangle visibleArea (scrollView.contentOffset.x, scrollView.contentOffset.y,
    MIN (scrollView.contentSize.width - scrollView.contentOffset.x, owner->getWidth()),
    MIN (scrollView.contentSize.height - scrollView.contentOffset.y, owner->getHeight()));
    owner->visibleAreaChanged (visibleArea);
    owner->updateVisibleArea();
    }

@end

BEGIN_JUCE_NAMESPACE

//==============================================================================
Viewport::Viewport (const String& name)
: scrollBarThickness (0),
singleStepX (16),
singleStepY (16),
showHScrollbar (false),
showVScrollbar (false),
deleteContent (true),
verticalScrollBar (false),
horizontalScrollBar (false)
{
JuceUIScrollView* scroll = [[JuceUIScrollView alloc] initWithOwner: this];
setView (scroll);

setInterceptsMouseClicks (false, true);
setWantsKeyboardFocus (true);

}

Viewport::~Viewport()
{
deleteContentComp();
}

//==============================================================================
void Viewport::visibleAreaChanged (const Rectangle&) {}
void Viewport::viewedComponentChanged (Component*) {}

//==============================================================================
void Viewport::deleteContentComp()
{
if (contentComp != nullptr)
contentComp->removeFromDesktop();

if (deleteContent)
{
    // This sets the content comp to a null pointer before deleting the old one, in case
    // anything tries to use the old one while it's in mid-deletion..
    ScopedPointer<Component> oldCompDeleter (contentComp);
}
else
{
    contentComp = nullptr;
}

}

void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
{
if (contentComp.get() != newViewedComponent)
{
deleteContentComp();
contentComp = newViewedComponent;
deleteContent = deleteComponentWhenNoLongerNeeded;

    if (contentComp != nullptr)
    {
        contentComp->addToDesktop (0, (UIView*) getView());
        contentComp->setVisible (true);
        setViewPosition (Point<int>());
        contentComp->addComponentListener (this);
    }
    
    viewedComponentChanged (contentComp);
    updateVisibleArea();
}

}

int Viewport::getMaximumVisibleWidth() const { return getWidth(); }
int Viewport::getMaximumVisibleHeight() const { return getHeight(); }

Point Viewport::viewportPosToCompPos (const Point& pos) const
{
jassert (contentComp != nullptr);
return Point (jmax (jmin (0, getWidth() - contentComp->getWidth()), jmin (0, -(pos.x))),
jmax (jmin (0, getHeight() - contentComp->getHeight()), jmin (0, -(pos.y))));
}

void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
{
setViewPosition (Point (xPixelsOffset, yPixelsOffset));
}

void Viewport::setViewPosition (const Point& newPosition)
{
if (contentComp != nullptr)
[(UIScrollView*) getView() setContentOffset: CGPointMake (newPosition.getX(), newPosition.getY())];
}

void Viewport::setViewPositionProportionately (const double x, const double y)
{
if (contentComp != nullptr)
setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
}

bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
{
if (contentComp != nullptr)
{
int dx = 0, dy = 0;

    if (horizontalScrollBar.isVisible() || contentComp->getX() < 0 || contentComp->getRight() > getWidth())
    {
        if (mouseX < activeBorderThickness)
            dx = activeBorderThickness - mouseX;
        else if (mouseX >= getWidth() - activeBorderThickness)
            dx = (getWidth() - activeBorderThickness) - mouseX;
        
        if (dx < 0)
            dx = jmax (dx, -maximumSpeed, getWidth() - contentComp->getRight());
        else
            dx = jmin (dx, maximumSpeed, -contentComp->getX());
    }
    
    if (verticalScrollBar.isVisible() || contentComp->getY() < 0 || contentComp->getBottom() > getHeight())
    {
        if (mouseY < activeBorderThickness)
            dy = activeBorderThickness - mouseY;
        else if (mouseY >= getHeight() - activeBorderThickness)
            dy = (getHeight() - activeBorderThickness) - mouseY;
        
        if (dy < 0)
            dy = jmax (dy, -maximumSpeed, getHeight() - contentComp->getBottom());
        else
            dy = jmin (dy, maximumSpeed, -contentComp->getY());
    }
    
    if (dx != 0 || dy != 0)
    {            
        [(UIScrollView*) getView() setContentOffset: CGPointMake (contentComp->getX() + dx, contentComp->getY() + dy)];
        
        return true;
    }
}

return false;

}

void Viewport::componentMovedOrResized (Component&, bool, bool)
{
updateVisibleArea();

UIScrollView* scroll = (UIScrollView*) getView();
scroll.contentSize = CGSizeMake (contentComp->getWidth(), contentComp->getHeight());

}

void Viewport::resized()
{
updateVisibleArea();
}

//==============================================================================
void Viewport::updateVisibleArea()
{
Rectangle contentArea = getLocalBounds();

Rectangle<int> contentBounds;
if (contentComp != nullptr)
    contentBounds = getLocalArea (contentComp, contentComp->getLocalBounds());

CGPoint contentOffset = ((UIScrollView*) getView()).contentOffset;
Point<int> visibleOrigin (contentOffset.x, contentOffset.y);

const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
                                  jmin (contentBounds.getWidth()  - visibleOrigin.x, contentArea.getWidth()),
                                  jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));

if (lastVisibleArea != visibleArea)
{
    lastVisibleArea = visibleArea;
    visibleAreaChanged (visibleArea);
}

horizontalScrollBar.handleUpdateNowIfNeeded();
verticalScrollBar.handleUpdateNowIfNeeded();

}

//==============================================================================
void Viewport::setSingleStepSizes (const int stepX, const int stepY)
{
// intentionally empty…
}

void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
const bool showHorizontalScrollbarIfNeeded)
{
// intentionally empty…
}

void Viewport::setScrollBarThickness (const int thickness)
{
// intentionally empty…
}

int Viewport::getScrollBarThickness() const
{
return scrollBarThickness > 0 ? scrollBarThickness
: getLookAndFeel().getDefaultScrollbarWidth();
}

void Viewport::setScrollBarButtonVisibility (const bool buttonsVisible)
{
// intentionally empty…
}

void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{
// intentionally empty…
}

void Viewport::mouseWheelMove (const MouseEvent& e, const float wheelIncrementX, const float wheelIncrementY)
{
// intentionally empty…
}

bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
// Does not apply…
return false;
}

bool Viewport::keyPressed (const KeyPress& key)
{
// Does not apply…
return false;
}

END_JUCE_NAMESPACE[/code]

juce_Viewport.h

[code]/*

This file is part of the JUCE library - "Jules’ Utility Class Extensions"
Copyright 2004-11 by Raw Material Software Ltd.


JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.

JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.


To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.

==============================================================================
*/

#ifndef JUCE_VIEWPORT_JUCEHEADER
#define JUCE_VIEWPORT_JUCEHEADER

#include “juce_ScrollBar.h”

#if JUCE_IOS
#include “…/…/juce_gui_extra/embedding/juce_UIViewComponent.h”
#endif

//==============================================================================
/**
A Viewport is used to contain a larger child component, and allows the child
to be automatically scrolled around.

To use a Viewport, just create one and set the component that goes inside it
using the setViewedComponent() method. When the child component changes size,
the Viewport will adjust its scrollbars accordingly.

A subclass of the viewport can be created which will receive calls to its
visibleAreaChanged() method when the subcomponent changes position or size.

/
class JUCE_API Viewport :
#if JUCE_IOS
public UIViewComponent,
#else
public Component,
#endif
private ComponentListener,
private ScrollBar::Listener
{
public:
//==============================================================================
/
* Creates a Viewport.

    The viewport is initially empty - use the setViewedComponent() method to
    add a child component for it to manage.
*/
explicit Viewport (const String& componentName = String::empty);

/** Destructor. */
~Viewport();

//==============================================================================
/** Sets the component that this viewport will contain and scroll around.

    This will add the given component to this Viewport and position it at (0, 0).

    (Don't add or remove any child components directly using the normal
    Component::addChildComponent() methods).

    @param newViewedComponent   the component to add to this viewport, or null to remove
                                the current component.
    @param deleteComponentWhenNoLongerNeeded    if true, the component will be deleted
                                automatically when the viewport is deleted or when a different
                                component is added. If false, the caller must manage the lifetime
                                of the component
    @see getViewedComponent
*/
void setViewedComponent (Component* newViewedComponent,
                         bool deleteComponentWhenNoLongerNeeded = true);

/** Returns the component that's currently being used inside the Viewport.

    @see setViewedComponent
*/
Component* getViewedComponent() const noexcept                  { return contentComp; }

//==============================================================================
/** Changes the position of the viewed component.

    The inner component will be moved so that the pixel at the top left of
    the viewport will be the pixel at position (xPixelsOffset, yPixelsOffset)
    within the inner component.

    This will update the scrollbars and might cause a call to visibleAreaChanged().

    @see getViewPositionX, getViewPositionY, setViewPositionProportionately
*/
void setViewPosition (int xPixelsOffset, int yPixelsOffset);

/** Changes the position of the viewed component.

    The inner component will be moved so that the pixel at the top left of
    the viewport will be the pixel at the specified coordinates within the
    inner component.

    This will update the scrollbars and might cause a call to visibleAreaChanged().

    @see getViewPositionX, getViewPositionY, setViewPositionProportionately
*/
void setViewPosition (const Point<int>& newPosition);

/** Changes the view position as a proportion of the distance it can move.

    The values here are from 0.0 to 1.0 - where (0, 0) would put the
    visible area in the top-left, and (1, 1) would put it as far down and
    to the right as it's possible to go whilst keeping the child component
    on-screen.
*/
void setViewPositionProportionately (double proportionX, double proportionY);

/** If the specified position is at the edges of the viewport, this method scrolls
    the viewport to bring that position nearer to the centre.

    Call this if you're dragging an object inside a viewport and want to make it scroll
    when the user approaches an edge. You might also find Component::beginDragAutoRepeat()
    useful when auto-scrolling.

    @param mouseX       the x position, relative to the Viewport's top-left
    @param mouseY       the y position, relative to the Viewport's top-left
    @param distanceFromEdge     specifies how close to an edge the position needs to be
                        before the viewport should scroll in that direction
    @param maximumSpeed the maximum number of pixels that the viewport is allowed
                        to scroll by.
    @returns            true if the viewport was scrolled
*/
bool autoScroll (int mouseX, int mouseY, int distanceFromEdge, int maximumSpeed);

/** Returns the position within the child component of the top-left of its visible area.
*/
const Point<int>& getViewPosition() const noexcept      { return lastVisibleArea.getPosition(); }

/** Returns the position within the child component of the top-left of its visible area.
    @see getViewWidth, setViewPosition
*/
int getViewPositionX() const noexcept                   { return lastVisibleArea.getX(); }

/** Returns the position within the child component of the top-left of its visible area.
    @see getViewHeight, setViewPosition
*/
int getViewPositionY() const noexcept                   { return lastVisibleArea.getY(); }

/** Returns the width of the visible area of the child component.

    This may be less than the width of this Viewport if there's a vertical scrollbar
    or if the child component is itself smaller.
*/
int getViewWidth() const noexcept                       { return lastVisibleArea.getWidth(); }

/** Returns the height of the visible area of the child component.

    This may be less than the height of this Viewport if there's a horizontal scrollbar
    or if the child component is itself smaller.
*/
int getViewHeight() const noexcept                      { return lastVisibleArea.getHeight(); }

/** Returns the width available within this component for the contents.

    This will be the width of the viewport component minus the width of a
    vertical scrollbar (if visible).
*/
int getMaximumVisibleWidth() const;

/** Returns the height available within this component for the contents.

    This will be the height of the viewport component minus the space taken up
    by a horizontal scrollbar (if visible).
*/
int getMaximumVisibleHeight() const;

//==============================================================================
/** Callback method that is called when the visible area changes.

    This will be called when the visible area is moved either be scrolling or
    by calls to setViewPosition(), etc.
*/
virtual void visibleAreaChanged (const Rectangle<int>& newVisibleArea);

/** Callback method that is called when the viewed component is added, removed or swapped. */
virtual void viewedComponentChanged (Component* newComponent);

//==============================================================================
/** Turns scrollbars on or off.

    If set to false, the scrollbars won't ever appear. When true (the default)
    they will appear only when needed.
*/
void setScrollBarsShown (bool showVerticalScrollbarIfNeeded,
                         bool showHorizontalScrollbarIfNeeded);

/** True if the vertical scrollbar is enabled.
    @see setScrollBarsShown
*/
bool isVerticalScrollBarShown() const noexcept              { return showVScrollbar; }

/** True if the horizontal scrollbar is enabled.
    @see setScrollBarsShown
*/
bool isHorizontalScrollBarShown() const noexcept            { return showHScrollbar; }

/** Changes the width of the scrollbars.

    If this isn't specified, the default width from the LookAndFeel class will be used.

    @see LookAndFeel::getDefaultScrollbarWidth
*/
void setScrollBarThickness (int thickness);

/** Returns the thickness of the scrollbars.

    @see setScrollBarThickness
*/
int getScrollBarThickness() const;

/** Changes the distance that a single-step click on a scrollbar button
    will move the viewport.
*/
void setSingleStepSizes (int stepX, int stepY);

/** Shows or hides the buttons on any scrollbars that are used.

    @see ScrollBar::setButtonVisibility
*/
void setScrollBarButtonVisibility (bool buttonsVisible);

/** Returns a pointer to the scrollbar component being used.
    Handy if you need to customise the bar somehow.
*/
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; }


//==============================================================================
/** @internal */
void resized();
/** @internal */
void scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart);
/** @internal */
void mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY);
/** @internal */
bool keyPressed (const KeyPress& key);
/** @internal */
void componentMovedOrResized (Component& component, bool wasMoved, bool wasResized);
/** @internal */
bool useMouseWheelMoveIfNeeded (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY);

private:
//==============================================================================
WeakReference contentComp;
Rectangle lastVisibleArea;
int scrollBarThickness;
int singleStepX, singleStepY;
bool showHScrollbar, showVScrollbar, deleteContent;
Component contentHolder;
ScrollBar verticalScrollBar;
ScrollBar horizontalScrollBar;
Point viewportPosToCompPos (const Point&) const;

#if JUCE_IOS
public:
#endif
void updateVisibleArea();
#if JUCE_IOS
private:
#endif
void deleteContentComp();

#if JUCE_CATCH_DEPRECATED_CODE_MISUSE
// If you get an error here, it’s because this method’s parameters have changed! See the new definition above…
virtual int visibleAreaChanged (int, int, int, int) { return 0; }
#endif

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Viewport);

};

#endif // JUCE_VIEWPORT_JUCEHEADER[/code]

juce_Viewport.cpp

[code]/*

This file is part of the JUCE library - "Jules’ Utility Class Extensions"
Copyright 2004-11 by Raw Material Software Ltd.


JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.

JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.


To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.

==============================================================================
*/

#if !JUCE_IOS

BEGIN_JUCE_NAMESPACE

//==============================================================================
Viewport::Viewport (const String& name)
: Component (name),
scrollBarThickness (0),
singleStepX (16),
singleStepY (16),
showHScrollbar (true),
showVScrollbar (true),
deleteContent (true),
verticalScrollBar (true),
horizontalScrollBar (false)
{
// content holder is used to clip the contents so they don’t overlap the scrollbars
addAndMakeVisible (&contentHolder);
contentHolder.setInterceptsMouseClicks (false, true);

addChildComponent (&verticalScrollBar);
addChildComponent (&horizontalScrollBar);

verticalScrollBar.addListener (this);
horizontalScrollBar.addListener (this);

setInterceptsMouseClicks (false, true);
setWantsKeyboardFocus (true);

}

Viewport::~Viewport()
{
deleteContentComp();
}

//==============================================================================
void Viewport::visibleAreaChanged (const Rectangle&) {}
void Viewport::viewedComponentChanged (Component*) {}

//==============================================================================
void Viewport::deleteContentComp()
{
if (deleteContent)
{
// This sets the content comp to a null pointer before deleting the old one, in case
// anything tries to use the old one while it’s in mid-deletion…
ScopedPointer oldCompDeleter (contentComp);
}
else
{
contentComp = nullptr;
}
}

void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
{
if (contentComp.get() != newViewedComponent)
{
deleteContentComp();
contentComp = newViewedComponent;
deleteContent = deleteComponentWhenNoLongerNeeded;

    if (contentComp != nullptr)
    {
        contentHolder.addAndMakeVisible (contentComp);
        setViewPosition (Point<int>());
        contentComp->addComponentListener (this);
    }

    viewedComponentChanged (contentComp);
    updateVisibleArea();
}

}

int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }

Point Viewport::viewportPosToCompPos (const Point& pos) const
{
jassert (contentComp != nullptr);
return Point (jmax (jmin (0, contentHolder.getWidth() - contentComp->getWidth()), jmin (0, -(pos.x))),
jmax (jmin (0, contentHolder.getHeight() - contentComp->getHeight()), jmin (0, -(pos.y))));
}

void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
{
setViewPosition (Point (xPixelsOffset, yPixelsOffset));
}

void Viewport::setViewPosition (const Point& newPosition)
{
if (contentComp != nullptr)
contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition));
}

void Viewport::setViewPositionProportionately (const double x, const double y)
{
if (contentComp != nullptr)
setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
}

bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
{
if (contentComp != nullptr)
{
int dx = 0, dy = 0;

    if (horizontalScrollBar.isVisible() || contentComp->getX() < 0 || contentComp->getRight() > getWidth())
    {
        if (mouseX < activeBorderThickness)
            dx = activeBorderThickness - mouseX;
        else if (mouseX >= contentHolder.getWidth() - activeBorderThickness)
            dx = (contentHolder.getWidth() - activeBorderThickness) - mouseX;

        if (dx < 0)
            dx = jmax (dx, -maximumSpeed, contentHolder.getWidth() - contentComp->getRight());
        else
            dx = jmin (dx, maximumSpeed, -contentComp->getX());
    }

    if (verticalScrollBar.isVisible() || contentComp->getY() < 0 || contentComp->getBottom() > getHeight())
    {
        if (mouseY < activeBorderThickness)
            dy = activeBorderThickness - mouseY;
        else if (mouseY >= contentHolder.getHeight() - activeBorderThickness)
            dy = (contentHolder.getHeight() - activeBorderThickness) - mouseY;

        if (dy < 0)
            dy = jmax (dy, -maximumSpeed, contentHolder.getHeight() - contentComp->getBottom());
        else
            dy = jmin (dy, maximumSpeed, -contentComp->getY());
    }

    if (dx != 0 || dy != 0)
    {
        contentComp->setTopLeftPosition (contentComp->getX() + dx,
                                         contentComp->getY() + dy);

        return true;
    }
}

return false;

}

void Viewport::componentMovedOrResized (Component&, bool, bool)
{
updateVisibleArea();
}

void Viewport::resized()
{
updateVisibleArea();
}

//==============================================================================
void Viewport::updateVisibleArea()
{
const int scrollbarWidth = getScrollBarThickness();
const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
const bool canShowHBar = showHScrollbar && canShowAnyBars;
const bool canShowVBar = showVScrollbar && canShowAnyBars;

bool hBarVisible = false, vBarVisible = false;
Rectangle<int> contentArea;

for (int i = 3; --i >= 0;)
{
    hBarVisible = canShowHBar && ! horizontalScrollBar.autoHides();
    vBarVisible = canShowVBar && ! verticalScrollBar.autoHides();
    contentArea = getLocalBounds();

    if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
    {
        hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
        vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());

        if (vBarVisible)
            contentArea.setWidth (getWidth() - scrollbarWidth);

        if (hBarVisible)
            contentArea.setHeight (getHeight() - scrollbarWidth);

        if (! contentArea.contains (contentComp->getBounds()))
        {
            hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
            vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
        }
    }

    if (vBarVisible)  contentArea.setWidth  (getWidth()  - scrollbarWidth);
    if (hBarVisible)  contentArea.setHeight (getHeight() - scrollbarWidth);

    if (contentComp == nullptr)
    {
        contentHolder.setBounds (contentArea);
        break;
    }

    const Rectangle<int> oldContentBounds (contentComp->getBounds());
    contentHolder.setBounds (contentArea);

    // If the content has changed its size, that might affect our scrollbars, so go round again and re-caclulate..
    if (oldContentBounds == contentComp->getBounds())
        break;
}

Rectangle<int> contentBounds;
if (contentComp != nullptr)
    contentBounds = contentHolder.getLocalArea (contentComp, contentComp->getLocalBounds());

Point<int> visibleOrigin (-contentBounds.getPosition());

if (hBarVisible)
{
    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();
}
else if (canShowHBar)
{
    visibleOrigin.setX (0);
}

if (vBarVisible)
{
    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();
}
else if (canShowVBar)
{
    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);

if (contentComp != nullptr)
{
    const Point<int> newContentCompPos (viewportPosToCompPos (visibleOrigin));

    if (contentComp->getBounds().getPosition() != newContentCompPos)
    {
        contentComp->setTopLeftPosition (newContentCompPos);  // (this will re-entrantly call updateVisibleArea again)
        return;
    }
}

const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
                                  jmin (contentBounds.getWidth()  - visibleOrigin.x, contentArea.getWidth()),
                                  jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));

if (lastVisibleArea != visibleArea)
{
    lastVisibleArea = visibleArea;
    visibleAreaChanged (visibleArea);
}

horizontalScrollBar.handleUpdateNowIfNeeded();
verticalScrollBar.handleUpdateNowIfNeeded();

}

//==============================================================================
void Viewport::setSingleStepSizes (const int stepX, const int stepY)
{
if (singleStepX != stepX || singleStepY != stepY)
{
singleStepX = stepX;
singleStepY = stepY;
updateVisibleArea();
}
}

void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
const bool showHorizontalScrollbarIfNeeded)
{
if (showVScrollbar != showVerticalScrollbarIfNeeded
|| showHScrollbar != showHorizontalScrollbarIfNeeded)
{
showVScrollbar = showVerticalScrollbarIfNeeded;
showHScrollbar = showHorizontalScrollbarIfNeeded;
updateVisibleArea();
}
}

void Viewport::setScrollBarThickness (const int thickness)
{
if (scrollBarThickness != thickness)
{
scrollBarThickness = thickness;
updateVisibleArea();
}
}

int Viewport::getScrollBarThickness() const
{
return scrollBarThickness > 0 ? scrollBarThickness
: getLookAndFeel().getDefaultScrollbarWidth();
}

void Viewport::setScrollBarButtonVisibility (const bool buttonsVisible)
{
verticalScrollBar.setButtonVisibility (buttonsVisible);
horizontalScrollBar.setButtonVisibility (buttonsVisible);
}

void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{
const int newRangeStartInt = roundToInt (newRangeStart);

if (scrollBarThatHasMoved == &horizontalScrollBar)
{
    setViewPosition (newRangeStartInt, getViewPositionY());
}
else if (scrollBarThatHasMoved == &verticalScrollBar)
{
    setViewPosition (getViewPositionX(), newRangeStartInt);
}

}

void Viewport::mouseWheelMove (const MouseEvent& e, const float wheelIncrementX, const float wheelIncrementY)
{
if (! useMouseWheelMoveIfNeeded (e, wheelIncrementX, wheelIncrementY))
Component::mouseWheelMove (e, wheelIncrementX, wheelIncrementY);
}

bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
{
if (! (e.mods.isAltDown() || e.mods.isCtrlDown()))
{
const bool hasVertBar = verticalScrollBar.isVisible();
const bool hasHorzBar = horizontalScrollBar.isVisible();

    if (hasHorzBar || hasVertBar)
    {
        if (wheelIncrementX != 0)
        {
            wheelIncrementX *= 14.0f * singleStepX;
            wheelIncrementX = (wheelIncrementX < 0) ? jmin (wheelIncrementX, -1.0f)
                                                    : jmax (wheelIncrementX, 1.0f);
        }

        if (wheelIncrementY != 0)
        {
            wheelIncrementY *= 14.0f * singleStepY;
            wheelIncrementY = (wheelIncrementY < 0) ? jmin (wheelIncrementY, -1.0f)
                                                    : jmax (wheelIncrementY, 1.0f);
        }

        Point<int> pos (getViewPosition());

        if (wheelIncrementX != 0 && wheelIncrementY != 0 && hasHorzBar && hasVertBar)
        {
            pos.setX (pos.x - roundToInt (wheelIncrementX));
            pos.setY (pos.y - roundToInt (wheelIncrementY));
        }
        else if (hasHorzBar && (wheelIncrementX != 0 || e.mods.isShiftDown() || ! hasVertBar))
        {
            if (wheelIncrementX == 0 && ! hasVertBar)
                wheelIncrementX = wheelIncrementY;

            pos.setX (pos.x - roundToInt (wheelIncrementX));
        }
        else if (hasVertBar && wheelIncrementY != 0)
        {
            pos.setY (pos.y - roundToInt (wheelIncrementY));
        }

        if (pos != getViewPosition())
        {
            setViewPosition (pos);
            return true;
        }
    }
}

return false;

}

bool Viewport::keyPressed (const KeyPress& key)
{
const bool isUpDownKey = key.isKeyCode (KeyPress::upKey)
|| key.isKeyCode (KeyPress::downKey)
|| key.isKeyCode (KeyPress::pageUpKey)
|| key.isKeyCode (KeyPress::pageDownKey)
|| key.isKeyCode (KeyPress::homeKey)
|| key.isKeyCode (KeyPress::endKey);

if (verticalScrollBar.isVisible() && isUpDownKey)
    return verticalScrollBar.keyPressed (key);

const bool isLeftRightKey = key.isKeyCode (KeyPress::leftKey)
                             || key.isKeyCode (KeyPress::rightKey);

if (horizontalScrollBar.isVisible() && (isUpDownKey || isLeftRightKey))
    return horizontalScrollBar.keyPressed (key);

return false;

}

END_JUCE_NAMESPACE

#else
#include “…/native/juce_ios_Viewport.mm”
#endif
[/code]