Skip to content

Container

#include <Container.hpp>

Inherits: Widget

A widget that arranges child widgets using a layout engine.

Container is the primary way to organize multiple widgets. It holds children and uses a Layout to position them. By default, it uses VerticalStackLayout, but you can specify any Layout (Grid, Absolute, custom, etc.).

Container is not focusable itself - focus passes through to focusable children.

Usage patterns:

// Default vertical stack
auto container = std::make_shared<Container>();
container->addChild(std::make_shared<Label>("Title"));
container->addChild(std::make_shared<Button>("Click me"));
// Horizontal stack
auto hbox = std::make_shared<Container>(
std::make_shared<StackLayout>(Axis::Horizontal)
);
// Grid layout
auto grid = std::make_shared<Container>(
std::make_shared<GridLayout>(2, 3) // 2 cols, 3 rows
);
ReturnNameDescription
Container inline explicitCreates a container with the specified layout.
voidaddChildAdds a child widget to the container.
voidsetLayout inlineChanges the layout engine.
core::Sizemeasure virtualMeasures how much space this widget needs.
voidlayout virtualPositions and sizes this widget and its children.
std::vector< std::shared_ptr< Widget > >getChildren virtual const inlineReturns this widget’s children.

inline explicit

inline explicit Container(std::shared_ptr< core::Layout > layout)

Creates a container with the specified layout.

  • layout The layout engine to use (default: VerticalStackLayout)

void addChild(std::shared_ptr< Widget > child, core::LayoutSpec spec)

Adds a child widget to the container.

The LayoutSpec controls how the child is sized and positioned within its slot.

  • child The widget to add

  • spec Layout specification for this child (optional)

Example Usage:

auto container = std::make_shared<Container>();
auto button = std::make_shared<Button>("Flex");
container->addChild(button, LayoutSpec{.ratio = 2.0f}); // Double weight

inline

inline void setLayout(std::shared_ptr< core::Layout > l)

Changes the layout engine.

  • l The new layout to use

virtual

virtual core::Size measure(int availW, int availH)

Measures how much space this widget needs.

Called during the measurement pass to determine widget size requirements. Should return the preferred size based on available space.

  • availW Maximum available width (may be ignored by widget)

  • availH Maximum available height (may be ignored by widget)

The Size this widget wants, or needs at minimum


virtual

virtual void layout(int x, int y, int width, int height)

Positions and sizes this widget and its children.

Called after measurement to actually place the widget at a specific location. If your widget has children, you must call their layout() methods here.

  • x The X coordinate for this widget

  • y The Y coordinate for this widget

  • width The width allocated to this widget

  • height The height allocated to this widget


virtual const inline

virtual inline std::vector< std::shared_ptr< Widget > > getChildren() const

Returns this widget’s children.

Vector of child widgets (empty if no children)

ReturnNameDescription
voiddrawContent virtualDraws this widget’s content.

virtual

virtual void drawContent(backend::Buffer & buf, core::Rect inner)

Draws this widget’s content.

Called by render() after background and borders are drawn. This is where you should draw your widget’s actual content. The inner rectangle accounts for borders and padding.

  • buf The buffer to draw to

  • inner The inner rectangle (excluding border/padding)

ReturnNameDescription
std::shared_ptr< core::Layout >layoutEngine
std::vector< core::ChildSlot >children

std::shared_ptr< core::Layout > layoutEngine

std::vector< core::ChildSlot > children