Class AdvancedGridLayout

Nested Relationships

Inheritance Relationships

Base Type

Class Documentation

class AdvancedGridLayout : public nanogui::Layout

Advanced Grid layout.

The is a fancier grid layout with support for items that span multiple rows or columns, and per-widget alignment flags. Each row and column additionally stores a stretch factor that controls how additional space is redistributed. The downside of this flexibility is that a layout anchor data structure must be provided for each widget.

An example:

using AdvancedGridLayout::Anchor;
Label *label = new Label(window, "A label");
// Add a centered label at grid position (1, 5), which spans two horizontal cells
layout->setAnchor(label, Anchor(1, 5, 2, 1, Alignment::Middle, Alignment::Middle));

The grid is initialized with user-specified column and row size vectors (which can be expanded later on if desired). If a size value of zero is specified for a column or row, the size is set to the maximum preferred size of any widgets contained in the same row or column. Any remaining space is redistributed according to the row and column stretch factors.

The high level usage somewhat resembles the classic HIG layout:

Public Functions

AdvancedGridLayout(const std::vector<int> &cols = {}, const std::vector<int> &rows = {}, int margin = 0)

Creates an AdvancedGridLayout with specified columns, rows, and margin.

int margin() const

The margin of this AdvancedGridLayout.

void setMargin(int margin)

Sets the margin of this AdvancedGridLayout.

int colCount() const

Return the number of cols.

int rowCount() const

Return the number of rows.

void appendRow(int size, float stretch = 0.f)

Append a row of the given size (and stretch factor)

void appendCol(int size, float stretch = 0.f)

Append a column of the given size (and stretch factor)

void setRowStretch(int index, float stretch)

Set the stretch factor of a given row.

void setColStretch(int index, float stretch)

Set the stretch factor of a given column.

void setAnchor(const Widget *widget, const Anchor &anchor)

Specify the anchor data structure for a given widget.

Anchor anchor(const Widget *widget) const

Retrieve the anchor data structure for a given widget.

virtual Vector2i preferredSize(NVGcontext *ctx, const Widget *widget) const

See Layout::preferredSize.

virtual void performLayout(NVGcontext *ctx, Widget *widget) const

See Layout::performLayout.

Protected Functions

void computeLayout(NVGcontext *ctx, const Widget *widget, std::vector<int> *grid) const

Computes the layout.

Protected Attributes

std::vector<int> mCols

The columns of this AdvancedGridLayout.

std::vector<int> mRows

The rows of this AdvancedGridLayout.

std::vector<float> mColStretch

The stretch for each column of this AdvancedGridLayout.

std::vector<float> mRowStretch

The stretch for each row of this AdvancedGridLayout.

std::unordered_map<const Widget *, Anchor> mAnchor

The mapping of widgets to their specified anchor points.

int mMargin

The margin around this AdvancedGridLayout.

struct Anchor

Helper struct to coordinate anchor points for the layout.

Public Functions

Anchor()

Creates a 0 Anchor.

Anchor(int x, int y, Alignment horiz = Alignment::Fill, Alignment vert = Alignment::Fill)

Create an Anchor at position (x, y) with specified Alignment.

Anchor(int x, int y, int w, int h, Alignment horiz = Alignment::Fill, Alignment vert = Alignment::Fill)

Create an Anchor at position (x, y) of size (w, h) with specified alignments.

operator std::string() const

Allows for printing out Anchor position, size, and alignment.

Public Members

uint8_t pos[2]

The (x, y) position.

uint8_t size[2]

The (x, y) size.

Alignment align[2]

The (x, y) Alignment.