Skip to content

Button

#include <Button.hpp>

Inherits: Widget

A clickable button widget.

Button displays text and responds to click events. It’s focusable and can be activated via Enter key or mouse click. Use setOnClick() to handle activation.

Example Usage:

auto btn = std::make_shared<Button>("Click me");
btn->setOnClick([](/api/widgets/button) {
std::cout << "Button clicked!\n";
});
ReturnNameDescription
Button inline explicitCreates a button with optional label text.
voidsetLabel inlineSets the button’s label text.
const std::string &getLabel const inlineGets the button’s label text.
voidsetOnClick inlineSets the callback to invoke when the button is activated. The callback is called when the user presses Enter (if focused) or clicks.
boolisFocusable virtual const inlineReturns whether this widget can receive keyboard focus.
core::Sizemeasure virtualMeasures how much space this widget needs.
boolonEvent virtualHandles an input event.

inline explicit

inline explicit Button(const std::string & label)

Creates a button with optional label text.

  • label The text to display on the button (default: empty)

inline

inline void setLabel(const std::string & l)

Sets the button’s label text.

  • l The new label text

const inline

inline const std::string & getLabel() const

Gets the button’s label text.

The current label


inline

inline void setOnClick(std::function< void()> cb)

Sets the callback to invoke when the button is activated. The callback is called when the user presses Enter (if focused) or clicks.

  • cb The function to call on activation (can be empty)

virtual const inline

virtual inline bool isFocusable() const

Returns whether this widget can receive keyboard focus.

Widgets like Button and Input return true; containers return false.

True if this widget can be focused, false otherwise


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 bool onEvent(const core::events::Event & event)

Handles an input event.

Called by the framework to deliver an event to this widget. The default implementation runs any listeners registered via addEventListener(), then returns false. Subclasses (Button, Input, etc.) override this to add built-in behavior, and should call Widget::onEvent(event) first to give user listeners a chance to handle the event before the widget’s own logic.

Return true if the event was handled, false to let it bubble to the parent widget.

  • event The input event

True if this widget (or one of its listeners) handled the event

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::stringlabel
std::function< void()>onClick

std::string label

std::function< void()> onClick