JUCE  v5.1.1-3-g1a0b28c73
JUCE API
StretchableLayoutManager Class Reference

For laying out a set of components, where the components have preferred sizes and size limits, but where they are allowed to stretch to fill the available space. More...

#include <juce_StretchableLayoutManager.h>

Collaboration diagram for StretchableLayoutManager:

Classes

struct  ItemLayoutProperties
 

Public Member Functions

 StretchableLayoutManager ()
 Creates an empty layout. More...
 
 ~StretchableLayoutManager ()
 Destructor. More...
 
void clearAllItems ()
 Clears all the properties that have been set with setItemLayout() and resets this object to its initial state. More...
 
int getItemCurrentAbsoluteSize (int itemIndex) const
 Returns the current size of one of the items. More...
 
int getItemCurrentPosition (int itemIndex) const
 Returns the current position of one of the items. More...
 
double getItemCurrentRelativeSize (int itemIndex) const
 Returns the current size of one of the items. More...
 
bool getItemLayout (int itemIndex, double &minimumSize, double &maximumSize, double &preferredSize) const
 For a numbered item, this returns its size limits and preferred size. More...
 
void layOutComponents (Component **components, int numComponents, int x, int y, int width, int height, bool vertically, bool resizeOtherDimension)
 Takes a set of components that correspond to the layout's items, and positions them to fill a space. More...
 
void setItemLayout (int itemIndex, double minimumSize, double maximumSize, double preferredSize)
 For a numbered item, this sets its size limits and preferred size. More...
 
void setItemPosition (int itemIndex, int newPosition)
 Moves one of the items, shifting along any other items as necessary in order to get it to the desired position. More...
 

Private Member Functions

int fitComponentsIntoSpace (int startIndex, int endIndex, int availableSpace, int startPos)
 
ItemLayoutPropertiesgetInfoFor (int itemIndex) const
 
int getMaximumSizeOfItems (int startIndex, int endIndex) const
 
int getMinimumSizeOfItems (int startIndex, int endIndex) const
 
void setTotalSize (int newTotalSize)
 
void updatePrefSizesToMatchCurrentPositions ()
 

Static Private Member Functions

static int sizeToRealSize (double size, int totalSpace)
 

Private Attributes

OwnedArray< ItemLayoutPropertiesitems
 
int totalSize
 

Detailed Description

For laying out a set of components, where the components have preferred sizes and size limits, but where they are allowed to stretch to fill the available space.

For example, if you have a component containing several other components, and each one should be given a share of the total size, you could use one of these to resize the child components when the parent component is resized. Then you could add a StretchableLayoutResizerBar to easily let the user rescale them.

A StretchableLayoutManager operates only in one dimension, so if you have a set of components stacked vertically on top of each other, you'd use one to manage their heights. To build up complex arrangements of components, e.g. for applications with multiple nested panels, you would use more than one StretchableLayoutManager. E.g. by using two (one vertical, one horizontal), you could create a resizable spreadsheet-style table.

E.g.

class MyComp : public Component
{
MyComp()
{
myLayout.setItemLayout (0, // for item 0
50, 100, // must be between 50 and 100 pixels in size
-0.6); // and its preferred size is 60% of the total available space
myLayout.setItemLayout (1, // for item 1
-0.2, -0.6, // size must be between 20% and 60% of the available space
50); // and its preferred size is 50 pixels
}
void resized()
{
// make a list of two of our child components that we want to reposition
Component* comps[] = { myComp1, myComp2 };
// this will position the 2 components, one above the other, to fit
// vertically into the rectangle provided.
myLayout.layOutComponents (comps, 2,
0, 0, getWidth(), getHeight(),
true);
}
};
See also
StretchableLayoutResizerBar

Constructor & Destructor Documentation

◆ StretchableLayoutManager()

StretchableLayoutManager::StretchableLayoutManager ( )

Creates an empty layout.

You'll need to add some item properties to the layout before it can be used to resize things - see setItemLayout().

◆ ~StretchableLayoutManager()

StretchableLayoutManager::~StretchableLayoutManager ( )

Destructor.

Member Function Documentation

◆ clearAllItems()

void StretchableLayoutManager::clearAllItems ( )

Clears all the properties that have been set with setItemLayout() and resets this object to its initial state.

◆ fitComponentsIntoSpace()

int StretchableLayoutManager::fitComponentsIntoSpace ( int  startIndex,
int  endIndex,
int  availableSpace,
int  startPos 
)
private

◆ getInfoFor()

ItemLayoutProperties* StretchableLayoutManager::getInfoFor ( int  itemIndex) const
private

◆ getItemCurrentAbsoluteSize()

int StretchableLayoutManager::getItemCurrentAbsoluteSize ( int  itemIndex) const

Returns the current size of one of the items.

This is only meaningful after layOutComponents() has been called, as it returns the last size that this item was given. If the layout was done vertically, it'll return the item's height in pixels; if it was horizontal, it'll return its width.

See also
getItemCurrentRelativeSize

◆ getItemCurrentPosition()

int StretchableLayoutManager::getItemCurrentPosition ( int  itemIndex) const

Returns the current position of one of the items.

This is only a valid call after layOutComponents() has been called, as it returns the last position that this item was placed at. If the layout was vertical, the value returned will be the y position of the top of the item, relative to the top of the rectangle in which the items were placed (so for example, item 0 will always have position of 0, even in the rectangle passed in to layOutComponents() wasn't at y = 0). If the layout was done horizontally, the position returned is the item's left-hand position, again relative to the x position of the rectangle used.

See also
getItemCurrentSize, setItemPosition

◆ getItemCurrentRelativeSize()

double StretchableLayoutManager::getItemCurrentRelativeSize ( int  itemIndex) const

Returns the current size of one of the items.

This is only meaningful after layOutComponents() has been called, as it returns the last size that this item was given. If the layout was done vertically, it'll return a negative value representing the item's height relative to the last size used for laying the components out; if the layout was done horizontally it'll be the proportion of its width.

See also
getItemCurrentAbsoluteSize

◆ getItemLayout()

bool StretchableLayoutManager::getItemLayout ( int  itemIndex,
double &  minimumSize,
double &  maximumSize,
double &  preferredSize 
) const

For a numbered item, this returns its size limits and preferred size.

Parameters
itemIndexthe index of the item.
minimumSizethe minimum size that this item is allowed to be - a positive number indicates an absolute size in pixels. A negative number indicates a proportion of the available space (e.g -0.5 is 50%)
maximumSizethe maximum size that this item is allowed to be - a positive number indicates an absolute size in pixels. A negative number indicates a proportion of the available space
preferredSizethe size that this item would like to be, if there's enough room. A positive number indicates an absolute size in pixels. A negative number indicates a proportion of the available space
Returns
false if the item's properties hadn't been set
See also
setItemLayout

◆ getMaximumSizeOfItems()

int StretchableLayoutManager::getMaximumSizeOfItems ( int  startIndex,
int  endIndex 
) const
private

◆ getMinimumSizeOfItems()

int StretchableLayoutManager::getMinimumSizeOfItems ( int  startIndex,
int  endIndex 
) const
private

◆ layOutComponents()

void StretchableLayoutManager::layOutComponents ( Component **  components,
int  numComponents,
int  x,
int  y,
int  width,
int  height,
bool  vertically,
bool  resizeOtherDimension 
)

Takes a set of components that correspond to the layout's items, and positions them to fill a space.

This will try to give each item its preferred size, whether that's a relative size or an absolute one.

Parameters
componentsan array of components that correspond to each of the numbered items that the StretchableLayoutManager object has been told about with setItemLayout()
numComponentsthe number of components in the array that is passed-in. This should be the same as the number of items this object has been told about.
xthe left of the rectangle in which the components should be laid out
ythe top of the rectangle in which the components should be laid out
widththe width of the rectangle in which the components should be laid out
heightthe height of the rectangle in which the components should be laid out
verticallyif true, the components will be positioned in a vertical stack, so that they fill the height of the rectangle. If false, they will be placed side-by-side in a horizontal line, filling the available width
resizeOtherDimensionif true, this means that the components will have their other dimension resized to fit the space - i.e. if the 'vertically' parameter is true, their x-positions and widths are adjusted to fit the x and width parameters; if 'vertically' is false, their y-positions and heights are adjusted to fit the y and height parameters.

◆ setItemLayout()

void StretchableLayoutManager::setItemLayout ( int  itemIndex,
double  minimumSize,
double  maximumSize,
double  preferredSize 
)

For a numbered item, this sets its size limits and preferred size.

Parameters
itemIndexthe index of the item to change.
minimumSizethe minimum size that this item is allowed to be - a positive number indicates an absolute size in pixels. A negative number indicates a proportion of the available space (e.g -0.5 is 50%)
maximumSizethe maximum size that this item is allowed to be - a positive number indicates an absolute size in pixels. A negative number indicates a proportion of the available space
preferredSizethe size that this item would like to be, if there's enough room. A positive number indicates an absolute size in pixels. A negative number indicates a proportion of the available space
See also
getItemLayout

◆ setItemPosition()

void StretchableLayoutManager::setItemPosition ( int  itemIndex,
int  newPosition 
)

Moves one of the items, shifting along any other items as necessary in order to get it to the desired position.

Calling this method will also update the preferred sizes of the items it shuffles along, so that they reflect their new positions.

(This is the method that a StretchableLayoutResizerBar uses to shift the items about when it's dragged).

Parameters
itemIndexthe item to move
newPositionthe absolute position that you'd like this item to move to. The item might not be able to always reach exactly this position, because other items may have minimum sizes that constrain how far it can go

◆ setTotalSize()

void StretchableLayoutManager::setTotalSize ( int  newTotalSize)
private

◆ sizeToRealSize()

static int StretchableLayoutManager::sizeToRealSize ( double  size,
int  totalSpace 
)
staticprivate

◆ updatePrefSizesToMatchCurrentPositions()

void StretchableLayoutManager::updatePrefSizesToMatchCurrentPositions ( )
private

Member Data Documentation

◆ items

OwnedArray<ItemLayoutProperties> StretchableLayoutManager::items
private

◆ totalSize

int StretchableLayoutManager::totalSize
private

The documentation for this class was generated from the following file: