Loading...
Searching...
No Matches
ml::RichTextBuffer Class Reference

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.

Detailed Description

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.

Editing model

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.

Attribute model

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.

See also
RichTextRenderer, TextInput, TextArea

Definition at line 68 of file RichTextBuffer.h.

Constructor & Destructor Documentation

◆ RichTextBuffer()

ml::RichTextBuffer::RichTextBuffer ( )
default

Member Function Documentation

◆ applyAttribute()

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.

Parameters
attrAttribute to apply. start and end are overwritten with the current selection range.

◆ backspace()

void ml::RichTextBuffer::backspace ( )

Delete the character immediately before the cursor (backspace). If a selection is active, deletes the selection instead.

◆ clear()

void ml::RichTextBuffer::clear ( )

Delete all text in the buffer.

◆ clearSelection()

void ml::RichTextBuffer::clearSelection ( )

Clear the selection without moving the cursor.

◆ copyToClipboard()

void ml::RichTextBuffer::copyToClipboard ( ) const

Copy the current selection to the system clipboard. Has no effect if nothing is selected.

◆ cutToClipboard()

void ml::RichTextBuffer::cutToClipboard ( )

Cut the current selection to the system clipboard. Has no effect if nothing is selected.

◆ deleteForward()

void ml::RichTextBuffer::deleteForward ( )

Delete the character immediately after the cursor (delete key). If a selection is active, deletes the selection instead.

◆ empty()

bool ml::RichTextBuffer::empty ( ) const
nodiscard

Return true if the buffer contains no characters.

◆ getAttributeAt()

TextAttribute ml::RichTextBuffer::getAttributeAt ( std::size_t index,
const sf::Font * defaultFont,
unsigned int defaultSize,
sf::Color defaultColor ) const
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.

Parameters
indexCharacter index to query.
defaultFontFallback font if no attribute sets one.
defaultSizeFallback size if no attribute sets one.
defaultColorFallback color if no attribute sets one.

◆ getAttributes()

const std::vector< TextAttribute > & ml::RichTextBuffer::getAttributes ( ) const
nodiscard

Return all attribute ranges.

◆ getCursor()

std::size_t ml::RichTextBuffer::getCursor ( ) const
nodiscard

Return the current cursor position (char index).

◆ getSelectedText()

std::string ml::RichTextBuffer::getSelectedText ( ) const
nodiscard

Return the currently selected text. Empty if no selection.

◆ getSelectionEnd()

std::size_t ml::RichTextBuffer::getSelectionEnd ( ) const
nodiscard

Return the selection end (always >= selectionStart).

◆ getSelectionStart()

std::size_t ml::RichTextBuffer::getSelectionStart ( ) const
nodiscard

Return the selection start (always <= selectionEnd).

◆ getText()

const std::string & ml::RichTextBuffer::getText ( ) const
nodiscard

Return the full text content as a UTF-8 string.

◆ hasSelection()

bool ml::RichTextBuffer::hasSelection ( ) const
nodiscard

Return true if a selection is active.

◆ insert()

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.

Parameters
textUTF-8 text to insert.

◆ insertChar()

void ml::RichTextBuffer::insertChar ( char32_t unicode)

Insert a single unicode character at the cursor position.

Parameters
unicodeUnicode code point to insert.

◆ moveCursor()

void ml::RichTextBuffer::moveCursor ( int delta,
bool extendSelection = false )

Move the cursor by delta characters, clamped to buffer bounds.

Parameters
deltaPositive = forward, negative = backward.
extendSelectionIf true, extends the selection instead of moving it.

◆ moveCursorByWord()

void ml::RichTextBuffer::moveCursorByWord ( int direction,
bool extendSelection = false )

Move the cursor to the start of the previous or next word.

Parameters
direction-1 for previous word, +1 for next word.
extendSelectionIf true, extends the selection.

◆ moveCursorToLineEnd()

void ml::RichTextBuffer::moveCursorToLineEnd ( bool extendSelection = false)

Move the cursor to the end of the current line.

Parameters
extendSelectionIf true, extends the selection.

◆ moveCursorToLineStart()

void ml::RichTextBuffer::moveCursorToLineStart ( bool extendSelection = false)

Move the cursor to the start of the current line.

Parameters
extendSelectionIf true, extends the selection.

◆ pasteFromClipboard()

void ml::RichTextBuffer::pasteFromClipboard ( )

Insert the system clipboard contents at the cursor.

◆ selectAll()

void ml::RichTextBuffer::selectAll ( )

Select all text.

◆ setCursor()

void ml::RichTextBuffer::setCursor ( std::size_t pos)

Set the cursor position clamped to [0, size()]. Clears the selection.

Parameters
posNew cursor position.

◆ setSelection()

void ml::RichTextBuffer::setSelection ( std::size_t start,
std::size_t end )

Set an explicit selection range. Cursor moves to end.

Parameters
startSelection start character index.
endSelection end character index.

◆ setText()

void ml::RichTextBuffer::setText ( const std::string & text)

Replace the entire buffer content. Cursor moves to end. Clears all attributes.

Parameters
textNew UTF-8 text content.

◆ size()

std::size_t ml::RichTextBuffer::size ( ) const
nodiscard

Return the number of characters in the buffer.


The documentation for this class was generated from the following file: