Input
#include <Input.hpp>Inherits:
Widget
A text input widget for user text entry.
Input provides a text field where users can type, edit, and submit text. It supports standard keyboard editing (arrow keys, backspace, delete, etc.) and displays a placeholder when empty.
Features:
-
Text editing with cursor
-
Placeholder text when empty
-
onChange callback when text changes
-
onSubmit callback when user presses Enter
-
Horizontal scrolling for long text
Example Usage:
auto input = std::make_shared<Input>("Enter name...");input->setOnChange([](/api/const std::string &text) { std::cout << "User typing: " << text << "\n";});input->setOnSubmit([](/api/const std::string &text) { std::cout << "User submitted: " << text << "\n";});Public Methods
Section titled “Public Methods”| Return | Name | Description |
|---|---|---|
Input inline explicit | Creates an input field with optional placeholder text. | |
void | setText inline | Sets the input field’s text. Moves the cursor to the end of the text. |
const std::string & | getText const inline | Gets the current text in the input field. |
void | setPlaceholder inline | Sets the placeholder text shown when the input is empty. |
void | setOnChange inline | Sets the callback invoked when text changes. Called after each character insertion or deletion. |
void | setOnSubmit inline | Sets the callback invoked when user presses Enter. Typically used to submit or confirm the input. |
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. |
inline explicit
inline explicit Input(const std::string & placeholder)Creates an input field with optional placeholder text.
Parameters
Section titled “Parameters”placeholderThe text to show when input is empty (default: empty)
setText
Section titled “setText”inline
inline void setText(const std::string & t)Sets the input field’s text. Moves the cursor to the end of the text.
Parameters
Section titled “Parameters”tThe text to set
getText
Section titled “getText”const inline
inline const std::string & getText() constGets the current text in the input field.
Returns
Section titled “Returns”The current text
setPlaceholder
Section titled “setPlaceholder”inline
inline void setPlaceholder(const std::string & p)Sets the placeholder text shown when the input is empty.
Parameters
Section titled “Parameters”pThe placeholder text
setOnChange
Section titled “setOnChange”inline
inline void setOnChange(std::function< void(const std::string &)> cb)Sets the callback invoked when text changes. Called after each character insertion or deletion.
Parameters
Section titled “Parameters”cbFunction receiving the current text (can be empty)
setOnSubmit
Section titled “setOnSubmit”inline
inline void setOnSubmit(std::function< void(const std::string &)> cb)Sets the callback invoked when user presses Enter. Typically used to submit or confirm the input.
Parameters
Section titled “Parameters”cbFunction receiving the final text (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 | text | |
std::string | placeholder | |
int | cursor | |
int | scroll | |
std::function< void(const std::string &)> | onChange | |
std::function< void(const std::string &)> | onSubmit |
std::string textplaceholder
Section titled “placeholder”std::string placeholdercursor
Section titled “cursor”int cursor = 0scroll
Section titled “scroll”int scroll = 0onChange
Section titled “onChange”std::function< void(const std::string &)> onChangeonSubmit
Section titled “onSubmit”std::function< void(const std::string &)> onSubmitPrivate Methods
Section titled “Private Methods”| Return | Name | Description |
|---|---|---|
void | insertChar | |
void | deleteBack | |
void | deleteForward | |
void | moveCursor | |
void | updateScroll |
insertChar
Section titled “insertChar”void insertChar(char c)deleteBack
Section titled “deleteBack”void deleteBack()deleteForward
Section titled “deleteForward”void deleteForward()moveCursor
Section titled “moveCursor”void moveCursor(int delta)updateScroll
Section titled “updateScroll”void updateScroll()