Button
Button
Section titled “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";});Public Methods
Section titled “Public Methods”| Return | Name | Description |
|---|---|---|
Button inline explicit | Creates a button with optional label text. | |
void | setLabel inline | Sets the button’s label text. |
const std::string & | getLabel const inline | Gets the button’s label text. |
void | setOnClick inline | Sets the callback to invoke when the button is activated. The callback is called when the user presses Enter (if focused) or clicks. |
bool | isFocusable virtual const inline | Returns whether this widget can receive keyboard focus. |
core::Size | measure virtual | Measures how much space this widget needs. |
bool | onEvent virtual | Handles an input event. |
Button
Section titled “Button”inline explicit
inline explicit Button(const std::string & label)Creates a button with optional label text.
Parameters
Section titled “Parameters”labelThe text to display on the button (default: empty)
setLabel
Section titled “setLabel”inline
inline void setLabel(const std::string & l)Sets the button’s label text.
Parameters
Section titled “Parameters”lThe new label text
getLabel
Section titled “getLabel”const inline
inline const std::string & getLabel() constGets the button’s label text.
Returns
Section titled “Returns”The current label
setOnClick
Section titled “setOnClick”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.
Parameters
Section titled “Parameters”cbThe function to call on activation (can be empty)
isFocusable
Section titled “isFocusable”virtual const inline
virtual inline bool isFocusable() constReturns whether this widget can receive keyboard focus.
Widgets like Button and Input return true; containers return false.
Returns
Section titled “Returns”True if this widget can be focused, false otherwise
measure
Section titled “measure”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.
Parameters
Section titled “Parameters”-
availWMaximum available width (may be ignored by widget) -
availHMaximum available height (may be ignored by widget)
Returns
Section titled “Returns”The Size this widget wants, or needs at minimum
onEvent
Section titled “onEvent”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.
Parameters
Section titled “Parameters”eventThe input event
Returns
Section titled “Returns”True if this widget (or one of its listeners) handled the event
Protected Methods
Section titled “Protected Methods”| Return | Name | Description |
|---|---|---|
void | drawContent virtual | Draws this widget’s content. |
drawContent
Section titled “drawContent”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.
Parameters
Section titled “Parameters”-
bufThe buffer to draw to -
innerThe inner rectangle (excluding border/padding)
Private Attributes
Section titled “Private Attributes”| Return | Name | Description |
|---|---|---|
std::string | label | |
std::function< void()> | onClick |
std::string labelonClick
Section titled “onClick”std::function< void()> onClick