Public Member Functions

Viewport Class Reference

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

Inherits Component, ComponentListener, and ScrollBar::Listener.

List of all members.

Public Member Functions

 Viewport (const String &componentName=String::empty)
 Creates a Viewport.
 ~Viewport ()
 Destructor.
void setViewedComponent (Component *newViewedComponent, bool deleteComponentWhenNoLongerNeeded=true)
 Sets the component that this viewport will contain and scroll around.
ComponentgetViewedComponent () const noexcept
 Returns the component that's currently being used inside the Viewport.
void setViewPosition (int xPixelsOffset, int yPixelsOffset)
 Changes the position of the viewed component.
void setViewPosition (const Point< int > &newPosition)
 Changes the position of the viewed component.
void setViewPositionProportionately (double proportionX, double proportionY)
 Changes the view position as a proportion of the distance it can move.
bool autoScroll (int mouseX, int mouseY, int distanceFromEdge, int maximumSpeed)
 If the specified position is at the edges of the viewport, this method scrolls the viewport to bring that position nearer to the centre.
const Point< int > getViewPosition () const noexcept
 Returns the position within the child component of the top-left of its visible area.
int getViewPositionX () const noexcept
 Returns the position within the child component of the top-left of its visible area.
int getViewPositionY () const noexcept
 Returns the position within the child component of the top-left of its visible area.
int getViewWidth () const noexcept
 Returns the width of the visible area of the child component.
int getViewHeight () const noexcept
 Returns the height of the visible area of the child component.
int getMaximumVisibleWidth () const
 Returns the width available within this component for the contents.
int getMaximumVisibleHeight () const
 Returns the height available within this component for the contents.
virtual void visibleAreaChanged (const Rectangle< int > &newVisibleArea)
 Callback method that is called when the visible area changes.
void setScrollBarsShown (bool showVerticalScrollbarIfNeeded, bool showHorizontalScrollbarIfNeeded)
 Turns scrollbars on or off.
bool isVerticalScrollBarShown () const noexcept
 True if the vertical scrollbar is enabled.
bool isHorizontalScrollBarShown () const noexcept
 True if the horizontal scrollbar is enabled.
void setScrollBarThickness (int thickness)
 Changes the width of the scrollbars.
int getScrollBarThickness () const
 Returns the thickness of the scrollbars.
void setSingleStepSizes (int stepX, int stepY)
 Changes the distance that a single-step click on a scrollbar button will move the viewport.
void setScrollBarButtonVisibility (bool buttonsVisible)
 Shows or hides the buttons on any scrollbars that are used.
ScrollBargetVerticalScrollBar () noexcept
 Returns a pointer to the scrollbar component being used.
ScrollBargetHorizontalScrollBar () noexcept
 Returns a pointer to the scrollbar component being used.
void resized ()
 Called when this component's size has been changed.
void scrollBarMoved (ScrollBar *scrollBarThatHasMoved, double newRangeStart)
 Called when a ScrollBar is moved.
void mouseWheelMove (const MouseEvent &e, float wheelIncrementX, float wheelIncrementY)
 Called when the mouse-wheel is moved.
bool keyPressed (const KeyPress &key)
 Called when a key is pressed.
void componentMovedOrResized (Component &component, bool wasMoved, bool wasResized)
 Called when the component's position or size changes.
bool useMouseWheelMoveIfNeeded (const MouseEvent &e, float wheelIncrementX, float wheelIncrementY)

Detailed Description

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.


Constructor & Destructor Documentation

Viewport::Viewport ( const String componentName = String::empty ) [explicit]

Creates a Viewport.

The viewport is initially empty - use the setViewedComponent() method to add a child component for it to manage.

Viewport::~Viewport (  )

Destructor.


Member Function Documentation

void Viewport::setViewedComponent ( Component newViewedComponent,
bool  deleteComponentWhenNoLongerNeeded = true 
)

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).

Parameters:
newViewedComponentthe component to add to this viewport, or null to remove the current component.
deleteComponentWhenNoLongerNeededif 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 also:
getViewedComponent
Component* Viewport::getViewedComponent (  ) const

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

See also:
setViewedComponent
void Viewport::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 position (xPixelsOffset, yPixelsOffset) within the inner component.

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

See also:
getViewPositionX, getViewPositionY, setViewPositionProportionately
void Viewport::setViewPosition ( const Point< int > &  newPosition )

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 also:
getViewPositionX, getViewPositionY, setViewPositionProportionately
void Viewport::setViewPositionProportionately ( double  proportionX,
double  proportionY 
)

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.

bool Viewport::autoScroll ( int  mouseX,
int  mouseY,
int  distanceFromEdge,
int  maximumSpeed 
)

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.

Parameters:
mouseXthe x position, relative to the Viewport's top-left
mouseYthe y position, relative to the Viewport's top-left
distanceFromEdgespecifies how close to an edge the position needs to be before the viewport should scroll in that direction
maximumSpeedthe maximum number of pixels that the viewport is allowed to scroll by.
Returns:
true if the viewport was scrolled
const Point<int> Viewport::getViewPosition (  ) const

Returns the position within the child component of the top-left of its visible area.

int Viewport::getViewPositionX (  ) const

Returns the position within the child component of the top-left of its visible area.

See also:
getViewWidth, setViewPosition
int Viewport::getViewPositionY (  ) const

Returns the position within the child component of the top-left of its visible area.

See also:
getViewHeight, setViewPosition
int Viewport::getViewWidth (  ) const

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 Viewport::getViewHeight (  ) const

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 Viewport::getMaximumVisibleWidth (  ) const

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 Viewport::getMaximumVisibleHeight (  ) 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).

virtual void Viewport::visibleAreaChanged ( const Rectangle< int > &  newVisibleArea ) [virtual]

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.

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

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.

bool Viewport::isVerticalScrollBarShown (  ) const

True if the vertical scrollbar is enabled.

See also:
setScrollBarsShown
bool Viewport::isHorizontalScrollBarShown (  ) const

True if the horizontal scrollbar is enabled.

See also:
setScrollBarsShown
void Viewport::setScrollBarThickness ( int  thickness )

Changes the width of the scrollbars.

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

See also:
LookAndFeel::getDefaultScrollbarWidth
int Viewport::getScrollBarThickness (  ) const

Returns the thickness of the scrollbars.

See also:
setScrollBarThickness
void Viewport::setSingleStepSizes ( int  stepX,
int  stepY 
)

Changes the distance that a single-step click on a scrollbar button will move the viewport.

void Viewport::setScrollBarButtonVisibility ( bool  buttonsVisible )

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

See also:
ScrollBar::setButtonVisibility
ScrollBar* Viewport::getVerticalScrollBar (  )

Returns a pointer to the scrollbar component being used.

Handy if you need to customise the bar somehow.

ScrollBar* Viewport::getHorizontalScrollBar (  )

Returns a pointer to the scrollbar component being used.

Handy if you need to customise the bar somehow.

void Viewport::resized (  ) [virtual]

Called when this component's size has been changed.

A component can implement this method to do things such as laying out its child components when its width or height changes.

The method is called synchronously as a result of the setBounds or setSize methods, so repeatedly changing a components size will repeatedly call its resized method (unlike things like repainting, where multiple calls to repaint are coalesced together).

If the component is a top-level window on the desktop, its size could also be changed by operating-system factors beyond the application's control.

See also:
moved, setSize

Reimplemented from Component.

void Viewport::scrollBarMoved ( ScrollBar scrollBarThatHasMoved,
double  newRangeStart 
) [virtual]

Called when a ScrollBar is moved.

Parameters:
scrollBarThatHasMovedthe bar that has moved
newRangeStartthe new range start of this bar

Implements ScrollBar::Listener.

void Viewport::mouseWheelMove ( const MouseEvent e,
float  wheelIncrementX,
float  wheelIncrementY 
) [virtual]

Called when the mouse-wheel is moved.

This callback is sent to the component that the mouse is over when the wheel is moved.

If not overridden, the component will forward this message to its parent, so that parent components can collect mouse-wheel messages that happen to child components which aren't interested in them.

Parameters:
edetails about the position and status of the mouse event
wheelIncrementXthe speed and direction of the horizontal scroll-wheel - a positive value means the wheel has been pushed to the right, negative means it was pushed to the left
wheelIncrementYthe speed and direction of the vertical scroll-wheel - a positive value means the wheel has been pushed upwards, negative means it was pushed downwards

Reimplemented from Component.

bool Viewport::keyPressed ( const KeyPress key ) [virtual]

Called when a key is pressed.

When a key is pressed, the component that has the keyboard focus will have this method called. Remember that a component will only be given the focus if its setWantsKeyboardFocus() method has been used to enable this.

If your implementation returns true, the event will be consumed and not passed on to any other listeners. If it returns false, the key will be passed to any KeyListeners that have been registered with this component. As soon as one of these returns true, the process will stop, but if they all return false, the event will be passed upwards to this component's parent, and so on.

The default implementation of this method does nothing and returns false.

See also:
keyStateChanged, getCurrentlyFocusedComponent, addKeyListener

Reimplemented from Component.

void Viewport::componentMovedOrResized ( Component component,
bool  wasMoved,
bool  wasResized 
) [virtual]

Called when the component's position or size changes.

Parameters:
componentthe component that was moved or resized
wasMovedtrue if the component's top-left corner has just moved
wasResizedtrue if the component's width or height has just changed
See also:
Component::setBounds, Component::resized, Component::moved

Reimplemented from ComponentListener.

bool Viewport::useMouseWheelMoveIfNeeded ( const MouseEvent e,
float  wheelIncrementX,
float  wheelIncrementY 
)

The documentation for this class was generated from the following file:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines