Pure data layer for rich text editing. More...
#include <Malena/Graphics/Text/RichTextBuffer.h>
Public Member Functions | |
| RichTextBuffer ()=default | |
| void | applyAttribute (TextAttribute attr) |
| Apply an attribute to the current selection. | |
| void | backspace () |
| Delete the character immediately before the cursor (backspace). If a selection is active, deletes the selection instead. | |
| void | clear () |
| Delete all text in the buffer. | |
| void | clearSelection () |
| Clear the selection without moving the cursor. | |
| void | copyToClipboard () const |
| Copy the current selection to the system clipboard. Has no effect if nothing is selected. | |
| void | cutToClipboard () |
| Cut the current selection to the system clipboard. Has no effect if nothing is selected. | |
| void | deleteForward () |
| Delete the character immediately after the cursor (delete key). If a selection is active, deletes the selection instead. | |
| bool | empty () const |
Return true if the buffer contains no characters. | |
| TextAttribute | getAttributeAt (std::size_t index, const sf::Font *defaultFont, unsigned int defaultSize, sf::Color defaultColor) const |
Return the effective TextAttribute at a given character index. | |
| const std::vector< TextAttribute > & | getAttributes () const |
| Return all attribute ranges. | |
| std::size_t | getCursor () const |
| Return the current cursor position (char index). | |
| std::string | getSelectedText () const |
| Return the currently selected text. Empty if no selection. | |
| std::size_t | getSelectionEnd () const |
| Return the selection end (always >= selectionStart). | |
| std::size_t | getSelectionStart () const |
| Return the selection start (always <= selectionEnd). | |
| const std::string & | getText () const |
| Return the full text content as a UTF-8 string. | |
| bool | hasSelection () const |
Return true if a selection is active. | |
| void | insert (const std::string &text) |
| Insert text at the cursor position. | |
| void | insertChar (char32_t unicode) |
| Insert a single unicode character at the cursor position. | |
| void | moveCursor (int delta, bool extendSelection=false) |
Move the cursor by delta characters, clamped to buffer bounds. | |
| void | moveCursorByWord (int direction, bool extendSelection=false) |
| Move the cursor to the start of the previous or next word. | |
| void | moveCursorToLineEnd (bool extendSelection=false) |
| Move the cursor to the end of the current line. | |
| void | moveCursorToLineStart (bool extendSelection=false) |
| Move the cursor to the start of the current line. | |
| void | pasteFromClipboard () |
| Insert the system clipboard contents at the cursor. | |
| void | selectAll () |
| Select all text. | |
| void | setCursor (std::size_t pos) |
| Set the cursor position clamped to [0, size()]. Clears the selection. | |
| void | setSelection (std::size_t start, std::size_t end) |
Set an explicit selection range. Cursor moves to end. | |
| void | setText (const std::string &text) |
| Replace the entire buffer content. Cursor moves to end. Clears all attributes. | |
| std::size_t | size () const |
| Return the number of characters in the buffer. | |
Pure data layer for rich text editing.
RichTextBuffer owns a flat UTF-8 string and a vector of TextAttribute ranges that style spans of it. It manages cursor position, selection, and all text editing operations. It has no rendering dependencies — all SFML drawing is handled by RichTextRenderer.
All insertions happen at the cursor position. All deletions operate relative to the cursor. Selection is defined by _selStart and _selEnd — when they differ, a selection is active.
Attributes are stored as non-overlapping ranges sorted by start. Applying an attribute to the current selection splits existing ranges at the selection boundaries and inserts or merges the new attribute.
Definition at line 68 of file RichTextBuffer.h.
|
default |
| void ml::RichTextBuffer::applyAttribute | ( | TextAttribute | attr | ) |
Apply an attribute to the current selection.
If no selection is active, the attribute is stored as the "pending" style for the next inserted character. Existing ranges are split at the selection boundaries and merged where possible.
| attr | Attribute to apply. start and end are overwritten with the current selection range. |
| void ml::RichTextBuffer::backspace | ( | ) |
Delete the character immediately before the cursor (backspace). If a selection is active, deletes the selection instead.
| void ml::RichTextBuffer::clear | ( | ) |
Delete all text in the buffer.
| void ml::RichTextBuffer::clearSelection | ( | ) |
Clear the selection without moving the cursor.
| void ml::RichTextBuffer::copyToClipboard | ( | ) | const |
Copy the current selection to the system clipboard. Has no effect if nothing is selected.
| void ml::RichTextBuffer::cutToClipboard | ( | ) |
Cut the current selection to the system clipboard. Has no effect if nothing is selected.
| void ml::RichTextBuffer::deleteForward | ( | ) |
Delete the character immediately after the cursor (delete key). If a selection is active, deletes the selection instead.
|
nodiscard |
Return true if the buffer contains no characters.
|
nodiscard |
Return the effective TextAttribute at a given character index.
Merges all attribute ranges that cover index, with later ranges taking precedence over earlier ones.
| index | Character index to query. |
| defaultFont | Fallback font if no attribute sets one. |
| defaultSize | Fallback size if no attribute sets one. |
| defaultColor | Fallback color if no attribute sets one. |
|
nodiscard |
Return all attribute ranges.
|
nodiscard |
Return the current cursor position (char index).
|
nodiscard |
Return the currently selected text. Empty if no selection.
|
nodiscard |
Return the selection end (always >= selectionStart).
|
nodiscard |
Return the selection start (always <= selectionEnd).
|
nodiscard |
Return the full text content as a UTF-8 string.
|
nodiscard |
Return true if a selection is active.
| void ml::RichTextBuffer::insert | ( | const std::string & | text | ) |
Insert text at the cursor position.
If a selection is active it is replaced. Cursor advances to the end of the inserted text.
| text | UTF-8 text to insert. |
| void ml::RichTextBuffer::insertChar | ( | char32_t | unicode | ) |
Insert a single unicode character at the cursor position.
| unicode | Unicode code point to insert. |
| void ml::RichTextBuffer::moveCursor | ( | int | delta, |
| bool | extendSelection = false ) |
Move the cursor by delta characters, clamped to buffer bounds.
| delta | Positive = forward, negative = backward. |
| extendSelection | If true, extends the selection instead of moving it. |
| void ml::RichTextBuffer::moveCursorByWord | ( | int | direction, |
| bool | extendSelection = false ) |
Move the cursor to the start of the previous or next word.
| direction | -1 for previous word, +1 for next word. |
| extendSelection | If true, extends the selection. |
| void ml::RichTextBuffer::moveCursorToLineEnd | ( | bool | extendSelection = false | ) |
Move the cursor to the end of the current line.
| extendSelection | If true, extends the selection. |
| void ml::RichTextBuffer::moveCursorToLineStart | ( | bool | extendSelection = false | ) |
Move the cursor to the start of the current line.
| extendSelection | If true, extends the selection. |
| void ml::RichTextBuffer::pasteFromClipboard | ( | ) |
Insert the system clipboard contents at the cursor.
| void ml::RichTextBuffer::selectAll | ( | ) |
Select all text.
| void ml::RichTextBuffer::setCursor | ( | std::size_t | pos | ) |
Set the cursor position clamped to [0, size()]. Clears the selection.
| pos | New cursor position. |
| void ml::RichTextBuffer::setSelection | ( | std::size_t | start, |
| std::size_t | end ) |
Set an explicit selection range. Cursor moves to end.
| start | Selection start character index. |
| end | Selection end character index. |
| void ml::RichTextBuffer::setText | ( | const std::string & | text | ) |
Replace the entire buffer content. Cursor moves to end. Clears all attributes.
| text | New UTF-8 text content. |
|
nodiscard |
Return the number of characters in the buffer.